From 1bbc989b60e057192967db5ec2fbf8c30c4e3f26 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Fri, 27 Mar 2020 20:26:40 +0100 Subject: [PATCH] Added hitboxes for the attachments on pipes --- CHANGELOG.md | 3 + .../refinedpipes/block/ItemPipeBlock.java | 81 ++++++++++++++++++- .../refinedpipes/network/Network.java | 12 +-- .../extractor/ExtractorAttachmentType.java | 2 +- .../refinedpipes/util/StringUtil.java | 14 ++++ 5 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/raoulvdberge/refinedpipes/util/StringUtil.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 179fea9..513b062 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Refined Pipes Changelog +## 0.1.4 +- Added hitboxes for the attachments on pipes (raoulvdberge) + ## 0.1.3 - Fixed Item Pipes using the wrong side of an inventory (raoulvdberge) diff --git a/src/main/java/com/raoulvdberge/refinedpipes/block/ItemPipeBlock.java b/src/main/java/com/raoulvdberge/refinedpipes/block/ItemPipeBlock.java index 328a10b..97aa3d8 100644 --- a/src/main/java/com/raoulvdberge/refinedpipes/block/ItemPipeBlock.java +++ b/src/main/java/com/raoulvdberge/refinedpipes/block/ItemPipeBlock.java @@ -21,6 +21,7 @@ import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.shapes.ISelectionContext; import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShapes; @@ -30,6 +31,7 @@ import net.minecraftforge.items.CapabilityItemHandler; import javax.annotation.Nullable; +import java.util.function.Predicate; public class ItemPipeBlock extends Block { public static final BooleanProperty NORTH = BooleanProperty.create("north"); @@ -54,6 +56,13 @@ public class ItemPipeBlock extends Block { public static final VoxelShape UP_EXTENSION_SHAPE = makeCuboidShape(4, 12, 4, 12, 16, 12); public static final VoxelShape DOWN_EXTENSION_SHAPE = makeCuboidShape(4, 0, 4, 12, 4, 12); + private static final VoxelShape NORTH_ATTACHMENT_SHAPE = makeCuboidShape(3, 3, 0, 13, 13, 3); + private static final VoxelShape EAST_ATTACHMENT_SHAPE = makeCuboidShape(13, 3, 3, 16, 13, 13); + private static final VoxelShape SOUTH_ATTACHMENT_SHAPE = makeCuboidShape(3, 3, 13, 13, 13, 16); + private static final VoxelShape WEST_ATTACHMENT_SHAPE = makeCuboidShape(0, 3, 3, 3, 13, 13); + private static final VoxelShape UP_ATTACHMENT_SHAPE = makeCuboidShape(3, 13, 3, 13, 16, 13); + private static final VoxelShape DOWN_ATTACHMENT_SHAPE = makeCuboidShape(3, 0, 3, 13, 3, 13); + private final ItemPipeType type; public ItemPipeBlock(ItemPipeType type) { @@ -75,17 +84,52 @@ public ItemPipeType getType() { @Override @SuppressWarnings("deprecation") public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) { + Direction dirClicked = getAttachmentDirectionClicked(pos, hit.getHitVec()); + + if (dirClicked == null) { + dirClicked = hit.getFace(); + } + ItemStack held = player.getHeldItemMainhand(); if (held.getItem() instanceof AttachmentItem) { - return addAttachment(player, world, pos, held, hit.getFace()); - } else if (held.isEmpty() && player.isCrouching()) { - return removeAttachment(world, pos, hit.getFace()); + return addAttachment(player, world, pos, held, dirClicked); + } else if (held.isEmpty()) { + return removeAttachment(world, pos, dirClicked); } return super.onBlockActivated(state, world, pos, player, hand, hit); } + @Nullable + private Direction getAttachmentDirectionClicked(BlockPos pos, Vec3d hit) { + if (NORTH_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.NORTH; + } + + if (EAST_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.EAST; + } + + if (SOUTH_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.SOUTH; + } + + if (WEST_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.WEST; + } + + if (UP_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.UP; + } + + if (DOWN_ATTACHMENT_SHAPE.getBoundingBox().grow(0.01).offset(pos).contains(hit)) { + return Direction.DOWN; + } + + return null; + } + private ActionResultType addAttachment(PlayerEntity player, World world, BlockPos pos, ItemStack attachment, Direction dir) { if (!world.isRemote) { ItemPipe pipe = NetworkManager.get(world).getPipe(pos); @@ -214,6 +258,37 @@ public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, I shape = VoxelShapes.or(shape, DOWN_EXTENSION_SHAPE); } + Predicate hasAttachment = (dir) -> false; + + TileEntity tile = world.getTileEntity(pos); + if (tile instanceof ItemPipeTileEntity) { + hasAttachment = ((ItemPipeTileEntity) tile)::hasAttachment; + } + + if (hasAttachment.test(Direction.NORTH) || state.get(INV_NORTH)) { + shape = VoxelShapes.or(shape, NORTH_ATTACHMENT_SHAPE); + } + + if (hasAttachment.test(Direction.EAST) || state.get(INV_EAST)) { + shape = VoxelShapes.or(shape, EAST_ATTACHMENT_SHAPE); + } + + if (hasAttachment.test(Direction.SOUTH) || state.get(INV_SOUTH)) { + shape = VoxelShapes.or(shape, SOUTH_ATTACHMENT_SHAPE); + } + + if (hasAttachment.test(Direction.WEST) || state.get(INV_WEST)) { + shape = VoxelShapes.or(shape, WEST_ATTACHMENT_SHAPE); + } + + if (hasAttachment.test(Direction.UP) || state.get(INV_UP)) { + shape = VoxelShapes.or(shape, UP_ATTACHMENT_SHAPE); + } + + if (hasAttachment.test(Direction.DOWN) || state.get(INV_DOWN)) { + shape = VoxelShapes.or(shape, DOWN_ATTACHMENT_SHAPE); + } + return shape; } diff --git a/src/main/java/com/raoulvdberge/refinedpipes/network/Network.java b/src/main/java/com/raoulvdberge/refinedpipes/network/Network.java index f175587..0e12674 100644 --- a/src/main/java/com/raoulvdberge/refinedpipes/network/Network.java +++ b/src/main/java/com/raoulvdberge/refinedpipes/network/Network.java @@ -2,6 +2,7 @@ import com.raoulvdberge.refinedpipes.network.graph.NetworkGraph; import com.raoulvdberge.refinedpipes.network.graph.scanner.NetworkGraphScannerResult; +import com.raoulvdberge.refinedpipes.util.StringUtil; import net.minecraft.nbt.CompoundNBT; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -20,7 +21,7 @@ public class Network { private boolean didDoInitialScan; public Network(BlockPos originPos) { - this(originPos, generateRandomString(new Random(), 8)); + this(originPos, StringUtil.randomString(new Random(), 8)); } public Network(BlockPos originPos, String id) { @@ -81,13 +82,4 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(id); } - - private static String generateRandomString(Random r, int length) { - StringBuilder word = new StringBuilder(length); - for (int i = 0; i < length; i++) { - int tmp = 'a' + r.nextInt('z' - 'a'); - word.append((char) tmp); - } - return word.toString(); - } } diff --git a/src/main/java/com/raoulvdberge/refinedpipes/network/pipe/attachment/extractor/ExtractorAttachmentType.java b/src/main/java/com/raoulvdberge/refinedpipes/network/pipe/attachment/extractor/ExtractorAttachmentType.java index daa72c2..0c2125c 100644 --- a/src/main/java/com/raoulvdberge/refinedpipes/network/pipe/attachment/extractor/ExtractorAttachmentType.java +++ b/src/main/java/com/raoulvdberge/refinedpipes/network/pipe/attachment/extractor/ExtractorAttachmentType.java @@ -85,7 +85,7 @@ private void update(Network network, ItemPipe pipe, Attachment attachment, Block .findNearestDestination(pipe.getPos(), d -> isDestinationApplicable(attachment, sourcePos, extracted, d)); if (destination == null) { - LOGGER.error("No destination found from " + pipe.getPos()); + LOGGER.warn("No destination found from " + pipe.getPos()); return; } diff --git a/src/main/java/com/raoulvdberge/refinedpipes/util/StringUtil.java b/src/main/java/com/raoulvdberge/refinedpipes/util/StringUtil.java new file mode 100644 index 0000000..e49dfed --- /dev/null +++ b/src/main/java/com/raoulvdberge/refinedpipes/util/StringUtil.java @@ -0,0 +1,14 @@ +package com.raoulvdberge.refinedpipes.util; + +import java.util.Random; + +public class StringUtil { + public static String randomString(Random r, int length) { + StringBuilder str = new StringBuilder(length); + for (int i = 0; i < length; i++) { + int tmp = 'a' + r.nextInt('z' - 'a'); + str.append((char) tmp); + } + return str.toString(); + } +}