Made ConfigUtil ready for message usage

This commit is contained in:
stijnb1234 2020-02-27 12:45:05 +01:00
parent 6319b181c4
commit 58dd349140
5 changed files with 87 additions and 14 deletions

View file

@ -620,7 +620,7 @@ public class V10LiftAPI {
Sign s = (Sign) bs;
ls.setOldText(s.getLine(3));
s.setLine(3, ConfigUtil.getColored("DefectText"));
s.setLine(3, ConfigUtil.getConfigText("DefectText"));
s.update();
}
@ -631,7 +631,7 @@ public class V10LiftAPI {
Sign s = (Sign) bs;
lift.setSignText(s.getLine(3));
s.setLine(3, ConfigUtil.getColored("DefectText"));
s.setLine(3, ConfigUtil.getConfigText("DefectText"));
s.update();
}
} else {
@ -733,8 +733,8 @@ public class V10LiftAPI {
bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState();
if (!(bs instanceof Sign)) continue;
sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue;
sign.setLine(3, ConfigUtil.getColored("DisabledText"));
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue;
sign.setLine(3, ConfigUtil.getConfigText("DisabledText"));
sign.update();
}
@ -748,7 +748,7 @@ public class V10LiftAPI {
}
sign = (Sign) bs;
ls.setOldText(sign.getLine(3));
sign.setLine(3, ConfigUtil.getColored("DisabledText"));
sign.setLine(3, ConfigUtil.getConfigText("DisabledText"));
sign.update();
}
} else {
@ -756,7 +756,7 @@ public class V10LiftAPI {
bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState();
if (!(bs instanceof Sign)) continue;
sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue;
sign.setLine(3, "");
sign.update();
}

View file

@ -942,7 +942,7 @@ public class V10LiftCommand implements CommandExecutor {
bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at edit command").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState();
if (!(bs instanceof Sign)) continue;
sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue;
sign.setLine(3, "");
sign.update();
}
@ -990,7 +990,7 @@ public class V10LiftCommand implements CommandExecutor {
bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at edit command").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState();
if (!(bs instanceof Sign)) continue;
sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue;
sign.setLine(3, ChatColor.RED + "Maintenance");
sign.update();
}
@ -1006,7 +1006,7 @@ public class V10LiftCommand implements CommandExecutor {
}
sign = (Sign) bs;
ls.setOldText(sign.getLine(3));
sign.setLine(3, ConfigUtil.getColored("MaintenanceText"));
sign.setLine(3, ConfigUtil.getConfigText("MaintenanceText"));
sign.update();
}
sender.sendMessage(ChatColor.GREEN + "Editor turned on!");

View file

@ -88,7 +88,7 @@ public class PlayerInteractListener implements Listener {
}
Sign sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) return;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) return;
String liftName = sign.getLine(1);
if (!DataManager.containsLift(liftName)) return;
@ -328,7 +328,7 @@ public class PlayerInteractListener implements Listener {
if (!(bs instanceof Sign)) return;
Sign sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) return;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) return;
String liftName = sign.getLine(1);
if (!DataManager.containsLift(liftName)) return;
@ -339,7 +339,7 @@ public class PlayerInteractListener implements Listener {
}
if (lift.isDefective()) {
if (sign.getLine(3).equals(ConfigUtil.getColored("DefectText")) && p.hasPermission("v10lift.repair") && a == Action.RIGHT_CLICK_BLOCK) {
if (sign.getLine(3).equals(ConfigUtil.getConfigText("DefectText")) && p.hasPermission("v10lift.repair") && a == Action.RIGHT_CLICK_BLOCK) {
int masterAmount = V10LiftPlugin.getSConfig().getFile().getInt("RepairAmount");
Optional<XMaterial> mat = XMaterial.matchXMaterial(Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString("RepairItem"), "RepairItem is null"));
if (!mat.isPresent()) {

View file

@ -2,13 +2,77 @@ package nl.SBDeveloper.V10Lift.Utils;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConfigUtil {
/* Helps building the messages */
/**
* Get a message from the config.yml
*
* @param path The path to look for
* @return The message
*/
@Nonnull
public static String getColored(@Nonnull String path) {
return ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString(path), "Message " + path + " not found!"));
public static String getConfigText(@Nonnull String path) {
return ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString(path), "Message " + path + " not found in config.yml!"));
}
/**
* Send a message from the messages.yml without variables
*
* @param p The commandsender to send it to
* @param path The path to look for
*/
public static void sendMessage(CommandSender p, @Nonnull String path) {
String fileMessage = V10LiftPlugin.getMessages().getFile().getString(path);
if (fileMessage == null) {
throw new NullPointerException("Message " + path + " not found in messages.yml!");
}
String[] messages = fileMessage.split("\n");
for (String message : messages) {
p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
}
}
/**
* Get a message from the messages.yml with variables
*
* @param p The commandsender to send it to
* @param path The path to look for
* @param replacement The replacements -> key: %Name% = value: TheName
* @return The message with replacements
*/
public static void sendMessage(CommandSender p, @Nonnull String path, HashMap<String, String> replacement) {
String fileMessage = V10LiftPlugin.getMessages().getFile().getString(path);
if (fileMessage == null) {
throw new NullPointerException("Message " + path + " not found in messages.yml!");
}
String[] messages = fileMessage.split("\n");
for (String message : messages) {
Pattern pattern = Pattern.compile("%(.*?)%");
Matcher matcher = pattern.matcher(message);
StringBuilder builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
String repl = replacement.get(matcher.group(1));
builder.append(message, i, matcher.start());
if (repl == null)
builder.append(matcher.group(0));
else
builder.append(repl);
i = matcher.end();
}
builder.append(message.substring(i));
p.sendMessage(ChatColor.translateAlternateColorCodes('&', builder.toString()));
}
}
}

View file

@ -25,6 +25,7 @@ public class V10LiftPlugin extends JavaPlugin {
private static V10LiftPlugin instance;
private static YamlFile config;
private static DBManager dbManager;
private static YamlFile messages;
private static V10LiftAPI api;
private static boolean vault = false;
@ -39,6 +40,10 @@ public class V10LiftPlugin extends JavaPlugin {
config = new YamlFile("config");
config.loadDefaults();
//Load the messages
messages = new YamlFile("messages");
messages.loadDefaults();
//Load the database
dbManager = new DBManager("data");
try {
@ -110,6 +115,10 @@ public class V10LiftPlugin extends JavaPlugin {
return dbManager;
}
public static YamlFile getMessages() {
return messages;
}
public static V10LiftAPI getAPI() {
return api;
}