From b55c2da2f6974d64ad7f62f484beaf95af33548e Mon Sep 17 00:00:00 2001 From: Thutmose Date: Tue, 11 Oct 2022 19:04:47 -0400 Subject: [PATCH 1/2] fixes some lag related pokemob gui bugs this should address the gui stuff in #1057 --- .../client/gui/pokemob/tabs/Inventory.java | 4 + .../core/client/gui/pokemob/tabs/Storage.java | 2 +- .../pokecube/core/inventory/CustomSlot.java | 25 +++ .../pokecube/core/inventory/TexturedSlot.java | 3 +- .../inventory/pokemob/PokemobContainer.java | 168 ++++++++++-------- 5 files changed, 124 insertions(+), 78 deletions(-) create mode 100644 src/main/java/pokecube/core/inventory/CustomSlot.java diff --git a/src/main/java/pokecube/core/client/gui/pokemob/tabs/Inventory.java b/src/main/java/pokecube/core/client/gui/pokemob/tabs/Inventory.java index e7cee0b4e1..4313a5ec69 100644 --- a/src/main/java/pokecube/core/client/gui/pokemob/tabs/Inventory.java +++ b/src/main/java/pokecube/core/client/gui/pokemob/tabs/Inventory.java @@ -167,6 +167,10 @@ public void init() parent.renderTooltip(pose, tooltip, x, y); })); + this.guard.setFGColor(guarding ? 0xFF00FF00 : 0xFFFF0000); + this.sit.setFGColor(sitting ? 0xFF00FF00 : 0xFFFF0000); + this.stay.setFGColor(staying ? 0xFF00FF00 : 0xFFFF0000); + final int k = (this.width - this.imageWidth) / 2; final int l = (this.height - this.imageHeight) / 2; diff --git a/src/main/java/pokecube/core/client/gui/pokemob/tabs/Storage.java b/src/main/java/pokecube/core/client/gui/pokemob/tabs/Storage.java index 0a35f2e618..8f4c140989 100644 --- a/src/main/java/pokecube/core/client/gui/pokemob/tabs/Storage.java +++ b/src/main/java/pokecube/core/client/gui/pokemob/tabs/Storage.java @@ -105,7 +105,7 @@ public void init() this.addRenderableWidget(new TooltipArea(k + 64, l + 54, 16, 16, TComponent.translatable("pokemob.gui.slot.storage.off_hand"), (x, y) -> { - Slot offhand_slot = menu.slots.get(0); + Slot offhand_slot = menu.slots.get(3); if (offhand_slot.hasItem()) return false; return PokecubeCore.getConfig().pokemobGuiTooltips; }, (b, pose, x, y) -> { diff --git a/src/main/java/pokecube/core/inventory/CustomSlot.java b/src/main/java/pokecube/core/inventory/CustomSlot.java new file mode 100644 index 0000000000..1af4823ee0 --- /dev/null +++ b/src/main/java/pokecube/core/inventory/CustomSlot.java @@ -0,0 +1,25 @@ +package pokecube.core.inventory; + +import net.minecraft.world.Container; +import net.minecraft.world.inventory.Slot; + +public class CustomSlot extends Slot +{ + private boolean active = true; + + public CustomSlot(final Container inventoryIn, final int index, final int xPosition, final int yPosition) + { + super(inventoryIn, index, xPosition, yPosition); + } + + public void setActive(boolean active) + { + this.active = active; + } + + @Override + public boolean isActive() + { + return active; + } +} \ No newline at end of file diff --git a/src/main/java/pokecube/core/inventory/TexturedSlot.java b/src/main/java/pokecube/core/inventory/TexturedSlot.java index 849e68c15e..85bdf644ec 100644 --- a/src/main/java/pokecube/core/inventory/TexturedSlot.java +++ b/src/main/java/pokecube/core/inventory/TexturedSlot.java @@ -3,10 +3,9 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.Container; import net.minecraft.world.inventory.InventoryMenu; -import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; -public class TexturedSlot extends Slot +public class TexturedSlot extends CustomSlot { public TexturedSlot(final Container inventoryIn, final int index, final int xPosition, final int yPosition, final String texture) diff --git a/src/main/java/pokecube/core/inventory/pokemob/PokemobContainer.java b/src/main/java/pokecube/core/inventory/pokemob/PokemobContainer.java index 256c914347..d1bc52bfb1 100644 --- a/src/main/java/pokecube/core/inventory/pokemob/PokemobContainer.java +++ b/src/main/java/pokecube/core/inventory/pokemob/PokemobContainer.java @@ -10,7 +10,6 @@ import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import pokecube.api.entity.pokemob.IPokemob; @@ -18,6 +17,7 @@ import pokecube.core.PokecubeItems; import pokecube.core.client.Resources; import pokecube.core.init.MenuTypes; +import pokecube.core.inventory.CustomSlot; import pokecube.core.inventory.TexturedSlot; import pokecube.core.network.pokemobs.PacketPokemobGui; import pokecube.core.utils.EntityTools; @@ -48,11 +48,86 @@ public PokemobContainer(final int id, final Inventory playerInv, final FriendlyB this.data = data; this.pokemobInv.startOpen(playerInv.player); this.playerInv = playerInv; + this.initSlots(); this.setMode(this.mode); } + private void initSlots() + { + int offhand = pokemobInv.getContainerSize() - 1; + + this.addSlot(new CustomSlot(this.pokemobInv, 0, 64, 18) + { + /** + * Check if the stack is a valid item for this slot. Always true + * beside for the armor slots. + */ + @Override + public boolean mayPlace(final ItemStack stack) + { + return super.mayPlace(stack) && stack.getItem() == Items.SADDLE; + } + }); + this.addSlot(new CustomSlot(this.pokemobInv, 1, 64, 36) + { + /** + * Returns the maximum stack size for a given slot (usually the same + * as getInventoryStackLimit(), but 1 in the case of armor slots) + */ + @Override + public int getMaxStackSize() + { + return 1; + } + + /** + * Check if the stack is a valid item for this slot. Always true + * beside for the armor slots. + */ + @Override + public boolean mayPlace(final ItemStack stack) + { + return !pokemob.getPokedexEntry().stock || PokecubeItems.isValidHeldItem(stack); + } + + @Override + public void onTake(final Player playerIn, final ItemStack stack) + { + final ItemStack old = this.getItem(); + if (ThutCore.proxy.isServerSide()) PokemobContainer.this.pokemob.getPokedexEntry() + .onHeldItemChange(stack, old, PokemobContainer.this.pokemob); + super.onTake(playerIn, stack); + } + + /** Helper method to put a stack in the slot. */ + @Override + public void set(final ItemStack stack) + { + // ItemStack old = getStack(); + super.set(stack); + if (ThutCore.proxy.isServerSide()) PokemobContainer.this.pokemob.setHeldItem(stack); + } + }); + this.addSlot(new TexturedSlot(this.pokemobInv, offhand, 64, 54, Resources.SLOT_ICON_BOOK)); + for (int k = 0; k < 5; ++k) this.addSlot(new CustomSlot(this.pokemobInv, 2 + k, 83 + k * 18, 18) + { + /** + * Check if the stack is a valid item for this slot. Always true + * beside for the armor slots. + */ + @Override + public boolean mayPlace(final ItemStack stack) + { + return true;// ItemList.isValidHeldItem(stack); + } + }); + + this.bindPlayerInventory(this.playerInv, -19); + } + public void setMode(final int mode) { + if (mode != this.mode && playerInv.player.level.isClientSide()) { PacketPokemobGui.sendPagePacket((byte) mode, pokemob.getEntity().getId()); @@ -60,86 +135,29 @@ public void setMode(final int mode) this.mode = (byte) mode; - this.slots.clear(); - this.lastSlots.clear(); - - int offhand = pokemobInv.getContainerSize() - 1; - - if (mode == PacketPokemobGui.STORAGE) + // Toggle activation of slots based on mode. + if (mode == PacketPokemobGui.MAIN) { - this.addSlot(new TexturedSlot(this.pokemobInv, offhand, 64, 54, Resources.SLOT_ICON_BOOK)); + // Main has all of them active + this.slots.forEach(s -> { + if (s instanceof CustomSlot slot) slot.setActive(true); + }); } - else if (this.mode == PacketPokemobGui.MAIN) + else if (mode == PacketPokemobGui.STORAGE) { - this.addSlot(new Slot(this.pokemobInv, 0, 64, 18) - { - /** - * Check if the stack is a valid item for this slot. Always true - * beside for the armor slots. - */ - @Override - public boolean mayPlace(final ItemStack stack) - { - return super.mayPlace(stack) && stack.getItem() == Items.SADDLE; - } + int offhand = pokemobInv.getContainerSize() - 1; + // Storage only has the offhand active + this.slots.forEach(s -> { + if (s instanceof CustomSlot slot) slot.setActive(s.getSlotIndex() == offhand); }); - this.addSlot(new Slot(this.pokemobInv, 1, 64, 36) - { - /** - * Returns the maximum stack size for a given slot (usually the - * same as getInventoryStackLimit(), but 1 in the case of armor - * slots) - */ - @Override - public int getMaxStackSize() - { - return 1; - } - - /** - * Check if the stack is a valid item for this slot. Always true - * beside for the armor slots. - */ - @Override - public boolean mayPlace(final ItemStack stack) - { - return !pokemob.getPokedexEntry().stock || PokecubeItems.isValidHeldItem(stack); - } - - @Override - public void onTake(final Player playerIn, final ItemStack stack) - { - final ItemStack old = this.getItem(); - if (ThutCore.proxy.isServerSide()) PokemobContainer.this.pokemob.getPokedexEntry() - .onHeldItemChange(stack, old, PokemobContainer.this.pokemob); - super.onTake(playerIn, stack); - } - - /** Helper method to put a stack in the slot. */ - @Override - public void set(final ItemStack stack) - { - // ItemStack old = getStack(); - super.set(stack); - if (ThutCore.proxy.isServerSide()) PokemobContainer.this.pokemob.setHeldItem(stack); - } - }); - this.addSlot(new TexturedSlot(this.pokemobInv, offhand, 64, 54, Resources.SLOT_ICON_BOOK)); - for (int k = 0; k < 5; ++k) this.addSlot(new Slot(this.pokemobInv, 2 + k, 83 + k * 18, 18) - { - /** - * Check if the stack is a valid item for this slot. Always true - * beside for the armor slots. - */ - @Override - public boolean mayPlace(final ItemStack stack) - { - return true;// ItemList.isValidHeldItem(stack); - } + } + else + { + // The rest have none active + this.slots.forEach(s -> { + if (s instanceof CustomSlot slot) slot.setActive(false); }); } - - this.bindPlayerInventory(this.playerInv, -19); } @Override From da73f44c293659145668be33dd9914bdaaa7f9c0 Mon Sep 17 00:00:00 2001 From: Thutmose Date: Tue, 11 Oct 2022 19:27:21 -0400 Subject: [PATCH 2/2] Update PacketPokemobGui.java fixes some flickering in the pokemob gui in high latency situations --- .../java/pokecube/core/network/pokemobs/PacketPokemobGui.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/pokecube/core/network/pokemobs/PacketPokemobGui.java b/src/main/java/pokecube/core/network/pokemobs/PacketPokemobGui.java index af6485b1d3..dc7a3b2650 100644 --- a/src/main/java/pokecube/core/network/pokemobs/PacketPokemobGui.java +++ b/src/main/java/pokecube/core/network/pokemobs/PacketPokemobGui.java @@ -35,7 +35,7 @@ public static void sendOpenPacket(Entity target, ServerPlayer player, byte mode) buffer.writeByte(mode); IPokemob pokemob = PokemobCaps.getPokemobFor(target); - if (pokemob != null) + if (pokemob != null && !(player.containerMenu instanceof PokemobContainer)) { StoreTask ai = null; for (final IAIRunnable run : pokemob.getTasks()) if (run instanceof StoreTask task) ai = task;