diff --git a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlock.java b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlock.java index f2a58b4..a78c3d9 100644 --- a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlock.java +++ b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlock.java @@ -50,7 +50,8 @@ public ServoMotorBlock(BlockBehaviour.Properties builder) { super(builder); this.registerDefaultState(this.stateDefinition.any() .setValue(FACING, Direction.NORTH) - .setValue(REProperties.SPINNING, Boolean.FALSE)); + .setValue(REProperties.SPINNING, Boolean.FALSE) + .setValue(REProperties.HAS_BEEN_ACTIVATED, Boolean.FALSE)); } @Override @@ -70,7 +71,7 @@ public BlockState mirror(BlockState state, Mirror mirrorIn) { @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { - builder.add(FACING, REProperties.SPINNING); + builder.add(FACING, REProperties.SPINNING, REProperties.HAS_BEEN_ACTIVATED); } @Override @@ -82,13 +83,20 @@ private void checkIfItShouldRotate(ServerLevel level, BlockPos pos, BlockState s { Direction direction = state.getValue(FACING); boolean isPowered = level.hasNeighborSignal(pos); - if (isPowered && !state.getValue(REProperties.SPINNING).booleanValue()) + if (isPowered + && !state.getValue(REProperties.HAS_BEEN_ACTIVATED).booleanValue() + && !state.getValue(REProperties.SPINNING).booleanValue()) { if (new ServoMotorStructureResolver(level, pos, direction).resolve()) { - level.blockEvent(pos, this, 0, direction.get3DDataValue()); + level.blockEvent(pos, this, 0, 0); } } + if (!isPowered + && state.getValue(REProperties.HAS_BEEN_ACTIVATED).booleanValue()) + { + level.blockEvent(pos, this, 1, 0); + } } @Override @@ -115,16 +123,22 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState stat @Override public boolean triggerEvent(BlockState state, Level level, BlockPos pos, int id, int param) { Direction direction = state.getValue(FACING); - if (id != 0) + if (id == 0) { - return false; + if (!this.startRotation(level, pos, direction)) + { + return false; + } } - - if (!this.startRotation(level, pos, direction)) + else if (id == 1) { - return false; + if (!level.setBlock(pos, level.getBlockState(pos) + .setValue(REProperties.HAS_BEEN_ACTIVATED, false), + Block.UPDATE_CLIENTS)) + { + return false; + } } - BlockEntity blockEntity = level.getBlockEntity(pos); if (blockEntity == null) { @@ -141,24 +155,24 @@ private boolean startRotation(Level level, BlockPos motorPos, Direction directio } BlockPos finalPos = servoMotorStructureResolver.getBlockToCycle(); if (!level.setBlock(motorPos, level.getBlockState(motorPos) - .setValue(REProperties.SPINNING, true), - Block.UPDATE_CLIENTS)) + .setValue(REProperties.SPINNING, true), + Block.UPDATE_CLIENTS)) { return false; } if (!level.setBlock(finalPos, level.getBlockState(finalPos) - .setValue(REProperties.SELECTOR_ORIENTATION, SelectorOrientation.ROTATING), - Block.UPDATE_CLIENTS)) + .setValue(REProperties.SELECTOR_ORIENTATION, SelectorOrientation.ROTATING), + Block.UPDATE_CLIENTS)) { return false; } ServoMotorBlockEntity blockEntity = new ServoMotorBlockEntity(motorPos, - level.getBlockState(motorPos), - finalPos, - servoMotorStructureResolver - .getBlocksToRotate(), - direction, - servoMotorStructureResolver.getGoal()); + level.getBlockState(motorPos), + finalPos, + servoMotorStructureResolver + .getBlocksToRotate(), + direction, + servoMotorStructureResolver.getGoal()); level.setBlockEntity(blockEntity); return true; } diff --git a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlockEntity.java b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlockEntity.java index 449e0f5..e4cf5b5 100644 --- a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlockEntity.java +++ b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/servo_motor/ServoMotorBlockEntity.java @@ -64,7 +64,10 @@ protected void saveAdditional(CompoundTag tag) { super.saveAdditional(tag); tag.put("cycledPos", NbtUtils.writeBlockPos(this.cycledPos)); ListTag rotatedTag = new ListTag(); - rotatedTag.addAll(rotatedPositions.stream().map(NbtUtils::writeBlockPos).collect(Collectors.toList())); + rotatedTag.addAll(rotatedPositions + .stream() + .map(NbtUtils::writeBlockPos) + .collect(Collectors.toList())); tag.putInt("rotatedPositionsLength", rotatedTag.size()); tag.put("rotatedPositions", rotatedTag); tag.putInt("direction", this.direction.get3DDataValue()); @@ -78,9 +81,9 @@ public void load(CompoundTag tag) { int rotatedPositionsSize = tag.getInt("rotatedPositionsLength"); ListTag rotatedTags = tag.getList("rotatedPositions", rotatedPositionsSize); this.rotatedPositions.addAll(rotatedTags.stream() - .map(rotatedTag -> (CompoundTag) rotatedTag) - .map(NbtUtils::readBlockPos) - .collect(Collectors.toList())); + .map(rotatedTag -> (CompoundTag) rotatedTag) + .map(NbtUtils::readBlockPos) + .collect(Collectors.toList())); this.direction = Direction.from3DDataValue(tag.getInt("direction")); this.progressO = this.progress = tag.getFloat("progress"); this.goalOrientation = SelectorOrientation.valueOf(tag.getString("goal")); @@ -98,12 +101,13 @@ public void finalTick() { this.level.removeBlockEntity(this.worldPosition); this.setRemoved(); this.level.setBlock(this.worldPosition, this.level.getBlockState(this.worldPosition) - .setValue(REProperties.SPINNING, false), - Block.UPDATE_CLIENTS | Block.UPDATE_NEIGHBORS); + .setValue(REProperties.SPINNING, false) + .setValue(REProperties.HAS_BEEN_ACTIVATED, true), + Block.UPDATE_CLIENTS | Block.UPDATE_NEIGHBORS); this.level.setBlock(this.cycledPos, this.level.getBlockState(this.cycledPos) - .setValue(REProperties.DRIVEN, true) - .setValue(REProperties.SELECTOR_ORIENTATION, this.goalOrientation), - Block.UPDATE_CLIENTS | Block.UPDATE_NEIGHBORS); + .setValue(REProperties.DRIVEN, true) + .setValue(REProperties.SELECTOR_ORIENTATION, this.goalOrientation), + Block.UPDATE_CLIENTS | Block.UPDATE_NEIGHBORS); } } diff --git a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/state/properties/REProperties.java b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/state/properties/REProperties.java index 7849f06..7df2d9d 100644 --- a/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/state/properties/REProperties.java +++ b/src/main/java/io/github/codetoil/redstoneelectronics/world/level/block/state/properties/REProperties.java @@ -27,6 +27,7 @@ public class REProperties { public static final IntegerProperty RESISTANCE_1_4 = IntegerProperty.create("resistance", 1, 4); public static final BooleanProperty SPINNING = BooleanProperty.create("spinning"); public static final BooleanProperty DRIVEN = BooleanProperty.create("driven"); + public static final BooleanProperty HAS_BEEN_ACTIVATED = BooleanProperty.create("has_been_activated"); public static void init() { }