Display Item Fix

Display items now only change when they are actually updated. Should increase tick performance!
This commit is contained in:
jameslfc19 2020-07-10 14:12:13 +01:00
parent 5ae0cf664c
commit 0bc7a09cc4
8 changed files with 82 additions and 57 deletions

View File

@ -128,6 +128,7 @@ public class VirtualCraftingHolder implements InventoryHolder {
}
isUpdatingRecipe = false;
updateGUI();
storage.onItemDisplayUpdate(result);
}
private void playSound(Sound sound, float volume, float pitch){

View File

@ -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);
}
}

View File

@ -52,6 +52,8 @@ public class Config {
storageTypes = new ArrayList<>();
storageTypes.add(chestLinkStorageType);
storageTypes.add(autoCraftingStorageType);
storageTypes.forEach(StorageType::onConfigLoad);
}
public static void save() {

View File

@ -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<ItemStack> 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<ItemStack,Integer> itemAmounts = getItemAmounts(inventory.getContents());
List<ItemStack> 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<ItemStack,Integer> getItemAmounts(ItemStack[] itemStacks){

View File

@ -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);
}
}
}

View File

@ -340,4 +340,13 @@ public abstract class StorageType<T extends AbstractStorage> {
return playerList;
}
/*
POST LOAD
*/
public void onConfigLoad(){
getMap().values().forEach(stringTHashMap -> stringTHashMap.values().forEach(AbstractStorage::postConfigLoad));
}
}

View File

@ -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

View File

@ -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