SteamWar/SpigotCore
Archiviert
13
0

Fix changing view distance
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Lixfel 2023-01-17 12:04:57 +01:00
Ursprung d073d3abde
Commit 42fdc0a3dc

Datei anzeigen

@ -30,7 +30,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -46,6 +45,7 @@ public class REntityServer implements Listener {
private final HashMap<Long, HashSet<REntity>> entities = new HashMap<>(); private final HashMap<Long, HashSet<REntity>> entities = new HashMap<>();
private final HashMap<Long, Set<Player>> players = new HashMap<>(); private final HashMap<Long, Set<Player>> players = new HashMap<>();
private final HashMap<Player, Location> lastLocation = new HashMap<>(); private final HashMap<Player, Location> lastLocation = new HashMap<>();
private final HashMap<Player, Integer> viewDistance = new HashMap<>();
public REntityServer() { public REntityServer() {
Core.getInstance().getServer().getPluginManager().registerEvents(this, Core.getInstance()); Core.getInstance().getServer().getPluginManager().registerEvents(this, Core.getInstance());
@ -54,12 +54,13 @@ public class REntityServer implements Listener {
public void addPlayer(Player player) { public void addPlayer(Player player) {
Location location = player.getLocation(); Location location = player.getLocation();
lastLocation.put(player, location); lastLocation.put(player, location);
viewDistance.put(player, viewRadius(player));
forChunkInView(player, location, (x, z) -> addPlayerToChunk(player, x, z)); forChunkInView(player, location, (x, z) -> addPlayerToChunk(player, x, z));
} }
public void removePlayer(Player player) { public void removePlayer(Player player) {
Location location = lastLocation.remove(player); forChunkInView(player, lastLocation.remove(player), (x, z) -> removePlayerFromChunk(player, x, z));
forChunkInView(player, location, (x, z) -> removePlayerFromChunk(player, x, z)); viewDistance.remove(player);
} }
public void close() { public void close() {
@ -117,8 +118,6 @@ public class REntityServer implements Listener {
entities.remove(id); entities.remove(id);
} }
//TODO on settings, on respawn? on boatmove?
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
Player player = e.getPlayer(); Player player = e.getPlayer();
@ -135,14 +134,17 @@ public class REntityServer implements Listener {
return; return;
lastLocation.put(player, to); lastLocation.put(player, to);
int viewDistance = viewRadius(player);
int toViewDistance = viewRadius(player);
forChunkInView(player, from, (x, z) -> { forChunkInView(player, from, (x, z) -> {
if(Math.abs(x - toX) > viewDistance || Math.abs(z - toZ) > viewDistance) { if(Math.abs(x - toX) > toViewDistance || Math.abs(z - toZ) > toViewDistance) {
removePlayerFromChunk(player, x, z); removePlayerFromChunk(player, x, z);
} }
}); });
int fromViewDistance = this.viewDistance.put(player, toViewDistance);
forChunkInView(player, to, (x, z) -> { forChunkInView(player, to, (x, z) -> {
if(Math.abs(x - fromX) > viewDistance || Math.abs(z - fromZ) > viewDistance) { if(Math.abs(x - fromX) > fromViewDistance || Math.abs(z - fromZ) > fromViewDistance) {
addPlayerToChunk(player, x, z); addPlayerToChunk(player, x, z);
} }
}); });
@ -172,7 +174,7 @@ public class REntityServer implements Listener {
private void forChunkInView(Player player, Location location, BiConsumer<Integer, Integer> func) { private void forChunkInView(Player player, Location location, BiConsumer<Integer, Integer> func) {
int chunkX = posToChunk(location.getX()); int chunkX = posToChunk(location.getX());
int chunkZ = posToChunk(location.getZ()); int chunkZ = posToChunk(location.getZ());
int viewDistance = viewRadius(player); int viewDistance = this.viewDistance.get(player);
for(int x = chunkX - viewDistance; x <= chunkX + viewDistance; x++) { for(int x = chunkX - viewDistance; x <= chunkX + viewDistance; x++) {
for(int z = chunkZ - viewDistance; z <= chunkZ + viewDistance; z++) { for(int z = chunkZ - viewDistance; z <= chunkZ + viewDistance; z++) {
@ -191,7 +193,7 @@ public class REntityServer implements Listener {
private void removePlayerFromChunk(Player player, int x, int z) { private void removePlayerFromChunk(Player player, int x, int z) {
long id = chunkToId(x, z); long id = chunkToId(x, z);
players.getOrDefault(id, Collections.emptySet()).remove(player); players.get(id).remove(player);
for(REntity entity : entities.getOrDefault(id, emptyEntities)) { for(REntity entity : entities.getOrDefault(id, emptyEntities)) {
entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet)); entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet));
} }
@ -222,7 +224,6 @@ public class REntityServer implements Listener {
} }
private long chunkToId(int x, int z) { private long chunkToId(int x, int z) {
//TODO negative coord clash?
return (long) x << 32 + z; return (long) x << 32 + z;
} }
} }