From 198812cebbbe6c52cadc302bf447a1c4b3348624 Mon Sep 17 00:00:00 2001 From: leonardo-dgs <17832817+leonardo-dgs@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:00:07 +0100 Subject: [PATCH] Clean up code --- src/net/milkbowl/vault/Vault.java | 134 ++++++++++++------------------ 1 file changed, 51 insertions(+), 83 deletions(-) diff --git a/src/net/milkbowl/vault/Vault.java b/src/net/milkbowl/vault/Vault.java index f085f0e..5f54b37 100644 --- a/src/net/milkbowl/vault/Vault.java +++ b/src/net/milkbowl/vault/Vault.java @@ -20,7 +20,6 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.Collection; -import java.util.concurrent.Callable; import java.util.logging.Logger; import net.milkbowl.vault.chat.Chat; @@ -84,8 +83,8 @@ public class Vault extends JavaPlugin { private static Logger log; private Permission perms; private String newVersionTitle = ""; - private double newVersion = 0; - private double currentVersion = 0; + private double newVersion; + private double currentVersion; private String currentVersionTitle = ""; private ServicesManager sm; private Vault plugin; @@ -102,7 +101,7 @@ public class Vault extends JavaPlugin { plugin = this; log = this.getLogger(); currentVersionTitle = getDescription().getVersion().split("-")[0]; - currentVersion = Double.valueOf(currentVersionTitle.replaceFirst("\\.", "")); + currentVersion = Double.parseDouble(currentVersionTitle.replaceFirst("\\.", "")); sm = getServer().getServicesManager(); // set defaults getConfig().addDefault("update-check", true); @@ -115,44 +114,32 @@ public class Vault extends JavaPlugin { getCommand("vault-info").setExecutor(this); getCommand("vault-convert").setExecutor(this); getServer().getPluginManager().registerEvents(new VaultListener(), this); - // Schedule to check the version every 30 minutes for an update. This is to update the most recent + // Schedule to check the version every 30 minutes for an update. This is to update the most recent // version so if an admin reconnects they will be warned about newer versions. - this.getServer().getScheduler().runTask(this, new Runnable() { - - @Override - public void run() { - // Programmatically set the default permission value cause Bukkit doesn't handle plugin.yml properly for Load order STARTUP plugins - org.bukkit.permissions.Permission perm = getServer().getPluginManager().getPermission("vault.update"); - if (perm == null) - { - perm = new org.bukkit.permissions.Permission("vault.update"); - perm.setDefault(PermissionDefault.OP); - plugin.getServer().getPluginManager().addPermission(perm); - } - perm.setDescription("Allows a user or the console to check for vault updates"); - - getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() { - - @Override - public void run() { - if (getServer().getConsoleSender().hasPermission("vault.update") && getConfig().getBoolean("update-check", true)) { - try { - log.info("Checking for Updates ... "); - newVersion = updateCheck(currentVersion); - if (newVersion > currentVersion) { - log.warning("Stable Version: " + newVersionTitle + " is out!" + " You are still running version: " + currentVersionTitle); - log.warning("Update at: https://dev.bukkit.org/projects/vault"); - } else if (currentVersion > newVersion) { - log.info("Stable Version: " + newVersionTitle + " | Current Version: " + currentVersionTitle); - } - } catch (Exception e) { - // ignore exceptions - } - } - } - }, 0, 432000); - + this.getServer().getScheduler().runTask(this, () -> { + // Programmatically set the default permission value cause Bukkit doesn't handle plugin.yml properly for Load order STARTUP plugins + org.bukkit.permissions.Permission perm = getServer().getPluginManager().getPermission("vault.update"); + if (perm == null) + { + perm = new org.bukkit.permissions.Permission("vault.update"); + perm.setDefault(PermissionDefault.OP); + plugin.getServer().getPluginManager().addPermission(perm); } + perm.setDescription("Allows a user or the console to check for vault updates"); + + getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> { + if (getServer().getConsoleSender().hasPermission("vault.update") && getConfig().getBoolean("update-check", true)) { + log.info("Checking for Updates ... "); + newVersion = updateCheck(currentVersion); + if (newVersion > currentVersion) { + log.warning("Stable Version: " + newVersionTitle + " is out!" + " You are still running version: " + currentVersionTitle); + log.warning("Update at: https://dev.bukkit.org/projects/vault"); + } else if (currentVersion > newVersion) { + log.info("Stable Version: " + newVersionTitle + " | Current Version: " + currentVersionTitle); + } + } + }, 0, 432000); + }); // Load up the Plugin metrics @@ -257,7 +244,7 @@ public class Vault extends JavaPlugin { Permission perms = new Permission_SuperPerms(this); sm.register(Permission.class, perms, this, ServicePriority.Lowest); - log.info(String.format("[Permission] SuperPermissions loaded as backup permission system.")); + log.info("[Permission] SuperPermissions loaded as backup permission system."); this.perms = sm.getRegistration(Permission.class).getProvider(); } @@ -319,7 +306,7 @@ public class Vault extends JavaPlugin { } Economy econ1 = null; Economy econ2 = null; - String economies = ""; + StringBuilder economies = new StringBuilder(); for (RegisteredServiceProvider econ : econs) { String econName = econ.getProvider().getName().replace(" ", ""); if (econName.equalsIgnoreCase(args[0])) { @@ -328,9 +315,9 @@ public class Vault extends JavaPlugin { econ2 = econ.getProvider(); } if (economies.length() > 0) { - economies += ", "; + economies.append(", "); } - economies += econName; + economies.append(econName); } if (econ1 == null) { @@ -352,11 +339,11 @@ public class Vault extends JavaPlugin { econ2.createPlayerAccount(op); double diff = econ1.getBalance(op) - econ2.getBalance(op); if (diff > 0) { - econ2.depositPlayer(op, diff); + econ2.depositPlayer(op, diff); } else if (diff < 0) { - econ2.withdrawPlayer(op, -diff); + econ2.withdrawPlayer(op, -diff); } - + } } sender.sendMessage("Converson complete, please verify the data before using it."); @@ -364,37 +351,37 @@ public class Vault extends JavaPlugin { private void infoCommand(CommandSender sender) { // Get String of Registered Economy Services - String registeredEcons = null; + StringBuilder registeredEcons = null; Collection> econs = this.getServer().getServicesManager().getRegistrations(Economy.class); for (RegisteredServiceProvider econ : econs) { Economy e = econ.getProvider(); if (registeredEcons == null) { - registeredEcons = e.getName(); + registeredEcons = new StringBuilder(e.getName()); } else { - registeredEcons += ", " + e.getName(); + registeredEcons.append(", ").append(e.getName()); } } // Get String of Registered Permission Services - String registeredPerms = null; + StringBuilder registeredPerms = null; Collection> perms = this.getServer().getServicesManager().getRegistrations(Permission.class); for (RegisteredServiceProvider perm : perms) { Permission p = perm.getProvider(); if (registeredPerms == null) { - registeredPerms = p.getName(); + registeredPerms = new StringBuilder(p.getName()); } else { - registeredPerms += ", " + p.getName(); + registeredPerms.append(", ").append(p.getName()); } } - String registeredChats = null; + StringBuilder registeredChats = null; Collection> chats = this.getServer().getServicesManager().getRegistrations(Chat.class); for (RegisteredServiceProvider chat : chats) { Chat c = chat.getProvider(); if (registeredChats == null) { - registeredChats = c.getName(); + registeredChats = new StringBuilder(c.getName()); } else { - registeredChats += ", " + c.getName(); + registeredChats.append(", ").append(c.getName()); } } @@ -451,13 +438,13 @@ public class Vault extends JavaPlugin { final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); - if (array.size() == 0) { + if (array.isEmpty()) { this.getLogger().warning("No files found, or Feed URL is bad."); return currentVersion; } // Pull the last version from the JSON newVersionTitle = ((String) ((JSONObject) array.get(array.size() - 1)).get("name")).replace("Vault", "").trim(); - return Double.valueOf(newVersionTitle.replaceFirst("\\.", "").trim()); + return Double.parseDouble(newVersionTitle.replaceFirst("\\.", "").trim()); } catch (Exception e) { log.info("There was an issue attempting to check for the latest version."); } @@ -472,21 +459,11 @@ public class Vault extends JavaPlugin { econ = rspEcon.getProvider(); } final String econName = econ != null ? econ.getName() : "No Economy"; - metrics.addCustomChart(new SimplePie("economy", new Callable() { - @Override - public String call() { - return econName; - } - })); + metrics.addCustomChart(new SimplePie("economy", () -> econName)); // Create our Permission Graph and Add our permission Plotters final String permName = Bukkit.getServer().getServicesManager().getRegistration(Permission.class).getProvider().getName(); - metrics.addCustomChart(new SimplePie("permission", new Callable() { - @Override - public String call() { - return permName; - } - })); + metrics.addCustomChart(new SimplePie("permission", () -> permName)); // Create our Chat Graph and Add our chat Plotters RegisteredServiceProvider rspChat = Bukkit.getServer().getServicesManager().getRegistration(Chat.class); @@ -495,12 +472,7 @@ public class Vault extends JavaPlugin { chat = rspChat.getProvider(); } final String chatName = chat != null ? chat.getName() : "No Chat"; - metrics.addCustomChart(new SimplePie("chat", new Callable() { - @Override - public String call() { - return chatName; - } - })); + metrics.addCustomChart(new SimplePie("chat", () -> chatName)); } public class VaultListener implements Listener { @@ -509,13 +481,9 @@ public class Vault extends JavaPlugin { public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); if (perms.has(player, "vault.update")) { - try { - if (newVersion > currentVersion) { - player.sendMessage("Vault " + newVersionTitle + " is out! You are running " + currentVersionTitle); - player.sendMessage("Update Vault at: " + VAULT_BUKKIT_URL); - } - } catch (Exception e) { - // Ignore exceptions + if (newVersion > currentVersion) { + player.sendMessage("Vault " + newVersionTitle + " is out! You are running " + currentVersionTitle); + player.sendMessage("Update Vault at: " + VAULT_BUKKIT_URL); } } }