Sign Update Packet

Only sends a packet to set sign invisible if  there is an item to display
This commit is contained in:
jameslfc19 2020-07-12 18:28:30 +01:00
parent 4d80974431
commit 0805827f17
2 changed files with 24 additions and 11 deletions

View File

@ -125,6 +125,8 @@ public class VirtualCraftingHolder implements InventoryHolder {
if(recipe != null){ if(recipe != null){
setCrafting(recipe); setCrafting(recipe);
playSound(Sound.BLOCK_NOTE_BLOCK_CHIME,0.5f,1f); playSound(Sound.BLOCK_NOTE_BLOCK_CHIME,0.5f,1f);
} else {
stopCraftingItems();
} }
isUpdatingRecipe = false; isUpdatingRecipe = false;
updateGUI(); updateGUI();
@ -185,7 +187,7 @@ public class VirtualCraftingHolder implements InventoryHolder {
} }
public void startCraftingItems(){ public void startCraftingItems(){
craftItemTask = new CraftItems(); if(craftItemTask == null || craftItemTask.isCancelled()) craftItemTask = new CraftItems();
} }
public void stopCraftingItems(){ public void stopCraftingItems(){
if(craftItemTask != null) craftItemTask.cancel(); if(craftItemTask != null) craftItemTask.cancel();
@ -234,6 +236,9 @@ public class VirtualCraftingHolder implements InventoryHolder {
//Play sound if crafting occured. //Play sound if crafting occured.
if(didCraft) if(location.getLocation().getWorld() != null) { if(didCraft) if(location.getLocation().getWorld() != null) {
location.getLocation().getWorld().playSound(location.getLocation(), Sound.BLOCK_DISPENSER_DISPENSE, 0.25f, 1f); location.getLocation().getWorld().playSound(location.getLocation(), Sound.BLOCK_DISPENSER_DISPENSE, 0.25f, 1f);
if(output.getHolder() instanceof VirtualInventoryHolder){
((VirtualInventoryHolder) output.getHolder()).getStorage().updateDisplayItem();
}
} }
} }
} }

View File

@ -46,6 +46,8 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
private List<LocationInfo> locationInfoList; private List<LocationInfo> locationInfoList;
private Inventory inventory; private Inventory inventory;
private int signUpdateTask;
public AbstractStorage(OfflinePlayer player, String identifier, Location location, Location signLocation){ public AbstractStorage(OfflinePlayer player, String identifier, Location location, Location signLocation){
this.player = player; this.player = player;
this.playerUUID = player.getUniqueId(); this.playerUUID = player.getUniqueId();
@ -118,16 +120,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
private void init(){ private void init(){
if(shouldDisplayArmourStands()) { if(shouldDisplayArmourStands()) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, () -> { startSignChangeTask();
for (LocationInfo locationInfo : locationInfoList) {
World world = locationInfo.getLocation().getWorld();
if (world != null) {
//Make client think sign is invisible.
if (displayItem != null) world.getPlayers().forEach(player -> player.sendBlockChange(locationInfo.getSignLocation(), air));
else locationInfo.getSignLocation().getBlock().getState().update();
}
}
}, 1, 1);
} else { } else {
for (LocationInfo locationInfo : locationInfoList) { for (LocationInfo locationInfo : locationInfoList) {
locationInfo.getSignLocation().getBlock().getState().update(); locationInfo.getSignLocation().getBlock().getState().update();
@ -135,6 +128,15 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
} }
} }
private int startSignChangeTask(){
return Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, () -> Bukkit.getOnlinePlayers().forEach(player -> {
for (LocationInfo locationInfo : locationInfoList) {
if (displayItem != null) player.sendBlockChange(locationInfo.getSignLocation(), air);
else locationInfo.getSignLocation().getBlock().getState().update();
}
}), 1, 5);
}
/** /**
* This is called after the config has loaded into memory. * This is called after the config has loaded into memory.
*/ */
@ -358,6 +360,12 @@ public abstract class AbstractStorage implements ConfigurationSerializable {
private ItemStack displayItem; private ItemStack displayItem;
public void onItemDisplayUpdate(ItemStack newItem){ public void onItemDisplayUpdate(ItemStack newItem){
if(displayItem == null || displayItem.getType().equals(Material.AIR)){
Bukkit.getScheduler().cancelTask(signUpdateTask);
signUpdateTask = -1;
} else {
if(signUpdateTask == -1) signUpdateTask = startSignChangeTask();
}
displayItem = newItem; displayItem = newItem;
updateClients(); updateClients();
} }