From 920e25115ab67f8faed683ea741742f81a933bb4 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Fri, 26 Jun 2020 01:11:47 +0200 Subject: [PATCH] Optimize Hopper logic --- .../net/minecraft/server/BlockHopper.java | 230 ++++++++++++++++++ .../minecraft/server/TileEntityHopper.java | 36 ++- 2 files changed, 262 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/minecraft/server/BlockHopper.java diff --git a/src/main/java/net/minecraft/server/BlockHopper.java b/src/main/java/net/minecraft/server/BlockHopper.java new file mode 100644 index 000000000..31ee358f8 --- /dev/null +++ b/src/main/java/net/minecraft/server/BlockHopper.java @@ -0,0 +1,230 @@ +package net.minecraft.server; + +import java.util.OptionalInt; +import net.minecraft.server.Block; +import net.minecraft.server.BlockActionContext; +import net.minecraft.server.BlockBase; +import net.minecraft.server.BlockPosition; +import net.minecraft.server.BlockProperties; +import net.minecraft.server.BlockStateBoolean; +import net.minecraft.server.BlockStateDirection; +import net.minecraft.server.BlockStateList; +import net.minecraft.server.BlockTileEntity; +import net.minecraft.server.Container; +import net.minecraft.server.Entity; +import net.minecraft.server.EntityHuman; +import net.minecraft.server.EntityLiving; +import net.minecraft.server.EnumBlockMirror; +import net.minecraft.server.EnumBlockRotation; +import net.minecraft.server.EnumDirection; +import net.minecraft.server.EnumHand; +import net.minecraft.server.EnumInteractionResult; +import net.minecraft.server.EnumRenderType; +import net.minecraft.server.IBlockAccess; +import net.minecraft.server.IBlockData; +import net.minecraft.server.IBlockDataHolder; +import net.minecraft.server.IBlockState; +import net.minecraft.server.IChatBaseComponent; +import net.minecraft.server.IHopper; +import net.minecraft.server.IInventory; +import net.minecraft.server.ITileInventory; +import net.minecraft.server.InventoryUtils; +import net.minecraft.server.ItemStack; +import net.minecraft.server.MinecraftKey; +import net.minecraft.server.MovingObjectPositionBlock; +import net.minecraft.server.OperatorBoolean; +import net.minecraft.server.PathMode; +import net.minecraft.server.StatisticList; +import net.minecraft.server.TileEntity; +import net.minecraft.server.TileEntityHopper; +import net.minecraft.server.VoxelShape; +import net.minecraft.server.VoxelShapeCollision; +import net.minecraft.server.VoxelShapes; +import net.minecraft.server.World; + +public class BlockHopper extends BlockTileEntity { + public static final BlockStateDirection FACING = BlockProperties.N; + public static final BlockStateBoolean ENABLED = BlockProperties.f; + private static final VoxelShape c = Block.a((double) 0.0, (double) 10.0, (double) 0.0, (double) 16.0, (double) 16.0, + (double) 16.0); + private static final VoxelShape d = Block.a((double) 4.0, (double) 4.0, (double) 4.0, (double) 12.0, (double) 10.0, + (double) 12.0); + private static final VoxelShape e = VoxelShapes.a((VoxelShape) d, (VoxelShape) c); + private static final VoxelShape f = VoxelShapes.a((VoxelShape) e, (VoxelShape) IHopper.a, + (OperatorBoolean) OperatorBoolean.ONLY_FIRST); + private static final VoxelShape g = VoxelShapes.a((VoxelShape) f, + (VoxelShape) Block.a((double) 6.0, (double) 0.0, (double) 6.0, (double) 10.0, (double) 4.0, (double) 10.0)); + private static final VoxelShape h = VoxelShapes.a((VoxelShape) f, (VoxelShape) Block.a((double) 12.0, (double) 4.0, + (double) 6.0, (double) 16.0, (double) 8.0, (double) 10.0)); + private static final VoxelShape i = VoxelShapes.a((VoxelShape) f, + (VoxelShape) Block.a((double) 6.0, (double) 4.0, (double) 0.0, (double) 10.0, (double) 8.0, (double) 4.0)); + private static final VoxelShape j = VoxelShapes.a((VoxelShape) f, (VoxelShape) Block.a((double) 6.0, (double) 4.0, + (double) 12.0, (double) 10.0, (double) 8.0, (double) 16.0)); + private static final VoxelShape k = VoxelShapes.a((VoxelShape) f, + (VoxelShape) Block.a((double) 0.0, (double) 4.0, (double) 6.0, (double) 4.0, (double) 8.0, (double) 10.0)); + private static final VoxelShape o = IHopper.a; + private static final VoxelShape p = VoxelShapes.a((VoxelShape) IHopper.a, (VoxelShape) Block.a((double) 12.0, + (double) 8.0, (double) 6.0, (double) 16.0, (double) 10.0, (double) 10.0)); + private static final VoxelShape q = VoxelShapes.a((VoxelShape) IHopper.a, + (VoxelShape) Block.a((double) 6.0, (double) 8.0, (double) 0.0, (double) 10.0, (double) 10.0, (double) 4.0)); + private static final VoxelShape r = VoxelShapes.a((VoxelShape) IHopper.a, (VoxelShape) Block.a((double) 6.0, + (double) 8.0, (double) 12.0, (double) 10.0, (double) 10.0, (double) 16.0)); + private static final VoxelShape s = VoxelShapes.a((VoxelShape) IHopper.a, + (VoxelShape) Block.a((double) 0.0, (double) 8.0, (double) 6.0, (double) 4.0, (double) 10.0, (double) 10.0)); + + public BlockHopper(BlockBase.Info var0) { + super(var0); + this.j((IBlockData) ((IBlockData) ((IBlockData) this.blockStateList.getBlockData()).set((IBlockState) FACING, + (Comparable) EnumDirection.DOWN)).set((IBlockState) ENABLED, (Comparable) Boolean.valueOf(true))); + } + + public VoxelShape b(IBlockData var0, IBlockAccess var1, BlockPosition var2, VoxelShapeCollision var3) { + switch ((EnumDirection) var0.get((IBlockState) FACING)) { + case DOWN : { + return g; + } + case NORTH : { + return i; + } + case SOUTH : { + return j; + } + case WEST : { + return k; + } + case EAST : { + return h; + } + } + return f; + } + + public VoxelShape a_(IBlockData var0, IBlockAccess var1, BlockPosition var2) { + switch ((EnumDirection) var0.get((IBlockState) FACING)) { + case DOWN : { + return o; + } + case NORTH : { + return q; + } + case SOUTH : { + return r; + } + case WEST : { + return s; + } + case EAST : { + return p; + } + } + return IHopper.a; + } + + public IBlockData getPlacedState(BlockActionContext var0) { + EnumDirection var1; + return (IBlockData) ((IBlockData) this.getBlockData().set((IBlockState) FACING, + (Comparable) ((var1 = var0.getClickedFace().opposite()).n() == EnumDirection.EnumAxis.Y + ? EnumDirection.DOWN + : var1))).set((IBlockState) ENABLED, (Comparable) Boolean.valueOf(true)); + } + + public TileEntity createTile(IBlockAccess var0) { + return new TileEntityHopper(); + } + + public void postPlace(World var0, BlockPosition var1, IBlockData var2, EntityLiving var3, ItemStack var4) { + TileEntity var5; + if (var4.hasName() && (var5 = var0.getTileEntity(var1)) instanceof TileEntityHopper) { + ((TileEntityHopper) var5).setCustomName(var4.getName()); + } + } + + public void onPlace(IBlockData var0, World var1, BlockPosition var2, IBlockData var3, boolean var4) { + if (var3.a(var0.getBlock())) { + return; + } + this.a(var1, var2, var0); + } + + public EnumInteractionResult interact(IBlockData var0, World var1, BlockPosition var2, EntityHuman var3, + EnumHand var4, MovingObjectPositionBlock var5) { + if (var1.isClientSide) { + return EnumInteractionResult.SUCCESS; + } + TileEntity var6 = var1.getTileEntity(var2); + if (var6 instanceof TileEntityHopper) { + var3.openContainer((ITileInventory) ((TileEntityHopper) var6)); + var3.a(StatisticList.INSPECT_HOPPER); + } + return EnumInteractionResult.CONSUME; + } + + public void doPhysics(IBlockData var0, World var1, BlockPosition var2, Block var3, BlockPosition var4, + boolean var5) { + this.a(var1, var2, var0); + //YAPFA start + TileEntity tileEntity = var1.getTileEntity(var2); + if (tileEntity instanceof TileEntityHopper) { + ((TileEntityHopper)tileEntity).flushCaches(); + } + // YAPFA end + } + + private void a(World var0, BlockPosition var1, IBlockData var2) { + boolean var3; + boolean bl = var3 = !var0.isBlockIndirectlyPowered(var1); + if (var3 != (Boolean) var2.get((IBlockState) ENABLED)) { + var0.setTypeAndData(var1, (IBlockData) var2.set((IBlockState) ENABLED, (Comparable) Boolean.valueOf(var3)), + 4); + } + } + + public void remove(IBlockData var0, World var1, BlockPosition var2, IBlockData var3, boolean var4) { + if (var0.a(var3.getBlock())) { + return; + } + TileEntity var5 = var1.getTileEntity(var2); + if (var5 instanceof TileEntityHopper) { + InventoryUtils.dropInventory((World) var1, (BlockPosition) var2, (IInventory) ((TileEntityHopper) var5)); + var1.updateAdjacentComparators(var2, (Block) this); + } + super.remove(var0, var1, var2, var3, var4); + } + + public EnumRenderType b(IBlockData var0) { + return EnumRenderType.MODEL; + } + + public boolean isComplexRedstone(IBlockData var0) { + return true; + } + + public int a(IBlockData var0, World var1, BlockPosition var2) { + return Container.a((TileEntity) var1.getTileEntity(var2)); + } + + public IBlockData a(IBlockData var0, EnumBlockRotation var1) { + return (IBlockData) var0.set((IBlockState) FACING, + (Comparable) var1.a((EnumDirection) var0.get((IBlockState) FACING))); + } + + public IBlockData a(IBlockData var0, EnumBlockMirror var1) { + return var0.a(var1.a((EnumDirection) var0.get((IBlockState) FACING))); + } + + protected void a(BlockStateList.a var0) { + var0.a(new IBlockState[]{FACING, ENABLED}); + } + + public void a(IBlockData var0, World var1, BlockPosition var2, Entity var3) { + TileEntity var4 = var1.getTileEntity(var2); + if (var4 instanceof TileEntityHopper) { + ((TileEntityHopper) var4).a(var3); + } + } + + public boolean a(IBlockData var0, IBlockAccess var1, BlockPosition var2, PathMode var3) { + return false; + } + +} \ No newline at end of file diff --git a/src/main/java/net/minecraft/server/TileEntityHopper.java b/src/main/java/net/minecraft/server/TileEntityHopper.java index 728c829c1..42bd33197 100644 --- a/src/main/java/net/minecraft/server/TileEntityHopper.java +++ b/src/main/java/net/minecraft/server/TileEntityHopper.java @@ -2,6 +2,7 @@ package net.minecraft.server; import java.util.Iterator; import java.util.List; +import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -611,14 +612,28 @@ public class TileEntityHopper extends TileEntityLootable implements IHopper, ITi @Nullable private IInventory l() { + if(this.cachedPush != null)return this.cachedPush; EnumDirection enumdirection = (EnumDirection) this.getBlock().get(BlockHopper.FACING); - return b(this.getWorld(), this.position.shift(enumdirection)); + this.cachedPush = b(this.getWorld(), this.position.shift(enumdirection)); + return this.cachedPush; } @Nullable public static IInventory b(IHopper ihopper) { - return a(ihopper.getWorld(), ihopper.x(), ihopper.z() + 1.0D, ihopper.A()); + //YAPFA start + if(ihopper instanceof TileEntityHopper) { + TileEntityHopper hopper = (TileEntityHopper) ihopper; + if(hopper.cachedAbove != null) { + return hopper.cachedAbove; + } + IInventory inv = a(ihopper.getWorld(), ihopper.x(), ihopper.z() + 1.0D, ihopper.A()); + hopper.cachedAbove = inv; + return inv; + } else { + return a(ihopper.getWorld(), ihopper.x(), ihopper.z() + 1.0D, ihopper.A()); + } + //YAPFA end } public static List c(IHopper ihopper) { @@ -658,7 +673,7 @@ public class TileEntityHopper extends TileEntityLootable implements IHopper, ITi } } } - + if (object == null && (!optimizeEntities || !org.bukkit.craftbukkit.util.CraftMagicNumbers.getMaterial(block).isOccluding())) { // Paper List list = world.getEntities((Entity) null, new AxisAlignedBB(d0 - 0.5D, d1 - 0.5D, d2 - 0.5D, d0 + 0.5D, d1 + 0.5D, d2 + 0.5D), IEntitySelector.d); @@ -666,7 +681,7 @@ public class TileEntityHopper extends TileEntityLootable implements IHopper, ITi object = (IInventory) list.get(world.random.nextInt(list.size())); } } - + return (IInventory) object; } @@ -728,4 +743,17 @@ public class TileEntityHopper extends TileEntityLootable implements IHopper, ITi protected Container createContainer(int i, PlayerInventory playerinventory) { return new ContainerHopper(i, playerinventory, this); } + + // YAPFA start + + private IInventory cachedAbove = null; + private IInventory cachedPush = null; + + public void flushCaches() { + cachedAbove = null; + cachedPush = null; + } + + // YAPFA end + } -- 2.25.1.windows.1