Gantry shaft placement assist

This commit is contained in:
simibubi 2021-02-07 18:09:08 +01:00
parent 6981848f8a
commit cb2c56d772
2 changed files with 54 additions and 11 deletions

View file

@ -72,17 +72,6 @@ public class SailBlock extends ProperDirectionalBlock {
return ActionResultType.PASS; return ActionResultType.PASS;
offset.placeInWorld(world, ((BlockItem) heldItem.getItem()).getBlock().getDefaultState(), player, heldItem); offset.placeInWorld(world, ((BlockItem) heldItem.getItem()).getBlock().getDefaultState(), player, heldItem);
/*BlockState blockState = ((BlockItem) heldItem.getItem()).getBlock()
.getDefaultState()
.with(FACING, state.get(FACING));
BlockPos offsetPos = new BlockPos(offset.getPos());
if (!world.isRemote && world.getBlockState(offsetPos).getMaterial().isReplaceable()) {
world.setBlockState(offsetPos, blockState);
if (!player.isCreative())
heldItem.shrink(1);
}*/
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }

View file

@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.relays.advanced;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllShapes; import com.simibubi.create.AllShapes;
@ -10,11 +11,18 @@ import com.simibubi.create.content.contraptions.base.DirectionalKineticBlock;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.foundation.utility.Iterate; import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.foundation.utility.placement.IPlacementHelper;
import com.simibubi.create.foundation.utility.placement.PlacementHelpers;
import com.simibubi.create.foundation.utility.placement.PlacementOffset;
import com.simibubi.create.foundation.utility.placement.util.PoleHelper;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType; import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BlockItemUseContext; import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext; import net.minecraft.item.ItemUseContext;
import net.minecraft.state.BooleanProperty; import net.minecraft.state.BooleanProperty;
import net.minecraft.state.EnumProperty; import net.minecraft.state.EnumProperty;
@ -25,8 +33,10 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis; import net.minecraft.util.Direction.Axis;
import net.minecraft.util.Hand;
import net.minecraft.util.IStringSerializable; import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader; import net.minecraft.world.IBlockReader;
@ -39,6 +49,8 @@ public class GantryShaftBlock extends DirectionalKineticBlock {
public static final IProperty<Part> PART = EnumProperty.create("part", Part.class); public static final IProperty<Part> PART = EnumProperty.create("part", Part.class);
public static final BooleanProperty POWERED = BlockStateProperties.POWERED; public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
private static final int placementHelperId = PlacementHelpers.register(new PlacementHelper());
public enum Part implements IStringSerializable { public enum Part implements IStringSerializable {
START, MIDDLE, END, SINGLE; START, MIDDLE, END, SINGLE;
@ -53,6 +65,25 @@ public class GantryShaftBlock extends DirectionalKineticBlock {
super.fillStateContainer(builder.add(PART, POWERED)); super.fillStateContainer(builder.add(PART, POWERED));
} }
@Override
public ActionResultType onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand,
BlockRayTraceResult ray) {
ItemStack heldItem = player.getHeldItem(hand);
IPlacementHelper placementHelper = PlacementHelpers.get(placementHelperId);
if (!placementHelper.matchesItem(heldItem))
return ActionResultType.PASS;
PlacementOffset offset = placementHelper.getOffset(world, state, pos, ray);
if (!offset.isReplaceable(world))
return ActionResultType.PASS;
offset.placeInWorld(world, ((BlockItem) heldItem.getItem()).getBlock()
.getDefaultState(), player, heldItem);
return ActionResultType.SUCCESS;
}
@Override @Override
public VoxelShape getShape(BlockState state, IBlockReader p_220053_2_, BlockPos p_220053_3_, public VoxelShape getShape(BlockState state, IBlockReader p_220053_2_, BlockPos p_220053_3_,
ISelectionContext p_220053_4_) { ISelectionContext p_220053_4_) {
@ -237,4 +268,27 @@ public class GantryShaftBlock extends DirectionalKineticBlock {
&& oldState.get(POWERED) == newState.get(POWERED); && oldState.get(POWERED) == newState.get(POWERED);
} }
public static class PlacementHelper extends PoleHelper<Direction> {
public PlacementHelper() {
super(AllBlocks.GANTRY_SHAFT::has, s -> s.get(FACING)
.getAxis(), FACING);
}
@Override
public Predicate<ItemStack> getItemPredicate() {
return AllBlocks.GANTRY_SHAFT::isIn;
}
@Override
public PlacementOffset getOffset(World world, BlockState state, BlockPos pos, BlockRayTraceResult ray) {
PlacementOffset offset = super.getOffset(world, state, pos, ray);
if (!offset.isSuccessful())
return offset;
return PlacementOffset.success(offset.getPos(), offset.getTransform()
.andThen(s -> s.with(POWERED, state.get(POWERED))));
}
}
} }