Skip to content

Commit

Permalink
Optimized mana enchanter multiblock checking (#4465)
Browse files Browse the repository at this point in the history
As per #4388, the enchanter no longer assumes its rotation could change
from one tick to another and performs its structure validation at a much
reduced rate while it is idle.
  • Loading branch information
TheRealWormbo authored Oct 24, 2023
1 parent 402eaa2 commit d9e949b
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[][] {
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -256,26 +259,31 @@ 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;
}
}
}

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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d9e949b

Please sign in to comment.