1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-18 15:37:38 +00:00

Don't use SpeakerPosition to get speaker's level

We use getLevel() specifically for reading the current registries
(and/or server). We don't need the exact position of the speaker to
query this, so add a dedicated method for it.

We actually had a similar method on 1.21.7 already for upgrades. This
just moves it to SpeakerPeripheral.

Fixes #2236.
This commit is contained in:
Jonathan Coates
2025-07-10 01:07:51 +01:00
parent 5fa724ed24
commit 2bf0aba455
5 changed files with 34 additions and 13 deletions

View File

@@ -9,6 +9,7 @@ import dan200.computercraft.core.util.Nullability;
import dan200.computercraft.shared.network.client.SpeakerStopClientMessage;
import dan200.computercraft.shared.network.server.ServerNetworking;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
@@ -47,7 +48,12 @@ public class SpeakerBlockEntity extends BlockEntity {
}
@Override
public SpeakerPosition getPosition() {
protected ServerLevel getLevel() {
return (ServerLevel) speaker.getLevel();
}
@Override
protected SpeakerPosition getPosition() {
return SpeakerPosition.of(speaker.getLevel(), Vec3.atCenterOf(speaker.getBlockPos()));
}

View File

@@ -32,7 +32,10 @@ import net.minecraft.util.Mth;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import org.jspecify.annotations.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import static dan200.computercraft.api.lua.LuaValues.checkFinite;
@@ -155,7 +158,9 @@ public abstract class SpeakerPeripheral implements IPeripheral {
}
}
public abstract SpeakerPosition getPosition();
protected abstract ServerLevel getLevel();
protected abstract SpeakerPosition getPosition();
public UUID getSource() {
return source;
@@ -255,8 +260,8 @@ public abstract class SpeakerPeripheral implements IPeripheral {
// Prevent playing music discs.
var soundEvent = BuiltInRegistries.SOUND_EVENT.get(identifier);
// TODO: Build a set of sound events at server startup, and cache this.
var level = Objects.requireNonNull(getPosition().level());
if (soundEvent != null && level.registryAccess().registry(Registries.JUKEBOX_SONG).orElseThrow().stream().anyMatch(x -> x.soundEvent().value() == soundEvent)) {
var level = getLevel();
if (soundEvent != null && level.registryAccess().registryOrThrow(Registries.JUKEBOX_SONG).stream().anyMatch(x -> x.soundEvent().value() == soundEvent)) {
return false;
}

View File

@@ -20,10 +20,8 @@ public abstract class UpgradeSpeakerPeripheral extends SpeakerPeripheral {
super.detach(computer);
// We could be in the process of shutting down the server, so we can't send packets in this case.
var level = getPosition().level();
if (level == null) return;
var server = level.getServer();
if (server == null || server.isStopped()) return;
var server = getLevel().getServer();
if (server.isStopped()) return;
ServerNetworking.sendToAllPlayers(new SpeakerStopClientMessage(getSource()), server);
}

View File

@@ -8,17 +8,23 @@ 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.server.level.ServerLevel;
import org.jspecify.annotations.Nullable;
public class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
final class PocketSpeakerPeripheral extends UpgradeSpeakerPeripheral {
private final IPocketAccess access;
public PocketSpeakerPeripheral(IPocketAccess access) {
PocketSpeakerPeripheral(IPocketAccess access) {
this.access = access;
}
@Override
public SpeakerPosition getPosition() {
protected ServerLevel getLevel() {
return access.getLevel();
}
@Override
protected SpeakerPosition getPosition() {
var entity = access.getEntity();
return entity == null ? SpeakerPosition.of(access.getLevel(), access.getPosition()) : SpeakerPosition.of(entity);
}

View File

@@ -13,6 +13,7 @@ import dan200.computercraft.api.upgrades.UpgradeType;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
import dan200.computercraft.shared.peripheral.speaker.UpgradeSpeakerPeripheral;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import org.jspecify.annotations.Nullable;
@@ -26,7 +27,12 @@ public class TurtleSpeaker extends AbstractTurtleUpgrade {
}
@Override
public SpeakerPosition getPosition() {
protected ServerLevel getLevel() {
return (ServerLevel) turtle.getLevel();
}
@Override
protected SpeakerPosition getPosition() {
return SpeakerPosition.of(turtle.getLevel(), Vec3.atCenterOf(turtle.getPosition()));
}