Skip to content

Commit

Permalink
Move 'last looked at' data to BlockLookUpdateHandler, rename some met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
2008Choco committed Feb 13, 2024
1 parent 90642e6 commit 96b5073
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())));
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -51,19 +50,21 @@ public final class FabricServerState implements MessageReceiver, VeinMinerClient
private int selectedPatternIndex = 0;
private List<NamespacedKey> 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(
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 96b5073

Please sign in to comment.