diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java b/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java index 18ab01f..cc44ed0 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java @@ -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(); } diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java index 23bd82e..b83b78a 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java @@ -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!"); diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java index 587ef17..ffc606a 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java @@ -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 mat = XMaterial.matchXMaterial(Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString("RepairItem"), "RepairItem is null")); if (!mat.isPresent()) { diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java b/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java index 4ec9b12..d2c6bd5 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java @@ -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 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())); + } } } diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index ef9e9ce..7606cb4 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -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; }