From 998dab052b2d76b3df95e7f9f7532cdf68c7d2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 7 Jan 2022 07:50:33 +0100 Subject: [PATCH] Reduce Side util class API surface a bit. --- .../cil/oc2/api/util/RobotOperationSide.java | 17 ++++++++++------- src/main/java/li/cil/oc2/api/util/Side.java | 10 +--------- .../RedstoneInterfaceBlockEntity.java | 8 +++++--- .../item/BlockOperationsModuleDevice.java | 4 ++-- .../item/InventoryOperationsModuleDevice.java | 8 ++++---- .../item/RedstoneInterfaceCardItemDevice.java | 11 +++++++---- .../oc2/common/util/HorizontalBlockUtils.java | 19 +++++++++---------- 7 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/main/java/li/cil/oc2/api/util/RobotOperationSide.java b/src/main/java/li/cil/oc2/api/util/RobotOperationSide.java index c4e07dfa..d7f18c45 100644 --- a/src/main/java/li/cil/oc2/api/util/RobotOperationSide.java +++ b/src/main/java/li/cil/oc2/api/util/RobotOperationSide.java @@ -32,14 +32,17 @@ public enum RobotOperationSide { this(parent.direction); } - public Direction getDirection() { - return direction; - } - - public static Direction getAdjustedDirection(@Nullable final RobotOperationSide side, final Entity entity) { + /** + * Gets the world-space direction for the specified side relative to the specified entity. + * + * @param entity the entity to which the side is relative. + * @param side the side to convert to a world-space direction. + * @return a world-space direction. + */ + public static Direction toGlobal(final Entity entity, @Nullable final RobotOperationSide side) { Direction direction = side == null - ? RobotOperationSide.FRONT.getDirection() - : side.getDirection(); + ? RobotOperationSide.FRONT.direction + : side.direction; if (direction.getAxis().isHorizontal()) { final int horizontalIndex = entity.getDirection().get2DDataValue(); for (int i = 0; i < horizontalIndex; i++) { diff --git a/src/main/java/li/cil/oc2/api/util/Side.java b/src/main/java/li/cil/oc2/api/util/Side.java index 14f45281..b542e184 100644 --- a/src/main/java/li/cil/oc2/api/util/Side.java +++ b/src/main/java/li/cil/oc2/api/util/Side.java @@ -8,7 +8,7 @@ import javax.annotation.Nullable; * This enum indicates a side of a block device. *

* It is intended to be used by {@link li.cil.oc2.api.bus.device.rpc.RPCDevice} APIs, - * providing both convenience for the called by providing a range of aliases, and also + * providing both convenience for the caller by providing a range of aliases, and also * stability, in case Mojang decide to rename the enum fields of the {@link Direction} * enum at some time in the future. */ @@ -67,14 +67,6 @@ public enum Side { return direction; } - public int get2DDataValue() { - return direction.get2DDataValue(); - } - - public int get3DDataValue() { - return direction.get3DDataValue(); - } - @Override public String toString() { return base != null ? base.toString() : super.toString(); diff --git a/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java b/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java index 08b1728d..c6a9df35 100644 --- a/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java +++ b/src/main/java/li/cil/oc2/common/blockentity/RedstoneInterfaceBlockEntity.java @@ -86,20 +86,22 @@ public final class RedstoneInterfaceBlockEntity extends BlockEntity implements N @Callback(name = GET_REDSTONE_OUTPUT, synchronize = false) public int getRedstoneOutput(@Parameter(SIDE) @Nullable final Side side) { if (side == null) throw new IllegalArgumentException(); + final int index = side.getDirection().get3DDataValue(); - return output[side.get3DDataValue()]; + return output[index]; } @Callback(name = SET_REDSTONE_OUTPUT) public void setRedstoneOutput(@Parameter(SIDE) @Nullable final Side side, @Parameter(VALUE) final int value) { if (side == null) throw new IllegalArgumentException(); + final int index = side.getDirection().get3DDataValue(); final byte clampedValue = (byte) Mth.clamp(value, 0, 15); - if (clampedValue == output[side.get3DDataValue()]) { + if (clampedValue == output[index]) { return; } - output[side.get3DDataValue()] = clampedValue; + output[index] = clampedValue; final Direction direction = HorizontalBlockUtils.toGlobal(getBlockState(), side); if (direction != null) { diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java index 77f40fdd..65cc3d57 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/BlockOperationsModuleDevice.java @@ -91,7 +91,7 @@ public final class BlockOperationsModuleDevice extends AbstractItemRPCDevice { final List oldItems = getItemsInRange(); - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); if (!tryHarvestBlock(serverLevel, entity.blockPosition().relative(direction))) { return false; } @@ -129,7 +129,7 @@ public final class BlockOperationsModuleDevice extends AbstractItemRPCDevice { return false; } - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); final BlockPos blockPos = entity.blockPosition().relative(direction); final Direction oppositeDirection = direction.getOpposite(); final BlockHitResult hit = new BlockHitResult( diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/InventoryOperationsModuleDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/InventoryOperationsModuleDevice.java index c8070717..5874b540 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/InventoryOperationsModuleDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/InventoryOperationsModuleDevice.java @@ -80,7 +80,7 @@ public final class InventoryOperationsModuleDevice extends AbstractItemRPCDevice } final int originalStackSize = stack.getCount(); - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); final List itemHandlers = getItemStackHandlersInDirection(direction).toList(); for (final IItemHandler handler : itemHandlers) { stack = ItemHandlerHelper.insertItemStacked(handler, stack, false); @@ -122,7 +122,7 @@ public final class InventoryOperationsModuleDevice extends AbstractItemRPCDevice } final int originalStackSize = stack.getCount(); - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); final Optional optional = getItemStackHandlersInDirection(direction).findFirst(); if (optional.isPresent()) { stack = optional.get().insertItem(intoSlot, stack, false); @@ -150,7 +150,7 @@ public final class InventoryOperationsModuleDevice extends AbstractItemRPCDevice return 0; } - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); final List handlers = getItemStackHandlersInDirection(direction).collect(Collectors.toList()); if (handlers.isEmpty()) { return takeFromWorld(count); @@ -167,7 +167,7 @@ public final class InventoryOperationsModuleDevice extends AbstractItemRPCDevice return 0; } - final Direction direction = RobotOperationSide.getAdjustedDirection(side, entity); + final Direction direction = RobotOperationSide.toGlobal(entity, side); return getItemStackHandlersInDirection(direction).findFirst().map(handler -> takeFromInventory(count, handler, fromSlot)).orElse(0); } diff --git a/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java b/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java index 98990f37..254352cf 100644 --- a/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java +++ b/src/main/java/li/cil/oc2/common/bus/device/item/RedstoneInterfaceCardItemDevice.java @@ -57,7 +57,8 @@ public final class RedstoneInterfaceCardItemDevice extends AbstractItemRPCDevice @Override public LazyOptional getCapability(@Nonnull final Capability capability, @Nullable final Direction side) { if (capability == Capabilities.REDSTONE_EMITTER && side != null) { - return LazyOptional.of(() -> capabilities[side.get3DDataValue()]).cast(); + final int index = side.get3DDataValue(); + return LazyOptional.of(() -> capabilities[index]).cast(); } return LazyOptional.empty(); @@ -101,20 +102,22 @@ public final class RedstoneInterfaceCardItemDevice extends AbstractItemRPCDevice @Callback(name = GET_REDSTONE_OUTPUT, synchronize = false) public int getRedstoneOutput(@Parameter(SIDE) @Nullable final Side side) { if (side == null) throw new IllegalArgumentException(); + final int index = side.getDirection().get3DDataValue(); - return output[side.get3DDataValue()]; + return output[index]; } @Callback(name = SET_REDSTONE_OUTPUT) public void setRedstoneOutput(@Parameter(SIDE) @Nullable final Side side, @Parameter(VALUE) final int value) { if (side == null) throw new IllegalArgumentException(); + final int index = side.getDirection().get3DDataValue(); final byte clampedValue = (byte) Mth.clamp(value, 0, 15); - if (clampedValue == output[side.get3DDataValue()]) { + if (clampedValue == output[index]) { return; } - output[side.get3DDataValue()] = clampedValue; + output[index] = clampedValue; final Direction direction = HorizontalBlockUtils.toGlobal(blockEntity.getBlockState(), side); if (direction != null) { diff --git a/src/main/java/li/cil/oc2/common/util/HorizontalBlockUtils.java b/src/main/java/li/cil/oc2/common/util/HorizontalBlockUtils.java index ae717fff..7bf71db4 100644 --- a/src/main/java/li/cil/oc2/common/util/HorizontalBlockUtils.java +++ b/src/main/java/li/cil/oc2/common/util/HorizontalBlockUtils.java @@ -18,18 +18,17 @@ public final class HorizontalBlockUtils { return null; } - final int index = direction.get2DDataValue(); - if (index < 0) { + if (direction.getAxis().isVertical()) { return direction; } - if (!blockState.hasProperty(HorizontalDirectionalBlock.FACING)) { return direction; } final Direction facing = blockState.getValue(HorizontalDirectionalBlock.FACING); - final int toLocal = HORIZONTAL_DIRECTION_COUNT - facing.get2DDataValue(); - final int rotatedIndex = (index + toLocal) % HORIZONTAL_DIRECTION_COUNT; + final int index = direction.get2DDataValue(); + final int toLocal = -facing.get2DDataValue(); + final int rotatedIndex = (index + toLocal + HORIZONTAL_DIRECTION_COUNT) % HORIZONTAL_DIRECTION_COUNT; return Direction.from2DDataValue(rotatedIndex); } @@ -39,16 +38,16 @@ public final class HorizontalBlockUtils { return null; } - final int index = side.get2DDataValue(); - if (index < 0) { - return side.getDirection(); + final Direction direction = side.getDirection(); + if (direction.getAxis().isVertical()) { + return direction; } - if (!blockState.hasProperty(HorizontalDirectionalBlock.FACING)) { - return side.getDirection(); + return direction; } final Direction facing = blockState.getValue(HorizontalDirectionalBlock.FACING); + final int index = direction.get2DDataValue(); final int toGlobal = facing.get2DDataValue(); final int rotatedIndex = (index + toGlobal) % HORIZONTAL_DIRECTION_COUNT; return Direction.from2DDataValue(rotatedIndex);