-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up and compartmentalize a lot of client rendering and key logic…
… code
- Loading branch information
Showing
9 changed files
with
396 additions
and
202 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
veinminer-fabric/src/main/java/wtf/choco/veinminer/client/BlockLookUpdateHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
veinminer-fabric/src/main/java/wtf/choco/veinminer/client/KeyHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.