Skip to content

Commit

Permalink
fix client code getting called by both sides
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew6rant committed Aug 18, 2023
1 parent f8cd4ae commit edd1b27
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.9
loader_version=0.14.21

# Mod Properties
mod_version=1.1.0
mod_version=1.1.1
maven_group=io.github.andrew6rant.autoslabs
archives_base_name=autoslabs

Expand Down
101 changes: 49 additions & 52 deletions src/main/java/io/github/andrew6rant/autoslabs/PlacementUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.minecraft.block.PaneBlock;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.enums.SlabType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
Expand Down Expand Up @@ -220,47 +220,46 @@ public static SlabType calcKleeSlab(BlockState breakState, BlockHitResult cast)
public static boolean canReplace(BlockState state, ItemPlacementContext context) {
ItemStack itemStack = context.getStack();
SlabType slabType = state.get(TYPE);
PlayerEntity entity = context.getPlayer();
if (entity == null) return false;
if (slabType != SlabType.DOUBLE && itemStack.isOf(state.getBlock().asItem())) {
if (context.canReplaceExisting()) {
HitResult hitResult = MinecraftClient.getInstance().crosshairTarget;
if (hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult result = (BlockHitResult) hitResult;
HitPart part = getHitPart(result);
boolean topHalfX = context.getHitPos().x - (double) context.getBlockPos().getX() > 0.5;
boolean topHalfY = context.getHitPos().y - (double) context.getBlockPos().getY() > 0.5;
boolean topHalfZ = context.getHitPos().z - (double) context.getBlockPos().getZ() > 0.5;
Direction direction = context.getSide();
VerticalType verticalType = state.get(VERTICAL_TYPE);
if (verticalType != null) {
if (verticalType == VerticalType.FALSE) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.UP || topHalfY && direction.getAxis().isHorizontal()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.DOWN || !topHalfY && direction.getAxis().isHorizontal()) {
return part == HitPart.CENTER;
}
BlockHitResult blockHitResult = PlacementUtil.calcRaycast(entity);
HitPart part = getHitPart(blockHitResult);
boolean topHalfX = context.getHitPos().x - (double) context.getBlockPos().getX() > 0.5;
boolean topHalfY = context.getHitPos().y - (double) context.getBlockPos().getY() > 0.5;
boolean topHalfZ = context.getHitPos().z - (double) context.getBlockPos().getZ() > 0.5;
Direction direction = context.getSide();
VerticalType verticalType = state.get(VERTICAL_TYPE);
if (verticalType != null) {
if (verticalType == VerticalType.FALSE) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.UP || topHalfY && direction.getAxis().isHorizontal()) {
return part == HitPart.CENTER;
}
} else if (verticalType == NORTH_SOUTH) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.NORTH || !topHalfZ && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.SOUTH || topHalfZ && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.DOWN || !topHalfY && direction.getAxis().isHorizontal()) {
return part == HitPart.CENTER;
}
} else if (verticalType == VerticalType.EAST_WEST) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.EAST || topHalfX && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.WEST || !topHalfX && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
}
} else if (verticalType == NORTH_SOUTH) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.NORTH || !topHalfZ && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.SOUTH || topHalfZ && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
}
} else if (verticalType == VerticalType.EAST_WEST) {
if (slabType == SlabType.BOTTOM) {
if (direction == Direction.EAST || topHalfX && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
} else {
if (direction == Direction.WEST || !topHalfX && direction.getAxis().isVertical()) {
return part == HitPart.CENTER;
}
}
}
Expand All @@ -279,20 +278,18 @@ public static BlockState calcPlacementState(ItemPlacementContext ctx, BlockState
BlockState blockState = ctx.getWorld().getBlockState(blockPos);
Direction ctxSide = ctx.getSide();
FluidState fluidState = ctx.getWorld().getFluidState(blockPos);
HitResult hitResult = MinecraftClient.getInstance().crosshairTarget;
if (hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult result = (BlockHitResult) hitResult;
HitPart part = getHitPart(result);
return switch (ctxSide) {
case UP -> calcUpPlacement(blockState, state, part, fluidState);
case DOWN -> calcDownPlacement(blockState, state, part, fluidState);
case NORTH -> calcNorthPlacement(blockState, state, part, fluidState);
case SOUTH -> calcSouthPlacement(blockState, state, part, fluidState);
case EAST -> calcEastPlacement(blockState, state, part, fluidState);
case WEST -> calcWestPlacement(blockState, state, part, fluidState);
};
}
return null;
PlayerEntity entity = ctx.getPlayer();
if (entity == null) return null;
BlockHitResult blockHitResult = PlacementUtil.calcRaycast(entity);
HitPart part = getHitPart(blockHitResult);
return switch (ctxSide) {
case UP -> calcUpPlacement(blockState, state, part, fluidState);
case DOWN -> calcDownPlacement(blockState, state, part, fluidState);
case NORTH -> calcNorthPlacement(blockState, state, part, fluidState);
case SOUTH -> calcSouthPlacement(blockState, state, part, fluidState);
case EAST -> calcEastPlacement(blockState, state, part, fluidState);
case WEST -> calcWestPlacement(blockState, state, part, fluidState);
};
}

public static BlockState calcUpPlacement(BlockState blockState, BlockState state, HitPart part, FluidState fluidState) {
Expand Down

0 comments on commit edd1b27

Please sign in to comment.