Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-dgs committed Mar 10, 2024
1 parent 460cb21 commit 198812c
Showing 1 changed file with 50 additions and 82 deletions.
132 changes: 50 additions & 82 deletions src/net/milkbowl/vault/Vault.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -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;
Expand All @@ -102,7 +101,7 @@ public void onEnable() {
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);
Expand All @@ -115,44 +114,32 @@ public void onEnable() {
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
}
}
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);
}
}, 0, 432000);

}
});

// Load up the Plugin metrics
Expand Down Expand Up @@ -257,7 +244,7 @@ private void loadPermission() {

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();
}
Expand Down Expand Up @@ -319,7 +306,7 @@ private void convertCommand(CommandSender sender, String[] args) {
}
Economy econ1 = null;
Economy econ2 = null;
String economies = "";
StringBuilder economies = new StringBuilder();
for (RegisteredServiceProvider<Economy> econ : econs) {
String econName = econ.getProvider().getName().replace(" ", "");
if (econName.equalsIgnoreCase(args[0])) {
Expand All @@ -328,9 +315,9 @@ private void convertCommand(CommandSender sender, String[] args) {
econ2 = econ.getProvider();
}
if (economies.length() > 0) {
economies += ", ";
economies.append(", ");
}
economies += econName;
economies.append(econName);
}

if (econ1 == null) {
Expand All @@ -352,49 +339,49 @@ private void convertCommand(CommandSender sender, String[] args) {
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.");
}

private void infoCommand(CommandSender sender) {
// Get String of Registered Economy Services
String registeredEcons = null;
StringBuilder registeredEcons = null;
Collection<RegisteredServiceProvider<Economy>> econs = this.getServer().getServicesManager().getRegistrations(Economy.class);
for (RegisteredServiceProvider<Economy> 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<RegisteredServiceProvider<Permission>> perms = this.getServer().getServicesManager().getRegistrations(Permission.class);
for (RegisteredServiceProvider<Permission> 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<RegisteredServiceProvider<Chat>> chats = this.getServer().getServicesManager().getRegistrations(Chat.class);
for (RegisteredServiceProvider<Chat> 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());
}
}

Expand Down Expand Up @@ -451,13 +438,13 @@ public double updateCheck(double currentVersion) {
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.");
}
Expand All @@ -472,21 +459,11 @@ private void findCustomData(Metrics metrics) {
econ = rspEcon.getProvider();
}
final String econName = econ != null ? econ.getName() : "No Economy";
metrics.addCustomChart(new SimplePie("economy", new Callable<String>() {
@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<String>() {
@Override
public String call() {
return permName;
}
}));
metrics.addCustomChart(new SimplePie("permission", () -> permName));

// Create our Chat Graph and Add our chat Plotters
RegisteredServiceProvider<Chat> rspChat = Bukkit.getServer().getServicesManager().getRegistration(Chat.class);
Expand All @@ -495,12 +472,7 @@ public String call() {
chat = rspChat.getProvider();
}
final String chatName = chat != null ? chat.getName() : "No Chat";
metrics.addCustomChart(new SimplePie("chat", new Callable<String>() {
@Override
public String call() {
return chatName;
}
}));
metrics.addCustomChart(new SimplePie("chat", () -> chatName));
}

public class VaultListener implements Listener {
Expand All @@ -509,13 +481,9 @@ public class VaultListener implements Listener {
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);
}
}
}
Expand Down

0 comments on commit 198812c

Please sign in to comment.