From dc416e91764da3a314e4ba1659203194fa72c908 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 24 Oct 2017 16:24:43 -0700 Subject: [PATCH] Added top ten functionality. Still needs more testing, but works on mysql. --- Level/locales/bsb_en_US.yml | 7 +- Level/src/bskyblock/addin/level/Commands.java | 45 +++++- Level/src/bskyblock/addin/level/Level.java | 89 ++++++++--- .../addin/level/LevelCalcByChunk.java | 24 +-- .../bskyblock/addin/level/LevelPlugin.java | 24 ++- Level/src/bskyblock/addin/level/TopTen.java | 146 ++++++++++-------- .../addin/level/database/object/Levels.java | 28 +--- .../level/database/object/TopTenList.java | 77 +++++++++ 8 files changed, 298 insertions(+), 142 deletions(-) create mode 100644 Level/src/bskyblock/addin/level/database/object/TopTenList.java diff --git a/Level/locales/bsb_en_US.yml b/Level/locales/bsb_en_US.yml index 8c3e1f0..4a8eba7 100755 --- a/Level/locales/bsb_en_US.yml +++ b/Level/locales/bsb_en_US.yml @@ -12,4 +12,9 @@ island: islandLevelIs: "Island level is" requiredPointsToNextLevel: "[points] points required until the next level" -levelDeaths: "([number] deaths)" \ No newline at end of file +levelDeaths: "([number] deaths)" +topten: + guiTitle: "Top Ten" + guiHeading: "[name]:[rank]" + islandLevel: "Level [level]" + \ No newline at end of file diff --git a/Level/src/bskyblock/addin/level/Commands.java b/Level/src/bskyblock/addin/level/Commands.java index 5567bb6..2945371 100644 --- a/Level/src/bskyblock/addin/level/Commands.java +++ b/Level/src/bskyblock/addin/level/Commands.java @@ -22,7 +22,7 @@ public class Commands extends CalculateLevel { } private void setupCommands() { - BSkyBlock bSkyBlock = BSkyBlock.getPlugin(); + // level command bSkyBlock.addSubCommand(new ArgumentHandler("island") { @Override @@ -32,17 +32,17 @@ public class Commands extends CalculateLevel { @Override public void execute(CommandSender sender, String[] args) { - plugin.getLogger().info("DEBUG: " + args); + //getLogger().info("DEBUG: " + args); if (args.length > 0) { // Asking for another player's level? - // Convert name to a UUID + // Convert name to a UUID final UUID playerUUID = bSkyBlock.getPlayers().getUUID(args[0], true); - plugin.getLogger().info("DEBUG: console player info UUID = " + playerUUID); + //getLogger().info("DEBUG: console player info UUID = " + playerUUID); if (playerUUID == null) { - Util.sendMessage(sender, ChatColor.RED + plugin.getLocale(sender).get("error.UnknownPlayer")); + sendMessage(sender, ChatColor.RED + getLocale(sender).get("error.UnknownPlayer")); return; } else { - Util.sendMessage(sender, ChatColor.GREEN + "Level is " + plugin.getIslandLevel(playerUUID)); + sendMessage(sender, ChatColor.GREEN + "Level is " + plugin.getIslandLevel(playerUUID)); return; } } @@ -76,7 +76,36 @@ public class Commands extends CalculateLevel { } }.alias("level")); - // Admin command + // top command + bSkyBlock.addSubCommand(new ArgumentHandler("island") { + + @Override + public CanUseResp canUse(CommandSender sender) { + if (sender instanceof Player) { + VaultHelper.hasPerm((Player)sender, Settings.PERMPREFIX + "island.topten"); + return new CanUseResp(true); + } + return new CanUseResp(false); + } + + @Override + public void execute(CommandSender sender, String[] args) { + plugin.getTopTen().topTenShow((Player)sender); + return; + } + + @Override + public Set tabComplete(CommandSender sender, String[] args) { + return null; + } + + @Override + public String[] usage(CommandSender sender) { + return new String[]{"", "View top ten"}; + } + }.alias("top")); + + // Admin level command bSkyBlock.addSubCommand(new ArgumentHandler("bsadmin") { @Override @@ -91,7 +120,7 @@ public class Commands extends CalculateLevel { } else { // Convert name to a UUID final UUID playerUUID = bSkyBlock.getPlayers().getUUID(args[0], true); - plugin.getLogger().info("DEBUG: console player info UUID = " + playerUUID); + //plugin.getLogger().info("DEBUG: console player info UUID = " + playerUUID); if (playerUUID == null) { Util.sendMessage(sender, ChatColor.RED + plugin.getLocale(sender).get("error.UnknownPlayer")); return; diff --git a/Level/src/bskyblock/addin/level/Level.java b/Level/src/bskyblock/addin/level/Level.java index 8b88b84..b0d77e7 100644 --- a/Level/src/bskyblock/addin/level/Level.java +++ b/Level/src/bskyblock/addin/level/Level.java @@ -3,6 +3,8 @@ package bskyblock.addin.level; import java.beans.IntrospectionException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map.Entry; import java.util.UUID; import org.bukkit.command.CommandSender; @@ -17,50 +19,59 @@ import us.tastybento.bskyblock.config.BSBLocale; import us.tastybento.bskyblock.database.BSBDatabase; import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler; +/** + * Addin to BSkyBlock that enables island level scoring and top ten functionality + * @author tastybento + * + */ public class Level extends JavaPlugin { + + // The BSkyBlock plugin instance. private BSkyBlock bSkyBlock; // Level calc checker BukkitTask checker = null; + // Locale manager for this plugin private LocaleManager localeManager; + // Database handler for level data private AbstractDatabaseHandler handler; + // The BSkyBlock database object private BSBDatabase database; - private Levels levelsCache; + // 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. + // TODO: Save regularly to avoid crash issues. + private HashMap levelsCache; + // The Top Ten object + private TopTen topTen; + + @SuppressWarnings("unchecked") @Override public void onEnable() { // Load the plugin's config new PluginConfig(this); - // Get the BSkyBlock plugin + // Get the BSkyBlock plugin. This will be available because this plugin depends on it in plugin.yml. bSkyBlock = BSkyBlock.getPlugin(); + // Check if it is enabled - it might be loaded, but not enabled. if (!bSkyBlock.isEnabled()) { this.setEnabled(false); return; } - // Set up database + // 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(bSkyBlock, Levels.class); - // Load the levels to a cache - levelsCache = new Levels(); - try { - levelsCache = handler.loadObject("addon-levels"); - if (levelsCache == null) { - levelsCache = new Levels(); - } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException - | InvocationTargetException | SecurityException | ClassNotFoundException | IntrospectionException - | SQLException e) { - e.printStackTrace(); - } - // Start the top ten - new TopTen(this); + // Initialize the cache + levelsCache = new HashMap<>(); + // Start the top ten and register it for clicks + topTen = new TopTen(this); + getServer().getPluginManager().registerEvents(topTen, this); // Local locales localeManager = new LocaleManager(this); // Register commands @@ -83,7 +94,12 @@ public class Level extends JavaPlugin { public void save(boolean async){ Runnable save = () -> { try { - handler.saveObject(levelsCache); + for (Entry en : levelsCache.entrySet()) { + Levels lv = new Levels(); + lv.setLevel(en.getValue()); + lv.setUniqueId(en.getKey().toString()); + handler.saveObject(lv); + } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) { // TODO Auto-generated catch block @@ -102,20 +118,39 @@ public class Level extends JavaPlugin { * @param targetPlayer * @return Level of player */ - public Long getIslandLevel(UUID targetPlayer) { + public long getIslandLevel(UUID targetPlayer) { //getLogger().info("DEBUG: getting island level for " + bSkyBlock.getPlayers().getName(targetPlayer)); - return levelsCache.getLevel(targetPlayer); + if (levelsCache.containsKey(targetPlayer)) { + return levelsCache.get(targetPlayer); + } + // Get from database + Levels 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 | IllegalArgumentException | InvocationTargetException + | SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return 0; } /** - * Save the player's level in the local cache + * Save the player's level * @param targetPlayer * @param level */ public void setIslandLevel(UUID targetPlayer, long level) { //getLogger().info("DEBUG: set island level to " + level + " for " + bSkyBlock.getPlayers().getName(targetPlayer)); - levelsCache.addLevel(targetPlayer, level); - save(true); + // Add to cache + levelsCache.put(targetPlayer, level); + topTen.topTenAddEntry(targetPlayer, level); } /** @@ -136,4 +171,12 @@ public class Level extends JavaPlugin { return localeManager.getLocale(uuid); } + public AbstractDatabaseHandler getHandler() { + return handler; + } + + public TopTen getTopTen() { + return topTen; + } + } diff --git a/Level/src/bskyblock/addin/level/LevelCalcByChunk.java b/Level/src/bskyblock/addin/level/LevelCalcByChunk.java index f617dbf..257db66 100644 --- a/Level/src/bskyblock/addin/level/LevelCalcByChunk.java +++ b/Level/src/bskyblock/addin/level/LevelCalcByChunk.java @@ -178,7 +178,7 @@ public class LevelCalcByChunk { plugin.getLogger().info("Block is outside protected area - z = " + (chunk.getZ() * 16 + z)); continue; } - + for (int y = 0; y < worldHeight; y++) { int type = chunk.getBlockTypeId(x, y, z); int data = chunk.getBlockData(x, y, z); @@ -359,7 +359,7 @@ public class LevelCalcByChunk { reportLines.add(type.getElement().toString() + ": " + String.format("%,d",type.getCount()) + " blocks (max " + limit + explain); } reportLines.add("=================================="); - reportLines.add("Blocks on island that are not in blockvalues.yml"); + reportLines.add("Blocks on island that are not in config.yml"); reportLines.add("Total number = " + String.format("%,d",ncCount.size())); //entriesSortedByCount = Multisets.copyHighestCountFirst(ncCount).entrySet(); entriesSortedByCount = ncCount.entrySet(); @@ -393,13 +393,10 @@ public class LevelCalcByChunk { if (DEBUG) plugin.getLogger().info("DEBUG: updating player"); - if (oldLevel != event.getLevel()) { - // Update player and team mates - plugin.setIslandLevel(targetPlayer, event.getLevel()); - if (DEBUG) - plugin.getLogger().info("DEBUG: set island level, now trying to save player"); - bSkyBlock.getPlayers().save(targetPlayer); - } + //if (oldLevel != event.getLevel()) { + // Update player and team mates + plugin.setIslandLevel(targetPlayer, event.getLevel()); + //} if (DEBUG) plugin.getLogger().info("DEBUG: save player, now looking at team members"); // Update any team members too @@ -415,15 +412,6 @@ public class LevelCalcByChunk { } if (DEBUG) { plugin.getLogger().info("DEBUG: finished team member saving"); - plugin.getLogger().info("DEBUG: updating top ten"); - } - if (bSkyBlock.getPlayers().inTeam(targetPlayer)) { - UUID leader = bSkyBlock.getIslands().getTeamLeader(targetPlayer); - if (leader != null) { - TopTen.topTenAddEntry(leader, event.getLevel()); - } - } else { - TopTen.topTenAddEntry(targetPlayer, event.getLevel()); } } diff --git a/Level/src/bskyblock/addin/level/LevelPlugin.java b/Level/src/bskyblock/addin/level/LevelPlugin.java index 64d93b2..022b90f 100644 --- a/Level/src/bskyblock/addin/level/LevelPlugin.java +++ b/Level/src/bskyblock/addin/level/LevelPlugin.java @@ -1,20 +1,36 @@ package bskyblock.addin.level; +import java.util.logging.Logger; + +import org.bukkit.command.CommandSender; + import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.config.BSBLocale; +import us.tastybento.bskyblock.util.Util; /** * Makes code look nicer * @author ben * */ -public class LevelPlugin { - protected Level plugin; - protected BSkyBlock bSkyBlock; +public abstract class LevelPlugin { + protected final Level plugin; + protected final BSkyBlock bSkyBlock; public LevelPlugin(Level plugin) { - super(); this.plugin = plugin; this.bSkyBlock = BSkyBlock.getPlugin(); } + public final Logger getLogger() { + return plugin.getLogger(); + } + + public final void sendMessage(CommandSender sender, String message) { + Util.sendMessage(sender, message); + } + + public final BSBLocale getLocale(CommandSender sender) { + return plugin.getLocale(sender); + } } diff --git a/Level/src/bskyblock/addin/level/TopTen.java b/Level/src/bskyblock/addin/level/TopTen.java index 90e019e..1255e89 100644 --- a/Level/src/bskyblock/addin/level/TopTen.java +++ b/Level/src/bskyblock/addin/level/TopTen.java @@ -17,9 +17,10 @@ package bskyblock.addin.level; -import java.io.File; +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -30,7 +31,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -43,8 +43,11 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; import bskyblock.addin.level.config.Settings; -import bskyblock.addin.level.util.MapUtil; +import bskyblock.addin.level.database.object.Levels; +import bskyblock.addin.level.database.object.TopTenList; import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.database.BSBDatabase; +import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler; /** * Handles all Top Ten List functions @@ -52,18 +55,27 @@ import us.tastybento.bskyblock.BSkyBlock; * @author tastybento * */ -public class TopTen implements Listener{ - private static Level plugin; +public class TopTen implements Listener { + private Level plugin; // Top ten list of players - private static Map topTenList = new HashMap(); - private static final int GUISIZE = 27; // Must be a multiple of 9 - private static final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; - private static final boolean DEBUG = false; - // Store this as a static because it's the same for everyone and saves memory cleanup - private static Inventory gui; + private TopTenList topTenList; + private final int GUISIZE = 27; // Must be a multiple of 9 + private final int[] SLOTS = new int[] {4, 12, 14, 19, 20, 21, 22, 23, 24, 25}; + private final boolean DEBUG = false; + // Store this as a because it's the same for everyone and saves memory cleanup + private Inventory gui; + private BSBDatabase database; + private AbstractDatabaseHandler handler; + @SuppressWarnings("unchecked") public TopTen(Level plugin) { - TopTen.plugin = plugin; + this.plugin = plugin; + // 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(BSkyBlock.getPlugin(), TopTenList.class); + topTenLoad(); } /** @@ -72,15 +84,7 @@ public class TopTen implements Listener{ * @param ownerUUID * @param l */ - public static void topTenAddEntry(UUID ownerUUID, long l) { - // Special case for removals. If a level of zero is given the player - // needs to be removed from the list - if (l < 1) { - if (topTenList.containsKey(ownerUUID)) { - topTenList.remove(ownerUUID); - } - return; - } + public void topTenAddEntry(UUID ownerUUID, long l) { // Try and see if the player is online Player player = plugin.getServer().getPlayer(ownerUUID); if (player != null) { @@ -90,8 +94,8 @@ public class TopTen implements Listener{ return; } } - topTenList.put(ownerUUID, l); - topTenList = MapUtil.sortByValue(topTenList); + topTenList.addLevel(ownerUUID, l); + topTenSave(); } /** @@ -99,7 +103,7 @@ public class TopTen implements Listener{ * * @param ownerUUID */ - public static void topTenRemoveEntry(UUID ownerUUID) { + public void topTenRemoveEntry(UUID ownerUUID) { topTenList.remove(ownerUUID); } @@ -107,7 +111,7 @@ public class TopTen implements Listener{ * Generates a sorted map of islands for the Top Ten list from all player * files */ - public static void topTenCreate() { + public void topTenCreate() { topTenCreate(null); } @@ -117,44 +121,58 @@ public class TopTen implements Listener{ * Runs asynchronously from the main thread. * @param sender */ - public static void topTenCreate(final CommandSender sender) { - // TODO + public void topTenCreate(final CommandSender sender) { + // Obtain all the levels for each known player + AbstractDatabaseHandler levelHandler = plugin.getHandler(); + try { + long index = 0; + for (Levels lv : levelHandler.loadObjects()) { + if (index++ % 1000 == 0) { + plugin.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 (BSkyBlock.getPlugin().getIslands().isOwner(playerUUID)) { + topTenList.addLevel(playerUUID, lv.getLevel()); + } + } + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + topTenSave(); } - public static void topTenSave() { + public void topTenSave() { + //plugin.getLogger().info("Saving top ten list"); if (topTenList == null) { + //plugin.getLogger().info("DEBUG: toptenlist = null!"); return; } - plugin.getLogger().info("Saving top ten list"); - // Make file - File topTenFile = new File(plugin.getDataFolder(), "topten.yml"); - // Make configuration - YamlConfiguration config = new YamlConfiguration(); - // Save config - - int rank = 0; - for (Map.Entry m : topTenList.entrySet()) { - if (rank++ == 10) { - break; - } - config.set("topten." + m.getKey().toString(), m.getValue()); - } try { - config.save(topTenFile); - plugin.getLogger().info("Saved top ten list"); - } catch (Exception e) { - plugin.getLogger().severe("Could not save top ten list!"); + handler.saveObject(topTenList); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException + | InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) { + // TODO Auto-generated catch block e.printStackTrace(); } } /** - * Loads the top ten from the file system topten.yml. If it does not exist - * then the top ten is created + * Loads the top ten from the database */ - public static void topTenLoad() { - topTenList.clear(); - // TODO + public void topTenLoad() { + try { + topTenList = handler.loadObject("topten"); + if (topTenList == null) { + topTenList = new TopTenList(); + } + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) { + e.printStackTrace(); + } } /** @@ -164,23 +182,22 @@ public class TopTen implements Listener{ * - the requesting player * @return - true if successful, false if no Top Ten list exists */ - public static boolean topTenShow(final Player player) { + public boolean topTenShow(final Player player) { if (DEBUG) plugin.getLogger().info("DEBUG: new GUI display"); // New GUI display (shown by default) if (topTenList == null) topTenCreate(); - topTenList = MapUtil.sortByValue(topTenList); // Create the top ten GUI if it does not exist if (gui == null) { - gui = Bukkit.createInventory(null, GUISIZE, plugin.getLocale(player.getUniqueId()).get("topTenGuiTitle")); + gui = Bukkit.createInventory(null, GUISIZE, plugin.getLocale(player.getUniqueId()).get("topten.guiTitle")); if (DEBUG) plugin.getLogger().info("DEBUG: creating GUI for the first time"); } // Reset gui.clear(); int i = 1; - Iterator> it = topTenList.entrySet().iterator(); + Iterator> it = topTenList.getTopTen().entrySet().iterator(); while (it.hasNext()) { Map.Entry m = it.next(); UUID playerUUID = m.getKey(); @@ -210,7 +227,7 @@ public class TopTen implements Listener{ return true; } - static ItemStack getSkull(int rank, Long long1, UUID player){ + ItemStack getSkull(int rank, Long long1, UUID player){ if (DEBUG) plugin.getLogger().info("DEBUG: Getting the skull"); String playerName = BSkyBlock.getPlugin().getPlayers().getName(player); @@ -223,10 +240,10 @@ public class TopTen implements Listener{ if (playerName == null) return null; SkullMeta meta = (SkullMeta) playerSkull.getItemMeta(); meta.setOwner(playerName); - meta.setDisplayName((plugin.getLocale(player).get("topTenGuiHeading").replace("[name]", BSkyBlock.getPlugin().getIslands().getIslandName(player))).replace("[rank]", String.valueOf(rank))); + meta.setDisplayName((plugin.getLocale(player).get("topten.guiHeading").replace("[name]", BSkyBlock.getPlugin().getIslands().getIslandName(player))).replace("[rank]", String.valueOf(rank))); //meta.setDisplayName(ChatColor.YELLOW + "" + ChatColor.BOLD + " " + ChatColor.YELLOW + "Island: " + ChatColor.GOLD + ChatColor.UNDERLINE + plugin.getGrid().getIslandName(player) + ChatColor.GRAY + " (#" + rank + ")"); List lore = new ArrayList(); - lore.add(ChatColor.YELLOW + plugin.getLocale(player).get("levelislandLevel") + " " + long1); + lore.add(ChatColor.YELLOW + plugin.getLocale(player).get("topten.islandLevel").replace("[level]", String.valueOf(long1))); if (BSkyBlock.getPlugin().getPlayers().inTeam(player)) { List memberList = new ArrayList<>(); for (UUID members : BSkyBlock.getPlugin().getIslands().getMembers(player)) { @@ -241,7 +258,7 @@ public class TopTen implements Listener{ return playerSkull; } - static void remove(UUID owner) { + void remove(UUID owner) { topTenList.remove(owner); } @@ -254,7 +271,7 @@ public class TopTen implements Listener{ } // The player that clicked the item Player player = (Player) event.getWhoClicked(); - if (!inventory.getTitle().equals(plugin.getLocale(player).get("topTenGuiTitle"))) { + if (!inventory.getTitle().equals(plugin.getLocale(player).get("topten.guiTitle"))) { return; } event.setCancelled(true); @@ -275,11 +292,4 @@ public class TopTen implements Listener{ } } - /** - * Get a sorted descending map of the top players - * @return the topTenList - may be more or less than ten - */ - public static Map getTopTenList() { - return topTenList; - } } diff --git a/Level/src/bskyblock/addin/level/database/object/Levels.java b/Level/src/bskyblock/addin/level/database/object/Levels.java index 352003a..08d969c 100644 --- a/Level/src/bskyblock/addin/level/database/object/Levels.java +++ b/Level/src/bskyblock/addin/level/database/object/Levels.java @@ -1,40 +1,28 @@ package bskyblock.addin.level.database.object; -import java.util.HashMap; -import java.util.UUID; - import us.tastybento.bskyblock.database.objects.DataObject; public class Levels extends DataObject { - private String uniqueId = "addon-levels"; - private HashMap islandLevel = new HashMap<>(); + private String uniqueId = ""; + private long level = 0; @Override public String getUniqueId() { - return "addon-levels"; + return uniqueId; } @Override public void setUniqueId(String uniqueId) { - // do nothing + this.uniqueId = uniqueId; } - public HashMap getIslandLevel() { - return islandLevel; + public long getLevel() { + return level; } - public void setIslandLevel(HashMap islandLevel) { - this.islandLevel = islandLevel; + public void setLevel(long level) { + this.level = level; } - public void addLevel(UUID uuid, Long level) { - this.islandLevel.put(uuid, level); - } - - public Long getLevel(UUID uuid) { - if (islandLevel.containsKey(uuid)) - return (long)islandLevel.get(uuid); - return 0L; - } } diff --git a/Level/src/bskyblock/addin/level/database/object/TopTenList.java b/Level/src/bskyblock/addin/level/database/object/TopTenList.java new file mode 100644 index 0000000..f2a22f8 --- /dev/null +++ b/Level/src/bskyblock/addin/level/database/object/TopTenList.java @@ -0,0 +1,77 @@ +package bskyblock.addin.level.database.object; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import us.tastybento.bskyblock.database.objects.DataObject; + +/** + * This class stores and sorts the top ten. + * @author ben + * + */ +public class TopTenList extends DataObject { + + private String uniqueId = "topten"; + private HashMap topTen = new HashMap<>(); + + public HashMap getTopTen() { + return topTen; + } + + public void setTopTen(HashMap topTen) { + this.topTen = topTen; + } + + @Override + public String getUniqueId() { + return uniqueId; + } + + @Override + public void setUniqueId(String uniqueId) { + this.uniqueId = uniqueId; + } + + /** + * Add level for this island owner or team leader, sort the top ten and limit to ten entries + * @param uuid - UUID of owner or team leader + * @param level - island level + */ + public void addLevel(UUID uuid, Long level) { + this.topTen.put(uuid, level); + sortTopTen(); + } + + /** + * Get the level for this UUID, or zero if the UUID is not found + * @param uuid + * @return island level + */ + public long getLevel(UUID uuid) { + if (topTen.containsKey(uuid)) + return topTen.get(uuid); + return 0L; + } + + /** + * Removes ownerUUID from the top ten + * @param ownerUUID + */ + public void remove(UUID ownerUUID) { + this.topTen.remove(ownerUUID); + } + + /** + * Sorts the top ten and limits it to 10 entries + */ + void sortTopTen() { + topTen = (HashMap) topTen.entrySet().stream() + .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())) + .limit(10) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } +}