From 0bc7a09cc4c2565c619f8f25598b06ea9c0c6400 Mon Sep 17 00:00:00 2001 From: jameslfc19 Date: Fri, 10 Jul 2020 14:12:13 +0100 Subject: [PATCH] Display Item Fix Display items now only change when they are actually updated. Should increase tick performance! --- .../interfaces/VirtualCraftingHolder.java | 1 + .../chests/listeners/InventoryListener.java | 12 ++++- .../minecraft/chests/serialize/Config.java | 2 + .../chests/sort/InventorySorter.java | 23 ++++----- .../storage/abstracts/AbstractStorage.java | 51 +++++++++++-------- .../chests/storage/abstracts/StorageType.java | 9 ++++ .../autocraft/AutoCraftingStorage.java | 20 +++----- .../storage/chestlink/ChestLinkStorage.java | 21 +++++--- 8 files changed, 82 insertions(+), 57 deletions(-) diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java index 0eca327..ae49c10 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java @@ -128,6 +128,7 @@ public class VirtualCraftingHolder implements InventoryHolder { } isUpdatingRecipe = false; updateGUI(); + storage.onItemDisplayUpdate(result); } private void playSound(Sound sound, float volume, float pitch){ diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java index 846bf6b..ce8fb2e 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/listeners/InventoryListener.java @@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.listeners; import com.jamesdpeters.minecraft.chests.ChestsPlusPlus; import com.jamesdpeters.minecraft.chests.api.ApiSpecific; import com.jamesdpeters.minecraft.chests.interfaces.VirtualCraftingHolder; +import com.jamesdpeters.minecraft.chests.sort.InventorySorter; import com.jamesdpeters.minecraft.chests.storage.autocraft.AutoCraftingStorage; import com.jamesdpeters.minecraft.chests.serialize.Config; import com.jamesdpeters.minecraft.chests.misc.Messages; @@ -39,6 +40,7 @@ public class InventoryListener implements Listener { if (storage != null) { event.setCancelled(true); if (event.getPlayer().hasPermission(Permissions.OPEN) && storage.hasPermission((Player) event.getPlayer())) { + storage.getInventory().getViewers().remove(event.getPlayer()); Utils.openChestInventory((Player) event.getPlayer(), storage, event.getInventory().getLocation()); } else { Messages.NO_PERMISSION((Player) event.getPlayer()); @@ -70,13 +72,15 @@ public class InventoryListener implements Listener { if (vHolder.didPlayerRemoteOpen(event.getPlayer().getUniqueId())) { Utils.closeInventorySound((Player) event.getPlayer(), event.getInventory()); } + event.getViewers().remove(event.getPlayer()); vHolder.getStorage().getLocations().forEach(locationInfo -> { Block block = locationInfo.getLocation().getBlock(); if(block.getState() instanceof Chest){ Chest chest = (Chest) block.getState(); - ApiSpecific.getChestOpener().setLidOpen(chest,false); + Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, () -> ApiSpecific.getChestOpener().setLidOpen(event.getInventory(),chest,false),1); } }); + vHolder.getStorage().onItemDisplayUpdate(InventorySorter.getMostCommonItem(event.getInventory())); } if(holder instanceof VirtualCraftingHolder){ ((VirtualCraftingHolder) holder).stopAnimation(); @@ -87,7 +91,11 @@ public class InventoryListener implements Listener { public void inventoryUpdate(InventoryInteractEvent event){ InventoryHolder holder = event.getInventory().getHolder(); if(holder instanceof VirtualInventoryHolder){ - Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, () -> ((VirtualInventoryHolder) event.getInventory().getHolder()).getStorage().sort(),1); + VirtualInventoryHolder vHolder = (VirtualInventoryHolder) holder; + Bukkit.getScheduler().scheduleSyncDelayedTask(ChestsPlusPlus.PLUGIN, () -> { + vHolder.getStorage().sort(); + vHolder.getStorage().onItemDisplayUpdate(InventorySorter.getMostCommonItem(event.getInventory())); + },1); } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/Config.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/Config.java index e4491be..36703a3 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/Config.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/serialize/Config.java @@ -52,6 +52,8 @@ public class Config { storageTypes = new ArrayList<>(); storageTypes.add(chestLinkStorageType); storageTypes.add(autoCraftingStorageType); + + storageTypes.forEach(StorageType::onConfigLoad); } public static void save() { diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/sort/InventorySorter.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/sort/InventorySorter.java index 04e5cb9..7baf2d4 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/sort/InventorySorter.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/sort/InventorySorter.java @@ -1,5 +1,6 @@ package com.jamesdpeters.minecraft.chests.sort; +import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -12,9 +13,9 @@ import java.util.Optional; public class InventorySorter { - public static void sort(Inventory inventory, SortMethod sortMethod){ + public static ItemStack[] sort(Inventory inventory, SortMethod sortMethod){ switch (sortMethod){ - case OFF: return; + case OFF: return inventory.getContents(); case NAME: { List condensed = condenseInventory(inventory.getContents()); condensed.sort((item1, item2) -> { @@ -24,22 +25,20 @@ public class InventorySorter { return item1.getType().name().compareTo(item2.getType().name()); } }); - ItemStack[] itemStacks = condensed.toArray(new ItemStack[0]); - inventory.setContents(itemStacks); - return; + // inventory.setContents(itemStacks); + return condensed.toArray(new ItemStack[0]); } case AMOUNT_DESC: { - sortByAmount(inventory,true); - return; + return sortByAmount(inventory,true); } case AMOUNT_ASC: { - sortByAmount(inventory,false); + return sortByAmount(inventory,false); } } + return inventory.getContents(); } - private static void sortByAmount(Inventory inventory, boolean descending){ - + private static ItemStack[] sortByAmount(Inventory inventory, boolean descending){ HashMap itemAmounts = getItemAmounts(inventory.getContents()); List condensed = condenseInventory(inventory.getContents()); @@ -56,8 +55,8 @@ public class InventorySorter { if(descending) itemOrder *= -1; return itemOrder; }); - ItemStack[] itemStacks = condensed.toArray(new ItemStack[0]); - inventory.setContents(itemStacks); + + return condensed.toArray(new ItemStack[0]); } private static HashMap getItemAmounts(ItemStack[] itemStacks){ diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java index 7509b68..726dc8d 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java @@ -1,6 +1,5 @@ package com.jamesdpeters.minecraft.chests.storage.abstracts; -import com.jamesdpeters.minecraft.chests.ChestsPlusPlus; import com.jamesdpeters.minecraft.chests.api.ApiSpecific; import com.jamesdpeters.minecraft.chests.misc.Permissions; import com.jamesdpeters.minecraft.chests.misc.Values; @@ -117,7 +116,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { } private void init(){ - if(shouldDisplayArmourStands()) Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, this::updateClients, 1, 5); +// if(shouldDisplayArmourStands()) Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, this::updateClients, 1, 5); } public abstract StorageType getStorageType(); @@ -144,10 +143,15 @@ public abstract class AbstractStorage implements ConfigurationSerializable { public abstract String getIdentifier(); public abstract boolean shouldDisplayArmourStands(); + /** + * This is called after the config has loaded into memory. + */ + public abstract void postConfigLoad(); + /** * @return whether to drop the inventory of this storage when it's removed. */ - public abstract boolean dropInventory(); + public abstract boolean doesDropInventory(); /** * This is the distance from a full block to the size of the storage block. (e.g Chest is smaller than a regular block.) @@ -235,11 +239,13 @@ public abstract class AbstractStorage implements ConfigurationSerializable { * @param location - location to drop. */ public void dropInventory(Location location){ - for(ItemStack item : getInventory().getContents()) { - if(location.getWorld() != null){ - if(item != null) { - location.getWorld().dropItemNaturally(location, item); - getInventory().remove(item); + if(doesDropInventory()) { + for (ItemStack item : getInventory().getContents()) { + if (location.getWorld() != null) { + if (item != null) { + location.getWorld().dropItemNaturally(location, item); + getInventory().remove(item); + } } } } @@ -309,10 +315,12 @@ public abstract class AbstractStorage implements ConfigurationSerializable { /* ARMOR STAND METHODS */ - /** - * @return the @{@link ItemStack} an @{@link ArmorStand} should be holding. - */ - protected abstract ItemStack getArmorStandItem(); + private static ItemStack displayItem; + + public void onItemDisplayUpdate(ItemStack newItem){ + displayItem = newItem; + updateClients(); + } private EulerAngle BLOCK_POSE = new EulerAngle( Math.toRadians( -15 ), Math.toRadians( -45 ), Math.toRadians(0) ); private EulerAngle STANDARD_ITEM_POSE = new EulerAngle(Math.toRadians(90),0,Math.toRadians(180)); @@ -324,7 +332,8 @@ public abstract class AbstractStorage implements ConfigurationSerializable { * spawned that displays the item. */ private void updateClients(){ - for (LocationInfo location : locationInfoList) { + if(locationInfoList == null) return; + for (LocationInfo location : locationInfoList){ World world = location.getLocation().getWorld(); Block block = location.getLocation().getBlock(); BlockData air = Material.AIR.createBlockData(); @@ -341,21 +350,19 @@ public abstract class AbstractStorage implements ConfigurationSerializable { Block anchor = block.getRelative(facing); - ItemStack displayItem = getArmorStandItem(); - if(displayItem != null && !ApiSpecific.getMaterialChecker().isIgnored(displayItem)) { boolean isBlock = !ApiSpecific.getMaterialChecker().isGraphically2D(displayItem); boolean isTool = ApiSpecific.getMaterialChecker().isTool(displayItem); - Location standLoc = isTool ? getHeldItemArmorStandLoc(anchor,facing) : getArmorStandLoc(anchor,facing, isBlock); + Location standLoc = isTool ? getHeldItemArmorStandLoc(anchor, facing) : getArmorStandLoc(anchor, facing, isBlock); //Make client think sign is invisible. player.sendBlockChange(anchor.getLocation(), air); //Get currently stored armorStand if there isn't one spawn it. ArmorStand stand = isTool ? location.getToolItemStand() : (isBlock ? location.getBlockStand() : location.getItemStand()); - if(stand == null || !stand.isValid()){ - stand = createArmorStand(world,standLoc,isBlock,isTool); + if(stand == null || !stand.isValid()) { + stand = createArmorStand(world, standLoc, isBlock, isTool); addArmorStand(isBlock, isTool, location, stand); } @@ -365,7 +372,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { stand.setFireTicks(Integer.MAX_VALUE); //Set other armor stand helmet to null. - if(isBlock){ + if(isBlock) { setArmorStandHelmet(location.getToolItemStand(), null); setArmorStandHelmet(location.getItemStand(), null); } else { @@ -377,9 +384,9 @@ public abstract class AbstractStorage implements ConfigurationSerializable { } else { anchor.getState().update(); - setArmorStandHelmet(location.getToolItemStand(),null); - setArmorStandHelmet(location.getItemStand(),null); - setArmorStandHelmet(location.getBlockStand(),null); + setArmorStandHelmet(location.getToolItemStand(), null); + setArmorStandHelmet(location.getItemStand(), null); + setArmorStandHelmet(location.getBlockStand(), null); } } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java index f01ca56..6df4ec1 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/StorageType.java @@ -340,4 +340,13 @@ public abstract class StorageType { return playerList; } + + /* + POST LOAD + */ + + public void onConfigLoad(){ + getMap().values().forEach(stringTHashMap -> stringTHashMap.values().forEach(AbstractStorage::postConfigLoad)); + } + } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java index 31555b0..91c726a 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java @@ -54,17 +54,6 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio initInventory(); } - @Override - protected ItemStack getArmorStandItem() { - if(recipeSerializable != null){ - if(recipeSerializable.getRecipe() != null){ - return recipeSerializable.getRecipe().getResult(); - } - } - return null; - } - - @Override public boolean storeInventory() { return false; @@ -89,8 +78,13 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio } @Override - public boolean dropInventory() { - return true; + public void postConfigLoad() { + + } + + @Override + public boolean doesDropInventory() { + return false; } @Override diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java index 06f1d9c..64a9569 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/chestlink/ChestLinkStorage.java @@ -72,11 +72,6 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe init(); } - @Override - protected ItemStack getArmorStandItem() { - return InventorySorter.getMostCommonItem(getInventory()); - } - private void init(){ VirtualChestToHopper chestToHopper = new VirtualChestToHopper(this); chestToHopper.start(); @@ -179,7 +174,12 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe } public void sort(){ - InventorySorter.sort(getInventory(), sortMethod); + ItemStack[] sortedInventory = InventorySorter.sort(getInventory(), sortMethod); + getInventory().setContents(sortedInventory); + } + + public void updateDisplayItem(){ + onItemDisplayUpdate(InventorySorter.getMostCommonItem(getInventory())); } @Override @@ -193,8 +193,13 @@ public class ChestLinkStorage extends AbstractStorage implements ConfigurationSe } @Override - public boolean dropInventory() { - return false; + public void postConfigLoad() { + onItemDisplayUpdate(InventorySorter.getMostCommonItem(getInventory())); + } + + @Override + public boolean doesDropInventory() { + return true; } @Override