diff --git a/resource/plugin.yml b/resource/plugin.yml index 57c20c4..842c05e 100644 --- a/resource/plugin.yml +++ b/resource/plugin.yml @@ -1,4 +1,4 @@ -version: 3.19.1.0 +version: 3.19.1.1 main: me.rockyhawk.commandpanels.CommandPanels name: CommandPanels author: RockyHawk diff --git a/src/me/rockyhawk/commandpanels/classresources/GetCustomHeads.java b/src/me/rockyhawk/commandpanels/classresources/GetCustomHeads.java index 1ceb9c6..e885a64 100644 --- a/src/me/rockyhawk/commandpanels/classresources/GetCustomHeads.java +++ b/src/me/rockyhawk/commandpanels/classresources/GetCustomHeads.java @@ -31,8 +31,7 @@ public class GetCustomHeads { this.plugin = pl; } - //contains cached player name and then base64 value (clears on /cpr reload) - //also will clear if the map reaches a length of 1000 which is roughly 135 KB RAM usage + //will not go above 2000 elements in the list which is roughly 270 KB RAM usage public HashMap playerHeadTextures = new HashMap<>(); public String getHeadBase64(ItemStack head) { diff --git a/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java b/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java index c8a1c83..66d29bb 100644 --- a/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java +++ b/src/me/rockyhawk/commandpanels/commands/Commandpanelsreload.java @@ -8,8 +8,11 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.IOException; @@ -51,8 +54,8 @@ public class Commandpanelsreload implements CommandExecutor { registerCommands(); } - //clear cached head textures - plugin.customHeads.playerHeadTextures.clear(); + //pre-cache any player head textures from panels + reloadCachedHeads(sender); sender.sendMessage(plugin.tex.colour(plugin.tag + plugin.config.getString("config.format.reload"))); }else{ @@ -110,4 +113,48 @@ public class Commandpanelsreload implements CommandExecutor { Bukkit.getConsoleSender().sendMessage("[CommandPanels]" + ChatColor.RED + " WARNING: Could not register custom commands!"); } } + + //reload player heads + public void reloadCachedHeads(CommandSender sender){ + new BukkitRunnable() { + public void run() { + for (Panel temp : plugin.panelList) { + ConfigurationSection yaml = temp.getConfig(); + //look through yaml for player heads + if(sender instanceof Player) { + Player player = ((Player) sender).getPlayer(); + checkKeysRecursive(temp, player, yaml); + }else{ + checkKeysRecursive(temp, null, yaml); + } + } + } + }.runTaskAsynchronously(this.plugin); + } + + //this will recursively check through all the keys in a panel for cps= values and find ones with Player names + private void checkKeysRecursive(Panel panel, Player player, ConfigurationSection section) { + for (String key : section.getKeys(false)) { + if (key.equals("material")) { + String value = section.getString(key); + //if value is a custom head + if (value.startsWith("cps=")) { + String[] words = value.split("\\s"); + //if value is the length of a players name + if (!words[1].equalsIgnoreCase("self") && words[1].length() <= 16) { + try { + String tempName = plugin.tex.placeholdersNoColour(panel, PanelPosition.Top, player, words[1]); + plugin.customHeads.getPlayerHead(tempName); //get the head cached + }catch (Exception ignore){} //ignore heads that cannot be cached without the panel being open + } + } + } + + if (section.isConfigurationSection(key)) { + // Recursive call to check nested sections + checkKeysRecursive(panel, player, section.getConfigurationSection(key)); + } + } + } + } \ No newline at end of file diff --git a/src/me/rockyhawk/commandpanels/openpanelsmanager/UtilsPanelsLoader.java b/src/me/rockyhawk/commandpanels/openpanelsmanager/UtilsPanelsLoader.java index 4b259fc..2711a31 100644 --- a/src/me/rockyhawk/commandpanels/openpanelsmanager/UtilsPanelsLoader.java +++ b/src/me/rockyhawk/commandpanels/openpanelsmanager/UtilsPanelsLoader.java @@ -14,7 +14,10 @@ import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; +import java.util.Random; public class UtilsPanelsLoader implements Listener { CommandPanels plugin; @@ -75,9 +78,10 @@ public class UtilsPanelsLoader implements Listener { //close panels and run commands for Top panel plugin.openPanels.closePanelForLoader(e.getPlayer().getName(),PanelPosition.Top); - //clear cached textures list on length limit - if(plugin.customHeads.playerHeadTextures.size() > 1000) { - plugin.customHeads.playerHeadTextures.clear(); + //clear cached textures list until length limit is reached + while (plugin.customHeads.playerHeadTextures.size() > 2000) { + List keys = new ArrayList<>(plugin.customHeads.playerHeadTextures.keySet()); + plugin.customHeads.playerHeadTextures.remove(keys.get(0)); } }