From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:59:17 -0400 Subject: [PATCH] Reduce object complexity to make block isValid calls more efficient diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java index 4eab34f932ae564aa038cecf4426b9f74a513ee7..3b553138f7fb5f19aa303ec18317b8258f10ce7e 100644 --- a/src/main/java/net/minecraft/world/level/block/Block.java +++ b/src/main/java/net/minecraft/world/level/block/Block.java @@ -109,6 +109,7 @@ public class Block extends BlockBehaviour implements ItemLike { // Paper end @Nullable private String descriptionId; + private int descriptionIdHash; // Leaf - Reduce object complexity to make block isValid calls more efficient @Nullable private Item item; private static final int CACHE_SIZE = 2048; @@ -481,6 +482,16 @@ public class Block extends BlockBehaviour implements ItemLike { return this.descriptionId; } + // Leaf start - Reduce object complexity to make block isValid calls more efficient + public int getDescriptionIdHash() { + if (this.descriptionIdHash == 0) { + this.descriptionIdHash = getDescriptionId().hashCode(); + } + + return this.descriptionIdHash; + } + // Leaf end - Reduce object complexity to make block isValid calls more efficient + public void fallOn(Level world, BlockState state, BlockPos pos, Entity entity, float fallDistance) { entity.causeFallDamage(fallDistance * fallDistanceMultiplier, fallDamageMultiplier, entity.damageSources().fall()); // Purpur } diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntityType.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntityType.java index 96b99aab3720e5bdf293fd4a95944c7218ce43c0..0577b192853b591db49c9c77ae536492e0fb4888 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntityType.java +++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntityType.java @@ -269,6 +269,7 @@ public class BlockEntityType { public static final BlockEntityType VAULT = register("vault", BlockEntityType.Builder.of(VaultBlockEntity::new, Blocks.VAULT)); private final BlockEntityType.BlockEntitySupplier factory; public final Set validBlocks; + public final it.unimi.dsi.fastutil.ints.IntOpenHashSet validBlocksByIdHash = new it.unimi.dsi.fastutil.ints.IntOpenHashSet(0); // Leaf - Reduce object complexity to make block isValid calls more efficient private final Type dataType; private final Holder.Reference> builtInRegistryHolder = BuiltInRegistries.BLOCK_ENTITY_TYPE.createIntrusiveHolder(this); @@ -290,6 +291,13 @@ public class BlockEntityType { this.factory = factory; this.validBlocks = blocks; this.dataType = type; + + // Leaf start - Reduce object complexity to make block isValid calls more efficient + for (Block block : blocks) { + int idHash = block.getDescriptionIdHash(); + this.validBlocksByIdHash.add(idHash); + } + // Leaf end - Reduce object complexity to make block isValid calls more efficient } @Nullable @@ -301,6 +309,13 @@ public class BlockEntityType { return this.validBlocks.contains(state.getBlock()); } + // Leaf start - Reduce object complexity to make block isValid calls more efficient + public boolean isValidByIdHash(BlockState state) { + int idHash = state.getBlock().getDescriptionIdHash(); + return this.validBlocksByIdHash.contains(idHash); + } + // Leaf end - Reduce object complexity to make block isValid calls more efficient + @Nullable public Holder.Reference> builtInRegistryHolder() { return this.builtInRegistryHolder; diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java index 308fde6ec1df5dce8e6c323f0cb07a99451a086e..ae693e08434d251ee57b89d606d3cbd51288d496 100644 --- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java +++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java @@ -1056,7 +1056,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p try { BlockState iblockdata = LevelChunk.this.getBlockState(blockposition); - if (this.blockEntity.getType().isValid(iblockdata)) { + if (this.blockEntity.getType().isValidByIdHash(iblockdata)) { // Leaf - Reduce object complexity to make block isValid calls more efficient this.ticker.tick(LevelChunk.this.level, this.blockEntity.getBlockPos(), iblockdata, this.blockEntity); this.loggedInvalidBlockState = false; // Paper start - Remove the Block Entity if it's invalid