Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Added hitboxes for the attachments on pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Mar 27, 2020
1 parent 54d64a7 commit 1bbc989
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -214,6 +258,37 @@ public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, I
shape = VoxelShapes.or(shape, DOWN_EXTENSION_SHAPE);
}

Predicate<Direction> 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;
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/com/raoulvdberge/refinedpipes/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/raoulvdberge/refinedpipes/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 1bbc989

Please sign in to comment.