Skip to content

Commit

Permalink
Fix Simon Says Solver (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzureAaron authored Dec 24, 2024
1 parent 6cf640e commit fbbd3ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/de/hysky/skyblocker/mixins/ClientWorldMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,33 @@
import net.minecraft.block.Blocks;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;

@Mixin(ClientWorld.class)
public class ClientWorldMixin {
public abstract class ClientWorldMixin implements BlockView {

@Inject(method = "handleBlockUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z"))
private void skyblocker$beforeBlockUpdate(CallbackInfo ci, @Local(argsOnly = true) BlockPos pos, @Share("old") LocalRef<@Nullable BlockState> oldState) {
oldState.set(getBlockState(pos));
}

/**
* @implNote The {@code pos} can be mutable when this is called by chunk delta updates, so if you want to copy it into memory
* (e.g. store it in a field/list/map) make sure to duplicate it via {@link BlockPos#toImmutable()}.
*/
//TODO might be worth creating an event for this
@Inject(method = "handleBlockUpdate", at = @At("RETURN"))
private void skyblocker$handleBlockUpdate(CallbackInfo ci, @Local(argsOnly = true) BlockPos pos, @Local(argsOnly = true) BlockState state) {
private void skyblocker$afterBlockUpdate(CallbackInfo ci, @Local(argsOnly = true) BlockPos pos, @Local(argsOnly = true) BlockState state, @Share("old") LocalRef<@Nullable BlockState> oldState) {
if (Utils.isInCrimson()) {
DojoManager.onBlockUpdate(pos.toImmutable(), state);
} else if (Utils.isInCrystalHollows()) {
Expand All @@ -37,6 +47,6 @@ public class ClientWorldMixin {
if (state.isOf(Blocks.BEACON)) BeaconHighlighter.beaconPositions.add(pos.toImmutable());
}

SimonSays.onBlockUpdate(pos, state);
SimonSays.onBlockUpdate(pos, state, oldState.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import java.util.Objects;

import org.jetbrains.annotations.Nullable;

public class SimonSays {
private static final Box BOARD_AREA = Box.enclosing(new BlockPos(111, 123, 92), new BlockPos(111, 120, 95));
private static final Box BUTTONS_AREA = Box.enclosing(new BlockPos(110, 123, 92), new BlockPos(110, 120, 95));
Expand Down Expand Up @@ -71,14 +73,14 @@ private static ActionResult onBlockInteract(PlayerEntity player, World world, Ha
//If the player goes out of the range required to receive block/chunk updates then their solver won't detect stuff but that
//doesn't matter because if they're doing pre-4 or something they won't be doing the ss, and if they end up needing to they can
//just reset it or have the other person finish the current sequence first then let them do it.
public static void onBlockUpdate(BlockPos pos, BlockState state) {
public static void onBlockUpdate(BlockPos pos, BlockState newState, @Nullable BlockState oldState) {
if (shouldProcess()) {
Vec3d posVec = Vec3d.of(pos);
Block block = state.getBlock();
Block newBlock = newState.getBlock();

if (BOARD_AREA.contains(posVec) && block.equals(Blocks.SEA_LANTERN)) {
if (BOARD_AREA.contains(posVec) && newBlock.equals(Blocks.OBSIDIAN) && oldState != null && oldState.getBlock().equals(Blocks.SEA_LANTERN)) {
SIMON_PATTERN.add(pos.toImmutable()); //Convert to immutable because chunk delta updates use the mutable variant
} else if (BUTTONS_AREA.contains(posVec) && block.equals(Blocks.AIR)) {
} else if (BUTTONS_AREA.contains(posVec) && newBlock.equals(Blocks.AIR)) {
//Upon reaching the showing of the next sequence we need to reset the state so that we don't show old data
//Otherwise, the nextIndex will go beyond 5 and that can cause bugs, it also helps with the other case noted above
reset();
Expand Down

0 comments on commit fbbd3ae

Please sign in to comment.