From 96b507376b1eb06a2a6d1695817ce140fcd5a573 Mon Sep 17 00:00:00 2001 From: 2008Choco Date: Tue, 13 Feb 2024 18:01:06 -0500 Subject: [PATCH] Move 'last looked at' data to BlockLookUpdateHandler, rename some methods --- .../client/BlockLookUpdateHandler.java | 52 ++++++++++---- .../veinminer/client/VeinMinerClient.java | 21 ++++-- .../client/network/FabricServerState.java | 69 +++---------------- .../client/render/WireframeShapeRenderer.java | 2 +- 4 files changed, 67 insertions(+), 77 deletions(-) diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/BlockLookUpdateHandler.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/BlockLookUpdateHandler.java index 1413a36..5f449da 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/BlockLookUpdateHandler.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/BlockLookUpdateHandler.java @@ -9,6 +9,7 @@ import net.minecraft.world.phys.BlockHitResult; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import wtf.choco.veinminer.client.network.FabricServerState; import wtf.choco.veinminer.config.ClientConfig; @@ -20,6 +21,10 @@ */ public final class BlockLookUpdateHandler { + private BlockPos lastLookedAtBlockPos; + private Direction lastLookedAtBlockFace; + private BlockState lastLookedAtBlockState; + private final VeinMinerClient client; BlockLookUpdateHandler(@NotNull VeinMinerClient client) { @@ -32,7 +37,7 @@ public final class BlockLookUpdateHandler { * * @param minecraft the minecraft instance */ - public void updateLastLookedPosition(@NotNull Minecraft minecraft) { + public void tick(@NotNull Minecraft minecraft) { if (!client.hasServerState()) { return; } @@ -42,36 +47,57 @@ public void updateLastLookedPosition(@NotNull Minecraft minecraft) { } FabricServerState serverState = client.getServerState(); - BlockPos position = hit.getBlockPos(); - Direction blockFace = hit.getDirection(); - BlockState blockState = minecraft.level.getBlockState(position); + BlockPos lookingAtPos = hit.getBlockPos(); + Direction lookingAtFace = hit.getDirection(); + BlockState lookingAtState = minecraft.level.getBlockState(lookingAtPos); - this.updateWireframeIfNecessary(serverState, position, blockFace, blockState); + this.updateWireframeIfNecessary(serverState, lookingAtPos, lookingAtFace, lookingAtState); // Updating the new last looked at position - if (minecraft.player != null && minecraft.player.level() != null && !minecraft.player.level().isEmptyBlock(position)) { - serverState.setLastLookedAt(position, blockFace, blockState); + if (minecraft.player != null && minecraft.player.level() != null && !minecraft.player.level().isEmptyBlock(lookingAtPos)) { + this.lastLookedAtBlockPos = lookingAtPos; + this.lastLookedAtBlockFace = lookingAtFace; + this.lastLookedAtBlockState = lookingAtState; } else { - serverState.setLastLookedAt(null, null, null); + this.reset(); } } + /** + * Reset the last looked at state data. + */ + public void reset() { + this.lastLookedAtBlockPos = null; + this.lastLookedAtBlockFace = null; + this.lastLookedAtBlockState = null; + } + + /** + * Get the last looked at {@link BlockPos}. + * + * @return the last looked at block pos + */ + @Nullable + public BlockPos getLastLookedAtBlockPos() { + return lastLookedAtBlockPos; + } + private void updateWireframeIfNecessary(@NotNull FabricServerState serverState, @NotNull BlockPos lookingAtPos, @NotNull Direction lookingAtFace, @NotNull BlockState lookingAtState) { ClientConfig config = serverState.getConfig(); if (!serverState.isActive() || !config.isAllowActivationKeybind()) { return; } - if (isLookingAtDifferentPositionOrState(serverState, lookingAtPos, lookingAtFace, lookingAtState)) { + if (isLookingAtDifferentPositionOrState(lookingAtPos, lookingAtFace, lookingAtState)) { serverState.resetShape(); serverState.sendMessage(new ServerboundRequestVeinMine(lookingAtPos.getX(), lookingAtPos.getY(), lookingAtPos.getZ())); } } - private boolean isLookingAtDifferentPositionOrState(@NotNull FabricServerState serverState, @NotNull BlockPos lookingAtPos, @NotNull Direction lookingAtFace, @NotNull BlockState lookingAtState) { - return !Objects.equals(serverState.getLastLookedAtBlockPos(), lookingAtPos) - || !Objects.equals(serverState.getLastLookedAtBlockFace(), lookingAtFace) - || !Objects.equals(serverState.getLastLookedAtBlockState(), lookingAtState); + private boolean isLookingAtDifferentPositionOrState(@NotNull BlockPos lookingAtPos, @NotNull Direction lookingAtFace, @NotNull BlockState lookingAtState) { + return !Objects.equals(lastLookedAtBlockPos, lookingAtPos) + || !Objects.equals(lastLookedAtBlockFace, lookingAtFace) + || !Objects.equals(lastLookedAtBlockState, lookingAtState); } } diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java index 9870544..dec3c7a 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java @@ -47,7 +47,7 @@ public final class VeinMinerClient implements ClientModInitializer { private FabricServerState serverState; private final KeyHandler keyHandler = new KeyHandler(this); - private final BlockLookUpdateHandler wireframeUpdateHandler = new BlockLookUpdateHandler(this); + private final BlockLookUpdateHandler blockLookUpdateHandler = new BlockLookUpdateHandler(this); private final PatternWheelHudComponent patternWheelRenderComponent = new PatternWheelHudComponent(); private final HudComponentRenderer hudComponentRenderer = new HudComponentRenderer(this, @@ -63,11 +63,14 @@ public void onInitializeClient() { ClientTickEvents.END_CLIENT_TICK.register(client -> { this.keyHandler.tick(); - this.wireframeUpdateHandler.updateLastLookedPosition(client); + this.blockLookUpdateHandler.tick(client); }); - ClientPlayConnectionEvents.INIT.register((handler, client) -> serverState = new FabricServerState(client)); - ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> serverState = null); + ClientPlayConnectionEvents.INIT.register((handler, client) -> serverState = new FabricServerState(this, client)); + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> { + this.serverState = null; + this.blockLookUpdateHandler.reset(); + }); // Once joined, we're going to send a handshake packet to let the server know we have the client mod installed ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> serverState.sendMessage(new ServerboundHandshake(VeinMiner.PROTOCOL.getVersion()))); @@ -103,6 +106,16 @@ public FabricServerState getServerState() { return serverState; } + /** + * Get the {@link BlockLookUpdateHandler} instance. + * + * @return the block look update handler + */ + @NotNull + public BlockLookUpdateHandler getBlockLookUpdateHandler() { + return blockLookUpdateHandler; + } + /** * Get the HudComponent for the pattern wheel. * diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/network/FabricServerState.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/network/FabricServerState.java index b3b9b21..ca7d4be 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/network/FabricServerState.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/network/FabricServerState.java @@ -9,10 +9,8 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; @@ -23,6 +21,7 @@ import wtf.choco.network.data.NamespacedKey; import wtf.choco.network.receiver.MessageReceiver; import wtf.choco.veinminer.VeinMiner; +import wtf.choco.veinminer.client.VeinMinerClient; import wtf.choco.veinminer.config.ClientConfig; import wtf.choco.veinminer.network.protocol.VeinMinerClientboundMessageListener; import wtf.choco.veinminer.network.protocol.VeinMinerServerboundMessageListener; @@ -51,19 +50,21 @@ public final class FabricServerState implements MessageReceiver, VeinMinerClient private int selectedPatternIndex = 0; private List patternKeys = null; - private BlockPos lastLookedAtBlockPos; - private Direction lastLookedAtBlockFace; - private BlockState lastLookedAtBlockState; private VoxelShape veinMineResultShape; + private final VeinMinerClient client; + /** * Construct a new {@link FabricServerState}. * - * @param client the {@link Minecraft} instance + * @param client the client instance + * @param minecraft the {@link Minecraft} instance */ - public FabricServerState(@NotNull Minecraft client) { + public FabricServerState(@NotNull VeinMinerClient client, @NotNull Minecraft minecraft) { + this.client = client; + // We'll enable VeinMiner if we're in single player development mode, just for testing - if (client.hasSingleplayerServer() && FabricLoader.getInstance().isDevelopmentEnvironment()) { + if (minecraft.hasSingleplayerServer() && FabricLoader.getInstance().isDevelopmentEnvironment()) { this.config = new ClientConfig(); this.enabledOnServer = true; this.patternKeys = List.of( @@ -200,56 +201,6 @@ public boolean hasPatternKeys() { return patternKeys != null && !patternKeys.isEmpty(); } - /** - * Set the last {@link BlockPos} and {@link Direction} the client has looked at. - * - * @param position the last looked at position - * @param blockFace the last looked at block face - * @param state the last looked at block state - */ - public void setLastLookedAt(@Nullable BlockPos position, @Nullable Direction blockFace, @Nullable BlockState state) { - this.lastLookedAtBlockPos = position; - this.lastLookedAtBlockFace = blockFace; - this.lastLookedAtBlockState = state; - - if (position == null || blockFace == null || state == null) { - this.resetShape(); - } - } - - /** - * Get the {@link BlockPos} last looked at by the client, or null if the client has not - * looked at a block. - * - * @return the last looked at block position, or null if none - */ - @Nullable - public BlockPos getLastLookedAtBlockPos() { - return lastLookedAtBlockPos; - } - - /** - * Get the block face {@link Direction} last looked at by the client, or null if the client - * has not looked at a block. - * - * @return the last looked at block face, or null if none - */ - @Nullable - public Direction getLastLookedAtBlockFace() { - return lastLookedAtBlockFace; - } - - /** - * Get the {@link BlockState} last looked at by the client, or null if the client has not - * looked at a block. - * - * @return the last looked at block state, or null if none - */ - @Nullable - public BlockState getLastLookedAtBlockState() { - return lastLookedAtBlockState; - } - /** * Get the {@link VoxelShape} outlining the last vein mine result received from the server. * @@ -312,7 +263,7 @@ public void handleSetConfig(@NotNull ClientboundSetConfig message) { public void handleVeinMineResults(@NotNull ClientboundVeinMineResults message) { this.resetShape(); - BlockPos origin = lastLookedAtBlockPos; + BlockPos origin = client.getBlockLookUpdateHandler().getLastLookedAtBlockPos(); if (origin == null) { return; } diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/WireframeShapeRenderer.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/WireframeShapeRenderer.java index e2ad212..1e8611d 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/WireframeShapeRenderer.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/WireframeShapeRenderer.java @@ -47,7 +47,7 @@ public void render(@NotNull WorldRenderContext context) { return; } - BlockPos origin = serverState.getLastLookedAtBlockPos(); + BlockPos origin = client.getBlockLookUpdateHandler().getLastLookedAtBlockPos(); VoxelShape shape = serverState.getVeinMineResultShape(); if (origin == null || shape == null) { return;