From 2c59b9122bda31f48b68af1702ede5e8e0e002e3 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Thu, 29 Dec 2022 08:41:29 +0000 Subject: [PATCH] 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. --- .../shared/pocket/peripherals/PocketModem.java | 6 ++---- .../pocket/peripherals/PocketModemPeripheral.java | 13 +++++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java index 7822a9525..11c1bf839 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModem.java @@ -25,16 +25,14 @@ public PocketModem(ResourceLocation id, ItemStack stack, boolean advanced) { @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); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModemPeripheral.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModemPeripheral.java index 43671d0fa..c858c4cf0 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModemPeripheral.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/peripherals/PocketModemPeripheral.java @@ -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