Prevent opening an Iron Chest if an ocelot is sitting on it!

* Matches vanilla behavior.
This commit is contained in:
Charsy89 committed 2025-06-23 02:04:01 -06:00
1 parent 9faaa0eb25
commit d63df23d64
1 file changed
+23
@@ -22,6 +22,7 @@ import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
@@ -29,6 +30,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
@@ -119,6 +121,12 @@ public class BlockIronChest extends BlockContainer {
return true;
}
if (isOcelotSitting(world, i, j, k)) {
// One or more Ocelots are sitting on this chest. Return true as if the player successfully used the chest,
// but don't open the chest!
return true;
}
player.openGui(IronChest.instance, ((TileEntityIronChest) te).getType().ordinal(), world, i, j, k);
return true;
}
@@ -281,4 +289,19 @@ public class BlockIronChest extends BlockContainer {
return false;
}
private static boolean isOcelotSitting(World world, int x, int y, int z) {
@SuppressWarnings("unchecked")
List<EntityOcelot> cats = world.getEntitiesWithinAABB(EntityOcelot.class, AxisAlignedBB.getBoundingBox(
(double)x, (double)(y + 1), (double)z, (double)(x + 1), (double)(y + 2), (double)(z + 1)));
for(EntityOcelot cat : cats) {
if(cat.isSitting()) {
// There are cats in the block above this chest and at least one of them is sitting.
return true;
}
}
// No cats or none of them are sitting. :(
return false;
}
}