From 279ad82cdc3197f99484e45a2325962c148fe6e5 Mon Sep 17 00:00:00 2001 From: TheRealWormbo Date: Fri, 20 Oct 2023 11:19:33 +0200 Subject: [PATCH] Optimized mana enchanter multiblock checking --- .../ManaEnchanterBlockEntity.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Xplat/src/main/java/vazkii/botania/common/block/block_entity/ManaEnchanterBlockEntity.java b/Xplat/src/main/java/vazkii/botania/common/block/block_entity/ManaEnchanterBlockEntity.java index 70d2de1e8a..4d79153c73 100644 --- a/Xplat/src/main/java/vazkii/botania/common/block/block_entity/ManaEnchanterBlockEntity.java +++ b/Xplat/src/main/java/vazkii/botania/common/block/block_entity/ManaEnchanterBlockEntity.java @@ -77,6 +77,7 @@ public class ManaEnchanterBlockEntity extends BotaniaBlockEntity implements Mana private static final String TAG_ITEM = "item"; private static final String TAG_ENCHANTS = "enchantsToApply"; private static final int CRAFT_EFFECT_EVENT = 0; + private static final int IDLE_CHECK_INTERVAL_TICKS = 10; private static final String[][] PATTERN = new String[][] { { @@ -136,6 +137,8 @@ public class ManaEnchanterBlockEntity extends BotaniaBlockEntity implements Mana public int stage3EndTicks = 0; + private int idleTicks = 0; + private int manaRequired = -1; private int mana = 0; @@ -256,8 +259,9 @@ public static void commonTick(Level level, BlockPos worldPosition, BlockState st for (BlockPos offset : PYLON_LOCATIONS.get(axis)) { BlockEntity tile = level.getBlockEntity(worldPosition.offset(offset)); if (tile instanceof PylonBlockEntity pylon) { - pylon.activated = self.stage == State.GATHER_MANA; - if (self.stage == State.GATHER_MANA) { + boolean gatheringMana = self.stage == State.GATHER_MANA; + pylon.activated = gatheringMana; + if (gatheringMana) { pylon.centerPos = worldPosition; } } @@ -265,17 +269,21 @@ public static void commonTick(Level level, BlockPos worldPosition, BlockState st if (self.stage != State.IDLE) { self.stageTicks++; + } else { + self.idleTicks++; } - if (level.isClientSide) { + if (level.isClientSide || self.stage == State.IDLE && self.idleTicks % IDLE_CHECK_INTERVAL_TICKS != 0) { return; } - if (FORMED_MULTIBLOCK.get().validate(level, worldPosition.below()) == null) { + Rotation rot = getAxisRotation(axis); + if (!FORMED_MULTIBLOCK.get().validate(level, worldPosition.below(), rot)) { level.setBlockAndUpdate(worldPosition, Blocks.LAPIS_BLOCK.defaultBlockState()); XplatAbstractions.INSTANCE.sendToNear(level, worldPosition, new BotaniaEffectPacket(EffectType.ENCHANTER_DESTROY, worldPosition.getX() + 0.5, worldPosition.getY() + 0.5, worldPosition.getZ() + 0.5)); level.playSound(null, worldPosition, BotaniaSounds.enchanterFade, SoundSource.BLOCKS, 1F, 1F); + return; } switch (self.stage) { @@ -465,6 +473,14 @@ public static Direction.Axis canEnchanterExist(Level world, BlockPos pos) { }; } + private static Rotation getAxisRotation(Direction.Axis axis) { + return switch (axis) { + case X -> Rotation.NONE; + case Z -> Rotation.CLOCKWISE_90; + default -> throw new IllegalStateException("Enchanter should only ever be facing in X or Z direction"); + }; + } + @Override public boolean canAttachSpark(ItemStack stack) { return true;