Skip to content

Commit

Permalink
Clean up and compartmentalize a lot of client rendering and key logic…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
2008Choco committed Feb 13, 2024
1 parent f12dd4e commit 13371d8
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package wtf.choco.veinminer.client;

import java.util.Objects;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.BlockHitResult;

import org.jetbrains.annotations.NotNull;

import wtf.choco.veinminer.client.network.FabricServerState;
import wtf.choco.veinminer.config.ClientConfig;
import wtf.choco.veinminer.network.protocol.serverbound.ServerboundRequestVeinMine;

/**
* A class handling the logic for when a player moves from tile to tile, requesting the
* server to update the vein mining for the new position (if necesary).
*/
public final class BlockLookUpdateHandler {

private final VeinMinerClient client;

BlockLookUpdateHandler(@NotNull VeinMinerClient client) {
this.client = client;
}

/**
* Update on the client the last looked position with the current looking position,
* and send a request to the server to update the wireframe if necessary.
*
* @param minecraft
*/
public void updateLastLookedPosition(@NotNull Minecraft minecraft) {
if (!client.hasServerState()) {
return;
}

if (!(minecraft.hitResult instanceof BlockHitResult hit)) {
return;
}

FabricServerState serverState = client.getServerState();
BlockPos position = hit.getBlockPos();
Direction blockFace = hit.getDirection();

this.updateWireframeIfNecessary(serverState, position, blockFace);

// Updating the new last looked at position
if (minecraft.player != null && minecraft.player.level() != null && !minecraft.player.level().isEmptyBlock(position)) {
serverState.setLastLookedAt(position, blockFace);
} else {
serverState.setLastLookedAt(null, null);
}
}

private void updateWireframeIfNecessary(@NotNull FabricServerState serverState, @NotNull BlockPos lookingAtPos, @NotNull Direction lookingAtFace) {
ClientConfig config = serverState.getConfig();
if (!serverState.isActive() || !config.isAllowActivationKeybind()) {
return;
}

if (isLookingAtDifferentPosition(serverState, lookingAtPos, lookingAtFace)) {
serverState.resetShape();
serverState.sendMessage(new ServerboundRequestVeinMine(lookingAtPos.getX(), lookingAtPos.getY(), lookingAtPos.getZ()));
}
}

private boolean isLookingAtDifferentPosition(@NotNull FabricServerState serverState, @NotNull BlockPos lookingAtPos, @NotNull Direction lookingAtFace) {
return !Objects.equals(serverState.getLastLookedAtBlockPos(), lookingAtPos) || !Objects.equals(serverState.getLastLookedAtBlockFace(), lookingAtFace);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package wtf.choco.veinminer.client;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.BlockHitResult;

import org.jetbrains.annotations.NotNull;

import wtf.choco.veinminer.client.network.FabricServerState;
import wtf.choco.veinminer.client.render.hud.PatternWheelHudComponent;
import wtf.choco.veinminer.config.ClientConfig;
import wtf.choco.veinminer.network.protocol.serverbound.ServerboundRequestVeinMine;
import wtf.choco.veinminer.network.protocol.serverbound.ServerboundToggleVeinMiner;

/**
* A class handling key press logic.
*/
public final class KeyHandler {

private boolean changingPatterns = false;

private final VeinMinerClient client;

KeyHandler(@NotNull VeinMinerClient client) {
this.client = client;
}

/**
* Tick the key logic.
*/
public void tick() {
if (!client.hasServerState()) {
return;
}

FabricServerState serverState = client.getServerState();
if (!serverState.isEnabledOnServer()) {
return;
}

ClientConfig config = serverState.getConfig();

this.handleActivationKeybind(serverState, config);
this.handlePatternSwitchKeybinds(serverState, config);
}

private void handleActivationKeybind(@NotNull FabricServerState serverState, @NotNull ClientConfig config) {
if (!config.isAllowActivationKeybind()) {
return;
}

boolean lastActive = serverState.isActive();
boolean active = VeinMinerClient.KEY_MAPPING_ACTIVATE_VEINMINER.isDown();
if (lastActive == active) {
return;
}

serverState.setActive(active);
serverState.sendMessage(new ServerboundToggleVeinMiner(active));

// If the player is activating vein miner and looking at a block, we also need to update the voxel shape
if (active) {
Minecraft minecraft = Minecraft.getInstance();
if (!(minecraft.hitResult instanceof BlockHitResult hit)) {
return;
}

BlockPos position = hit.getBlockPos();
serverState.resetShape();
serverState.sendMessage(new ServerboundRequestVeinMine(position.getX(), position.getY(), position.getZ()));
}
}

private void handlePatternSwitchKeybinds(@NotNull FabricServerState serverState, @NotNull ClientConfig config) {
if (!config.isAllowPatternSwitchingKeybind()) {
return;
}

boolean lastChangingPatterns = changingPatterns;
this.changingPatterns = (VeinMinerClient.KEY_MAPPING_NEXT_PATTERN.isDown() || VeinMinerClient.KEY_MAPPING_PREVIOUS_PATTERN.isDown());

if (lastChangingPatterns ^ changingPatterns) {
boolean next;

// There has to be a smarter way to write this...
if (VeinMinerClient.KEY_MAPPING_NEXT_PATTERN.isDown()) {
next = true;
} else if (VeinMinerClient.KEY_MAPPING_PREVIOUS_PATTERN.isDown()) {
next = false;
} else {
return;
}

PatternWheelHudComponent patternWheel = client.getPatternWheelRenderComponent();

// If the HUD wheel isn't rendered yet, push a render call but don't change the pattern
if (patternWheel.shouldRender(config, serverState)) {
serverState.changePattern(next);
}

patternWheel.pushRender();
}
}

}
Loading

0 comments on commit 13371d8

Please sign in to comment.