From cb2c56d772e23fd1405d2d89b11e7a041b2cbc6a Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Sun, 7 Feb 2021 18:09:08 +0100 Subject: [PATCH] Gantry shaft placement assist --- .../structureMovement/bearing/SailBlock.java | 11 ---- .../relays/advanced/GantryShaftBlock.java | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/SailBlock.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/SailBlock.java index fbc1517c7..8e12c734a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/SailBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/bearing/SailBlock.java @@ -72,17 +72,6 @@ public class SailBlock extends ProperDirectionalBlock { return ActionResultType.PASS; 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; } diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/GantryShaftBlock.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/GantryShaftBlock.java index 6fa9bd927..cca015c0a 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/GantryShaftBlock.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/GantryShaftBlock.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.relays.advanced; import java.util.ArrayList; import java.util.List; +import java.util.function.Predicate; import com.simibubi.create.AllBlocks; 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.foundation.utility.Iterate; 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.BlockRenderType; 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.ItemStack; import net.minecraft.item.ItemUseContext; import net.minecraft.state.BooleanProperty; import net.minecraft.state.EnumProperty; @@ -25,8 +33,10 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResultType; import net.minecraft.util.Direction; import net.minecraft.util.Direction.Axis; +import net.minecraft.util.Hand; import net.minecraft.util.IStringSerializable; 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.VoxelShape; import net.minecraft.world.IBlockReader; @@ -39,6 +49,8 @@ public class GantryShaftBlock extends DirectionalKineticBlock { public static final IProperty PART = EnumProperty.create("part", Part.class); public static final BooleanProperty POWERED = BlockStateProperties.POWERED; + private static final int placementHelperId = PlacementHelpers.register(new PlacementHelper()); + public enum Part implements IStringSerializable { START, MIDDLE, END, SINGLE; @@ -53,6 +65,25 @@ public class GantryShaftBlock extends DirectionalKineticBlock { 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 public VoxelShape getShape(BlockState state, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) { @@ -237,4 +268,27 @@ public class GantryShaftBlock extends DirectionalKineticBlock { && oldState.get(POWERED) == newState.get(POWERED); } + public static class PlacementHelper extends PoleHelper { + + public PlacementHelper() { + super(AllBlocks.GANTRY_SHAFT::has, s -> s.get(FACING) + .getAxis(), FACING); + } + + @Override + public Predicate 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)))); + } + + } + }