This commit is contained in:
rockyhawk64 2024-05-26 12:21:35 +10:00
parent 44e4c5831e
commit 2bc6259e41
8 changed files with 77 additions and 79 deletions

View File

@ -1,4 +1,4 @@
version: 3.21.2.4
version: 3.21.2.5
main: me.rockyhawk.commandpanels.CommandPanels
name: CommandPanels
author: RockyHawk

View File

@ -5,7 +5,7 @@ import io.lumine.mythic.lib.api.item.NBTItem;
import me.rockyhawk.commandpanels.api.CommandPanelsAPI;
import me.rockyhawk.commandpanels.api.Panel;
import me.rockyhawk.commandpanels.classresources.ExecuteOpenVoids;
import me.rockyhawk.commandpanels.classresources.GetCustomHeads;
import me.rockyhawk.commandpanels.classresources.customheads.GetCustomHeads;
import me.rockyhawk.commandpanels.classresources.HasSections;
import me.rockyhawk.commandpanels.classresources.ItemCreation;
import me.rockyhawk.commandpanels.classresources.placeholders.expansion.CpPlaceholderExpansion;

View File

@ -1,4 +1,4 @@
package me.rockyhawk.commandpanels.classresources;
package me.rockyhawk.commandpanels.classresources.customheads;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
@ -26,8 +26,8 @@ public class GetCustomHeads {
this.plugin = pl;
}
//will not go above 2000 elements in the list which is roughly 270 KB RAM usage
public HashMap<String, String> playerHeadTextures = new HashMap<>();
//will not go above 2000 elements in the list
public HashSet<SavedCustomHead> savedCustomHeads = new HashSet<>();
public String getHeadBase64(ItemStack head) {
if (plugin.getHeads.ifSkullOrHead(head.getType().toString()) && head.hasItemMeta()) {
@ -75,8 +75,11 @@ public class GetCustomHeads {
}
//get texture if already cached
if(playerHeadTextures.containsKey(name)) {
return getCustomHead(playerHeadTextures.get(name));
for(SavedCustomHead head : savedCustomHeads){
if(head.playerName == null) {continue;}
if(head.playerName.equals(name)){
return head.headItem;
}
}
//create ItemStack
@ -115,11 +118,11 @@ public class GetCustomHeads {
String valueReader = new Scanner(texturesConnection.getInputStream(),
StandardCharsets.UTF_8.name()).useDelimiter("\\A").next();
String value = valueReader.split("\"value\" : \"")[1].split("\"")[0];
playerHeadTextures.put(name, value);
// Once the API call is finished, update the ItemStack on the main thread
Bukkit.getScheduler().runTask(plugin, () -> {
itemStack.setItemMeta(getCustomHead(value).getItemMeta());
savedCustomHeads.add(new SavedCustomHead(itemStack, value, name));
});
} catch (Exception ignore) {
// Ignore as errors should be skipped and no need to show in console
@ -132,6 +135,13 @@ public class GetCustomHeads {
//used to get heads from Base64 Textures
@SuppressWarnings("deprecation")
public ItemStack getCustomHead(String b64stringtexture) {
//check for any saved heads
for(SavedCustomHead head : savedCustomHeads){
if(head.base64.equals(b64stringtexture)){
return head.headItem;
}
}
//get head from base64
GameProfile profile = new GameProfile(UUID.randomUUID(), "");
PropertyMap propertyMap = profile.getProperties();

View File

@ -0,0 +1,20 @@
package me.rockyhawk.commandpanels.classresources.customheads;
import org.bukkit.inventory.ItemStack;
public class SavedCustomHead {
public String base64;
public String playerName = null;
public ItemStack headItem;
public SavedCustomHead(ItemStack head, String base64value) {
base64 = base64value;
headItem = head;
}
public SavedCustomHead(ItemStack head, String base64value, String player) {
playerName = player;
base64 = base64value;
headItem = head;
}
}

View File

@ -54,8 +54,8 @@ public class Commandpanelsreload implements CommandExecutor {
registerCommands();
}
//pre-cache any player head textures from panels
reloadCachedHeads(sender);
//pre-cache any player head textures from panels will be reloaded
plugin.customHeads.savedCustomHeads.clear();
sender.sendMessage(plugin.tex.colour(plugin.tag + plugin.config.getString("config.format.reload")));
}else{
@ -114,47 +114,4 @@ public class Commandpanelsreload implements CommandExecutor {
}
}
//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));
}
}
}
}

View File

@ -46,11 +46,9 @@ public class ItemPaywall implements Listener {
//try to remove the item and determine outcome
PaywallOutput removedItem = PaywallOutput.Blocked;
if (e.doDelete){
if(removeItem(e.p, sellItem, ignoreNBT)){
if(removeItem(e.p, sellItem, ignoreNBT, e.doDelete)){
removedItem = PaywallOutput.Passed;
}
}
//send message and return
if (removedItem == PaywallOutput.Blocked) {
@ -73,22 +71,22 @@ public class ItemPaywall implements Listener {
}
}
public boolean removeItem(Player p, ItemStack itemToRemove, boolean ignoreNBT) {
public boolean removeItem(Player p, ItemStack itemToRemove, boolean ignoreNBT, boolean doDelete) {
InventoryOperationResult result;
if (plugin.inventorySaver.hasNormalInventory(p)) {
result = removeItemFromInventory(p.getInventory().getContents(), itemToRemove, ignoreNBT);
result = removeItemFromInventory(p.getInventory().getContents(), itemToRemove, ignoreNBT, doDelete);
p.getInventory().setContents(result.getInventory());
} else {
ItemStack[] savedInventory = plugin.inventorySaver.getNormalInventory(p);
result = removeItemFromInventory(savedInventory, itemToRemove, ignoreNBT);
result = removeItemFromInventory(savedInventory, itemToRemove, ignoreNBT, doDelete);
plugin.inventorySaver.inventoryConfig.set(p.getUniqueId().toString(), plugin.itemSerializer.itemStackArrayToBase64(result.getInventory()));
}
return result.isSuccess(); // Return the success status of the inventory operation
}
private InventoryOperationResult removeItemFromInventory(ItemStack[] inventory, ItemStack itemToRemove, boolean ignoreNBT) {
private InventoryOperationResult removeItemFromInventory(ItemStack[] inventory, ItemStack itemToRemove, boolean ignoreNBT, boolean doDelete) {
int amountToRemove = itemToRemove.getAmount();
int count = 0;
@ -98,10 +96,16 @@ public class ItemPaywall implements Listener {
}
}
//return false if the player doesn't have enough items
if (count < amountToRemove) {
return new InventoryOperationResult(false, inventory); // Not enough items, return with original inventory unchanged
}
//return true because there are enough items, but don't run the code to remove them
if(!doDelete){
return new InventoryOperationResult(true, inventory);
}
for (int i = 0; i < inventory.length; i++) {
ItemStack currentItem = inventory[i];
if (currentItem != null && plugin.itemCreate.isIdentical(currentItem, itemToRemove, !ignoreNBT)) {

View File

@ -5,6 +5,7 @@ import me.rockyhawk.commandpanels.api.Panel;
import me.rockyhawk.commandpanels.api.PanelClosedEvent;
import me.rockyhawk.commandpanels.api.PanelsInterface;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.*;
@ -130,7 +131,18 @@ public class OpenPanelsLoader {
}
}
public boolean isNBTInjected(ItemStack itm){
//if any CommandPanels items exist outside a panel, they will be removed
public void deleteCommandPanelsItems(Player p){
for(ItemStack itm : p.getInventory().getContents()){
if(itm != null){
if (plugin.nbt.hasNBT(itm, "CommandPanelsItem")) {
p.getInventory().remove(itm);
}
}
}
}
//checks if an item is from cpanel or not
public boolean isCommandPanelsItem(ItemStack itm){
if(itm != null){
return plugin.nbt.hasNBT(itm,"CommandPanelsItem");
}

View File

@ -3,7 +3,9 @@ package me.rockyhawk.commandpanels.openpanelsmanager;
import me.rockyhawk.commandpanels.CommandPanels;
import me.rockyhawk.commandpanels.api.Panel;
import me.rockyhawk.commandpanels.api.PanelClosedEvent;
import me.rockyhawk.commandpanels.classresources.customheads.SavedCustomHead;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -12,10 +14,8 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
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.Iterator;
import java.util.Objects;
public class UtilsPanelsLoader implements Listener {
@ -30,13 +30,7 @@ public class UtilsPanelsLoader implements Listener {
plugin.openPanels.closePanelForLoader(e.getPlayer().getName(),PanelPosition.Top);
Player p = e.getPlayer();
p.updateInventory();
for(ItemStack itm : p.getInventory().getContents()){
if(itm != null){
if (plugin.nbt.hasNBT(itm, "CommandPanelsItem")) {
p.getInventory().remove(itm);
}
}
}
plugin.openPanels.deleteCommandPanelsItems(p);
}
//tell panel loader that player has closed the panel (there is also one of these in EditorUtils)
@ -81,9 +75,10 @@ public class UtilsPanelsLoader implements Listener {
plugin.openPanels.closePanelForLoader(e.getPlayer().getName(),PanelPosition.Top);
//clear cached textures list until length limit is reached
while (plugin.customHeads.playerHeadTextures.size() > 2000) {
List<String> keys = new ArrayList<>(plugin.customHeads.playerHeadTextures.keySet());
plugin.customHeads.playerHeadTextures.remove(keys.get(0));
Iterator<SavedCustomHead> iterator = plugin.customHeads.savedCustomHeads.iterator();
while (plugin.customHeads.savedCustomHeads.size() > 2000 && iterator.hasNext()) {
iterator.next(); // Move to next element
iterator.remove(); // Remove the element
}
}
@ -92,10 +87,10 @@ public class UtilsPanelsLoader implements Listener {
//this will check to ensure an item is not from CommandPanels on inventory open
Player p = (Player)e.getWhoClicked();
if(!plugin.openPanels.hasPanelOpen(p.getName(),PanelPosition.Top)){
for(ItemStack itm : p.getInventory().getContents()){
if(plugin.openPanels.isNBTInjected(itm)){
p.getInventory().remove(itm);
}
if(e.getCurrentItem() == null){return;}
if(e.getCurrentItem().getType() == Material.AIR){return;}
if(plugin.openPanels.isCommandPanelsItem(e.getCurrentItem())){
p.getInventory().remove(e.getCurrentItem());
}
}
}