From 458655ada2680b1aeb83aa554eafcc5ac4a15d17 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Fri, 15 Mar 2019 12:26:32 -0500 Subject: [PATCH] Ensure inventories and multiblock servants mark dirty when contents change Using a new method markDirtyFast as the vanilla method also does a ton of block updates --- .../mantle/multiblock/MultiServantLogic.java | 20 +++++++++---------- .../mantle/tileentity/MantleTileEntity.java | 10 ++++++++++ .../mantle/tileentity/TileInventory.java | 20 ++++++++++++++++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/main/java/slimeknights/mantle/multiblock/MultiServantLogic.java b/src/main/java/slimeknights/mantle/multiblock/MultiServantLogic.java index 45fd61b..48630b9 100644 --- a/src/main/java/slimeknights/mantle/multiblock/MultiServantLogic.java +++ b/src/main/java/slimeknights/mantle/multiblock/MultiServantLogic.java @@ -5,22 +5,19 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; import net.minecraft.network.play.server.SPacketUpdateTileEntity; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.ForgeRegistries; +import slimeknights.mantle.tileentity.MantleTileEntity; import javax.annotation.Nonnull; -public class MultiServantLogic extends TileEntity implements IServantLogic { +public class MultiServantLogic extends MantleTileEntity implements IServantLogic { boolean hasMaster; - BlockPos master; - Block masterBlock; - IBlockState state; public boolean canUpdate() { @@ -36,8 +33,8 @@ public class MultiServantLogic extends TileEntity implements IServantLogic { return false; } - if(this.world.getBlockState(this.master).getBlock() == this.masterBlock && this.world - .getBlockState(this.master) == this.state) { + if(this.world.getBlockState(this.master).getBlock() == this.masterBlock + && this.world.getBlockState(this.master) == this.state) { return true; } else { @@ -57,6 +54,7 @@ public class MultiServantLogic extends TileEntity implements IServantLogic { this.master = pos; this.state = this.world.getBlockState(this.master); this.masterBlock = this.state.getBlock(); + this.markDirtyFast(); } public void removeMaster() { @@ -64,6 +62,7 @@ public class MultiServantLogic extends TileEntity implements IServantLogic { this.master = null; this.masterBlock = null; this.state = null; + this.markDirtyFast(); } @Override @@ -73,8 +72,8 @@ public class MultiServantLogic extends TileEntity implements IServantLogic { @Deprecated public boolean verifyMaster(IMasterLogic logic, BlockPos pos) { - return this.master.equals(pos) && this.world.getBlockState(pos) == this.state && this.world.getBlockState(pos) - .getBlock() == this.masterBlock; + return this.master.equals(pos) && this.world.getBlockState(pos) == this.state + && this.world.getBlockState(pos).getBlock() == this.masterBlock; } @Override @@ -90,8 +89,7 @@ public class MultiServantLogic extends TileEntity implements IServantLogic { @Override public void invalidateMaster(IMasterLogic master, World w, BlockPos pos) { - this.hasMaster = false; - master = null; + this.removeMaster(); } @Override diff --git a/src/main/java/slimeknights/mantle/tileentity/MantleTileEntity.java b/src/main/java/slimeknights/mantle/tileentity/MantleTileEntity.java index c8a47b9..0f90d79 100644 --- a/src/main/java/slimeknights/mantle/tileentity/MantleTileEntity.java +++ b/src/main/java/slimeknights/mantle/tileentity/MantleTileEntity.java @@ -7,4 +7,14 @@ public class MantleTileEntity extends TileEntity { public boolean isClient() { return this.getWorld() != null && this.getWorld().isRemote; } + + /** + * Marks the chunk dirty without performing comparator updates or block state checks + * Used since most of our markDirty calls only adjust TE data + */ + public void markDirtyFast() { + if (world != null) { + world.markChunkDirty(pos, this); + } + } } diff --git a/src/main/java/slimeknights/mantle/tileentity/TileInventory.java b/src/main/java/slimeknights/mantle/tileentity/TileInventory.java index 136570a..f1227dd 100644 --- a/src/main/java/slimeknights/mantle/tileentity/TileInventory.java +++ b/src/main/java/slimeknights/mantle/tileentity/TileInventory.java @@ -51,6 +51,7 @@ public class TileInventory extends MantleTileEntity implements IInventory { return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing); } + @SuppressWarnings("unchecked") @Override public T getCapability(@Nonnull Capability capability, EnumFacing facing) { if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { @@ -80,12 +81,17 @@ public class TileInventory extends MantleTileEntity implements IInventory { } public void resize(int size) { + // save effort if the size did not change + if(size == inventory.size()) { + return; + } ItemStackList newInventory = ItemStackList.withSize(size); for (int i = 0; i < size && i < inventory.size(); i++) { newInventory.set(i, inventory.get(i)); } inventory = newInventory; + this.markDirtyFast(); } @Override @@ -104,16 +110,23 @@ public class TileInventory extends MantleTileEntity implements IInventory { return; } + ItemStack current = inventory.get(slot); inventory.set(slot, itemstack); if(!itemstack.isEmpty() && itemstack.getCount() > getInventoryStackLimit()) { itemstack.setCount(getInventoryStackLimit()); } + if(!ItemStack.areItemStacksEqual(current, itemstack)) { + this.markDirtyFast(); + } } @Nonnull @Override public ItemStack decrStackSize(int slot, int quantity) { + if(quantity <= 0) { + return ItemStack.EMPTY; + } ItemStack itemStack = getStackInSlot(slot); if(itemStack.isEmpty()) { @@ -123,18 +136,19 @@ public class TileInventory extends MantleTileEntity implements IInventory { // whole itemstack taken out if(itemStack.getCount() <= quantity) { setInventorySlotContents(slot, ItemStack.EMPTY); - this.markDirty(); + this.markDirtyFast(); return itemStack; } // split itemstack itemStack = itemStack.splitStack(quantity); // slot is empty, set to ItemStack.EMPTY + // isn't this redundant to the above check? if(getStackInSlot(slot).getCount() == 0) { setInventorySlotContents(slot, ItemStack.EMPTY); } - this.markDirty(); + this.markDirtyFast(); // return remainder return itemStack; } @@ -200,7 +214,7 @@ public class TileInventory extends MantleTileEntity implements IInventory { } return - entityplayer.getDistanceSq((double) pos.getX() + 0.5D, (double) pos.getY() + 0.5D, (double) pos.getZ() + 0.5D) + entityplayer.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D) <= 64D; }