Fix interact checking

Correctly track egg when right-clicking on an allay. Also cleaned up references to dragon egg material checking
This commit is contained in:
J0nasL 2023-07-01 04:35:18 -04:00
parent 3c7d400fed
commit edc67fa1f4
4 changed files with 44 additions and 14 deletions

View file

@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CompassMeta;
import io.github.J0hnL0cke.egghunt.Model.Data;
import io.github.J0hnL0cke.egghunt.Model.Egg;
import io.github.J0hnL0cke.egghunt.Model.Data.Egg_Storage_Type;
public class CommandHandler {
@ -41,7 +42,7 @@ public class CommandHandler {
if (type == Egg_Storage_Type.BLOCK) {
Block eggBlock = data.getEggBlock();
if (eggBlock.getType() == Material.DRAGON_EGG) {
if (Egg.isEgg(eggBlock.getType())) {
storageMsg = "has been placed";
} else {

View file

@ -115,7 +115,7 @@ public class EggDestroyListener implements Listener {
for (BlockState blockState : blocks) {
console_log(String.format("Block update at %s: from %s to %s", blockState.getLocation(),
blockState.getBlock().getType(), blockState.getType()));
if (blockState.getBlock().getType().equals(Material.DRAGON_EGG)) {
if (Egg.isEgg(blockState.getBlock())) {
egg_affected = true;
l = blockState.getLocation();
//extra check to make sure egg is removed

View file

@ -86,16 +86,16 @@ public class EventScheduler extends BukkitRunnable {
} else if (entity instanceof LivingEntity) {
LivingEntity mob = (((LivingEntity) entity));
EntityEquipment inv = mob.getEquipment();
if (inv.getItemInMainHand().getType().equals(Material.DRAGON_EGG)) {
if (Egg.isEgg(inv.getItemInMainHand())) {
return true;
}
if (inv.getItemInOffHand().getType().equals(Material.DRAGON_EGG)) {
if (Egg.isEgg(inv.getItemInOffHand())) {
return true;
}
} else if (entity instanceof FallingBlock) {
return ((FallingBlock) entity).getBlockData().getMaterial().equals(Material.DRAGON_EGG);
return Egg.isEgg(((FallingBlock) entity));
} else if (entity instanceof Item) {
return ((Item) entity).getItemStack().getType().equals(Material.DRAGON_EGG);
return Egg.isEgg((Item) entity);
}
return false;
}

View file

@ -97,16 +97,45 @@ public class MiscListener implements Listener {
}
/**
* Handle the egg being placed in an item frame
* Handle the egg being placed in an item frame or allay
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInteract(PlayerInteractEntityEvent event) {
if (event.getRightClicked() instanceof ItemFrame) {
PlayerInventory player_inv = event.getPlayer().getInventory();
if (Egg.isEgg(player_inv.getItemInMainHand()) || Egg.isEgg(player_inv.getItemInOffHand())) {
data.updateEggLocation(event.getRightClicked());
data.setEggOwner(event.getPlayer());
}
PlayerInventory playerInv = event.getPlayer().getInventory();
switch (event.getRightClicked().getType()) {
case ALLAY:
Allay allay = (Allay) event.getRightClicked();
if (!(allay.getEquipment().getItemInMainHand().getType().equals(Material.AIR))) { //if allay has an item
if (Egg.isEgg(allay.getEquipment().getItemInMainHand())
&& playerInv.getItemInMainHand().getType().equals(Material.AIR)) {
//interaction with empty main hand will remove the egg from the allay and put it in the player's inventory
data.updateEggLocation(event.getPlayer());
data.setEggOwner(event.getPlayer());
}
break; //allay has an item, giving it the egg is not possible
}
//otherwise, continue to next check
case ITEM_FRAME:
case GLOW_ITEM_FRAME:
ItemStack heldItem = playerInv.getItemInMainHand();
if (heldItem.getType().equals(Material.AIR)) { //if main hand empty, check offhand
heldItem = playerInv.getItemInOffHand();
}
if (!heldItem.getType().equals(Material.AIR)) { //if item in either hand
if (Egg.isEgg(heldItem)) {
data.updateEggLocation(event.getRightClicked());
data.setEggOwner(event.getPlayer());
}
}
default:
break;
}
}
@ -150,7 +179,7 @@ public class MiscListener implements Listener {
//egg lands
data.updateEggLocation(event.getBlock());
} else if (event.getBlock().getType() == Material.DRAGON_EGG) {
} else if (Egg.isEgg(event.getBlock())) {
//egg begins falling
data.updateEggLocation(event.getEntity());
}