1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-15 04:30:29 +00:00

Set location when creating a pocket modem

We now call getLevel() when attaching the peripheral, so need the
position to be available immediately. Fixes #1274.

I /think/ the entity should always be present, as peripherals are
only created on startup or when calling pocket.equipBack, both of which
require a player.[^1]

I suspect this was a little broken before (the level wouldn't be
available if a modem received a message before the position had
been set), but that would be much rarer.

I'm not 100% convinced about the thread-safety of this code (the writes
to level may not be immediately visible on other threads), so may need
to think about that.

[^1]: Note that when peripherals come to be /attached/ they may no
longer have a player (as there's a gap between turning a computer on and
it actually starting). However, the level/position will have been
initialised by then, so this isn't a problem.
This commit is contained in:
Jonathan Coates 2022-12-29 08:41:29 +00:00
parent d2c7b944ab
commit 2c59b9122b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 11 additions and 8 deletions

View File

@ -25,16 +25,14 @@ public class PocketModem extends AbstractPocketUpgrade {
@Nullable
@Override
public IPeripheral createPeripheral(IPocketAccess access) {
return new PocketModemPeripheral(advanced);
return new PocketModemPeripheral(advanced, access);
}
@Override
public void update(IPocketAccess access, @Nullable IPeripheral peripheral) {
if (!(peripheral instanceof PocketModemPeripheral modem)) return;
var entity = access.getEntity();
if (entity != null) modem.setLocation(entity.getCommandSenderWorld(), entity.getEyePosition(1));
modem.setLocation(access);
var state = modem.getModemState();
if (state.pollChanged()) access.setLight(state.isOpen() ? 0xBA0000 : -1);

View File

@ -6,6 +6,7 @@
package dan200.computercraft.shared.pocket.peripherals;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.pocket.IPocketAccess;
import dan200.computercraft.shared.peripheral.modem.ModemState;
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessModemPeripheral;
import net.minecraft.world.level.Level;
@ -17,13 +18,17 @@ public class PocketModemPeripheral extends WirelessModemPeripheral {
private @Nullable Level level = null;
private Vec3 position = Vec3.ZERO;
public PocketModemPeripheral(boolean advanced) {
public PocketModemPeripheral(boolean advanced, IPocketAccess access) {
super(new ModemState(), advanced);
setLocation(access);
}
void setLocation(Level level, Vec3 position) {
this.position = position;
this.level = level;
void setLocation(IPocketAccess access) {
var entity = access.getEntity();
if (entity != null) {
level = entity.getCommandSenderWorld();
position = entity.getEyePosition(1);
}
}
@Override