Fixed inventory tracking

Added more methods to Egg class for checking for egg
This commit is contained in:
J0nasL 2023-07-08 15:13:37 -04:00
parent a17cf4cf63
commit 127618c13a
5 changed files with 102 additions and 46 deletions

View file

@ -142,9 +142,10 @@ public class EggDestroyListener implements Listener {
block.setType(Material.AIR);
}
} else {
if (blockState instanceof Container) { //TODO exclude item frames, which aren't destroyed by this
Container cont = (Container) blockState;
if (cont.getInventory().contains(Material.DRAGON_EGG)) {
if (cont.getInventory().contains(Material.DRAGON_EGG)) { //TODO update for egg in shulker/bundle
destroy = true;
cont.getInventory().remove(Material.DRAGON_EGG); //make sure egg is removed from container
}

View file

@ -77,34 +77,14 @@ public class EventScheduler extends BukkitRunnable {
private void respawnEgg(Location respawnLoc) {
if (config.getRespawnEgg()) {
if (config.getRespawnImmediately()) {
Egg.respawnEgg(config);
} else {
data.resetEggLocation();
Announcement.announce("It will respawn the next time the dragon is defeated", logger);
}
}
}
public boolean hasEgg(Entity entity) {
if (entity instanceof Player) {
return ((Player) entity).getInventory().contains(Material.DRAGON_EGG);
} else if (entity instanceof LivingEntity) {
LivingEntity mob = (((LivingEntity) entity));
EntityEquipment inv = mob.getEquipment();
if (Egg.hasEgg(inv.getItemInMainHand())) {
return true;
}
if (Egg.hasEgg(inv.getItemInOffHand())) {
return true;
}
} else if (entity instanceof FallingBlock) {
return Egg.hasEgg(((FallingBlock) entity));
} else if (entity instanceof Item) {
return Egg.hasEgg((Item) entity);
}
return false;
}
public boolean isUnderWorld(Entity entity) {
return entity.getLocation().getY() < UNDER_WORLD_HEIGHT;
}

View file

@ -107,8 +107,8 @@ public class InventoryListener implements Listener {
//force an egg in the ender chest to be dropped if enabled in config and if the player is not in creative
if (otherInv.getType().equals(InventoryType.ENDER_CHEST)) {
if (config.getDropEnderchestedEgg() && player.getGameMode() != GameMode.CREATIVE) {
if (otherInv.contains(Material.DRAGON_EGG)) {
ItemStack egg = otherInv.getItem(otherInv.first(Material.DRAGON_EGG));
if (otherInv.contains(Material.DRAGON_EGG)) {
ItemStack egg = otherInv.getItem(otherInv.first(Material.DRAGON_EGG)); //TODO make this work with bundles/shulkers
Location playerLoc = player.getLocation();
otherInv.remove(egg);
Item i = playerLoc.getWorld().dropItem(playerLoc, egg); //TODO use drop item function
@ -126,12 +126,12 @@ public class InventoryListener implements Listener {
return;
}
if (player.getInventory().contains(Material.DRAGON_EGG)) {
if (Egg.hasEgg(player.getInventory())) {
//if the player has the egg in their inventory, it will stay there
data.updateEggLocation(player);
data.setEggOwner(player);
} else if (otherInv.contains(Material.DRAGON_EGG)) {
} else if (Egg.hasEgg(otherInv)) {
if (otherInv.getType() != InventoryType.PLAYER) { //TODO check how this affects player viewing own inventory (egg on head/offhand?)
@ -156,7 +156,7 @@ public class InventoryListener implements Listener {
public void onInventoryMove(InventoryMoveItemEvent event) {
//check if the item being moved is the egg
if (Egg.hasEgg(event.getItem())) {
if (event.getDestination().firstEmpty() != -1 || event.getDestination().contains(Material.DRAGON_EGG)) {
if (event.getDestination().firstEmpty() != -1 || Egg.hasEgg(event.getDestination())) {
data.updateEggLocation(event.getDestination());
}
}

View file

@ -185,7 +185,7 @@ public class MiscListener implements Listener {
*/
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerDeath(PlayerDeathEvent event) {
if (!event.getKeepInventory() && event.getEntity().getInventory().contains(Material.DRAGON_EGG)) {
if (!event.getKeepInventory() && Egg.hasEgg(event.getEntity().getInventory())) {
data.resetEggOwner(false);
//change the death message

View file

@ -10,7 +10,9 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
@ -93,10 +95,10 @@ public class Egg {
case SHULKER_BOX:
BlockStateMeta meta = (BlockStateMeta) stack.getItemMeta();
ShulkerBox box = (ShulkerBox) meta.getBlockState();
return hasOnlyEgg(box.getInventory());
return hasEgg(box.getInventory());
case BUNDLE:
BundleMeta bundle = (BundleMeta) stack.getItemMeta();
return hasOnlyEgg(bundle);
return hasEgg(bundle);
default:
return false;
}
@ -114,7 +116,63 @@ public class Egg {
BlockState state = block.getState();
if (state instanceof Container) {
Container cont = (Container) state;
return hasOnlyEgg(cont.getInventory());
return hasEgg(cont.getInventory());
}
return false;
}
/**
* Checks if the given Entity is holding the dragon egg. Also returns false if the provided entity is null.
* @param entity
* @return True if the entity is holding the dragon egg or a container that is holding the egg, otherwise false
*/
public boolean hasEgg(Entity entity) {
if (entity instanceof Player) {
return Egg.hasEgg(((Player) entity).getInventory());
} else if (entity instanceof LivingEntity) {
LivingEntity mob = (((LivingEntity) entity));
EntityEquipment inv = mob.getEquipment();
if (Egg.hasEgg(inv.getItemInMainHand())) {
return true;
} else if (Egg.hasEgg(inv.getItemInOffHand())) {
return true;
}
} else if (entity instanceof FallingBlock) {
return Egg.hasEgg(((FallingBlock) entity));
} else if (entity instanceof Item) {
return Egg.hasEgg(((Item) entity));
}
return false;
}
public boolean removeEgg(Entity entity) {
if (entity instanceof Player) {
if (Egg.hasEgg(((Player) entity).getInventory())) {
//TODO
}
} else if (entity instanceof LivingEntity) {
LivingEntity mob = (((LivingEntity) entity));
EntityEquipment inv = mob.getEquipment();
if (Egg.hasEgg(inv.getItemInMainHand())) {
inv.setItemInMainHand(null);
return true;
} else if (Egg.hasEgg(inv.getItemInOffHand())) {
inv.setItemInOffHand(null);
return true;
}
} else if (entity instanceof FallingBlock) {
FallingBlock block = (FallingBlock) entity;
if (Egg.hasEgg(block)) {
block.remove();
return true;
}
} else if (entity instanceof Item) {
Item item = (Item) entity;
if (Egg.hasEgg(item)) {
item.remove();
return true;
}
}
return false;
}
@ -143,6 +201,23 @@ public class Egg {
return isEgg(stack.getType()) || containsEgg(stack);
}
/**
* Check if the given Inventory contains the dragon egg or a container holding the egg. Also returns false if the provided inventory is null.
* @param inventory Inventory to check
* @return True if the inventory contains the a dragon egg item or an item holding the egg, otherwise false
*/
public static boolean hasEgg(Inventory inventory) {
if (inventory == null) {
return false;
}
for (ItemStack stack : inventory.getContents()) {
if (hasEgg(stack)){
return true;
}
}
return false;
}
/**
* Check if the given Item is the dragon egg or is holding the egg. Also returns false if the provided item is null.
* @param item Item to check
@ -179,6 +254,20 @@ public class Egg {
return isEgg(block.getBlockData().getMaterial()); //don't check containsEgg because shulker can't be falling block
}
/**
* Check if the given BundleMeta contains the dragon egg or a container holding the egg. Also returns false if the provided bundle is null.
* @param bundle Bundle to check
* @return True if the bundle contains either the dragon egg or a container (another bundle) holding the egg, otherwise false
*/
public static boolean hasEgg(BundleMeta bundle) {
for (ItemStack stack : bundle.getItems()) {
if (hasEgg(stack)) {
return true;
}
}
return false;
}
/**
* Check if the given ItemStack is the dragon egg. Also returns false if the provided stack is null.
* @param stack ItemStack to check
@ -239,19 +328,5 @@ public class Egg {
return inventory.contains(Material.DRAGON_EGG);
}
/**
* Check if the given BundleMeta contains the dragon egg. Also returns false if the provided bundle is null.
* @param bundle Bundle to check
* @return True if the bundle contains a dragon egg, otherwise false
*/
public static boolean hasOnlyEgg(BundleMeta bundle) {
for (ItemStack stack : bundle.getItems()) {
if (isOnlyEgg(stack)) {
return true;
}
}
return false;
}
}