mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-03-13 06:58:12 +00:00
Only send pocket computer updates to players in range
Previously we sent it to all players in the current level. This updates the check to only send it to players tracking the current chunk.
This commit is contained in:
parent
6c8e64ffcd
commit
70a31855ac
@ -28,9 +28,12 @@ import net.minecraft.world.entity.LivingEntity;
|
|||||||
import net.minecraft.world.entity.item.ItemEntity;
|
import net.minecraft.world.entity.item.ItemEntity;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
|
import net.minecraft.world.level.ChunkPos;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.*;
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class PocketServerComputer extends ServerComputer implements IPocketAccess {
|
public class PocketServerComputer extends ServerComputer implements IPocketAccess {
|
||||||
private @Nullable IPocketUpgrade upgrade;
|
private @Nullable IPocketUpgrade upgrade;
|
||||||
@ -43,7 +46,7 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
|||||||
private int oldLightColour = -1;
|
private int oldLightColour = -1;
|
||||||
private @Nullable ComputerState oldComputerState;
|
private @Nullable ComputerState oldComputerState;
|
||||||
|
|
||||||
private final Set<ServerPlayer> tracking = new HashSet<>();
|
private Set<ServerPlayer> tracking = Set.of();
|
||||||
|
|
||||||
public PocketServerComputer(ServerLevel world, BlockPos position, int computerID, @Nullable String label, ComputerFamily family) {
|
public PocketServerComputer(ServerLevel world, BlockPos position, int computerID, @Nullable String label, ComputerFamily family) {
|
||||||
super(world, position, computerID, label, family, Config.pocketTermWidth, Config.pocketTermHeight);
|
super(world, position, computerID, label, family, Config.pocketTermWidth, Config.pocketTermHeight);
|
||||||
@ -152,8 +155,9 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
|||||||
protected void tickServer() {
|
protected void tickServer() {
|
||||||
super.tickServer();
|
super.tickServer();
|
||||||
|
|
||||||
// Find any players which have gone missing and remove them from the tracking list.
|
// Get the new set of players tracking the current position.
|
||||||
tracking.removeIf(player -> !player.isAlive() || player.level() != getLevel());
|
var newTracking = getLevel().getChunkSource().chunkMap.getPlayers(new ChunkPos(getPosition()), false);
|
||||||
|
var trackingChanged = tracking.size() != newTracking.size() || !tracking.containsAll(newTracking);
|
||||||
|
|
||||||
// And now find any new players, add them to the tracking list, and broadcast state where appropriate.
|
// And now find any new players, add them to the tracking list, and broadcast state where appropriate.
|
||||||
var state = getState();
|
var state = getState();
|
||||||
@ -162,18 +166,16 @@ public class PocketServerComputer extends ServerComputer implements IPocketAcces
|
|||||||
oldLightColour = lightColour;
|
oldLightColour = lightColour;
|
||||||
|
|
||||||
// Broadcast the state to all players
|
// Broadcast the state to all players
|
||||||
tracking.addAll(getLevel().players());
|
ServerNetworking.sendToPlayers(new PocketComputerDataMessage(this, false), newTracking);
|
||||||
ServerNetworking.sendToPlayers(new PocketComputerDataMessage(this, false), tracking);
|
} else if (trackingChanged) {
|
||||||
} else {
|
|
||||||
// Broadcast the state to new players.
|
// Broadcast the state to new players.
|
||||||
List<ServerPlayer> added = new ArrayList<>();
|
var added = newTracking.stream().filter(x -> !tracking.contains(x)).toList();
|
||||||
for (var player : getLevel().players()) {
|
|
||||||
if (tracking.add(player)) added.add(player);
|
|
||||||
}
|
|
||||||
if (!added.isEmpty()) {
|
if (!added.isEmpty()) {
|
||||||
ServerNetworking.sendToPlayers(new PocketComputerDataMessage(this, false), added);
|
ServerNetworking.sendToPlayers(new PocketComputerDataMessage(this, false), added);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (trackingChanged) tracking = Set.copyOf(newTracking);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user