Skip to content

Commit

Permalink
More motor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetoil committed Feb 5, 2023
1 parent f5dcf00 commit 86bcc3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,7 +71,7 @@ public BlockState mirror(BlockState state, Mirror mirrorIn) {

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, REProperties.SPINNING);
builder.add(FACING, REProperties.SPINNING, REProperties.HAS_BEEN_ACTIVATED);
}

@Override
Expand All @@ -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
Expand All @@ -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)
{
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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"));
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down

0 comments on commit 86bcc3a

Please sign in to comment.