1
0
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:
Jonathan Coates 2024-07-28 21:15:25 +01:00
parent ed0b156e05
commit cbe075b001
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 51 additions and 27 deletions

View File

@ -9,8 +9,10 @@ import dan200.computercraft.api.upgrades.UpgradeBase;
import dan200.computercraft.api.upgrades.UpgradeData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.ApiStatus;
import javax.annotation.Nullable;
@ -21,6 +23,20 @@ import java.util.Map;
*/
@ApiStatus.NonExtendable
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.
* <p>

View File

@ -16,8 +16,10 @@ import dan200.computercraft.shared.network.server.ServerNetworking;
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
import java.util.Collections;
@ -34,6 +36,7 @@ public final class PocketBrain implements IPocketAccess {
private final PocketServerComputer computer;
private PocketHolder holder;
private Vec3 position;
private boolean dirty = false;
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) {
this.computer = new PocketServerComputer(this, holder, computerID, label, family);
this.holder = holder;
this.position = holder.pos();
this.upgrade = UpgradeData.copyOf(upgrade);
invalidatePeripheral();
}
@ -66,6 +70,7 @@ public final class PocketBrain implements IPocketAccess {
* @param newHolder The new holder
*/
public void updateHolder(PocketHolder newHolder) {
position = newHolder.pos();
computer.setPosition(newHolder.level(), newHolder.blockPos());
var oldHolder = this.holder;
@ -94,6 +99,18 @@ public final class PocketBrain implements IPocketAccess {
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
public @Nullable Entity getEntity() {
return holder instanceof PocketHolder.EntityHolder entity && holder.isValid(computer) ? entity.entity() : null;

View File

@ -11,6 +11,7 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.phys.Vec3;
/**
* An object that holds a pocket computer item.
@ -23,6 +24,13 @@ public sealed interface PocketHolder permits PocketHolder.EntityHolder {
*/
ServerLevel level();
/**
* The position of this holder.
*
* @return The position of this holder.
*/
Vec3 pos();
/**
* The block position of this holder.
*
@ -59,6 +67,11 @@ public sealed interface PocketHolder permits PocketHolder.EntityHolder {
return (ServerLevel) entity().level();
}
@Override
default Vec3 pos() {
return entity().getEyePosition();
}
@Override
default BlockPos blockPos() {
return entity().blockPosition();

View File

@ -31,8 +31,6 @@ public class PocketModem extends AbstractPocketUpgrade {
public void update(IPocketAccess access, @Nullable IPeripheral peripheral) {
if (!(peripheral instanceof PocketModemPeripheral modem)) return;
modem.setLocation(access);
var state = modem.getModemState();
if (state.pollChanged()) access.setLight(state.isOpen() ? 0xBA0000 : -1);
}

View File

@ -14,31 +14,21 @@ import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
public class PocketModemPeripheral extends WirelessModemPeripheral {
private @Nullable Level level = null;
private Vec3 position = Vec3.ZERO;
private final IPocketAccess access;
public PocketModemPeripheral(boolean advanced, IPocketAccess access) {
super(new ModemState(), advanced);
setLocation(access);
}
void setLocation(IPocketAccess access) {
var entity = access.getEntity();
if (entity != null) {
level = entity.level();
position = entity.getEyePosition(1);
}
this.access = access;
}
@Override
public Level getLevel() {
if (level == null) throw new IllegalStateException("Using modem before position has been defined");
return level;
return access.getLevel();
}
@Override
public Vec3 getPosition() {
return position;
return access.getPosition();
}
@Override

View File

@ -8,15 +8,11 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.pocket.IPocketAccess;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
import dan200.computercraft.shared.peripheral.speaker.UpgradeSpeakerPeripheral;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import javax.annotation.Nullable;
public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
private final IPocketAccess access;
private @Nullable Level level;
private Vec3 position = Vec3.ZERO;
public PocketSpeakerPeripheral(IPocketAccess access) {
this.access = access;
@ -25,7 +21,7 @@ public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
@Override
public SpeakerPosition getPosition() {
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
@ -35,12 +31,6 @@ public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
@Override
public void update() {
var entity = access.getEntity();
if (entity != null) {
level = entity.level();
position = entity.position();
}
super.update();
access.setLight(madeSound() ? 0x3320fc : -1);