Fixed inventoryClose tracking

Fixed inventoryClose tracking based on container/merchant status
Also fixed egg in block container reporting for locateegg
This commit is contained in:
J0nasL 2023-07-18 16:57:27 -04:00
parent f7191688f4
commit 0d7db4a572
4 changed files with 35 additions and 18 deletions

View file

@ -54,7 +54,7 @@ public class CommandHandler {
if (type == Egg_Storage_Type.BLOCK) { if (type == Egg_Storage_Type.BLOCK) {
Block eggBlock = data.getEggBlock(); Block eggBlock = data.getEggBlock();
if (Egg.hasEgg(eggBlock)) { if (Egg.isOnlyEgg(eggBlock)) {
storageMsg = "has been placed"; storageMsg = "has been placed";
} else { } else {

View file

@ -4,6 +4,9 @@ package io.github.J0hnL0cke.egghunt.Controller;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; 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.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; 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.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant; 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.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 InventoryHolder holder = otherInv.getHolder();
//this is a container (chest, furnace, hopper minecart, etc), so the egg will remain here when the inventory is closed
data.updateEggLocation(otherInv);
} else { if (holder instanceof Entity) {
//this is not a container (anvil, crafting table, villager, etc), so it will move back to the player's inventory if (holder instanceof Merchant) { //villager/wandering trader
//note- if the player's inventory is full, it will instead drop as an item, which will trigger the item drop event //items will not be stored in this inventory, and will instead revert to the player
data.updateEggLocation(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);
}
} }
} }
} }

View file

@ -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) { 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){ if (inv.getHolder() instanceof Entity){
// Hopper minecart, llama, or some other entity has the egg // Hopper minecart, llama, or some other entity has the egg
for (Entity e : inv.getLocation().getWorld().getEntities()) { for (Entity e : inv.getLocation().getWorld().getEntities()) {
@ -370,8 +372,8 @@ public class Data {
log("could not find correct inventoryHolder entity!"); log("could not find correct inventoryHolder entity!");
} else { } else {
// Hopper picked up the egg // Block contains the egg
updateEggLocation(inv.getLocation().getBlock()); //TODO check if this cast works or if .getLocation().getBlock() is needed updateEggLocation(inv.getLocation().getBlock());
} }
} }

View file

@ -204,12 +204,7 @@ public class Egg {
if (inventory == null) { if (inventory == null) {
return false; return false;
} }
for (ItemStack stack : inventory.getContents()) { return hasEgg(inventory.getContents());
if (hasEgg(stack)) {
return true;
}
}
return false;
} }
/** /**