diff --git a/pom.xml b/pom.xml index 68df47d..5909eac 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.wfector chestshopnotifier - 1.2.1-SNAPSHOT + 1.3.0-SNAPSHOT ChestShopNotifier Bukkit plugin to shows all transactions made in your ChestShops throughout history. @@ -131,4 +131,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/wfector/command/CleanDatabase.java b/src/main/java/com/wfector/command/CleanDatabase.java index d5a9325..c6209eb 100644 --- a/src/main/java/com/wfector/command/CleanDatabase.java +++ b/src/main/java/com/wfector/command/CleanDatabase.java @@ -2,7 +2,6 @@ import com.wfector.notifier.ChestShopNotifier; import com.wfector.util.Time; -import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.scheduler.BukkitRunnable; @@ -50,9 +49,9 @@ public void run() { } statement.executeUpdate("DELETE FROM csnUUID" + whereStr + ";"); - sender.sendMessage(ChatColor.RED + "Cleaned database from " + (readOnly ? "read" : "all") + " entries!"); + sender.sendMessage(plugin.getMessage("database-cleaned", "amount", (readOnly ? "read" : "all"))); } catch (SQLException e) { - sender.sendMessage(ChatColor.RED + "Database error while executing this command!"); + sender.sendMessage(plugin.getMessage("database-error-oncommand")); e.printStackTrace(); } finally { ChestShopNotifier.close(c); diff --git a/src/main/java/com/wfector/command/Clear.java b/src/main/java/com/wfector/command/Clear.java index 5504477..d169b0e 100644 --- a/src/main/java/com/wfector/command/Clear.java +++ b/src/main/java/com/wfector/command/Clear.java @@ -8,7 +8,6 @@ import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.UUIDs.NameManager; import com.wfector.notifier.ChestShopNotifier; -import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -31,9 +30,9 @@ public void run() { Statement statement = c.createStatement(); statement.executeUpdate("DELETE FROM csnUUID WHERE `Unread`='1' AND `ShopOwnerId`='" + senderId.toString() + "'"); - if(plugin.getMessage("history-clear") != null) sender.sendMessage(plugin.getMessage("history-clear")); + sender.sendMessage(plugin.getMessage("history-clear")); } catch (SQLException e) { - sender.sendMessage(ChatColor.RED + "Database error while executing this command!"); + sender.sendMessage(plugin.getMessage("database-error-oncommand")); e.printStackTrace(); } finally { ChestShopNotifier.close(c); diff --git a/src/main/java/com/wfector/command/CommandRunner.java b/src/main/java/com/wfector/command/CommandRunner.java index 3e3dbc7..af66360 100644 --- a/src/main/java/com/wfector/command/CommandRunner.java +++ b/src/main/java/com/wfector/command/CommandRunner.java @@ -6,7 +6,6 @@ import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.UUIDs.NameManager; import com.wfector.notifier.BatchRunner; -import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -24,22 +23,22 @@ public CommandRunner(ChestShopNotifier plugin) { public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) { if(args.length == 0) { - Help.SendDialog(sender); + new Help(plugin).SendDialog(sender); + return true; } else { if(args[0].equalsIgnoreCase("reload") && (sender.hasPermission("csn.command.reload"))) { - sender.sendMessage(ChatColor.LIGHT_PURPLE + "ChestShop Notifier // " + ChatColor.GRAY + "Reloading, please wait..."); - plugin.updateConfiguration(sender); + sender.sendMessage(plugin.getMessage("reload-cmd")); return true; } else if(args[0].equalsIgnoreCase("convert") && sender.hasPermission("csn.admin")) { if(!plugin.isPluginEnabled()) { - sender.sendMessage(ChatColor.RED + "Invalid database connection. Please edit config and /csn reload."); + sender.sendMessage(plugin.getMessage("database-error")); return true; } - sender.sendMessage(ChatColor.RED + "Attempting to convert database..."); + sender.sendMessage(plugin.getMessage("database-convert")); plugin.getLogger().log(Level.INFO, "Attempting to convert database..."); new Converter(plugin, sender).runTaskAsynchronously(plugin); @@ -47,17 +46,17 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, } else if(args[0].equalsIgnoreCase("upload") && sender.hasPermission("csn.command.upload")) { if(!plugin.isPluginEnabled()) { - sender.sendMessage(ChatColor.RED + "Invalid database connection. Please edit config and /csn reload."); + sender.sendMessage(plugin.getMessage("database-error")); return true; } new BatchRunner(plugin).runTaskAsynchronously(plugin); - sender.sendMessage(ChatColor.RED + "Batch executed!"); + sender.sendMessage(plugin.getMessage("database-upload")); } else if(args[0].equalsIgnoreCase("cleandatabase") && sender.hasPermission("csn.command.cleandatabase")) { if(!plugin.isPluginEnabled()) { - sender.sendMessage(ChatColor.RED + "Invalid database connection. Please edit config and /csn reload."); + sender.sendMessage(plugin.getMessage("database-error")); return true; } @@ -68,7 +67,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, CleanDatabase.Parameter param = CleanDatabase.Parameter.getFromInput(args[i]); if (param != null) { if (i + 1 + param.getArgs().length > args.length) { - sender.sendMessage(ChatColor.RED + "Missing parameter arguments: " + param.getUsage()); + sender.sendMessage(plugin.getMessage("missing-arguments", "usage", param.getUsage())); return true; } switch (param) { @@ -77,7 +76,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, int days = Integer.parseInt(args[i + 1]); cleaner.cleanBefore(days); } catch (NumberFormatException e) { - sender.sendMessage(ChatColor.RED + args[i + 1] + " is not a valid number input for " + param.getUsage() + "!"); + sender.sendMessage(plugin.getMessage("invalid-number", "typo", args[i + 1]).replace("{usage}", param.getUsage())); return true; } break; @@ -91,7 +90,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, if (userId != null) { cleaner.cleanUser(userId); } else { - sender.sendMessage(ChatColor.RED + args[i + 1] + " is not a valid username/uuid input for " + param.getUsage() + "!"); + sender.sendMessage(plugin.getMessage("invalid-username", "typo", args[i + 1]).replace("{usage}", param.getUsage())); //should work? } break; case READ_ONLY: @@ -111,13 +110,13 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, return true; } else if(args[0].equalsIgnoreCase("help") && sender.hasPermission("csn.command")) { - Help.SendDialog(sender); + new Help(plugin).SendDialog(sender); return true; } else if(args[0].equalsIgnoreCase("history") && sender.hasPermission("csn.command.history")) { if(!plugin.isPluginEnabled()) { - sender.sendMessage(ChatColor.RED + "Invalid database connection. Please edit config and /csn reload."); + sender.sendMessage(plugin.getMessage("database-error")); return true; } @@ -139,7 +138,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, if (target != null) { userId = target.getUniqueId(); } else { - sender.sendMessage(ChatColor.RED + "The user '" + args[1] + "' was not found."); + sender.sendMessage(plugin.getMessage("user-not-found", "player", args[1])); return true; } } @@ -147,12 +146,12 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, try { page = Integer.parseInt(args[2]); } catch (NumberFormatException e2) { - sender.sendMessage(ChatColor.YELLOW + args[2] + ChatColor.RED + " is not a valid page number input for /csn history !"); + sender.sendMessage(plugin.getMessage("page-not-found-other", "page", args[2])); return true; } } } else { - sender.sendMessage(ChatColor.YELLOW + args[1] + ChatColor.RED + " is not a valid page number input for /csn history !"); + sender.sendMessage(plugin.getMessage("page-not-found", "page", args[1])); return true; } } @@ -168,7 +167,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, } else if(args[0].equalsIgnoreCase("clear") && sender.hasPermission("csn.command.clear")) { if(!plugin.isPluginEnabled()) { - sender.sendMessage(ChatColor.RED + "Invalid database connection. Please edit config and /csn reload."); + sender.sendMessage(plugin.getMessage("database-error")); return true; } @@ -178,7 +177,7 @@ public boolean onCommand(final CommandSender sender, Command cmd, String label, } } - sender.sendMessage(ChatColor.RED + "Command not recognized. Type /csn help for help."); + sender.sendMessage(plugin.getMessage("unrecognized-command")); return true; } } diff --git a/src/main/java/com/wfector/command/Converter.java b/src/main/java/com/wfector/command/Converter.java index b1e67db..0c75ab2 100644 --- a/src/main/java/com/wfector/command/Converter.java +++ b/src/main/java/com/wfector/command/Converter.java @@ -10,7 +10,6 @@ import java.util.logging.Level; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import com.wfector.notifier.ChestShopNotifier; @@ -31,10 +30,10 @@ public Converter(ChestShopNotifier plugin, CommandSender sender) { public void run() { if (convertDatabase()) { - sender.sendMessage(ChatColor.RED + "Database converted!"); + sender.sendMessage(plugin.getMessage("database-converted")); plugin.getLogger().log(Level.INFO, "Database converted!"); } else { - sender.sendMessage(ChatColor.RED + "Error while trying to convert! Maybe you don't have a 'csn' table?"); + sender.sendMessage(plugin.getMessage("database-convert-fail")); plugin.getLogger().log(Level.SEVERE, "Error while trying to convert! Maybe you don't have a 'csn' table?"); } } diff --git a/src/main/java/com/wfector/command/Help.java b/src/main/java/com/wfector/command/Help.java index 26e1cdb..498b434 100644 --- a/src/main/java/com/wfector/command/Help.java +++ b/src/main/java/com/wfector/command/Help.java @@ -2,54 +2,52 @@ import java.util.ArrayList; -import org.bukkit.ChatColor; +import com.wfector.notifier.ChestShopNotifier; import org.bukkit.command.CommandSender; public class Help { + private final ChestShopNotifier plugin; - public static void SendDialog(CommandSender sender) { + public Help(ChestShopNotifier main) { + plugin = main; + } - ArrayList helpItems = new ArrayList(); + public void SendDialog(CommandSender sender) { - helpItems.add("&dChestShop Notifier // &7Commands"); - helpItems.add("&c "); - helpItems.add("&7 /csn &dhelp &f- Plugin usage & commands"); - if (sender.hasPermission("csn.command.history")) { - helpItems.add("&7 /csn &dhistory []&f- View sales"); + ArrayList helpItems = new ArrayList(); + helpItems.add(plugin.getMessage("help.header")); + helpItems.add(plugin.getMessage("help.help")); + if(sender.hasPermission("csn.command.history")) { + helpItems.add(plugin.getMessage("help.history")); } - if (sender.hasPermission("csn.command.read")) { - helpItems.add("&7 /csn &dread &f- Mark all sales as read"); + if(sender.hasPermission("csn.command.read")) { + helpItems.add(plugin.getMessage("help.read")); } - if (sender.hasPermission("csn.command.clear")) { - helpItems.add("&7 /csn &dclear &f- Remove read sales"); + if(sender.hasPermission("csn.command.clear")) { + helpItems.add(plugin.getMessage("help.clear")); } - helpItems.add("&c "); - - if (sender.hasPermission("csn.command.history.others")) { - helpItems.add("&7 /csn &dhistory []&f- View sales of another player"); + if(sender.hasPermission("csn.command.history.others")) { + helpItems.add(plugin.getMessage("help.history-others")); } - if (sender.hasPermission("csn.command.cleandatabase")) { - helpItems.add("&7 /csn &ccleandatabase &f- Remove database entries. Parameters:"); - helpItems.add("&c --older-than, -o &f- Removes entries older than "); - helpItems.add("&c --user, -user &f- Removes entries from a single user only"); - helpItems.add("&c --read-only, -r &f- Removes only read entries, default behaviour"); - helpItems.add("&c --all, -a &f- Removes all entries"); + if(sender.hasPermission("csn.command.cleandatabase")) { + helpItems.add(plugin.getMessage("help.cleandatabase")); + helpItems.add(plugin.getMessage("help.cleandatabase-older-than")); + helpItems.add(plugin.getMessage("help.cleandatabase-user")); + helpItems.add(plugin.getMessage("help.cleandatabase-read-only")); + helpItems.add(plugin.getMessage("help.cleandatabase-all")); } - if (sender.hasPermission("csn.command.upload")) { - helpItems.add("&7 /csn &cupload &f- Force update databases"); + if(sender.hasPermission("csn.command.upload")) { + helpItems.add(plugin.getMessage("help.upload")); } - if (sender.hasPermission("csn.command.convert")) { - helpItems.add("&7 /csn &cconvert &f- Convert database to UUIDs"); + if(sender.hasPermission("csn.command.convert")) { + helpItems.add(plugin.getMessage("help.convert")); } - - if (sender.hasPermission("csn.command.reload")) { - helpItems.add("&7 /csn &creload &f- Reload configuration"); + if(sender.hasPermission("csn.command.reload")) { + helpItems.add(plugin.getMessage("help.reload")); } for(String item : helpItems) { - sender.sendMessage(ChatColor.translateAlternateColorCodes('&', item)); + sender.sendMessage(item); } - } - } diff --git a/src/main/java/com/wfector/command/History.java b/src/main/java/com/wfector/command/History.java index 9b7abad..d42e4f9 100644 --- a/src/main/java/com/wfector/command/History.java +++ b/src/main/java/com/wfector/command/History.java @@ -72,9 +72,8 @@ private void gatherResults() { if (markRead) { Statement readStatement = c.createStatement(); int rowsUpdated = readStatement.executeUpdate("UPDATE csnUUID SET `Unread`='1' WHERE `ShopOwnerId`='" + userId.toString() + "'"); - - if (rowsUpdated > 0 && plugin.getMessage("history-marked-read") != null) - sender.sendMessage(plugin.getMessage("history-marked-read")); + if (rowsUpdated > 0) + sender.sendMessage(plugin.getMessage("history-marked-read")); } } catch (SQLException e) { @@ -97,18 +96,16 @@ public void addEntry(HistoryEntry addEntry) { private void showResults() { boolean other = !(sender instanceof Player) || !((Player) sender).getUniqueId().equals(userId); - if(plugin.getMessage("history-caption") != null) { - String message = plugin.getMessage("history-caption"); - if (other) { - String userName = NameManager.getUsername(userId); - message += ChatColor.GRAY + " (" + (userName != null ? userName : userId) + ")"; - } - sender.sendMessage(message); + String message = plugin.getMessage("history-caption"); + if (other) { + String userName = NameManager.getUsername(userId); + message += ChatColor.GRAY + " (" + (userName != null ? userName : userId) + ")"; } + sender.sendMessage(message); sender.sendMessage(""); if(historyEntries.isEmpty()) { - if(plugin.getMessage("history-empty") != null) sender.sendMessage(plugin.getMessage("history-empty")); + sender.sendMessage(plugin.getMessage("history-empty")); return; } @@ -133,14 +130,14 @@ private void showResults() { } sender.sendMessage(" "); - if (maxPages > 1 && plugin.getMessage("history-footer-page") != null) { + if (maxPages > 1) { sender.sendMessage( plugin.getMessage("history-footer-page") .replace("{current}", String.valueOf(page)) .replace("{pages}", String.valueOf(maxPages)) ); } - if (!other && sender.hasPermission("csn.command.clear") && plugin.getMessage("history-footer-clear") != null) { + if (!other && sender.hasPermission("csn.command.clear")) { sender.sendMessage(plugin.getMessage("history-footer-clear")); } diff --git a/src/main/java/com/wfector/notifier/ChestShopNotifier.java b/src/main/java/com/wfector/notifier/ChestShopNotifier.java index dfa4f29..4ff0610 100644 --- a/src/main/java/com/wfector/notifier/ChestShopNotifier.java +++ b/src/main/java/com/wfector/notifier/ChestShopNotifier.java @@ -115,14 +115,14 @@ public void run() { if(pluginEnabled) { getLogger().log(Level.INFO, "Database connected!"); if (sender != null) { - sender.sendMessage(ChatColor.LIGHT_PURPLE + "ChestShop Notifier // " + ChatColor.GREEN + "Reloaded!"); - sender.sendMessage(ChatColor.LIGHT_PURPLE + "ChestShop Notifier // " + ChatColor.GREEN + "Database connected!"); + sender.sendMessage(getMessage("reload-success")); + sender.sendMessage(getMessage("reload-database-success")); } } else { getLogger().log(Level.WARNING, "Failed to connect to the database! Disabling connections!"); if (sender != null) { - sender.sendMessage(ChatColor.LIGHT_PURPLE + "ChestShop Notifier // " + ChatColor.GREEN + "Reloaded!"); - sender.sendMessage(ChatColor.LIGHT_PURPLE + "ChestShop Notifier // " + ChatColor.RED + "Database failed to connect!"); + sender.sendMessage(getMessage("reload-success")); + sender.sendMessage(getMessage("reload-database-fail")); } } } @@ -138,14 +138,25 @@ public static void close(Connection c) { } } } - /** - * Gets a message from the config file. - * @param string The name of the message to get - * @return The message or null if it doesn't exist + * returns a texty string + * + * @param key the config path + * @param replacements Optional replacements. Use {index} in the message to address them. + * @return the text */ - public String getMessage(String string) { - return (getConfig().contains("messages." + string)) ? ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages." + string)) : null; + public String getMessage(String key, String... replacements) { + String s = getConfig().getString("messages." + key); + if (s != null && !s.isEmpty()) { + for (int i = 0; i < replacements.length; i++) { + if (i + 1 < replacements.length) { + s = s.replace("{" + i + "}", replacements[i + 1]); + } + } + return ChatColor.translateAlternateColorCodes('&', s); + } else { + return "Missing string 'messages." + key + "' in config.yml"; + } } @EventHandler diff --git a/src/main/java/com/wfector/notifier/LoginRunner.java b/src/main/java/com/wfector/notifier/LoginRunner.java index 0431c29..381a8b2 100644 --- a/src/main/java/com/wfector/notifier/LoginRunner.java +++ b/src/main/java/com/wfector/notifier/LoginRunner.java @@ -7,9 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.Date; import java.util.UUID; -import java.util.logging.Level; public class LoginRunner extends BukkitRunnable { private final ChestShopNotifier plugin; @@ -45,13 +43,8 @@ public void run() { if (amount > 0) { plugin.debug("Ran for user '" + p.getName() + "'"); - if(plugin.getMessage("sales") != null) { - p.sendMessage(plugin.getMessage("sales").replace("{sales}", String.valueOf(amount))); - } - - if(plugin.getMessage("history-cmd") != null) { - p.sendMessage(plugin.getMessage("history-cmd")); - } + p.sendMessage(plugin.getMessage("sales", "sales", String.valueOf(amount))); + p.sendMessage(plugin.getMessage("history-cmd")); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9059a26..86a6eaa 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -37,4 +37,39 @@ messages: history-footer-page: "&c- Page &f{current}&c/&f{pages}&6 - &7/csn history " history-footer-clear: "&f- &cTo remove all old entries, type /csn clear" history-clear: "&cHistory cleared! New sales will continue to be recorded." - history-marked-read: "&cUnread entries marked read!" \ No newline at end of file + history-marked-read: "&cUnread entries marked read!" + # new translations + reload-cmd: "&dChestShop Notifier // &7Reloading, please wait..." + reload-success: "&aReloaded!" + reload-database-success: "&aDatabase connected!" + reload-database-fail: "&cDatabase failed to connect!" + user-not-found: "&cThe user '{player}' was not found." + page-not-found: "&e{page} &cis not a valid page number input for /csn history !" + page-not-found-other: "&e{page} &cis not a valid page number input for /csn history !" + database-error: "&cInvalid database connection. Please edit config and /csn reload." + database-error-oncommand: "&cDatabase error while executing this command!" + database-cleaned: "&cCleaned database from {amount} entries!" + database-uploaded: "&cBatch executed!" + database-convert: "&cAttempting to convert database..." + database-convert-fail: "&cError while trying to convert! Maybe you don't have a 'csn' table?" + database-converted: "&cDatabase converted!" + unrecognized-command: "&cCommand not recognized. Type /csn help for help." + missing-arguments: "&cMissing parameter arguments: {usage}" + invalid-number: "&c{typo} is not a valid number input for {usage}!" + invalid-username: "&c{typo} is not a valid username/uuid input for {usage}!" + help: + header: "&dChestShop Notifier // &7Commands" + help: "&7 /csn &dhelp &f- Plugin usage & commands" + history: "&7 /csn &dhistory []&f- View sales" + read: "&7 /csn &dread &f- Mark all sales as read" + clear: "&7 /csn &dclear &f- Remove read sales" + history-others: "&7 /csn &dhistory []&f- View sales of another player" + cleandatabase: "&7 /csn &ccleandatabase &f- Remove database entries. Parameters:" + cleandatabase-older-than: "&c --older-than, -o &f- Removes entries older than " + cleandatabase-user: "&c --user, -user &f- Removes entries from a single user only" + cleandatabase-read-only: "&c --read-only, -r &f- Removes only read entries, default behaviour" + cleandatabase-all: "&c --all, -a &f- Removes all entries" + upload: "&7 /csn &cupload &f- Force update databases" + convert: "&7 /csn &cconvert &f- Convert database to UUIDs" + reload: "&7 /csn &creload &f- Reload configuration" + diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ca74e60..fd4eec6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ name: ${project.name} version: '${minecraft.plugin.version}' description: ${project.description} author: BetaBlaze -authors: [Phoenix616] +authors: [Phoenix616, FrankHeijden] depend: [ChestShop] commands: csn: @@ -58,4 +58,4 @@ permissions: csn.history.others: description: Allow lookups on other users children: - csn.command.history.others: true \ No newline at end of file + csn.command.history.others: true