From dee5ba55c6f57f19628df3418a6311251925cf1e Mon Sep 17 00:00:00 2001 From: Tastybento Date: Sun, 15 Apr 2018 17:32:09 -0700 Subject: [PATCH] Updated to work with latest BSB Database --- addon.yml | 4 +- .../java/bskyblock/addon/level/Level.java | 67 +++++--------- .../java/bskyblock/addon/level/TopTen.java | 91 ++++--------------- 3 files changed, 42 insertions(+), 120 deletions(-) diff --git a/addon.yml b/addon.yml index 132afb7..4361161 100755 --- a/addon.yml +++ b/addon.yml @@ -2,7 +2,7 @@ name: BSkyBlock-Level main: bskyblock.addon.level.Level version: 0.1 -authors: [tastybento] +authors: tastybento permissions: bskyblock.intopten: @@ -11,7 +11,7 @@ permissions: bskyblock.island.level: description: Player can use level command default: true - bskyblock.island.topten: + bskyblock.island.top: description: Player can use top ten command default: true bskyblock.admin.level: diff --git a/src/main/java/bskyblock/addon/level/Level.java b/src/main/java/bskyblock/addon/level/Level.java index 98b9cde..5e51ed0 100644 --- a/src/main/java/bskyblock/addon/level/Level.java +++ b/src/main/java/bskyblock/addon/level/Level.java @@ -1,7 +1,5 @@ package bskyblock.addon.level; -import java.beans.IntrospectionException; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -17,7 +15,6 @@ import us.tastybento.bskyblock.Constants; import us.tastybento.bskyblock.api.addons.Addon; import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.user.User; -import us.tastybento.bskyblock.database.AbstractDatabaseHandler; import us.tastybento.bskyblock.database.BSBDatabase; @@ -27,15 +24,12 @@ import us.tastybento.bskyblock.database.BSBDatabase; * */ public class Level extends Addon { - + // Settings private Settings settings; // Database handler for level data - private AbstractDatabaseHandler handler; - - // The BSkyBlock database object - private BSBDatabase database; + private BSBDatabase handler; // A cache of island levels. Island levels are not kept in memory unless required. // The cache is saved when the server shuts down and the plugin is disabled. @@ -44,7 +38,7 @@ public class Level extends Addon { // The Top Ten object private TopTen topTen; - + // Level calculator private LevelPresenter levelCalc; @@ -58,10 +52,6 @@ public class Level extends Addon { levelCalc.calculateIslandLevel(user, playerUUID, b); } - public AbstractDatabaseHandler getHandler() { - return handler; - } - /** * Get level from cache for a player * @param targetPlayer @@ -73,19 +63,13 @@ public class Level extends Addon { } // Get from database LevelsData level; - try { - level = handler.loadObject(targetPlayer.toString()); - if (level == null) { - // We do not know this player, set to zero - return 0; - } - levelsCache.put(targetPlayer, level.getLevel()); - return level.getLevel(); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException - | ClassNotFoundException | IntrospectionException e) { - getLogger().severe("Could not load player's level! " + e.getMessage()); + level = handler.loadObject(targetPlayer.toString()); + if (level == null) { + // We do not know this player, set to zero + return 0; } - return 0; + levelsCache.put(targetPlayer, level.getLevel()); + return level.getLevel(); } /** @@ -94,19 +78,14 @@ public class Level extends Addon { public final Settings getSettings() { return settings; } - + public TopTen getTopTen() { return topTen; } private void load() { - try { - for (LevelsData level : handler.loadObjects()) { - levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel()); - } - } catch (InstantiationException | IllegalAccessException | InvocationTargetException - | ClassNotFoundException | IntrospectionException e) { - getLogger().severe("Could not load levels cache data! " + e.getMessage()); + for (LevelsData level : handler.loadObjects()) { + levelsCache.put(UUID.fromString(level.getUniqueId()), level.getLevel()); } } @@ -118,7 +97,6 @@ public class Level extends Addon { } } - @SuppressWarnings("unchecked") @Override public void onEnable() { // Check if it is enabled - it might be loaded, but not enabled. @@ -130,10 +108,9 @@ public class Level extends Addon { // Load the plugin's config settings = new Settings(this); // Get the BSkyBlock database - database = BSBDatabase.getDatabase(); // Set up the database handler to store and retrieve Island classes // Note that these are saved by the BSkyBlock database - handler = (AbstractDatabaseHandler) database.getHandler(LevelsData.class); + handler = new BSBDatabase<>(this, LevelsData.class); // Initialize the cache levelsCache = new HashMap<>(); // Load all the levels @@ -159,15 +136,11 @@ public class Level extends Addon { */ public void save(boolean async){ Runnable save = () -> { - try { - for (Entry en : levelsCache.entrySet()) { - LevelsData lv = new LevelsData(); - lv.setLevel(en.getValue()); - lv.setUniqueId(en.getKey().toString()); - handler.saveObject(lv); - } - } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) { - getLogger().severe("Could not save levels async! " + e.getMessage()); + for (Entry en : levelsCache.entrySet()) { + LevelsData lv = new LevelsData(); + lv.setLevel(en.getValue()); + lv.setUniqueId(en.getKey().toString()); + handler.saveObject(lv); } }; if(async){ @@ -188,4 +161,8 @@ public class Level extends Addon { topTen.addEntry(targetPlayer, level); } + public BSBDatabase getHandler() { + return handler; + } + } diff --git a/src/main/java/bskyblock/addon/level/TopTen.java b/src/main/java/bskyblock/addon/level/TopTen.java index b095282..373c6da 100644 --- a/src/main/java/bskyblock/addon/level/TopTen.java +++ b/src/main/java/bskyblock/addon/level/TopTen.java @@ -1,7 +1,5 @@ package bskyblock.addon.level; -import java.beans.IntrospectionException; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -23,7 +21,6 @@ import us.tastybento.bskyblock.api.panels.PanelItem.ClickHandler; import us.tastybento.bskyblock.api.panels.builders.PanelBuilder; import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder; import us.tastybento.bskyblock.api.user.User; -import us.tastybento.bskyblock.database.AbstractDatabaseHandler; import us.tastybento.bskyblock.database.BSBDatabase; /** @@ -38,17 +35,13 @@ public class TopTen implements Listener { private TopTenData topTenList; private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; private final boolean DEBUG = true; - private BSBDatabase database; - private AbstractDatabaseHandler handler; + private BSBDatabase handler; - @SuppressWarnings("unchecked") public TopTen(Level addon) { this.addon = addon; - // Set up database - database = BSBDatabase.getDatabase(); // Set up the database handler to store and retrieve the TopTenList class // Note that these are saved in the BSkyBlock database - handler = (AbstractDatabaseHandler) database.getHandler(TopTenData.class); + handler = new BSBDatabase<>(addon, TopTenData.class); loadTopTen(); } @@ -79,24 +72,18 @@ public class TopTen implements Listener { */ public void create() { // Obtain all the levels for each known player - AbstractDatabaseHandler levelHandler = addon.getHandler(); - try { - long index = 0; - for (LevelsData lv : levelHandler.loadObjects()) { - if (index++ % 1000 == 0) { - addon.getLogger().info("Processed " + index + " players for top ten"); - } - // Convert to UUID - UUID playerUUID = UUID.fromString(lv.getUniqueId()); - // Check if the player is an owner or team leader - if (addon.getIslands().isOwner(playerUUID)) { - topTenList.addLevel(playerUUID, lv.getLevel()); - } + BSBDatabase levelHandler = addon.getHandler(); + long index = 0; + for (LevelsData lv : levelHandler.loadObjects()) { + if (index++ % 1000 == 0) { + addon.getLogger().info("Processed " + index + " players for top ten"); + } + // Convert to UUID + UUID playerUUID = UUID.fromString(lv.getUniqueId()); + // Check if the player is an owner or team leader + if (addon.getIslands().isOwner(playerUUID)) { + topTenList.addLevel(playerUUID, lv.getLevel()); } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | SecurityException | ClassNotFoundException | IntrospectionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } saveTopTen(); } @@ -208,48 +195,12 @@ public class TopTen implements Listener { * Loads the top ten from the database */ public void loadTopTen() { - try { - topTenList = handler.loadObject("topten"); - if (topTenList == null) { - topTenList = new TopTenData(); - } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | SecurityException | ClassNotFoundException | IntrospectionException e) { - e.printStackTrace(); + topTenList = handler.loadObject("topten"); + if (topTenList == null) { + topTenList = new TopTenData(); } } - /* - @SuppressWarnings("deprecation") - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true) - public void onInventoryClick(InventoryClickEvent event) { - Inventory inventory = event.getInventory(); // The inventory that was clicked in - if (inventory.getName() == null) { - return; - } - // The player that clicked the item - Player player = (Player) event.getWhoClicked(); - if (!inventory.getTitle().equals("topten.guiTitle")) { - return; - } - event.setCancelled(true); - player.updateInventory(); - if(event.getCurrentItem() != null && event.getCurrentItem().getType().equals(Material.SKULL_ITEM) && event.getCurrentItem().hasItemMeta()){ - player.closeInventory(); - // Fire click event - TopTenClick clickEvent = new TopTenClick(((SkullMeta)event.getCurrentItem().getItemMeta()).getOwningPlayer().getName()); - addon.getServer().getPluginManager().callEvent(clickEvent); - return; - } - if (event.getSlotType().equals(SlotType.OUTSIDE)) { - player.closeInventory(); - return; - } - if (event.getClick().equals(ClickType.SHIFT_RIGHT)) { - player.closeInventory(); - return; - } - } - */ + /** * Removes ownerUUID from the top ten list * @@ -265,13 +216,7 @@ public class TopTen implements Listener { //plugin.getLogger().info("DEBUG: toptenlist = null!"); return; } - try { - handler.saveObject(topTenList); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException - | IntrospectionException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + handler.saveObject(topTenList); } }