From 2ab7a8680284c3f780f812eac09c7ffeb94c11d7 Mon Sep 17 00:00:00 2001 From: BadBones69 Date: Fri, 7 Jul 2017 16:30:35 -0400 Subject: [PATCH] v1.2.3 Update Bug Fix: - Error when MCUpdate goes down. - Error from a method that was running async when it shouldn't have been. Changes: - Cleaned up some code for easier reading. --- .classpath | 10 + .gitignore | 2 + Notes | 21 +- plugin.yml | 2 +- src/me/badbones69/crazyauctions/Category.java | 22 +- .../crazyauctions/CrazyAuctions.java | 9 +- src/me/badbones69/crazyauctions/GUI.java | 20 +- src/me/badbones69/crazyauctions/MCUpdate.java | 370 ++++++++++++------ src/me/badbones69/crazyauctions/Main.java | 18 +- src/me/badbones69/crazyauctions/Methods.java | 14 +- .../{CM.java => CurrencyManager.java} | 9 +- .../crazyauctions/currency/Vault.java | 9 + 12 files changed, 333 insertions(+), 173 deletions(-) rename src/me/badbones69/crazyauctions/currency/{CM.java => CurrencyManager.java} (92%) diff --git a/.classpath b/.classpath index 64b8e24..2d421c1 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,16 @@ + + + + + + + + + + diff --git a/.gitignore b/.gitignore index b698ab3..c3ad418 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *.prefs *.class + +bin/ diff --git a/Notes b/Notes index 934f907..2e54ca8 100644 --- a/Notes +++ b/Notes @@ -1,7 +1,18 @@ -Features: - (Request) Add it so you can sell more then just 64 of a stack. - (Request) Add a history purchase history. - (Bug) When you collect an item from the expired list with a fully inventory, the item gets lost. +Feature Requests: + - Add it so you can sell more then just 64 of a stack. + - Add a history purchase history. + - Add a taxation option. + - Add an option to broadcast a message when a player adds a new item. + - Add a black list to block names and lores. + - Add an option to set Bidding as the 1st GUI. + - Add the command translation for Spanish and Portugees. + - Add MySQL support. + - Make the command messages all configurable. + - Add a tax system for when a player buys an item. + - Add an option to search for items by ID. + +Reports: + - When you collect an item from the expired list with a fully inventory, the item gets lost. Bug Fixes: - @@ -13,4 +24,4 @@ Changes: - Removed: - - MCStats metrics has been removed due to it being dead. \ No newline at end of file + - \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index a519f75..2571740 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,7 +2,7 @@ name: CrazyAuctions author: BadBones69 main: me.badbones69.crazyauctions.Main website: https://www.spigotmc.org/resources/authors/kicjow.9719/ -version: 1.2.2 +version: 1.2.3 depend: [Vault] description: > A plugin to auction off items globally. diff --git a/src/me/badbones69/crazyauctions/Category.java b/src/me/badbones69/crazyauctions/Category.java index 7bcf2ab..8a4ac5b 100644 --- a/src/me/badbones69/crazyauctions/Category.java +++ b/src/me/badbones69/crazyauctions/Category.java @@ -6,9 +6,14 @@ import org.bukkit.Material; public enum Category{ - NONE("None", new ArrayList()), OTHER("Other", getOthers()), - ARMOR("Armor", getArmor()), WEAPONS("Weapons", getWeapons()), TOOLS("Tools", getTools()), - FOOD("Food", getFood()), POTIONS("Potions", getPotions()), BLOCKS("Blocks", getBlocks()); + NONE("None", new ArrayList()), + OTHER("Other", getOthers()), + ARMOR("Armor", getArmor()), + WEAPONS("Weapons", getWeapons()), + TOOLS("Tools", getTools()), + FOOD("Food", getFood()), + POTIONS("Potions", getPotions()), + BLOCKS("Blocks", getBlocks()); private String Name; private ArrayList Items; @@ -18,8 +23,8 @@ public enum Category{ * @param name Name of the Shop Type. */ private Category(String name, ArrayList items){ - this.Name=name; - this.Items=items; + this.Name = name; + this.Items = items; } /** @@ -70,6 +75,7 @@ public enum Category{ ma.add(Material.DIAMOND_BOOTS); return ma; } + private static ArrayList getTools(){ ArrayList ma = new ArrayList(); ma.add(Material.WOOD_PICKAXE); @@ -90,6 +96,7 @@ public enum Category{ ma.add(Material.DIAMOND_HOE); return ma; } + private static ArrayList getWeapons(){ ArrayList ma = new ArrayList(); ma.add(Material.WOOD_SWORD); @@ -103,6 +110,7 @@ public enum Category{ ma.add(Material.BOW); return ma; } + private static ArrayList getFood(){ ArrayList ma = new ArrayList(); for(Material m : Material.values()){ @@ -112,11 +120,13 @@ public enum Category{ } return ma; } + private static ArrayList getPotions(){ ArrayList ma = new ArrayList(); ma.add(Material.POTION); return ma; } + private static ArrayList getBlocks(){ ArrayList ma = new ArrayList(); for(Material m : Material.values()){ @@ -126,6 +136,7 @@ public enum Category{ } return ma; } + private static ArrayList getOthers(){ ArrayList ma = new ArrayList(); for(Material m : Material.values()){ @@ -136,4 +147,5 @@ public enum Category{ } return ma; } + } \ No newline at end of file diff --git a/src/me/badbones69/crazyauctions/CrazyAuctions.java b/src/me/badbones69/crazyauctions/CrazyAuctions.java index ae5708c..92a283b 100644 --- a/src/me/badbones69/crazyauctions/CrazyAuctions.java +++ b/src/me/badbones69/crazyauctions/CrazyAuctions.java @@ -8,9 +8,12 @@ import org.bukkit.inventory.ItemStack; public class CrazyAuctions { - static CrazyAuctions instance = new CrazyAuctions(); + public static CrazyAuctions instance; public static CrazyAuctions getInstance() { + if(instance == null){ + instance = new CrazyAuctions(); + } return instance; } @@ -33,7 +36,7 @@ public class CrazyAuctions { if(data.contains("Items")){ for(String i : data.getConfigurationSection("Items").getKeys(false)){ if(data.getString("Items." + i + ".Seller").equalsIgnoreCase(player.getName())){ - if(data.getBoolean("Items."+i+".Biddable")){ + if(data.getBoolean("Items." + i + ".Biddable")){ if(type == Shop.BID){ items.add(data.getItemStack("Items."+i+".Item").clone()); } @@ -48,4 +51,4 @@ public class CrazyAuctions { return items; } -} +} \ No newline at end of file diff --git a/src/me/badbones69/crazyauctions/GUI.java b/src/me/badbones69/crazyauctions/GUI.java index 62c0ea7..0360804 100644 --- a/src/me/badbones69/crazyauctions/GUI.java +++ b/src/me/badbones69/crazyauctions/GUI.java @@ -19,7 +19,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; -import me.badbones69.crazyauctions.currency.CM; +import me.badbones69.crazyauctions.currency.CurrencyManager; public class GUI implements Listener{ @@ -43,7 +43,7 @@ public class GUI implements Listener{ data.set("Items.Clear", null); Main.settings.saveData(); } - if(cat!=null){ + if(cat != null){ Cat.put(player, cat); }else{ Cat.put(player, Category.NONE); @@ -51,7 +51,7 @@ public class GUI implements Listener{ if(data.contains("Items")){ for(String i : data.getConfigurationSection("Items").getKeys(false)){ List lore = new ArrayList(); - if(cat.getItems().contains(data.getItemStack("Items."+i+".Item").getType())||cat==Category.NONE){ + if(cat.getItems().contains(data.getItemStack("Items."+i+".Item").getType()) || cat == Category.NONE){ if(data.getBoolean("Items."+i+".Biddable")){ if(sell==Shop.BID){ String seller = data.getString("Items."+i+".Seller"); @@ -493,9 +493,9 @@ public class GUI implements Listener{ String ID = BiddingID.get(player); int bid = Bidding.get(player); String topBidder = data.getString("Items."+ID+".TopBidder"); - if(CM.getMoney(player)bid){ @@ -705,7 +705,7 @@ public class GUI implements Listener{ return; } Long cost = data.getLong("Items."+i+".Price"); - if(CM.getMoney(player) { - report(); - if (!upToDate) { - pl.getServer().getConsoleSender().sendMessage(format(updateMessage)); + /** + * Start up the MCUpdater. + * + * @param plugin + * The plugin using this. + * @throws IOException + */ + public MCUpdate(Plugin plugin) throws IOException { + if (plugin != null) { + this.pl = plugin; + // I should add a custom configuration for MCUpdate itself + Bukkit.getPluginManager().registerEvents(this, plugin); + setPingInterval(900); } - }, 0, PING_INTERVAL * 20); - } - return true; - } + } - private int getOnlinePlayers() { - try { - Method onlinePlayerMethod = Server.class.getMethod("getOnlinePlayers"); - if (onlinePlayerMethod.getReturnType().equals(Collection.class)) { - return ((Collection) onlinePlayerMethod.invoke(Bukkit.getServer())).size(); - } else { - return ((Player[]) onlinePlayerMethod.invoke(Bukkit.getServer())).length; - } - } catch (Exception ex) { - } - return 0; - } + /** + * + * Start up the MCUpdater. + * + * @param plugin + * The plugin using this. + * @param activate + * Toggle if it starts the MCUpdater when used. + * @throws IOException + */ + public MCUpdate(Plugin plugin, Boolean activate) throws IOException { + if (plugin != null) { + this.pl = plugin; + // I should add a custom configuration for MCUpdate itself + Bukkit.getPluginManager().registerEvents(this, plugin); + setPingInterval(900); + if (activate) { + startLogging(); + } + } + } - private void report() { - String ver = pl.getDescription().getVersion(); - String name = pl.getDescription().getName(); - int playersOnline = this.getOnlinePlayers(); - boolean onlineMode = pl.getServer().getOnlineMode(); - String serverVersion = pl.getServer().getVersion(); + /** + * Call when you wan't to start the updater. + * + * @return True if everything starts and false if it doesn't start. + */ + public boolean startLogging() { + // Is MCUpdate already running? + if (task == null) { + // Begin hitting the server with glorious data + task = pl.getServer().getScheduler().runTaskTimerAsynchronously(pl, () -> { + report(); + if (!upToDate) { + if (checkUpdate) { + pl.getServer().getConsoleSender().sendMessage(format(updateMessage)); + } + } + }, 0, PING_INTERVAL * 20); + } + return true; + } - String osname = System.getProperty("os.name"); - String osarch = System.getProperty("os.arch"); - String osversion = System.getProperty("os.version"); - String java_version = System.getProperty("java.version"); - int coreCount = Runtime.getRuntime().availableProcessors(); + /** + * Call when you want to stop the updater. + * + * @return True if it successfully stoped and false if couldn't. + */ + public boolean stopLogging() { + if (task != null) { + try { + task.cancel(); + return true; + } catch (Exception e) { + } + } else { + return true; + } + return false; + } - String report = "{ \"report\": {"; - report += toJson("plugin", name) + ","; - report += toJson("version", ver) + ","; - report += toJson("playersonline", playersOnline + "") + ","; - report += toJson("onlinemode", onlineMode + "") + ","; - report += toJson("serverversion", serverVersion) + ","; + /** + * Check if MCUpdate is logging information. + * + * @return True if it is logging info and false if not. + */ + public Boolean isLogging() { + return task != null; + } - report += toJson("osname", osname) + ","; - report += toJson("osarch", osarch) + ","; - report += toJson("osversion", osversion) + ","; - report += toJson("javaversion", java_version) + ","; - report += toJson("corecount", coreCount + "") + ""; + /** + * Set if the updater uses the internal update checker. + * + * @param checkUpdate + * True if you want to use the internal update checker and false + * if not. + */ + public void checkUpdate(Boolean checkUpdate) { + this.checkUpdate = checkUpdate; + } - report += "} }"; + /** + * Checks if the internal updater is active. + * + * @return True if the internal updater is activated and false if not. + */ + public Boolean needsUpdated() { + return checkUpdate; + } - byte[] data = report.getBytes(); + /** + * Set the rate the information is sent to MCUpdate.org. + * + * @param PING_INTERVAL + * The rate at which the data is sent in seconds. + */ + public void setPingInterval(int PING_INTERVAL) { + this.PING_INTERVAL = PING_INTERVAL; + } - try { + /** + * Get the rate which the data is sent to MCUpdate.org. + * + * @return The rate the data is sent in seconds. + */ + public int getPingInterval() { + return PING_INTERVAL; + } - URL url = new URL(BASE_URL); - URLConnection c = url.openConnection(); - c.setConnectTimeout(2500); - c.setReadTimeout(3500); + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + Player p = e.getPlayer(); + if (p.isOp() && !upToDate) { + if (checkUpdate) { + p.sendMessage(format(updateMessage)); + } + } + } - c.addRequestProperty("User-Agent", "MCUPDATE/" + VERSION); - c.addRequestProperty("Content-Type", "application/json"); - c.addRequestProperty("Content-Length", Integer.toString(data.length)); - c.addRequestProperty("Accept", "application/json"); - c.addRequestProperty("Connection", "close"); + private int getOnlinePlayers() { + try { + Method onlinePlayerMethod = Server.class.getMethod("getOnlinePlayers"); + if (onlinePlayerMethod.getReturnType().equals(Collection.class)) { + return ((Collection) onlinePlayerMethod.invoke(Bukkit.getServer())).size(); + } else { + return ((Player[]) onlinePlayerMethod.invoke(Bukkit.getServer())).length; + } + } catch (Exception ex) { + } + return 0; + } - c.setDoOutput(true); + private void report() { + String ver = pl.getDescription().getVersion(); + String name = pl.getDescription().getName(); + int playersOnline = this.getOnlinePlayers(); + boolean onlineMode = pl.getServer().getOnlineMode(); + String serverVersion = pl.getServer().getVersion(); - OutputStream os = c.getOutputStream(); - os.write(data); - os.flush(); + String osname = System.getProperty("os.name"); + String osarch = System.getProperty("os.arch"); + String osversion = System.getProperty("os.version"); + String java_version = System.getProperty("java.version"); + int coreCount = Runtime.getRuntime().availableProcessors(); - BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); - String endData = br.readLine().trim(); + String report = "{ \"report\": {"; + report += toJson("plugin", name) + ","; + report += toJson("version", ver) + ","; + report += toJson("playersonline", playersOnline + "") + ","; + report += toJson("onlinemode", onlineMode + "") + ","; + report += toJson("serverversion", serverVersion) + ","; - String serverMessage = getString(endData, "message"); - String cVersion = getString(endData, "pl_Version"); - updateMessage = getString(endData, "update_Message"); + report += toJson("osname", osname) + ","; + report += toJson("osarch", osarch) + ","; + report += toJson("osversion", osversion) + ","; + report += toJson("javaversion", java_version) + ","; + report += toJson("corecount", coreCount + "") + ""; - if (!serverMessage.equals("ERROR")) { - if (!ver.equals(cVersion)) { - upToDate = false; - } - } - br.close(); + report += "} }"; - } catch (IOException ignored) { - } - } + byte[] data = report.getBytes(); - private String getString(String data, String key) { - String dat = data.replace("{ \"Response\": {\"", ""); - dat = dat.replace("\"} }", ""); - List list = Arrays.asList(dat.split("\",\"")); + try { - for (String stub : list) { - List list2 = Arrays.asList(stub.split("\":\"")); - if (key.equals(list2.get(0))) { - return list2.get(1); - } - } - return null; - } + URL url = new URL(BASE_URL); + URLConnection c = url.openConnection(); + c.setConnectTimeout(2500); + c.setReadTimeout(3500); - private static String toJson(String key, String value) { - return "\"" + key + "\":\"" + value + "\""; - } + c.addRequestProperty("User-Agent", "MCUPDATE/" + VERSION); + c.addRequestProperty("Content-Type", "application/json"); + c.addRequestProperty("Content-Length", Integer.toString(data.length)); + c.addRequestProperty("Accept", "application/json"); + c.addRequestProperty("Connection", "close"); - private static String format(String format) { - return ChatColor.translateAlternateColorCodes('&', format); - } + c.setDoOutput(true); + + OutputStream os = c.getOutputStream(); + os.write(data); + os.flush(); + + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); + String endData = br.readLine().trim(); + + String serverMessage = getString(endData, "message"); + String cVersion = getString(endData, "pl_Version"); + updateMessage = getString(endData, "update_Message"); + + if (serverMessage != null) { + if (!serverMessage.equals("ERROR")) { + if (cVersion != null) { + if (!ver.equals(cVersion)) { + upToDate = false; + } + } + } + } + br.close(); + + } catch (Exception ignored) { + } + } + + private String getString(String data, String key) { + String dat = data.replace("{ \"Response\": {\"", ""); + dat = dat.replace("\"} }", ""); + List list = Arrays.asList(dat.split("\",\"")); + + for (String stub : list) { + List list2 = Arrays.asList(stub.split("\":\"")); + if (key.equals(list2.get(0))) { + return list2.get(1); + } + } + return ""; + } + + private static String toJson(String key, String value) { + return "\"" + key + "\":\"" + value + "\""; + } + + private static String format(String format) { + if (format != null) { + return ChatColor.translateAlternateColorCodes('&', format); + } else { + return ""; + } + } - public void setPingInterval(int PING_INTERVAL) { - this.PING_INTERVAL = PING_INTERVAL; - } } \ No newline at end of file diff --git a/src/me/badbones69/crazyauctions/Main.java b/src/me/badbones69/crazyauctions/Main.java index 7a36997..21ca7a7 100644 --- a/src/me/badbones69/crazyauctions/Main.java +++ b/src/me/badbones69/crazyauctions/Main.java @@ -23,13 +23,7 @@ public class Main extends JavaPlugin implements Listener{ public static SettingsManager settings = SettingsManager.getInstance(); public static CrazyAuctions auc = CrazyAuctions.getInstance(); - int file = 0; - - @Override - public void onDisable(){ - Bukkit.getScheduler().cancelTask(file); - settings.saveData(); - } + private int file = 0; @Override public void onEnable(){ @@ -54,6 +48,12 @@ public class Main extends JavaPlugin implements Listener{ } catch (IOException e) {} } + @Override + public void onDisable(){ + Bukkit.getScheduler().cancelTask(file); + settings.saveData(); + } + public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args){ if(commandLable.equalsIgnoreCase("CrazyAuctions") || commandLable.equalsIgnoreCase("CrazyAuction") || commandLable.equalsIgnoreCase("CA") || commandLable.equalsIgnoreCase("AH") @@ -299,7 +299,7 @@ public class Main extends JavaPlugin implements Listener{ public void run() { Methods.updateAuction(); } - }.runTaskTimerAsynchronously(this, 20, 5*20); + }.runTaskTimer(this, 20, 5*20); } private ArrayList getDamageableItems(){ @@ -355,4 +355,4 @@ public class Main extends JavaPlugin implements Listener{ return ma; } -} +} \ No newline at end of file diff --git a/src/me/badbones69/crazyauctions/Methods.java b/src/me/badbones69/crazyauctions/Methods.java index 196c66e..35a8a58 100644 --- a/src/me/badbones69/crazyauctions/Methods.java +++ b/src/me/badbones69/crazyauctions/Methods.java @@ -23,20 +23,18 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.Plugin; -import me.badbones69.crazyauctions.currency.CM; +import me.badbones69.crazyauctions.currency.CurrencyManager; public class Methods { public static Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("CrazyAuctions"); public static String color(String msg){ - msg = ChatColor.translateAlternateColorCodes('&', msg); - return msg; + return ChatColor.translateAlternateColorCodes('&', msg); } public static String removeColor(String msg){ - msg = ChatColor.stripColor(msg); - return msg; + return ChatColor.stripColor(msg); } public static String getPrefix(){ @@ -399,12 +397,12 @@ public class Methods { if(cal.after(expireTime)){ int num = 1; for(;data.contains("OutOfTime/Cancelled."+num);num++); - if(data.getBoolean("Items."+i+".Biddable")&&!data.getString("Items."+i+".TopBidder").equalsIgnoreCase("None")&&CM.getMoney(Methods.getPlayer(data.getString("Items."+i+".TopBidder")))>=data.getInt("Items."+i+".Price")){ + if(data.getBoolean("Items."+i+".Biddable")&&!data.getString("Items."+i+".TopBidder").equalsIgnoreCase("None")&&CurrencyManager.getMoney(Methods.getPlayer(data.getString("Items."+i+".TopBidder")))>=data.getInt("Items."+i+".Price")){ String winner = data.getString("Items."+i+".TopBidder"); String seller = data.getString("Items."+i+".Seller"); Long price = data.getLong("Items."+i+".Price"); - CM.addMoney(Methods.getOfflinePlayer(seller), price); - CM.removeMoney(Methods.getOfflinePlayer(winner), price); + CurrencyManager.addMoney(Methods.getOfflinePlayer(seller), price); + CurrencyManager.removeMoney(Methods.getOfflinePlayer(winner), price); if(Methods.isOnline(winner)){ Player player = Methods.getPlayer(winner); player.sendMessage(Methods.getPrefix()+Methods.color(msg.getString("Messages.Win-Bidding") diff --git a/src/me/badbones69/crazyauctions/currency/CM.java b/src/me/badbones69/crazyauctions/currency/CurrencyManager.java similarity index 92% rename from src/me/badbones69/crazyauctions/currency/CM.java rename to src/me/badbones69/crazyauctions/currency/CurrencyManager.java index bd6aef3..ffdf155 100644 --- a/src/me/badbones69/crazyauctions/currency/CM.java +++ b/src/me/badbones69/crazyauctions/currency/CurrencyManager.java @@ -6,7 +6,8 @@ import org.bukkit.entity.Player; import me.badbones69.crazyauctions.Main; -public enum CM { // Currency Manager +public enum CurrencyManager { // Currency Manager + VAULT("Vault", "Money"); String PluginName, Name; @@ -17,7 +18,7 @@ public enum CM { // Currency Manager * @param name * Name of the Currency. */ - private CM(String pluginname, String name) { + private CurrencyManager(String pluginname, String name) { this.PluginName = pluginname; this.Name = name; } @@ -41,8 +42,8 @@ public enum CM { // Currency Manager * Name of the Type you want. * @return Returns the Currency as a Enum. */ - public static CM getFromName(String name) { - for (CM type : CM.values()) { + public static CurrencyManager getFromName(String name) { + for (CurrencyManager type : CurrencyManager.values()) { if (type.getPluginName().equalsIgnoreCase(name)) { return type; } diff --git a/src/me/badbones69/crazyauctions/currency/Vault.java b/src/me/badbones69/crazyauctions/currency/Vault.java index 8852ec9..9614437 100644 --- a/src/me/badbones69/crazyauctions/currency/Vault.java +++ b/src/me/badbones69/crazyauctions/currency/Vault.java @@ -9,14 +9,17 @@ import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.EconomyResponse; public class Vault { + public static Economy econ = null; public static EconomyResponse r; + public static boolean hasVault(){ if(Bukkit.getServer().getPluginManager().getPlugin("Vault")!=null){ return true; } return false; } + public static boolean setupEconomy(){ if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null){ return false; @@ -28,22 +31,28 @@ public class Vault { econ = rsp.getProvider(); return econ != null; } + public static Long getMoney(Player player){ if(player != null){ return (long) econ.getBalance(player); } return 0L; } + public static void removeMoney(Player player, Long amount){ econ.withdrawPlayer(player, amount); } + public static void removeMoney(OfflinePlayer player, Long amount){ econ.withdrawPlayer(player, amount); } + public static void addMoney(Player player, Long amount){ econ.depositPlayer(player, amount); } + public static void addMoney(OfflinePlayer player, Long amount){ econ.depositPlayer(player, amount); } + } \ No newline at end of file