Skip to content

Commit 17d99c3

Browse files
committed
abstract peripheral ticker
1 parent 7e4b041 commit 17d99c3

File tree

15 files changed

+60
-66
lines changed

15 files changed

+60
-66
lines changed

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/ChatBoxPeripheral.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ public final MethodResult sendToastToPlayer(@NotNull IArguments arguments) throw
485485
});
486486
}
487487

488+
@Override
488489
public void update() {
489490
lastConsumedMessage = Events.traverseChatMessages(lastConsumedMessage, message -> {
490491
for (IComputerAccess computer : getConnectedComputers()) {

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/DistanceDetectorPeripheral.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public float getConfiguredMaxRange() {
7171
return APConfig.PERIPHERALS_CONFIG.distanceDetectorRange.get().floatValue();
7272
}
7373

74+
public int getUpdateRate() {
75+
return APConfig.PERIPHERALS_CONFIG.distanceDetectorUpdateRate.get();
76+
}
77+
7478
public float getMaxRange() {
7579
return Float.intBitsToFloat(this.maxRange.get());
7680
}
@@ -168,7 +172,7 @@ public final boolean getLaserVisibility() {
168172
return this.getShowLaser();
169173
}
170174

171-
@LuaFunction(value = {"setIgnoreTransparency"})
175+
@LuaFunction(value = "setIgnoreTransparency")
172176
public final void setIgnoreTransparencyLua(boolean enable) {
173177
this.setIgnoreTransparent(enable);
174178
}
@@ -231,17 +235,17 @@ public final boolean shouldCalculatePeriodically() {
231235
return this.getCalculatePeriodically();
232236
}
233237

234-
@LuaFunction(value = {"setCalculatePeriodically"})
238+
@LuaFunction(value = "setCalculatePeriodically")
235239
public final void setCalculatePeriodicallyLua(boolean shouldCalculatePeriodically) {
236240
this.setCalculatePeriodically(shouldCalculatePeriodically);
237241
}
238242

239-
@LuaFunction(value = {"setMaxRange"})
243+
@LuaFunction(value = "setMaxRange")
240244
public final void setMaxRangeLua(double maxDistance) {
241245
this.setMaxRange((float) maxDistance);
242246
}
243247

244-
@LuaFunction(value = {"getMaxRange"})
248+
@LuaFunction(value = "getMaxRange")
245249
public final double getMaxRangeLua() {
246250
return this.getMaxRange();
247251
}
@@ -273,6 +277,17 @@ public double calculateAndUpdateDistance() {
273277
return distance;
274278
}
275279

280+
@Override
281+
public void update() {
282+
if (this.getCalculatePeriodically() && this.getLevel().getGameTime() % this.getUpdateRate() == 0) {
283+
// We calculate the distance every 2 ticks, so we do not have to run the getDistance function of the peripheral
284+
// on the main thread which prevents the 1 tick yield time of the function.
285+
// The calculateDistance function is not thread safe, so we have to run it on the main thread.
286+
// It should be okay to run that function every 2 ticks, calculating it does not take too much time.
287+
this.calculateAndUpdateDistance();
288+
}
289+
}
290+
276291
protected HitResult getHitResult(Vec3 from, Vec3 to) {
277292
Level level = this.getLevel();
278293
ClipContext.ShapeGetter shapeGetter = this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER;

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/PlayerDetectorPeripheral.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
1515
import de.srendi.advancedperipherals.common.blocks.base.PeripheralBlockEntity;
1616
import de.srendi.advancedperipherals.common.configuration.APConfig;
17+
import de.srendi.advancedperipherals.common.events.Events;
1718
import de.srendi.advancedperipherals.common.util.CoordUtil;
1819
import de.srendi.advancedperipherals.common.util.LuaConverter;
1920
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
@@ -33,6 +34,7 @@ public class PlayerDetectorPeripheral extends BasePeripheral<IPeripheralOwner> {
3334

3435
public static final String PERIPHERAL_TYPE = "player_detector";
3536
private static final int MAX_RANGE = APConfig.PERIPHERALS_CONFIG.playerDetMaxRange.get();
37+
private long lastConsumedMessage = Events.getLastPlayerMessageID() - 1;
3638

3739
public PlayerDetectorPeripheral(PeripheralBlockEntity<?> tileEntity) {
3840
super(PERIPHERAL_TYPE, new BlockEntityPeripheralOwner<>(tileEntity));
@@ -265,4 +267,13 @@ private List<ServerPlayer> getPlayers() {
265267
private ServerPlayer getPlayer(String name) {
266268
return ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayerByName(name);
267269
}
270+
271+
@Override
272+
public void update() {
273+
lastConsumedMessage = Events.traversePlayerMessages(lastConsumedMessage, message -> getConnectedComputers().forEach(computer -> {
274+
if(message.eventName().equals("playerChangedDimension")) {
275+
computer.queueEvent(message.eventName(), message.playerName(), message.fromDimension(), message.toDimension());
276+
} else computer.queueEvent(message.eventName(), message.playerName(), message.fromDimension());
277+
}));
278+
}
268279
}

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/turtles/TurtleChatBoxUpgrade.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,4 @@ public ModelResourceLocation getRightModel() {
2929
protected ChatBoxPeripheral buildPeripheral(@NotNull ITurtleAccess turtle, @NotNull TurtleSide side) {
3030
return new ChatBoxPeripheral(turtle, side);
3131
}
32-
33-
@Override
34-
public void update(@NotNull ITurtleAccess turtle, @NotNull TurtleSide side) {
35-
super.update(turtle, side);
36-
if (turtle.getLevel().isClientSide)
37-
return;
38-
39-
if (turtle.getPeripheral(side) instanceof ChatBoxPeripheral chatBox) {
40-
chatBox.update();
41-
}
42-
}
4332
}

src/main/java/de/srendi/advancedperipherals/common/blocks/base/BaseDetectorEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public void load(@NotNull CompoundTag nbt) {
136136

137137
@Override
138138
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
139-
if (!level.isClientSide) {
139+
super.handleTick(level, state, type);
140+
if (!level.isClientSide()) {
140141
this.transferRate = this.proxy.getAndResetTransfered();
141142
}
142143
}

src/main/java/de/srendi/advancedperipherals/common/blocks/base/PeripheralBlockEntity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import net.minecraft.world.item.ItemStack;
2323
import net.minecraft.world.level.Level;
2424
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
25+
import net.minecraft.world.level.block.entity.BlockEntity;
2526
import net.minecraft.world.level.block.entity.BlockEntityType;
2627
import net.minecraft.world.level.block.state.BlockState;
2728
import net.minecraftforge.common.capabilities.Capability;
@@ -243,4 +244,9 @@ public ComputerSide getComputerSide(Direction direction) {
243244
FrontAndTop orientation = getBlockState().getValue(BaseBlock.ORIENTATION);
244245
return CoordUtil.getComputerSide(orientation, direction);
245246
}
247+
248+
@Override
249+
public <U extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<U> type) {
250+
this.getLazyPeripheral().ifPresent(BasePeripheral<?>::update);
251+
}
246252
}

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/ChatBoxEntity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import de.srendi.advancedperipherals.common.blocks.base.PeripheralBlockEntity;
55
import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes;
66
import net.minecraft.core.BlockPos;
7-
import net.minecraft.world.level.Level;
8-
import net.minecraft.world.level.block.entity.BlockEntity;
9-
import net.minecraft.world.level.block.entity.BlockEntityType;
107
import net.minecraft.world.level.block.state.BlockState;
118
import org.jetbrains.annotations.NotNull;
129

@@ -21,9 +18,4 @@ public ChatBoxEntity(BlockPos pos, BlockState state) {
2118
protected ChatBoxPeripheral createPeripheral() {
2219
return new ChatBoxPeripheral(this);
2320
}
24-
25-
@Override
26-
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
27-
this.getLazyPeripheral().ifPresent(ChatBoxPeripheral::update);
28-
}
2921
}

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import net.minecraft.core.Direction;
1010
import net.minecraft.nbt.CompoundTag;
1111
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
12-
import net.minecraft.world.level.Level;
13-
import net.minecraft.world.level.block.entity.BlockEntity;
14-
import net.minecraft.world.level.block.entity.BlockEntityType;
1512
import net.minecraft.world.level.block.state.BlockState;
1613
import net.minecraft.world.phys.AABB;
1714
import net.minecraft.world.phys.Vec3;
@@ -84,22 +81,6 @@ public void setDetectionType(DistanceDetectorPeripheral.DetectionType detectionT
8481
this.detectionType = detectionType;
8582
}
8683

87-
@Override
88-
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
89-
DistanceDetectorPeripheral peripheral = this.getPeripheral();
90-
if (peripheral == null) {
91-
return;
92-
}
93-
94-
if (level.getGameTime() % APConfig.PERIPHERALS_CONFIG.distanceDetectorUpdateRate.get() == 0 && this.getCalculatePeriodically()) {
95-
// We calculate the distance every 2 ticks, so we do not have to run the getDistance function of the peripheral
96-
// on the main thread which prevents the 1 tick yield time of the function.
97-
// The calculateDistance function is not thread safe, so we have to run it on the main thread.
98-
// It should be okay to run that function every 2 ticks, calculating it does not take too much time.
99-
peripheral.calculateAndUpdateDistance();
100-
}
101-
}
102-
10384
@Override
10485
public AABB getRenderBoundingBox() {
10586
float currentDistance = this.getCurrentDistance();

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/MeBridgeEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected MeBridgePeripheral createPeripheral() {
5454

5555
@Override
5656
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
57-
if (!this.level.isClientSide) {
57+
super.handleTick(level, state, type);
58+
if (!this.level.isClientSide()) {
5859
if (!initialized) {
5960
MeBridgePeripheral peripheral = this.getPeripheral();
6061
if (peripheral == null) {

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/PlayerDetectorEntity.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,19 @@
22

33
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.PlayerDetectorPeripheral;
44
import de.srendi.advancedperipherals.common.blocks.base.PeripheralBlockEntity;
5-
import de.srendi.advancedperipherals.common.events.Events;
65
import de.srendi.advancedperipherals.common.setup.APBlockEntityTypes;
76
import net.minecraft.core.BlockPos;
8-
import net.minecraft.world.level.Level;
9-
import net.minecraft.world.level.block.entity.BlockEntity;
10-
import net.minecraft.world.level.block.entity.BlockEntityType;
117
import net.minecraft.world.level.block.state.BlockState;
128
import org.jetbrains.annotations.NotNull;
139

1410
public class PlayerDetectorEntity extends PeripheralBlockEntity<PlayerDetectorPeripheral> {
15-
private Long lastConsumedMessage;
16-
1711
public PlayerDetectorEntity(BlockPos pos, BlockState state) {
1812
super(APBlockEntityTypes.PLAYER_DETECTOR.get(), pos, state);
19-
lastConsumedMessage = Events.getLastPlayerMessageID() - 1;
2013
}
2114

2215
@NotNull
2316
@Override
2417
protected PlayerDetectorPeripheral createPeripheral() {
2518
return new PlayerDetectorPeripheral(this);
2619
}
27-
28-
@Override
29-
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
30-
lastConsumedMessage = Events.traversePlayerMessages(lastConsumedMessage, message -> getConnectedComputers().forEach(computer -> {
31-
if(message.eventName().equals("playerChangedDimension")) {
32-
computer.queueEvent(message.eventName(), message.playerName(), message.fromDimension(), message.toDimension());
33-
} else computer.queueEvent(message.eventName(), message.playerName(), message.fromDimension());
34-
}));
35-
}
3620
}

0 commit comments

Comments
 (0)