Reduce Side util class API surface a bit.

This commit is contained in:
Florian Nücke
2022-01-07 07:50:33 +01:00
parent 94e57af093
commit 998dab052b
7 changed files with 38 additions and 39 deletions

View File

@@ -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++) {

View File

@@ -8,7 +8,7 @@ import javax.annotation.Nullable;
* This enum indicates a side of a block device.
* <p>
* 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();

View File

@@ -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) {

View File

@@ -91,7 +91,7 @@ public final class BlockOperationsModuleDevice extends AbstractItemRPCDevice {
final List<ItemEntity> 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(

View File

@@ -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<IItemHandler> 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<IItemHandler> 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<IItemHandler> 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);
}

View File

@@ -57,7 +57,8 @@ public final class RedstoneInterfaceCardItemDevice extends AbstractItemRPCDevice
@Override
public <T> LazyOptional<T> getCapability(@Nonnull final Capability<T> 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) {

View File

@@ -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);