Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from Lelebees/horizontal-slabs
Browse files Browse the repository at this point in the history
Horizontal slabs
  • Loading branch information
Lelebees authored Mar 15, 2024
2 parents 38b6003 + f850e61 commit 3a7b54a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Mod Properties
mod_version=0.2.0
mod_version=0.2.1
mod_name=LelesBetterSlabs
modid=betterslabs
maven_group=nl.lelebees.betterslabs
2 changes: 1 addition & 1 deletion src/main/java/nl/lelebees/betterslabs/BetterSlabsMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class BetterSlabsMod implements ModInitializer {

@Override
public void onInitialize() {
LOGGER.info("Running Lele's BetterSlabs v0.2.0!");
LOGGER.info("Running Lele's BetterSlabs v0.2.1!");
}
}
18 changes: 18 additions & 0 deletions src/main/java/nl/lelebees/betterslabs/extras/LeleUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nl.lelebees.betterslabs.extras;

import finalforeach.cosmicreach.world.blocks.BlockState;

public class LeleUtil {
public static BlockState fetchNewState(BlockState blockState, String newOrientation) {
String[] blockStateId = blockState.stringId.split("=");
StringBuilder stateIdBuilder = new StringBuilder();
for (String string : blockStateId) {
if (string.equals(blockStateId[blockStateId.length - 1])) {
stateIdBuilder.append(newOrientation);
continue;
}
stateIdBuilder.append(string).append("=");
}
return BlockState.getInstance(blockState.getBlockId() + "[" + stateIdBuilder + "]");
}
}
10 changes: 6 additions & 4 deletions src/main/java/nl/lelebees/betterslabs/extras/ViewDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import static finalforeach.cosmicreach.world.Direction.*;

public enum ViewDirection {
NORTH("verticalNegX", NEG_X),
SOUTH("verticalPosX", POS_X),
EAST("verticalPosZ", POS_Z),
WEST("verticalNegZ", NEG_Z);
NORTH("NegX", NEG_X),
SOUTH("PosX", POS_X),
EAST("PosZ", POS_Z),
WEST("NegZ", NEG_Z),
UP("PosY", POS_Y),
DOWN("NegY", NEG_Y);

private final String orientation;
private final Direction direction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nl.lelebees.betterslabs.mixin;


import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import finalforeach.cosmicreach.gamestates.InGame;
Expand All @@ -10,33 +12,51 @@
import finalforeach.cosmicreach.world.blocks.BlockState;
import nl.lelebees.betterslabs.extras.ViewDirection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static nl.lelebees.betterslabs.extras.LeleUtil.fetchNewState;

@Mixin(BlockSelection.class)
public class BlockPlacementMixin {

@Inject(method = "placeBlock", at = @At("HEAD"))
private void injected(World world, BlockState targetBlockState, BlockPosition blockPos, double timeSinceLastInteract, CallbackInfo ci, @Local LocalRef<BlockState> blockStateLocalRef) {
private void verticalSlabs(World world, BlockState targetBlockState, BlockPosition blockPos, double timeSinceLastInteract, CallbackInfo ci, @Local(argsOnly = true) LocalRef<BlockState> blockStateLocalRef) {
BlockState blockState = blockStateLocalRef.get();
if (!blockState.stringId.contains("type=vertical")) {
return;
}
String newOrientation = ViewDirection.getViewDirection(InGame.getLocalPlayer().getEntity()).getOrientation();

String[] blockStateId = blockState.stringId.split("=");
StringBuilder stateIdBuilder = new StringBuilder();
for (String string : blockStateId) {
if (string.equals(blockStateId[blockStateId.length - 1])) {
stateIdBuilder.append(newOrientation);
continue;
}
stateIdBuilder.append(string).append("=");
String newOrientation = "vertical" + ViewDirection.getViewDirection(InGame.getLocalPlayer().getEntity()).getOrientation();
blockStateLocalRef.set(fetchNewState(blockState, newOrientation));
}

@WrapOperation(method = "raycast", at = @At(value = "INVOKE", target = "Lfinalforeach/cosmicreach/world/BlockSelection;placeBlock(Lfinalforeach/cosmicreach/world/World;Lfinalforeach/cosmicreach/world/blocks/BlockState;Lfinalforeach/cosmicreach/world/BlockPosition;D)V"))
private void horizontalSlabs(BlockSelection instance, World world, BlockState targetBlockState, BlockPosition targetBlockPos, double timeSinceBlockModify, Operation<Void> original, @Local(name = "breakingBlockPos") BlockPosition selectedBlockPosition) {
// TODO: If the face is side, place the slab corresponding to the half of the face the player is looking at (top half > top slab, bottom half > bottom slab)
if (!targetBlockState.stringId.contains("type=bottom") && !targetBlockState.stringId.contains("type=top")) {
original.call(instance, world, targetBlockState, targetBlockPos, timeSinceBlockModify);
return;
}
int deltaY = targetBlockPos.getGlobalY() - selectedBlockPosition.getGlobalY();

String saveKey = blockState.getBlockId() + "[" + stateIdBuilder + "]";
blockState = BlockState.getInstance(saveKey);
blockStateLocalRef.set(blockState);
String[] blockStateId = targetBlockState.stringId.split("=");
String orientation = blockStateId[blockStateId.length - 1];
orientation = determineOrientation(deltaY, orientation);
original.call(instance, world, fetchNewState(targetBlockState, orientation), targetBlockPos, timeSinceBlockModify);
}

@Unique
private String determineOrientation(int deltaY, String originalOrientation) {
if (deltaY == 0) {
// TODO: determine if it should be a bottom or top slab here
System.out.println("side!");
return originalOrientation;
}
if (deltaY < 0) {
return "top";
}
return "bottom";
}
}
20 changes: 5 additions & 15 deletions src/main/java/nl/lelebees/betterslabs/mixin/HotbarMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,18 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static nl.lelebees.betterslabs.extras.LeleUtil.fetchNewState;

@Mixin(Hotbar.class)
public class HotbarMixin {

@Inject(method = "pickBlock", at = @At("HEAD"))
private void blockSlabPicks(BlockState blockState, CallbackInfo ci, @Local LocalRef<BlockState> blockStateLocalRef) {
private void blockSlabPicks(BlockState blockState, CallbackInfo ci, @Local(argsOnly = true) LocalRef<BlockState> blockStateLocalRef) {
BlockState targetBlock = blockStateLocalRef.get();
if (!targetBlock.stringId.contains("type=vertical")) {
return;
}
String orientation = ViewDirection.WEST.getOrientation();
// TODO: YAY! reusable code!
String[] blockStateId = targetBlock.stringId.split("=");
StringBuilder stateIdBuilder = new StringBuilder();
for (String string : blockStateId) {
if (string.equals(blockStateId[blockStateId.length - 1])) {
stateIdBuilder.append(orientation);
continue;
}
stateIdBuilder.append(string).append("=");
}
String saveKey = targetBlock.getBlockId() + "[" + stateIdBuilder + "]";
targetBlock = BlockState.getInstance(saveKey);
blockStateLocalRef.set(targetBlock);
String orientation = "vertical" + ViewDirection.WEST.getOrientation();
blockStateLocalRef.set(fetchNewState(targetBlock, orientation));
}
}

0 comments on commit 3a7b54a

Please sign in to comment.