From 6c7adf2e428cd566d3ea83102ae9026ed22b6745 Mon Sep 17 00:00:00 2001 From: elliot Date: Sat, 25 Sep 2021 12:29:18 +0200 Subject: [PATCH] Improve map command- don't delete items When you get a map it places the current item into the first available slot- if not free, it drops the item to the ground. --- src/cx/sfy/LagAssist/MonTools.java | 460 +++++++++++++++-------------- 1 file changed, 238 insertions(+), 222 deletions(-) diff --git a/src/cx/sfy/LagAssist/MonTools.java b/src/cx/sfy/LagAssist/MonTools.java index d67cf5c..dee2b1e 100644 --- a/src/cx/sfy/LagAssist/MonTools.java +++ b/src/cx/sfy/LagAssist/MonTools.java @@ -1,222 +1,238 @@ -package cx.sfy.LagAssist; - -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerItemHeldEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.map.MapView; - -import cx.sfy.LagAssist.maps.TpsRender; -import cx.sfy.LagAssist.minebench.SpecsGetter; -import cx.sfy.LagAssist.packets.Reflection; -import cx.sfy.LagAssist.utils.VersionMgr; - -public class MonTools implements Listener { - - public static ItemStack mapitem = VersionMgr.getMap(); - public static ItemMeta mapitemmeta = mapitem.getItemMeta(); - - public static List actionmon = new ArrayList(); - public static List mapusers = new ArrayList(); - private static DecimalFormat format = new DecimalFormat("#0.00"); - - private static String stbmsg = Main.config.getString("stats-bar.message"); - private static int stbinterv = Main.config.getInt("stats-bar.tps-interval"); - private static int stbshowdl = Main.config.getInt("stats-bar.show-delay"); - - public static void Enabler(boolean reload) { - if (!reload) { - Main.p.getServer().getPluginManager().registerEvents(new MonTools(), Main.p); - } - - Bukkit.getLogger().info(" §e[§a✔§e] §fMapVisualizer."); - mapitemmeta.setDisplayName("§c§lLag§f§lAssist §e§lMonitor"); - mapitem.setItemMeta(mapitemmeta); - - int mapid = VersionMgr.getMapId(mapitem); - - MapView view = Reflection.getMapView(mapid); - - if (view != null) { - view.getRenderers().clear(); - view.addRenderer(new TpsRender()); - } - - - StatsBar(); - } - - public static void StatsBar() { - Bukkit.getScheduler().runTaskTimer(Main.p, () -> { - if (actionmon.size() == 0) { - return; - } - - boolean found = false; - - for (int i = 0; i 20) ? 20 : ExactTPS.getTPS(stbinterv); - - String chunks = String.valueOf(getChunkCount()); - String ents = String.valueOf(getEntCount()); - - Bukkit.getScheduler().runTaskAsynchronously(Main.p, () -> { - if (actionmon.isEmpty()) { - return; - } - String tps; - if (tpsraw > 18) { - tps = "§a" + format.format(tpsraw); - } else if (tpsraw > 15) { - tps = "§e" + format.format(tpsraw); - } else { - tps = "§c" + format.format(tpsraw); - } - String s = ChatColor.translateAlternateColorCodes('&', - stbmsg.replaceAll("\\{TPS\\}", tps) - .replaceAll("\\{MEM\\}", format.format(SpecsGetter.FreeRam() / 1024)) - .replaceAll("\\{CHKS\\}", chunks) - .replaceAll("\\{ENT\\}", ents)); - for (UUID u : actionmon) { - Player p = Bukkit.getPlayer(u); - - if (p == null) { - continue; - } - - Reflection.sendAction(p, s); - } - }); - - }, stbshowdl, stbshowdl); - } - - public static int getEntCount() { - int lng = 0; - for (World w : Bukkit.getWorlds()) { - lng += w.getEntities().size(); - } - return lng; - } - - public static int getChunkCount() { - int lng = 0; - for (World w : Bukkit.getWorlds()) { - lng += w.getLoadedChunks().length; - } - return lng; - } - - @EventHandler - public void onSlotChange(PlayerItemHeldEvent e) { - Player p = e.getPlayer(); - - ItemStack old = p.getInventory().getItem(e.getPreviousSlot()); - ItemStack nw = p.getInventory().getItem(e.getNewSlot()); - - if (runNew(nw, p)) { - return; - } - runOld(old, p); - } - -// @EventHandler(priority = EventPriority.LOWEST) -// public void onMapLoad(MapInitializeEvent e) { -// Main.p.getLogger().warning("0"); -// MapView view = e.getMap(); -// -// if (Reflection.getMapId(e.get) != Data.getMapId()) { -// return; -// } -// -// Main.p.getLogger().warning("1"); -// -// view.addRenderer(new TpsRender()); -// } - - public static void giveMap(Player p) { - PlayerInventory inv = p.getInventory(); - int slot = inv.getHeldItemSlot(); - - inv.setItem(slot, MonTools.mapitem); - - UUID UUID = p.getUniqueId(); - - if (!mapusers.contains(UUID)) { - mapusers.add(UUID); - } - } - - private static void runOld(ItemStack old, Player p) { - if (old == null) { - return; - } - - if (!old.hasItemMeta()) { - return; - } - ItemMeta ometa = old.getItemMeta(); - if (!ometa.hasDisplayName()) { - return; - } - if (ometa.getDisplayName() != mapitemmeta.getDisplayName()) { - return; - } - - UUID UUID = p.getUniqueId(); - - if (!mapusers.contains(UUID)) { - mapusers.add(UUID); - } - } - - private static boolean runNew(ItemStack nw, Player p) { - - if (!p.hasPermission("lagassist.use")) { - return false; - } - - if (nw == null) { - return false; - } - - if (!nw.hasItemMeta()) { - return false; - } - ItemMeta nwmeta = nw.getItemMeta(); - if (!nwmeta.hasDisplayName()) { - return false; - } - if (nwmeta.getDisplayName() != mapitemmeta.getDisplayName()) { - return false; - } - - UUID UUID = p.getUniqueId(); - - if (!mapusers.contains(UUID)) { - mapusers.add(UUID); - } - return true; - } -} +package cx.sfy.LagAssist; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.map.MapView; + +import cx.sfy.LagAssist.maps.TpsRender; +import cx.sfy.LagAssist.minebench.SpecsGetter; +import cx.sfy.LagAssist.packets.Reflection; +import cx.sfy.LagAssist.utils.VersionMgr; + +public class MonTools implements Listener { + + public static ItemStack mapitem = VersionMgr.getMap(); + public static ItemMeta mapitemmeta = mapitem.getItemMeta(); + + public static List actionmon = new ArrayList(); + public static List mapusers = new ArrayList(); + private static DecimalFormat format = new DecimalFormat("#0.00"); + + private static String stbmsg = Main.config.getString("stats-bar.message"); + private static int stbinterv = Main.config.getInt("stats-bar.tps-interval"); + private static int stbshowdl = Main.config.getInt("stats-bar.show-delay"); + + public static void Enabler(boolean reload) { + if (!reload) { + Main.p.getServer().getPluginManager().registerEvents(new MonTools(), Main.p); + } + + Bukkit.getLogger().info(" §e[§a✔§e] §fMapVisualizer."); + mapitemmeta.setDisplayName("§c§lLag§f§lAssist §e§lMonitor"); + mapitem.setItemMeta(mapitemmeta); + + int mapid = VersionMgr.getMapId(mapitem); + + MapView view = Reflection.getMapView(mapid); + + if (view != null) { + view.getRenderers().clear(); + view.addRenderer(new TpsRender()); + } + + + StatsBar(); + } + + public static void StatsBar() { + Bukkit.getScheduler().runTaskTimer(Main.p, () -> { + if (actionmon.size() == 0) { + return; + } + + boolean found = false; + + for (int i = 0; i 20) ? 20 : ExactTPS.getTPS(stbinterv); + + String chunks = String.valueOf(getChunkCount()); + String ents = String.valueOf(getEntCount()); + + Bukkit.getScheduler().runTaskAsynchronously(Main.p, () -> { + if (actionmon.isEmpty()) { + return; + } + String tps; + if (tpsraw > 18) { + tps = "§a" + format.format(tpsraw); + } else if (tpsraw > 15) { + tps = "§e" + format.format(tpsraw); + } else { + tps = "§c" + format.format(tpsraw); + } + String s = ChatColor.translateAlternateColorCodes('&', + stbmsg.replaceAll("\\{TPS\\}", tps) + .replaceAll("\\{MEM\\}", format.format(SpecsGetter.FreeRam() / 1024)) + .replaceAll("\\{CHKS\\}", chunks) + .replaceAll("\\{ENT\\}", ents)); + for (UUID u : actionmon) { + Player p = Bukkit.getPlayer(u); + + if (p == null) { + continue; + } + + Reflection.sendAction(p, s); + } + }); + + }, stbshowdl, stbshowdl); + } + + public static int getEntCount() { + int lng = 0; + for (World w : Bukkit.getWorlds()) { + lng += w.getEntities().size(); + } + return lng; + } + + public static int getChunkCount() { + int lng = 0; + for (World w : Bukkit.getWorlds()) { + lng += w.getLoadedChunks().length; + } + return lng; + } + + @EventHandler + public void onSlotChange(PlayerItemHeldEvent e) { + Player p = e.getPlayer(); + + ItemStack old = p.getInventory().getItem(e.getPreviousSlot()); + ItemStack nw = p.getInventory().getItem(e.getNewSlot()); + + if (runNew(nw, p)) { + return; + } + runOld(old, p); + } + +// @EventHandler(priority = EventPriority.LOWEST) +// public void onMapLoad(MapInitializeEvent e) { +// Main.p.getLogger().warning("0"); +// MapView view = e.getMap(); +// +// if (Reflection.getMapId(e.get) != Data.getMapId()) { +// return; +// } +// +// Main.p.getLogger().warning("1"); +// +// view.addRenderer(new TpsRender()); +// } + + public static void giveMap(Player p) { + // Get the player's inventory + PlayerInventory inv = p.getInventory(); + // Get the value of the active slot + int slot = inv.getHeldItemSlot(); + // Get the item that is in the user's active slot + ItemStack item = inv.getItem(slot) + + if (inventory.firstEmpty() == -1){ + // if inventory is full, drop it to the ground (item is an ItemStack) + player.getWorld().dropItem(player.getLocation().add(0, 1, 0), item); + } else{ + // if there is a empty place, put it in + int newItemSlot = inventory.firstEmpty(); + inventory.setItem(newItemSlot, item); + } + + // Set the active slot to the map + inv.setItem(slot, MonTools.mapitem); + + // Get the user's UUID + UUID UUID = p.getUniqueId(); + + // Add the user's UUID to the mapusers array + if (!mapusers.contains(UUID)) { + mapusers.add(UUID); + } + } + + private static void runOld(ItemStack old, Player p) { + if (old == null) { + return; + } + + if (!old.hasItemMeta()) { + return; + } + ItemMeta ometa = old.getItemMeta(); + if (!ometa.hasDisplayName()) { + return; + } + if (ometa.getDisplayName() != mapitemmeta.getDisplayName()) { + return; + } + + UUID UUID = p.getUniqueId(); + + if (!mapusers.contains(UUID)) { + mapusers.add(UUID); + } + } + + private static boolean runNew(ItemStack nw, Player p) { + + if (!p.hasPermission("lagassist.use")) { + return false; + } + + if (nw == null) { + return false; + } + + if (!nw.hasItemMeta()) { + return false; + } + ItemMeta nwmeta = nw.getItemMeta(); + if (!nwmeta.hasDisplayName()) { + return false; + } + if (nwmeta.getDisplayName() != mapitemmeta.getDisplayName()) { + return false; + } + + UUID UUID = p.getUniqueId(); + + if (!mapusers.contains(UUID)) { + mapusers.add(UUID); + } + return true; + } +}