Skip to content

Commit

Permalink
Slabs now placed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed Oct 6, 2020
1 parent 244dd61 commit cbbea9c
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fi.dy.masa.litematica.interfaces.IClientPlayerInteractionManager;
import fi.dy.masa.litematica.world.SchematicWorldHandler;
import fi.dy.masa.litematica.world.WorldSchematic;
import fi.dy.masa.malilib.util.BlockUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Material;
Expand All @@ -15,6 +16,9 @@
import net.minecraft.entity.MovementType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Property;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
Expand All @@ -27,6 +31,8 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Mixin(ClientPlayerEntity.class)
public class MixinClientPlayerEntity extends AbstractClientPlayerEntity {
Expand Down Expand Up @@ -68,7 +74,7 @@ private void onPlayerMoveInput(MovementType type, Vec3d movement, CallbackInfo c
// Check if player is holding right block
if (!isBlockInHand(targetSchematicBlock)) continue;

if (tryToPlace(pos)) {
if (tryToPlaceBlock(pos)) {
lastPlaced = new Date().getTime();
break loop;
};
Expand All @@ -77,17 +83,52 @@ private void onPlayerMoveInput(MovementType type, Vec3d movement, CallbackInfo c
}
}

private boolean tryToPlace(BlockPos pos) {
int getBlockHalf(BlockState state) {
for (Property<?> prop : state.getProperties()) {
if (prop.getName().equals("type") || prop.getName().equals("half")) {
return state.get(prop).toString().equals("top") ? 1 : 0;
}
}

return -1;
}

Direction getBlockDirection(BlockState state) {
for (Property<?> prop : state.getProperties()) {
if (prop instanceof DirectionProperty) {
return (Direction)state.get(prop);
}
}

return null;
}

private boolean tryToPlaceBlock(BlockPos pos) {
BlockState state = SchematicWorldHandler.getSchematicWorld().getBlockState(pos);
Direction dir = BlockUtils.getFirstPropertyFacingValue(state);

Vec3d posVec = Vec3d.ofCenter(pos);

Direction schDir = getBlockDirection(state);
int half = getBlockHalf(state);

for(Direction side : Direction.values()) {
if (half == 1 && side.equals(Direction.DOWN)) continue;
if (half == 0 && side.equals(Direction.UP)) continue;

BlockPos neighbor = pos.offset(side);

if (!canBeClicked(neighbor))
continue;

Vec3d hitVec = posVec.add(Vec3d.of(side.getVector()).multiply(0.5));

if (half == 1 && !side.equals(Direction.UP)) {
hitVec = hitVec.add(0, 0.25, 0);
} else if (half == 0 && !side.equals(Direction.DOWN)) {
hitVec = hitVec.add(0, -0.25, 0);
}

((IClientPlayerInteractionManager) client.interactionManager).rightClickBlock(neighbor,
side.getOpposite(), hitVec);

Expand Down

0 comments on commit cbbea9c

Please sign in to comment.