mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-10 01:10:30 +00:00
Expose level+position in IPocketAccess
This allows pocket upgrades (modems and speakers) to read the position directly, rather than checking whether the entity is present.
This commit is contained in:
parent
ed0b156e05
commit
cbe075b001
@ -9,8 +9,10 @@ import dan200.computercraft.api.upgrades.UpgradeBase;
|
|||||||
import dan200.computercraft.api.upgrades.UpgradeData;
|
import dan200.computercraft.api.upgrades.UpgradeData;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@ -21,6 +23,20 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
@ApiStatus.NonExtendable
|
@ApiStatus.NonExtendable
|
||||||
public interface IPocketAccess {
|
public interface IPocketAccess {
|
||||||
|
/**
|
||||||
|
* Get the level in which the pocket computer exists.
|
||||||
|
*
|
||||||
|
* @return The pocket computer's level.
|
||||||
|
*/
|
||||||
|
ServerLevel getLevel();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the position of the pocket computer.
|
||||||
|
*
|
||||||
|
* @return The pocket computer's position.
|
||||||
|
*/
|
||||||
|
Vec3 getPosition();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the entity holding this item.
|
* Gets the entity holding this item.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -16,8 +16,10 @@ import dan200.computercraft.shared.network.server.ServerNetworking;
|
|||||||
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
|
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
import net.minecraft.server.level.ServerLevel;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -34,6 +36,7 @@ public final class PocketBrain implements IPocketAccess {
|
|||||||
private final PocketServerComputer computer;
|
private final PocketServerComputer computer;
|
||||||
|
|
||||||
private PocketHolder holder;
|
private PocketHolder holder;
|
||||||
|
private Vec3 position;
|
||||||
|
|
||||||
private boolean dirty = false;
|
private boolean dirty = false;
|
||||||
private @Nullable UpgradeData<IPocketUpgrade> upgrade;
|
private @Nullable UpgradeData<IPocketUpgrade> upgrade;
|
||||||
@ -43,6 +46,7 @@ public final class PocketBrain implements IPocketAccess {
|
|||||||
public PocketBrain(PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family, @Nullable UpgradeData<IPocketUpgrade> upgrade) {
|
public PocketBrain(PocketHolder holder, int computerID, @Nullable String label, ComputerFamily family, @Nullable UpgradeData<IPocketUpgrade> upgrade) {
|
||||||
this.computer = new PocketServerComputer(this, holder, computerID, label, family);
|
this.computer = new PocketServerComputer(this, holder, computerID, label, family);
|
||||||
this.holder = holder;
|
this.holder = holder;
|
||||||
|
this.position = holder.pos();
|
||||||
this.upgrade = UpgradeData.copyOf(upgrade);
|
this.upgrade = UpgradeData.copyOf(upgrade);
|
||||||
invalidatePeripheral();
|
invalidatePeripheral();
|
||||||
}
|
}
|
||||||
@ -66,6 +70,7 @@ public final class PocketBrain implements IPocketAccess {
|
|||||||
* @param newHolder The new holder
|
* @param newHolder The new holder
|
||||||
*/
|
*/
|
||||||
public void updateHolder(PocketHolder newHolder) {
|
public void updateHolder(PocketHolder newHolder) {
|
||||||
|
position = newHolder.pos();
|
||||||
computer.setPosition(newHolder.level(), newHolder.blockPos());
|
computer.setPosition(newHolder.level(), newHolder.blockPos());
|
||||||
|
|
||||||
var oldHolder = this.holder;
|
var oldHolder = this.holder;
|
||||||
@ -94,6 +99,18 @@ public final class PocketBrain implements IPocketAccess {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerLevel getLevel() {
|
||||||
|
return computer.getLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Vec3 getPosition() {
|
||||||
|
// This method can be called from off-thread, and so we must use the cached position rather than rereading
|
||||||
|
// from the holder.
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Entity getEntity() {
|
public @Nullable Entity getEntity() {
|
||||||
return holder instanceof PocketHolder.EntityHolder entity && holder.isValid(computer) ? entity.entity() : null;
|
return holder instanceof PocketHolder.EntityHolder entity && holder.isValid(computer) ? entity.entity() : null;
|
||||||
|
@ -11,6 +11,7 @@ import net.minecraft.server.level.ServerLevel;
|
|||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
import net.minecraft.world.entity.Entity;
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.entity.item.ItemEntity;
|
import net.minecraft.world.entity.item.ItemEntity;
|
||||||
|
import net.minecraft.world.phys.Vec3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object that holds a pocket computer item.
|
* An object that holds a pocket computer item.
|
||||||
@ -23,6 +24,13 @@ public sealed interface PocketHolder permits PocketHolder.EntityHolder {
|
|||||||
*/
|
*/
|
||||||
ServerLevel level();
|
ServerLevel level();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The position of this holder.
|
||||||
|
*
|
||||||
|
* @return The position of this holder.
|
||||||
|
*/
|
||||||
|
Vec3 pos();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The block position of this holder.
|
* The block position of this holder.
|
||||||
*
|
*
|
||||||
@ -59,6 +67,11 @@ public sealed interface PocketHolder permits PocketHolder.EntityHolder {
|
|||||||
return (ServerLevel) entity().level();
|
return (ServerLevel) entity().level();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default Vec3 pos() {
|
||||||
|
return entity().getEyePosition();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default BlockPos blockPos() {
|
default BlockPos blockPos() {
|
||||||
return entity().blockPosition();
|
return entity().blockPosition();
|
||||||
|
@ -31,8 +31,6 @@ public class PocketModem extends AbstractPocketUpgrade {
|
|||||||
public void update(IPocketAccess access, @Nullable IPeripheral peripheral) {
|
public void update(IPocketAccess access, @Nullable IPeripheral peripheral) {
|
||||||
if (!(peripheral instanceof PocketModemPeripheral modem)) return;
|
if (!(peripheral instanceof PocketModemPeripheral modem)) return;
|
||||||
|
|
||||||
modem.setLocation(access);
|
|
||||||
|
|
||||||
var state = modem.getModemState();
|
var state = modem.getModemState();
|
||||||
if (state.pollChanged()) access.setLight(state.isOpen() ? 0xBA0000 : -1);
|
if (state.pollChanged()) access.setLight(state.isOpen() ? 0xBA0000 : -1);
|
||||||
}
|
}
|
||||||
|
@ -14,31 +14,21 @@ import net.minecraft.world.phys.Vec3;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class PocketModemPeripheral extends WirelessModemPeripheral {
|
public class PocketModemPeripheral extends WirelessModemPeripheral {
|
||||||
private @Nullable Level level = null;
|
private final IPocketAccess access;
|
||||||
private Vec3 position = Vec3.ZERO;
|
|
||||||
|
|
||||||
public PocketModemPeripheral(boolean advanced, IPocketAccess access) {
|
public PocketModemPeripheral(boolean advanced, IPocketAccess access) {
|
||||||
super(new ModemState(), advanced);
|
super(new ModemState(), advanced);
|
||||||
setLocation(access);
|
this.access = access;
|
||||||
}
|
|
||||||
|
|
||||||
void setLocation(IPocketAccess access) {
|
|
||||||
var entity = access.getEntity();
|
|
||||||
if (entity != null) {
|
|
||||||
level = entity.level();
|
|
||||||
position = entity.getEyePosition(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Level getLevel() {
|
public Level getLevel() {
|
||||||
if (level == null) throw new IllegalStateException("Using modem before position has been defined");
|
return access.getLevel();
|
||||||
return level;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vec3 getPosition() {
|
public Vec3 getPosition() {
|
||||||
return position;
|
return access.getPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -8,15 +8,11 @@ import dan200.computercraft.api.peripheral.IPeripheral;
|
|||||||
import dan200.computercraft.api.pocket.IPocketAccess;
|
import dan200.computercraft.api.pocket.IPocketAccess;
|
||||||
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
|
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
|
||||||
import dan200.computercraft.shared.peripheral.speaker.UpgradeSpeakerPeripheral;
|
import dan200.computercraft.shared.peripheral.speaker.UpgradeSpeakerPeripheral;
|
||||||
import net.minecraft.world.level.Level;
|
|
||||||
import net.minecraft.world.phys.Vec3;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
|
public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
|
||||||
private final IPocketAccess access;
|
private final IPocketAccess access;
|
||||||
private @Nullable Level level;
|
|
||||||
private Vec3 position = Vec3.ZERO;
|
|
||||||
|
|
||||||
public PocketSpeakerPeripheral(IPocketAccess access) {
|
public PocketSpeakerPeripheral(IPocketAccess access) {
|
||||||
this.access = access;
|
this.access = access;
|
||||||
@ -25,7 +21,7 @@ public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
|
|||||||
@Override
|
@Override
|
||||||
public SpeakerPosition getPosition() {
|
public SpeakerPosition getPosition() {
|
||||||
var entity = access.getEntity();
|
var entity = access.getEntity();
|
||||||
return entity == null ? SpeakerPosition.of(level, position) : SpeakerPosition.of(entity);
|
return entity == null ? SpeakerPosition.of(access.getLevel(), access.getPosition()) : SpeakerPosition.of(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -35,12 +31,6 @@ public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
var entity = access.getEntity();
|
|
||||||
if (entity != null) {
|
|
||||||
level = entity.level();
|
|
||||||
position = entity.position();
|
|
||||||
}
|
|
||||||
|
|
||||||
super.update();
|
super.update();
|
||||||
|
|
||||||
access.setLight(madeSound() ? 0x3320fc : -1);
|
access.setLight(madeSound() ? 0x3320fc : -1);
|
||||||
|
Loading…
Reference in New Issue
Block a user