Skip to content
Closed
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 117 additions & 67 deletions src/main/java/carpetextra/utils/BlockPlacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,167 @@

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.ComparatorBlock;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.DetectorRailBlock;
import net.minecraft.block.FacingBlock;
import net.minecraft.block.GlazedTerracottaBlock;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.ObserverBlock;
import net.minecraft.block.PistonBlock;
import net.minecraft.block.PoweredRailBlock;
import net.minecraft.block.RailBlock;
import net.minecraft.block.RepeaterBlock;
import net.minecraft.block.StairsBlock;
import net.minecraft.block.TrapdoorBlock;
import net.minecraft.block.WallMountedBlock;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.block.enums.ChestType;
import net.minecraft.block.enums.ComparatorMode;
import net.minecraft.block.enums.RailShape;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
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.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class BlockPlacer
{
public class BlockPlacer {
private static Boolean HasDirectionProperty(BlockState state) {
//malilib code
for (Property<?> prop : state.getProperties()) {
if (prop instanceof DirectionProperty) {
return true;
}
}
return false;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this and instead save the result of getFirstDirectionProperty in a field (since it will be needed later anyway) and check != null

private static DirectionProperty getFirstDirectionProperty(BlockState state) {
//malilib code
for (Property<?> prop : state.getProperties()) {
if (prop instanceof DirectionProperty) {
return (DirectionProperty) prop;
}
}
return null;
}

private static Boolean IsBlockAttachableChest(Block originBlock, Direction Facing, BlockPos checkPos, World world) {
BlockState checkState = world.getBlockState(checkPos);
if (checkState == null) {
return false;
}
if (originBlock.getName().equals(checkState.getBlock().getName())) {
return checkState.get(ChestBlock.FACING).equals(Facing) && checkState.get(ChestBlock.CHEST_TYPE) == ChestType.SINGLE;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static Boolean IsBlockAttachableChest(Block originBlock, Direction Facing, BlockPos checkPos, World world) {
BlockState checkState = world.getBlockState(checkPos);
if (checkState == null) {
return false;
}
if (originBlock.getName().equals(checkState.getBlock().getName())) {
return checkState.get(ChestBlock.FACING).equals(Facing) && checkState.get(ChestBlock.CHEST_TYPE) == ChestType.SINGLE;
}
private static boolean isBlockAttachableChest(Block originBlock, Direction facing, BlockPos checkPos, World world) {
BlockState checkState = world.getBlockState(checkPos);
if (checkState == null) {
return false;
}
if (originBlock.getName().equals(checkState.getBlock().getName())) {
return checkState.get(ChestBlock.FACING).equals(facing) && checkState.get(ChestBlock.CHEST_TYPE) == ChestType.SINGLE;
}

Also isn't there a better way than checking the name of the block? I think there must be some other method to checking the state equals.

return false;
}

public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
//actual alternative block placement code

Direction facing;
Direction facing = Direction.NORTH;
Vec3d vec3d = context.getHitPos();
BlockPos pos = context.getBlockPos();
double hitX = vec3d.x - pos.getX();
if (hitX<2) // vanilla
BlockState state = block.getDefaultState();
if (hitX < 2 || !(block instanceof AbstractRailBlock) && !HasDirectionProperty(state)) // vanilla
Copy link
Collaborator

@altrisi altrisi Sep 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here this could be something like

DirectionProperty directionProperty = getFirstDirectionProperty(state);
if (... && directionProperty != null)

and then keep the directionProperty for use later.

return null;
int code = (int)(hitX-2)/2;
int code = (int) (hitX - 2) / 2;

//
// now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
// since its actually using it. Its private - maybe with Reflections?
//
PlayerEntity placer = context.getPlayer();
World world = context.getWorld();

if (block instanceof GlazedTerracottaBlock)
{
facing = Direction.byId(code);
if(facing == Direction.UP || facing == Direction.DOWN)
if (block instanceof AbstractRailBlock){
RailShape shapeEnumFound = RailShape.values()[code];
if (block instanceof RailBlock){
state = state.with(RailBlock.SHAPE,shapeEnumFound);
}
else
{
facing = placer.getHorizontalFacing().getOpposite();
state = state.with(PoweredRailBlock.SHAPE,shapeEnumFound);
}
return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
}
else if (block instanceof ObserverBlock)
else
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(ObserverBlock.POWERED, true);
}
else if (block instanceof RepeaterBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
DirectionProperty property = getFirstDirectionProperty(state);
int FacingId = code % 16;
facing = Direction.byId(FacingId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int FacingId = code % 16;
facing = Direction.byId(FacingId);
int facingId = code % 16;
facing = Direction.byId(facingId);

if (property.getValues().contains(facing) == false) {
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
state = state.with(property, facing);
}



//check blocks with additional states first
if (block instanceof RepeaterBlock) {
state = state
.with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
.with(RepeaterBlock.LOCKED, Boolean.FALSE);
}
else if (block instanceof TrapdoorBlock)
{
facing = Direction.byId(code % 16);
if(facing == Direction.UP || facing == Direction.DOWN)
{
facing = placer.getHorizontalFacing().getOpposite();
}
return block.getDefaultState()
.with(TrapdoorBlock.FACING, facing)
} else if (block instanceof TrapdoorBlock) {
state = state
.with(TrapdoorBlock.OPEN, Boolean.FALSE)
.with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
.with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
}
else if (block instanceof ComparatorBlock)
{
facing = Direction.byId(code % 16);
if((facing == Direction.UP) || (facing == Direction.DOWN))
{
facing = placer.getHorizontalFacing().getOpposite();
}
ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
return block.getDefaultState()
.with(HorizontalFacingBlock.FACING, facing)
} else if (block instanceof ComparatorBlock) {
ComparatorMode m = (hitX >= 16) ? ComparatorMode.SUBTRACT : ComparatorMode.COMPARE;
state = state
.with(ComparatorBlock.POWERED, Boolean.FALSE)
.with(ComparatorBlock.MODE, m);
}
else if (block instanceof DispenserBlock)
{
return block.getDefaultState()
.with(DispenserBlock.FACING, Direction.byId(code))
.with(DispenserBlock.TRIGGERED, Boolean.FALSE);
}
else if (block instanceof PistonBlock)
{
return block.getDefaultState()
.with(FacingBlock.FACING, Direction.byId(code))
.with(PistonBlock.EXTENDED, Boolean.FALSE);
}
else if (block instanceof StairsBlock)
} else if (block instanceof StairsBlock) {
state = block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, facing)
.with(StairsBlock.HALF, (hitX >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM);
} else if (block instanceof WallMountedBlock) {
//unsupported
return null;
} else if (block instanceof ChestBlock)
//-x +x
//+x east -x west +z south -z north
{
return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
.with(StairsBlock.FACING, Direction.byId(code % 16))
.with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
if (facing == Direction.SOUTH) {
if (IsBlockAttachableChest(block, facing, pos.west(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.LEFT);
}
if (IsBlockAttachableChest(block, facing, pos.east(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.RIGHT);
}
} else if (facing == Direction.WEST) //-z +z
{
if (IsBlockAttachableChest(block, facing, pos.north(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.LEFT);
}
if (IsBlockAttachableChest(block, facing, pos.south(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.RIGHT);
}
} else if (facing == Direction.NORTH) //+x -x
{
if (IsBlockAttachableChest(block, facing, pos.east(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.LEFT);
}
if (IsBlockAttachableChest(block, facing, pos.west(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.RIGHT);
}
} else if (facing == Direction.EAST) //+z -z
{
if (IsBlockAttachableChest(block, facing, pos.south(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.LEFT);
}
if (IsBlockAttachableChest(block, facing, pos.north(), world)) {
return state.with(ChestBlock.CHEST_TYPE, ChestType.RIGHT);
}
}
return state.with(ChestBlock.CHEST_TYPE, ChestType.SINGLE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the only way of doing this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it can use .Direction.rotateYClockwise() and rotateYCounterclockwise() instead of repeated facing checks, I'll try it

}
return null;
return state;
}
}