From 0d7db4a5721ffe8b93805bb4e11d2343e8ab452d Mon Sep 17 00:00:00 2001 From: J0nasL <51202569+J0hnL0cke@users.noreply.github.com> Date: Tue, 18 Jul 2023 16:57:27 -0400 Subject: [PATCH] Fixed inventoryClose tracking Fixed inventoryClose tracking based on container/merchant status Also fixed egg in block container reporting for locateegg --- .../egghunt/Controller/CommandHandler.java | 2 +- .../egghunt/Controller/InventoryListener.java | 34 +++++++++++++++---- .../github/J0hnL0cke/egghunt/Model/Data.java | 10 +++--- .../github/J0hnL0cke/egghunt/Model/Egg.java | 7 +--- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/J0hnL0cke/egghunt/Controller/CommandHandler.java b/src/main/java/io/github/J0hnL0cke/egghunt/Controller/CommandHandler.java index c5608cc..893d6f8 100644 --- a/src/main/java/io/github/J0hnL0cke/egghunt/Controller/CommandHandler.java +++ b/src/main/java/io/github/J0hnL0cke/egghunt/Controller/CommandHandler.java @@ -54,7 +54,7 @@ public class CommandHandler { if (type == Egg_Storage_Type.BLOCK) { Block eggBlock = data.getEggBlock(); - if (Egg.hasEgg(eggBlock)) { + if (Egg.isOnlyEgg(eggBlock)) { storageMsg = "has been placed"; } else { diff --git a/src/main/java/io/github/J0hnL0cke/egghunt/Controller/InventoryListener.java b/src/main/java/io/github/J0hnL0cke/egghunt/Controller/InventoryListener.java index 0669899..cf555c0 100644 --- a/src/main/java/io/github/J0hnL0cke/egghunt/Controller/InventoryListener.java +++ b/src/main/java/io/github/J0hnL0cke/egghunt/Controller/InventoryListener.java @@ -4,6 +4,9 @@ package io.github.J0hnL0cke.egghunt.Controller; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Container; +import org.bukkit.block.DoubleChest; +import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -19,6 +22,7 @@ import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Merchant; @@ -133,15 +137,31 @@ public class InventoryListener implements Listener { if (otherInv.getType() != InventoryType.PLAYER) { //TODO check how this affects player viewing own inventory (egg on head/offhand?) - if (!(otherInv.getHolder() instanceof Merchant)) { //chest boat/llama but not villager/wandering trader - //this is a container (chest, furnace, hopper minecart, etc), so the egg will remain here when the inventory is closed - data.updateEggLocation(otherInv); + InventoryHolder holder = otherInv.getHolder(); - } else { - //this is not a container (anvil, crafting table, villager, etc), so it will move back to the player's inventory - //note- if the player's inventory is full, it will instead drop as an item, which will trigger the item drop event - data.updateEggLocation(player); + if (holder instanceof Entity) { + if (holder instanceof Merchant) { //villager/wandering trader + //items will not be stored in this inventory, and will instead revert to the player + logger.log("inventory close- inside merchant entity (reverts to player)"); + data.updateEggLocation(player); + } else { + //this is an entity that stores items (hopper minecart, chest boat, llama, etc), so the egg will remain here when the inventory is closed + logger.log("inventory close- inside non-merchant entity"); + data.updateEggLocation(otherInv); + } + + } else { //this inventory holder is a block + if (holder instanceof Container || holder instanceof DoubleChest) { //DoubleChest implements InventoryHolder directly instead of implementing Container + //this is a container (furnace, chest boat, etc), so the egg will stay here + logger.log("inventory close- inside container block"); + data.updateEggLocation(otherInv); + } else { + //this is not a container (anvil, crafting table, villager, etc), so it will move back to the player's inventory + //note- if the player's inventory is full, it will instead drop as an item, which will trigger the item drop event + logger.log("inventory close- inside non-container block (reverts to player)"); + data.updateEggLocation(player); + } } } } diff --git a/src/main/java/io/github/J0hnL0cke/egghunt/Model/Data.java b/src/main/java/io/github/J0hnL0cke/egghunt/Model/Data.java index 98c0fbb..1f930b8 100644 --- a/src/main/java/io/github/J0hnL0cke/egghunt/Model/Data.java +++ b/src/main/java/io/github/J0hnL0cke/egghunt/Model/Data.java @@ -352,9 +352,11 @@ public class Data { } } + /** + * likely to be called when a generic inventory is the most info given for what picked up an item, like on hopper collect event + * if more info is available (such as Entity or Block instance), better to pass that instead, although this should still work + */ public void updateEggLocation(Inventory inv) { - //likely to be called when a generic inventory is the most info given for what picked up an item, like on hopper collect event - //if more info is available (such as Entity or Block instance), better to pass that instead, although this should still work if (inv.getHolder() instanceof Entity){ // Hopper minecart, llama, or some other entity has the egg for (Entity e : inv.getLocation().getWorld().getEntities()) { @@ -370,8 +372,8 @@ public class Data { log("could not find correct inventoryHolder entity!"); } else { - // Hopper picked up the egg - updateEggLocation(inv.getLocation().getBlock()); //TODO check if this cast works or if .getLocation().getBlock() is needed + // Block contains the egg + updateEggLocation(inv.getLocation().getBlock()); } } diff --git a/src/main/java/io/github/J0hnL0cke/egghunt/Model/Egg.java b/src/main/java/io/github/J0hnL0cke/egghunt/Model/Egg.java index 9a0e794..8e09f5b 100644 --- a/src/main/java/io/github/J0hnL0cke/egghunt/Model/Egg.java +++ b/src/main/java/io/github/J0hnL0cke/egghunt/Model/Egg.java @@ -204,12 +204,7 @@ public class Egg { if (inventory == null) { return false; } - for (ItemStack stack : inventory.getContents()) { - if (hasEgg(stack)) { - return true; - } - } - return false; + return hasEgg(inventory.getContents()); } /**