ForgePatch/patches/minecraft/net/minecraft/tileentity/ChestTileEntity.java.patch

60 lines
2.5 KiB
Diff

--- a/net/minecraft/tileentity/ChestTileEntity.java
+++ b/net/minecraft/tileentity/ChestTileEntity.java
@@ -38,6 +38,7 @@
protected float field_145986_n;
protected int field_145987_o;
private int field_145983_q;
+ private net.minecraftforge.common.util.LazyOptional<net.minecraftforge.items.IItemHandlerModifiable> chestHandler;
protected ChestTileEntity(TileEntityType<?> p_i48287_1_) {
super(p_i48287_1_);
@@ -202,7 +203,7 @@
public static int func_195481_a(IBlockReader p_195481_0_, BlockPos p_195481_1_) {
BlockState blockstate = p_195481_0_.func_180495_p(p_195481_1_);
- if (blockstate.func_177230_c().func_235695_q_()) {
+ if (blockstate.hasTileEntity()) {
TileEntity tileentity = p_195481_0_.func_175625_s(p_195481_1_);
if (tileentity instanceof ChestTileEntity) {
return ((ChestTileEntity)tileentity).field_145987_o;
@@ -221,4 +222,39 @@
protected Container func_213906_a(int p_213906_1_, PlayerInventory p_213906_2_) {
return ChestContainer.func_216992_a(p_213906_1_, p_213906_2_, this);
}
+
+ @Override
+ public void func_145836_u() {
+ super.func_145836_u();
+ if (this.chestHandler != null) {
+ this.chestHandler.invalidate();
+ this.chestHandler = null;
+ }
+ }
+
+ @Override
+ public <T> net.minecraftforge.common.util.LazyOptional<T> getCapability(net.minecraftforge.common.capabilities.Capability<T> cap, Direction side) {
+ if (!this.field_145846_f && cap == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
+ if (this.chestHandler == null)
+ this.chestHandler = net.minecraftforge.common.util.LazyOptional.of(this::createHandler);
+ return this.chestHandler.cast();
+ }
+ return super.getCapability(cap, side);
+ }
+
+ private net.minecraftforge.items.IItemHandlerModifiable createHandler() {
+ BlockState state = this.func_195044_w();
+ if (!(state.func_177230_c() instanceof ChestBlock)) {
+ return new net.minecraftforge.items.wrapper.InvWrapper(this);
+ }
+ IInventory inv = ChestBlock.func_226916_a_((ChestBlock) state.func_177230_c(), state, func_145831_w(), func_174877_v(), true);
+ return new net.minecraftforge.items.wrapper.InvWrapper(inv == null ? this : inv);
+ }
+
+ @Override
+ protected void invalidateCaps() {
+ super.invalidateCaps();
+ if (chestHandler != null)
+ chestHandler.invalidate();
+ }
}