aboutsummaryrefslogtreecommitdiffhomepage
path: root/patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch
diff options
context:
space:
mode:
Diffstat (limited to 'patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch')
-rw-r--r--patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch1599
1 files changed, 1599 insertions, 0 deletions
diff --git a/patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch b/patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch
new file mode 100644
index 0000000000..1db65bcf21
--- /dev/null
+++ b/patch-remap/mache-vineflower/net/minecraft/server/players/PlayerList.java.patch
@@ -0,0 +1,1599 @@
+--- a/net/minecraft/server/players/PlayerList.java
++++ b/net/minecraft/server/players/PlayerList.java
+@@ -5,15 +5,19 @@
+ import com.google.common.collect.Sets;
+ import com.mojang.authlib.GameProfile;
+ import com.mojang.logging.LogUtils;
++import com.mojang.serialization.DataResult;
+ import com.mojang.serialization.Dynamic;
+ import java.io.File;
+ import java.net.SocketAddress;
+ import java.nio.file.Path;
+ import java.text.SimpleDateFormat;
+ import java.time.Instant;
++import java.util.Collection;
+ import java.util.EnumSet;
++import java.util.Iterator;
+ import java.util.List;
+ import java.util.Map;
++import java.util.Objects;
+ import java.util.Optional;
+ import java.util.Set;
+ import java.util.UUID;
+@@ -70,6 +74,7 @@
+ import net.minecraft.server.level.ServerPlayer;
+ import net.minecraft.server.network.CommonListenerCookie;
+ import net.minecraft.server.network.ServerGamePacketListenerImpl;
++import net.minecraft.server.network.ServerLoginPacketListenerImpl;
+ import net.minecraft.sounds.SoundEvents;
+ import net.minecraft.sounds.SoundSource;
+ import net.minecraft.stats.ServerStatsCounter;
+@@ -80,11 +85,10 @@
+ import net.minecraft.world.effect.MobEffectInstance;
+ import net.minecraft.world.entity.Entity;
+ import net.minecraft.world.entity.EntityType;
+-import net.minecraft.world.entity.player.Player;
+ import net.minecraft.world.level.GameRules;
+ import net.minecraft.world.level.Level;
+ import net.minecraft.world.level.block.Blocks;
+-import net.minecraft.world.level.block.state.BlockState;
++import net.minecraft.world.level.block.state.IBlockData;
+ import net.minecraft.world.level.border.BorderChangeListener;
+ import net.minecraft.world.level.border.WorldBorder;
+ import net.minecraft.world.level.dimension.DimensionType;
+@@ -95,10 +99,29 @@
+ import net.minecraft.world.scores.DisplaySlot;
+ import net.minecraft.world.scores.Objective;
+ import net.minecraft.world.scores.PlayerTeam;
+-import net.minecraft.world.scores.Team;
+ import org.slf4j.Logger;
+
++// CraftBukkit start
++import java.util.stream.Collectors;
++import net.minecraft.server.dedicated.DedicatedServer;
++import org.bukkit.Location;
++import org.bukkit.craftbukkit.CraftServer;
++import org.bukkit.craftbukkit.CraftWorld;
++import org.bukkit.craftbukkit.entity.CraftPlayer;
++import org.bukkit.craftbukkit.util.CraftChatMessage;
++import org.bukkit.craftbukkit.util.CraftLocation;
++import org.bukkit.entity.Player;
++import org.bukkit.event.player.PlayerChangedWorldEvent;
++import org.bukkit.event.player.PlayerJoinEvent;
++import org.bukkit.event.player.PlayerLoginEvent;
++import org.bukkit.event.player.PlayerQuitEvent;
++import org.bukkit.event.player.PlayerRespawnEvent;
++import org.bukkit.event.player.PlayerRespawnEvent.RespawnReason;
++import org.bukkit.event.player.PlayerSpawnChangeEvent;
++// CraftBukkit end
++
+ public abstract class PlayerList {
++
+ public static final File USERBANLIST_FILE = new File("banned-players.json");
+ public static final File IPBANLIST_FILE = new File("banned-ips.json");
+ public static final File OPLIST_FILE = new File("ops.json");
+@@ -109,478 +132,801 @@
+ private static final int SEND_PLAYER_INFO_INTERVAL = 600;
+ private static final SimpleDateFormat BAN_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
+ private final MinecraftServer server;
+- private final List<ServerPlayer> players = Lists.newArrayList();
++ public final List<ServerPlayer> players = new java.util.concurrent.CopyOnWriteArrayList(); // CraftBukkit - ArrayList -> CopyOnWriteArrayList: Iterator safety
+ private final Map<UUID, ServerPlayer> playersByUUID = Maps.newHashMap();
+- private final UserBanList bans = new UserBanList(USERBANLIST_FILE);
+- private final IpBanList ipBans = new IpBanList(IPBANLIST_FILE);
+- private final ServerOpList ops = new ServerOpList(OPLIST_FILE);
+- private final UserWhiteList whitelist = new UserWhiteList(WHITELIST_FILE);
+- private final Map<UUID, ServerStatsCounter> stats = Maps.newHashMap();
+- private final Map<UUID, PlayerAdvancements> advancements = Maps.newHashMap();
+- private final PlayerDataStorage playerIo;
++ private final UserBanList bans;
++ private final IpBanList ipBans;
++ private final ServerOpList ops;
++ private final UserWhiteList whitelist;
++ // CraftBukkit start
++ // private final Map<UUID, ServerStatisticManager> stats;
++ // private final Map<UUID, AdvancementDataPlayer> advancements;
++ // CraftBukkit end
++ public final PlayerDataStorage playerIo;
+ private boolean doWhiteList;
+ private final LayeredRegistryAccess<RegistryLayer> registries;
+- protected final int maxPlayers;
++ public int maxPlayers;
+ private int viewDistance;
+ private int simulationDistance;
+ private boolean allowCheatsForAllPlayers;
+ private static final boolean ALLOW_LOGOUTIVATOR = false;
+ private int sendAllPlayerInfoIn;
+
++ // CraftBukkit start
++ private CraftServer cserver;
++
+ public PlayerList(MinecraftServer server, LayeredRegistryAccess<RegistryLayer> registries, PlayerDataStorage playerIo, int maxPlayers) {
++ this.cserver = server.server = new CraftServer((DedicatedServer) server, this);
++ server.console = org.bukkit.craftbukkit.command.ColouredConsoleSender.getInstance();
++ server.reader.addCompleter(new org.bukkit.craftbukkit.command.ConsoleCommandCompleter(server.server));
++ // CraftBukkit end
++
++ this.bans = new UserBanList(PlayerList.USERBANLIST_FILE);
++ this.ipBans = new IpBanList(PlayerList.IPBANLIST_FILE);
++ this.ops = new ServerOpList(PlayerList.OPLIST_FILE);
++ this.whitelist = new UserWhiteList(PlayerList.WHITELIST_FILE);
++ // CraftBukkit start
++ // this.stats = Maps.newHashMap();
++ // this.advancements = Maps.newHashMap();
++ // CraftBukkit end
+ this.server = server;
+ this.registries = registries;
+ this.maxPlayers = maxPlayers;
+ this.playerIo = playerIo;
+ }
+
+- public void placeNewPlayer(Connection connection, ServerPlayer serverPlayer, CommonListenerCookie commonListenerCookie) {
+- GameProfile gameProfile = serverPlayer.getGameProfile();
+- GameProfileCache profileCache = this.server.getProfileCache();
+- String string;
+- if (profileCache != null) {
+- Optional<GameProfile> optional = profileCache.get(gameProfile.getId());
+- string = optional.map(GameProfile::getName).orElse(gameProfile.getName());
+- profileCache.add(gameProfile);
++ public void placeNewPlayer(Connection networkmanager, ServerPlayer entityplayer, CommonListenerCookie commonlistenercookie) {
++ GameProfile gameprofile = entityplayer.getGameProfile();
++ GameProfileCache usercache = this.server.getProfileCache();
++ String s;
++
++ if (usercache != null) {
++ Optional<GameProfile> optional = usercache.get(gameprofile.getId());
++
++ s = (String) optional.map(GameProfile::getName).orElse(gameprofile.getName());
++ usercache.add(gameprofile);
+ } else {
+- string = gameProfile.getName();
++ s = gameprofile.getName();
+ }
+
+- CompoundTag compoundTag = this.load(serverPlayer);
+- ResourceKey<Level> resourceKey = compoundTag != null
+- ? DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, compoundTag.get("Dimension"))).resultOrPartial(LOGGER::error).orElse(Level.OVERWORLD)
+- : Level.OVERWORLD;
+- ServerLevel level = this.server.getLevel(resourceKey);
+- ServerLevel serverLevel;
+- if (level == null) {
+- LOGGER.warn("Unknown respawn dimension {}, defaulting to overworld", resourceKey);
+- serverLevel = this.server.overworld();
++ CompoundTag nbttagcompound = this.load(entityplayer);
++ ResourceKey resourcekey;
++ // CraftBukkit start - Better rename detection
++ if (nbttagcompound != null && nbttagcompound.contains("bukkit")) {
++ CompoundTag bukkit = nbttagcompound.getCompound("bukkit");
++ s = bukkit.contains("lastKnownName", 8) ? bukkit.getString("lastKnownName") : s;
++ }
++ // CraftBukkit end
++
++ if (nbttagcompound != null) {
++ DataResult<ResourceKey<Level>> dataresult = DimensionType.parseLegacy(new Dynamic(NbtOps.INSTANCE, nbttagcompound.get("Dimension"))); // CraftBukkit - decompile error
++ Logger logger = PlayerList.LOGGER;
++
++ Objects.requireNonNull(logger);
++ resourcekey = (ResourceKey) dataresult.resultOrPartial(logger::error).orElse(entityplayer.serverLevel().dimension()); // CraftBukkit - SPIGOT-7507: If no dimension, fall back to existing dimension loaded from "WorldUUID", which in turn defaults to World.OVERWORLD
+ } else {
+- serverLevel = level;
++ resourcekey = entityplayer.serverLevel().dimension(); // CraftBukkit - SPIGOT-7507: If no dimension, fall back to existing dimension loaded from "WorldUUID", which in turn defaults to World.OVERWORLD
+ }
+
+- serverPlayer.setServerLevel(serverLevel);
+- String loggableAddress = connection.getLoggableAddress(this.server.logIPs());
+- LOGGER.info(
+- "{}[{}] logged in with entity id {} at ({}, {}, {})",
+- serverPlayer.getName().getString(),
+- loggableAddress,
+- serverPlayer.getId(),
+- serverPlayer.getX(),
+- serverPlayer.getY(),
+- serverPlayer.getZ()
+- );
+- LevelData levelData = serverLevel.getLevelData();
+- serverPlayer.loadGameTypes(compoundTag);
+- ServerGamePacketListenerImpl serverGamePacketListenerImpl = new ServerGamePacketListenerImpl(
+- this.server, connection, serverPlayer, commonListenerCookie
+- );
+- GameRules gameRules = serverLevel.getGameRules();
+- boolean _boolean = gameRules.getBoolean(GameRules.RULE_DO_IMMEDIATE_RESPAWN);
+- boolean _boolean1 = gameRules.getBoolean(GameRules.RULE_REDUCEDDEBUGINFO);
+- boolean _boolean2 = gameRules.getBoolean(GameRules.RULE_LIMITED_CRAFTING);
+- serverGamePacketListenerImpl.send(
+- new ClientboundLoginPacket(
+- serverPlayer.getId(),
+- levelData.isHardcore(),
+- this.server.levelKeys(),
+- this.getMaxPlayers(),
+- this.viewDistance,
+- this.simulationDistance,
+- _boolean1,
+- !_boolean,
+- _boolean2,
+- serverPlayer.createCommonSpawnInfo(serverLevel)
+- )
+- );
+- serverGamePacketListenerImpl.send(new ClientboundChangeDifficultyPacket(levelData.getDifficulty(), levelData.isDifficultyLocked()));
+- serverGamePacketListenerImpl.send(new ClientboundPlayerAbilitiesPacket(serverPlayer.getAbilities()));
+- serverGamePacketListenerImpl.send(new ClientboundSetCarriedItemPacket(serverPlayer.getInventory().selected));
+- serverGamePacketListenerImpl.send(new ClientboundUpdateRecipesPacket(this.server.getRecipeManager().getRecipes()));
+- this.sendPlayerPermissionLevel(serverPlayer);
+- serverPlayer.getStats().markAllDirty();
+- serverPlayer.getRecipeBook().sendInitialRecipeBook(serverPlayer);
+- this.updateEntireScoreboard(serverLevel.getScoreboard(), serverPlayer);
++ ResourceKey<Level> resourcekey1 = resourcekey;
++ ServerLevel worldserver = this.server.getLevel(resourcekey1);
++ ServerLevel worldserver1;
++
++ if (worldserver == null) {
++ PlayerList.LOGGER.warn("Unknown respawn dimension {}, defaulting to overworld", resourcekey1);
++ worldserver1 = this.server.overworld();
++ } else {
++ worldserver1 = worldserver;
++ }
++
++ entityplayer.setServerLevel(worldserver1);
++ String s1 = networkmanager.getLoggableAddress(this.server.logIPs());
++
++ // CraftBukkit - Moved message to after join
++ // PlayerList.LOGGER.info("{}[{}] logged in with entity id {} at ({}, {}, {})", new Object[]{entityplayer.getName().getString(), s1, entityplayer.getId(), entityplayer.getX(), entityplayer.getY(), entityplayer.getZ()});
++ LevelData worlddata = worldserver1.getLevelData();
++
++ entityplayer.loadGameTypes(nbttagcompound);
++ ServerGamePacketListenerImpl playerconnection = new ServerGamePacketListenerImpl(this.server, networkmanager, entityplayer, commonlistenercookie);
++ GameRules gamerules = worldserver1.getGameRules();
++ boolean flag = gamerules.getBoolean(GameRules.RULE_DO_IMMEDIATE_RESPAWN);
++ boolean flag1 = gamerules.getBoolean(GameRules.RULE_REDUCEDDEBUGINFO);
++ boolean flag2 = gamerules.getBoolean(GameRules.RULE_LIMITED_CRAFTING);
++
++ playerconnection.send(new ClientboundLoginPacket(entityplayer.getId(), worlddata.isHardcore(), this.server.levelKeys(), this.getMaxPlayers(), this.viewDistance, this.simulationDistance, flag1, !flag, flag2, entityplayer.createCommonSpawnInfo(worldserver1)));
++ entityplayer.getBukkitEntity().sendSupportedChannels(); // CraftBukkit
++ playerconnection.send(new ClientboundChangeDifficultyPacket(worlddata.getDifficulty(), worlddata.isDifficultyLocked()));
++ playerconnection.send(new ClientboundPlayerAbilitiesPacket(entityplayer.getAbilities()));
++ playerconnection.send(new ClientboundSetCarriedItemPacket(entityplayer.getInventory().selected));
++ playerconnection.send(new ClientboundUpdateRecipesPacket(this.server.getRecipeManager().getRecipes()));
++ this.sendPlayerPermissionLevel(entityplayer);
++ entityplayer.getStats().markAllDirty();
++ entityplayer.getRecipeBook().sendInitialRecipeBook(entityplayer);
++ this.updateEntireScoreboard(worldserver1.getScoreboard(), entityplayer);
+ this.server.invalidateStatus();
+- MutableComponent mutableComponent;
+- if (serverPlayer.getGameProfile().getName().equalsIgnoreCase(string)) {
+- mutableComponent = Component.translatable("multiplayer.player.joined", serverPlayer.getDisplayName());
++ MutableComponent ichatmutablecomponent;
++
++ if (entityplayer.getGameProfile().getName().equalsIgnoreCase(s)) {
++ ichatmutablecomponent = Component.translatable("multiplayer.player.joined", entityplayer.getDisplayName());
+ } else {
+- mutableComponent = Component.translatable("multiplayer.player.joined.renamed", serverPlayer.getDisplayName(), string);
++ ichatmutablecomponent = Component.translatable("multiplayer.player.joined.renamed", entityplayer.getDisplayName(), s);
+ }
++ // CraftBukkit start
++ ichatmutablecomponent.withStyle(ChatFormatting.YELLOW);
++ String joinMessage = CraftChatMessage.fromComponent(ichatmutablecomponent);
+
+- this.broadcastSystemMessage(mutableComponent.withStyle(ChatFormatting.YELLOW), false);
+- serverGamePacketListenerImpl.teleport(serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), serverPlayer.getYRot(), serverPlayer.getXRot());
+- ServerStatus status = this.server.getStatus();
+- if (status != null) {
+- serverPlayer.sendServerStatus(status);
++ playerconnection.teleport(entityplayer.getX(), entityplayer.getY(), entityplayer.getZ(), entityplayer.getYRot(), entityplayer.getXRot());
++ ServerStatus serverping = this.server.getStatus();
++
++ if (serverping != null) {
++ entityplayer.sendServerStatus(serverping);
+ }
+
+- serverPlayer.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(this.players));
+- this.players.add(serverPlayer);
+- this.playersByUUID.put(serverPlayer.getUUID(), serverPlayer);
+- this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(serverPlayer)));
+- this.sendLevelInfo(serverPlayer, serverLevel);
+- serverLevel.addNewPlayer(serverPlayer);
+- this.server.getCustomBossEvents().onPlayerConnect(serverPlayer);
++ // entityplayer.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(this.players)); // CraftBukkit - replaced with loop below
++ this.players.add(entityplayer);
++ this.playersByUUID.put(entityplayer.getUUID(), entityplayer);
++ // this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer))); // CraftBukkit - replaced with loop below
+
+- for (MobEffectInstance mobEffectInstance : serverPlayer.getActiveEffects()) {
+- serverGamePacketListenerImpl.send(new ClientboundUpdateMobEffectPacket(serverPlayer.getId(), mobEffectInstance));
++ // CraftBukkit start
++ CraftPlayer bukkitPlayer = entityplayer.getBukkitEntity();
++
++ // Ensure that player inventory is populated with its viewer
++ entityplayer.containerMenu.transferTo(entityplayer.containerMenu, bukkitPlayer);
++
++ PlayerJoinEvent playerJoinEvent = new PlayerJoinEvent(bukkitPlayer, joinMessage);
++ cserver.getPluginManager().callEvent(playerJoinEvent);
++
++ if (!entityplayer.connection.isAcceptingMessages()) {
++ return;
+ }
+
+- if (compoundTag != null && compoundTag.contains("RootVehicle", 10)) {
+- CompoundTag compound = compoundTag.getCompound("RootVehicle");
+- Entity entity = EntityType.loadEntityRecursive(
+- compound.getCompound("Entity"), serverLevel, entity2 -> !serverLevel.addWithUUID(entity2) ? null : entity2
+- );
++ joinMessage = playerJoinEvent.getJoinMessage();
++
++ if (joinMessage != null && joinMessage.length() > 0) {
++ for (Component line : org.bukkit.craftbukkit.util.CraftChatMessage.fromString(joinMessage)) {
++ server.getPlayerList().broadcastSystemMessage(line, false);
++ }
++ }
++ // CraftBukkit end
++
++ // CraftBukkit start - sendAll above replaced with this loop
++ ClientboundPlayerInfoUpdatePacket packet = ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer));
++
++ for (int i = 0; i < this.players.size(); ++i) {
++ ServerPlayer entityplayer1 = (ServerPlayer) this.players.get(i);
++
++ if (entityplayer1.getBukkitEntity().canSee(bukkitPlayer)) {
++ entityplayer1.connection.send(packet);
++ }
++
++ if (!bukkitPlayer.canSee(entityplayer1.getBukkitEntity())) {
++ continue;
++ }
++
++ entityplayer.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer1)));
++ }
++ entityplayer.sentListPacket = true;
++ // CraftBukkit end
++
++ entityplayer.getEntityData().refresh(entityplayer); // CraftBukkit - BungeeCord#2321, send complete data to self on spawn
++
++ this.sendLevelInfo(entityplayer, worldserver1);
++
++ // CraftBukkit start - Only add if the player wasn't moved in the event
++ if (entityplayer.level() == worldserver1 && !worldserver1.players().contains(entityplayer)) {
++ worldserver1.addNewPlayer(entityplayer);
++ this.server.getCustomBossEvents().onPlayerConnect(entityplayer);
++ }
++
++ worldserver1 = entityplayer.serverLevel(); // CraftBukkit - Update in case join event changed it
++ // CraftBukkit end
++ Iterator iterator = entityplayer.getActiveEffects().iterator();
++
++ while (iterator.hasNext()) {
++ MobEffectInstance mobeffect = (MobEffectInstance) iterator.next();
++
++ playerconnection.send(new ClientboundUpdateMobEffectPacket(entityplayer.getId(), mobeffect));
++ }
++
++ if (nbttagcompound != null && nbttagcompound.contains("RootVehicle", 10)) {
++ CompoundTag nbttagcompound1 = nbttagcompound.getCompound("RootVehicle");
++ // CraftBukkit start
++ ServerLevel finalWorldServer = worldserver1;
++ Entity entity = EntityType.loadEntityRecursive(nbttagcompound1.getCompound("Entity"), finalWorldServer, (entity1) -> {
++ return !finalWorldServer.addWithUUID(entity1) ? null : entity1;
++ // CraftBukkit end
++ });
++
+ if (entity != null) {
+- UUID uUID;
+- if (compound.hasUUID("Attach")) {
+- uUID = compound.getUUID("Attach");
++ UUID uuid;
++
++ if (nbttagcompound1.hasUUID("Attach")) {
++ uuid = nbttagcompound1.getUUID("Attach");
+ } else {
+- uUID = null;
++ uuid = null;
+ }
+
+- if (entity.getUUID().equals(uUID)) {
+- serverPlayer.startRiding(entity, true);
++ Iterator iterator1;
++ Entity entity1;
++
++ if (entity.getUUID().equals(uuid)) {
++ entityplayer.startRiding(entity, true);
+ } else {
+- for (Entity entity1 : entity.getIndirectPassengers()) {
+- if (entity1.getUUID().equals(uUID)) {
+- serverPlayer.startRiding(entity1, true);
++ iterator1 = entity.getIndirectPassengers().iterator();
++
++ while (iterator1.hasNext()) {
++ entity1 = (Entity) iterator1.next();
++ if (entity1.getUUID().equals(uuid)) {
++ entityplayer.startRiding(entity1, true);
+ break;
+ }
+ }
+ }
+
+- if (!serverPlayer.isPassenger()) {
+- LOGGER.warn("Couldn't reattach entity to player");
++ if (!entityplayer.isPassenger()) {
++ PlayerList.LOGGER.warn("Couldn't reattach entity to player");
+ entity.discard();
++ iterator1 = entity.getIndirectPassengers().iterator();
+
+- for (Entity entity1x : entity.getIndirectPassengers()) {
+- entity1x.discard();
++ while (iterator1.hasNext()) {
++ entity1 = (Entity) iterator1.next();
++ entity1.discard();
+ }
+ }
+ }
+ }
+
+- serverPlayer.initInventoryMenu();
++ entityplayer.initInventoryMenu();
++ // CraftBukkit - Moved from above, added world
++ PlayerList.LOGGER.info("{}[{}] logged in with entity id {} at ([{}]{}, {}, {})", entityplayer.getName().getString(), s1, entityplayer.getId(), worldserver1.serverLevelData.getLevelName(), entityplayer.getX(), entityplayer.getY(), entityplayer.getZ());
+ }
+
+- protected void updateEntireScoreboard(ServerScoreboard scoreboard, ServerPlayer player) {
++ public void updateEntireScoreboard(ServerScoreboard scoreboard, ServerPlayer player) {
+ Set<Objective> set = Sets.newHashSet();
++ Iterator iterator = scoreboard.getPlayerTeams().iterator();
+
+- for (PlayerTeam playerTeam : scoreboard.getPlayerTeams()) {
+- player.connection.send(ClientboundSetPlayerTeamPacket.createAddOrModifyPacket(playerTeam, true));
++ while (iterator.hasNext()) {
++ PlayerTeam scoreboardteam = (PlayerTeam) iterator.next();
++
++ player.connection.send(ClientboundSetPlayerTeamPacket.createAddOrModifyPacket(scoreboardteam, true));
+ }
+
+- for (DisplaySlot displaySlot : DisplaySlot.values()) {
+- Objective displayObjective = scoreboard.getDisplayObjective(displaySlot);
+- if (displayObjective != null && !set.contains(displayObjective)) {
+- for (Packet<?> packet : scoreboard.getStartTrackingPackets(displayObjective)) {
++ DisplaySlot[] adisplayslot = DisplaySlot.values();
++ int i = adisplayslot.length;
++
++ for (int j = 0; j < i; ++j) {
++ DisplaySlot displayslot = adisplayslot[j];
++ Objective scoreboardobjective = scoreboard.getDisplayObjective(displayslot);
++
++ if (scoreboardobjective != null && !set.contains(scoreboardobjective)) {
++ List<Packet<?>> list = scoreboard.getStartTrackingPackets(scoreboardobjective);
++ Iterator iterator1 = list.iterator();
++
++ while (iterator1.hasNext()) {
++ Packet<?> packet = (Packet) iterator1.next();
++
+ player.connection.send(packet);
+ }
+
+- set.add(displayObjective);
++ set.add(scoreboardobjective);
+ }
+ }
++
+ }
+
+ public void addWorldborderListener(ServerLevel level) {
++ if (playerIo != null) return; // CraftBukkit
+ level.getWorldBorder().addListener(new BorderChangeListener() {
+ @Override
+ public void onBorderSizeSet(WorldBorder border, double size) {
+- PlayerList.this.broadcastAll(new ClientboundSetBorderSizePacket(border));
++ PlayerList.this.broadcastAll(new ClientboundSetBorderSizePacket(border), border.world); // CraftBukkit
+ }
+
+ @Override
+- public void onBorderSizeLerping(WorldBorder border, double oldSize, double newSize, long time) {
+- PlayerList.this.broadcastAll(new ClientboundSetBorderLerpSizePacket(border));
++ public void onBorderSizeLerping(WorldBorder border, double oldSize, double d1, long newSize) {
++ PlayerList.this.broadcastAll(new ClientboundSetBorderLerpSizePacket(border), border.world); // CraftBukkit
+ }
+
+ @Override
+- public void onBorderCenterSet(WorldBorder border, double x, double z) {
+- PlayerList.this.broadcastAll(new ClientboundSetBorderCenterPacket(border));
++ public void onBorderCenterSet(WorldBorder border, double x, double d1) {
++ PlayerList.this.broadcastAll(new ClientboundSetBorderCenterPacket(border), border.world); // CraftBukkit
+ }
+
+ @Override
+ public void onBorderSetWarningTime(WorldBorder border, int warningTime) {
+- PlayerList.this.broadcastAll(new ClientboundSetBorderWarningDelayPacket(border));
++ PlayerList.this.broadcastAll(new ClientboundSetBorderWarningDelayPacket(border), border.world); // CraftBukkit
+ }
+
+ @Override
+ public void onBorderSetWarningBlocks(WorldBorder border, int warningBlocks) {
+- PlayerList.this.broadcastAll(new ClientboundSetBorderWarningDistancePacket(border));
++ PlayerList.this.broadcastAll(new ClientboundSetBorderWarningDistancePacket(border), border.world); // CraftBukkit
+ }
+
+ @Override
+- public void onBorderSetDamagePerBlock(WorldBorder border, double damagePerBlock) {
+- }
++ public void onBorderSetDamagePerBlock(WorldBorder border, double damagePerBlock) {}
+
+ @Override
+- public void onBorderSetDamageSafeZOne(WorldBorder border, double damageSafeZone) {
+- }
++ public void onBorderSetDamageSafeZOne(WorldBorder border, double damageSafeZone) {}
+ });
+ }
+
+ @Nullable
+ public CompoundTag load(ServerPlayer player) {
+- CompoundTag loadedPlayerTag = this.server.getWorldData().getLoadedPlayerTag();
+- CompoundTag compoundTag;
+- if (this.server.isSingleplayerOwner(player.getGameProfile()) && loadedPlayerTag != null) {
+- compoundTag = loadedPlayerTag;
+- player.load(loadedPlayerTag);
+- LOGGER.debug("loading single player");
++ CompoundTag nbttagcompound = this.server.getWorldData().getLoadedPlayerTag();
++ CompoundTag nbttagcompound1;
++
++ if (this.server.isSingleplayerOwner(player.getGameProfile()) && nbttagcompound != null) {
++ nbttagcompound1 = nbttagcompound;
++ player.load(nbttagcompound);
++ PlayerList.LOGGER.debug("loading single player");
+ } else {
+- compoundTag = this.playerIo.load(player);
++ nbttagcompound1 = this.playerIo.load(player);
+ }
+
+- return compoundTag;
++ return nbttagcompound1;
+ }
+
+ protected void save(ServerPlayer player) {
++ if (!player.getBukkitEntity().isPersistent()) return; // CraftBukkit
+ this.playerIo.save(player);
+- ServerStatsCounter serverStatsCounter = this.stats.get(player.getUUID());
+- if (serverStatsCounter != null) {
+- serverStatsCounter.save();
++ ServerStatsCounter serverstatisticmanager = (ServerStatsCounter) player.getStats(); // CraftBukkit
++
++ if (serverstatisticmanager != null) {
++ serverstatisticmanager.save();
+ }
+
+- PlayerAdvancements playerAdvancements = this.advancements.get(player.getUUID());
+- if (playerAdvancements != null) {
+- playerAdvancements.save();
++ PlayerAdvancements advancementdataplayer = (PlayerAdvancements) player.getAdvancements(); // CraftBukkit
++
++ if (advancementdataplayer != null) {
++ advancementdataplayer.save();
+ }
++
+ }
+
+- public void remove(ServerPlayer player) {
+- ServerLevel serverLevel = player.serverLevel();
+- player.awardStat(Stats.LEAVE_GAME);
+- this.save(player);
+- if (player.isPassenger()) {
+- Entity rootVehicle = player.getRootVehicle();
+- if (rootVehicle.hasExactlyOnePlayerPassenger()) {
+- LOGGER.debug("Removing player mount");
+- player.stopRiding();
+- rootVehicle.getPassengersAndSelf().forEach(entity -> entity.setRemoved(Entity.RemovalReason.UNLOADED_WITH_PLAYER));
++ public String remove(ServerPlayer entityplayer) { // CraftBukkit - return string
++ ServerLevel worldserver = entityplayer.serverLevel();
++
++ entityplayer.awardStat(Stats.LEAVE_GAME);
++
++ // CraftBukkit start - Quitting must be before we do final save of data, in case plugins need to modify it
++ // See SPIGOT-5799, SPIGOT-6145
++ if (entityplayer.containerMenu != entityplayer.inventoryMenu) {
++ entityplayer.closeContainer();
++ }
++
++ PlayerQuitEvent playerQuitEvent = new PlayerQuitEvent(entityplayer.getBukkitEntity(), entityplayer.kickLeaveMessage != null ? entityplayer.kickLeaveMessage : "\u00A7e" + entityplayer.getScoreboardName() + " left the game");
++ cserver.getPluginManager().callEvent(playerQuitEvent);
++ entityplayer.getBukkitEntity().disconnect(playerQuitEvent.getQuitMessage());
++
++ entityplayer.doTick(); // SPIGOT-924
++ // CraftBukkit end
++
++ this.save(entityplayer);
++ if (entityplayer.isPassenger()) {
++ Entity entity = entityplayer.getRootVehicle();
++
++ if (entity.hasExactlyOnePlayerPassenger()) {
++ PlayerList.LOGGER.debug("Removing player mount");
++ entityplayer.stopRiding();
++ entity.getPassengersAndSelf().forEach((entity1) -> {
++ entity1.setRemoved(Entity.RemovalReason.UNLOADED_WITH_PLAYER);
++ });
+ }
+ }
+
+- player.unRide();
+- serverLevel.removePlayerImmediately(player, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
+- player.getAdvancements().stopListening();
+- this.players.remove(player);
+- this.server.getCustomBossEvents().onPlayerDisconnect(player);
+- UUID uUID = player.getUUID();
+- ServerPlayer serverPlayer = this.playersByUUID.get(uUID);
+- if (serverPlayer == player) {
+- this.playersByUUID.remove(uUID);
+- this.stats.remove(uUID);
+- this.advancements.remove(uUID);
++ entityplayer.unRide();
++ worldserver.removePlayerImmediately(entityplayer, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
++ entityplayer.getAdvancements().stopListening();
++ this.players.remove(entityplayer);
++ this.server.getCustomBossEvents().onPlayerDisconnect(entityplayer);
++ UUID uuid = entityplayer.getUUID();
++ ServerPlayer entityplayer1 = (ServerPlayer) this.playersByUUID.get(uuid);
++
++ if (entityplayer1 == entityplayer) {
++ this.playersByUUID.remove(uuid);
++ // CraftBukkit start
++ // this.stats.remove(uuid);
++ // this.advancements.remove(uuid);
++ // CraftBukkit end
+ }
+
+- this.broadcastAll(new ClientboundPlayerInfoRemovePacket(List.of(player.getUUID())));
++ // CraftBukkit start
++ // this.broadcastAll(new ClientboundPlayerInfoRemovePacket(List.of(entityplayer.getUUID())));
++ ClientboundPlayerInfoRemovePacket packet = new ClientboundPlayerInfoRemovePacket(List.of(entityplayer.getUUID()));
++ for (int i = 0; i < players.size(); i++) {
++ ServerPlayer entityplayer2 = (ServerPlayer) this.players.get(i);
++
++ if (entityplayer2.getBukkitEntity().canSee(entityplayer.getBukkitEntity())) {
++ entityplayer2.connection.send(packet);
++ } else {
++ entityplayer2.getBukkitEntity().onEntityRemove(entityplayer);
++ }
++ }
++ // This removes the scoreboard (and player reference) for the specific player in the manager
++ cserver.getScoreboardManager().removePlayer(entityplayer.getBukkitEntity());
++ // CraftBukkit end
++
++ return playerQuitEvent.getQuitMessage(); // CraftBukkit
+ }
+
+- @Nullable
+- public Component canPlayerLogin(SocketAddress socketAddress, GameProfile gameProfile) {
+- if (this.bans.isBanned(gameProfile)) {
+- UserBanListEntry userBanListEntry = this.bans.get(gameProfile);
+- MutableComponent mutableComponent = Component.translatable("multiplayer.disconnect.banned.reason", userBanListEntry.getReason());
+- if (userBanListEntry.getExpires() != null) {
+- mutableComponent.append(
+- Component.translatable("multiplayer.disconnect.banned.expiration", BAN_DATE_FORMAT.format(userBanListEntry.getExpires()))
+- );
++ // CraftBukkit start - Whole method, SocketAddress to LoginListener, added hostname to signature, return EntityPlayer
++ public ServerPlayer canPlayerLogin(ServerLoginPacketListenerImpl loginlistener, GameProfile gameprofile) {
++ MutableComponent ichatmutablecomponent;
++
++ // Moved from processLogin
++ UUID uuid = gameprofile.getId();
++ List<ServerPlayer> list = Lists.newArrayList();
++
++ ServerPlayer entityplayer;
++
++ for (int i = 0; i < this.players.size(); ++i) {
++ entityplayer = (ServerPlayer) this.players.get(i);
++ if (entityplayer.getUUID().equals(uuid)) {
++ list.add(entityplayer);
+ }
++ }
+
+- return mutableComponent;
+- } else if (!this.isWhiteListed(gameProfile)) {
+- return Component.translatable("multiplayer.disconnect.not_whitelisted");
+- } else if (this.ipBans.isBanned(socketAddress)) {
+- IpBanListEntry ipBanListEntry = this.ipBans.get(socketAddress);
+- MutableComponent mutableComponent = Component.translatable("multiplayer.disconnect.banned_ip.reason", ipBanListEntry.getReason());
+- if (ipBanListEntry.getExpires() != null) {
+- mutableComponent.append(
+- Component.translatable("multiplayer.disconnect.banned_ip.expiration", BAN_DATE_FORMAT.format(ipBanListEntry.getExpires()))
+- );
++ Iterator iterator = list.iterator();
++
++ while (iterator.hasNext()) {
++ entityplayer = (ServerPlayer) iterator.next();
++ save(entityplayer); // CraftBukkit - Force the player's inventory to be saved
++ entityplayer.connection.disconnect(Component.translatable("multiplayer.disconnect.duplicate_login"));
++ }
++
++ // Instead of kicking then returning, we need to store the kick reason
++ // in the event, check with plugins to see if it's ok, and THEN kick
++ // depending on the outcome.
++ SocketAddress socketaddress = loginlistener.connection.getRemoteAddress();
++
++ ServerPlayer entity = new ServerPlayer(this.server, this.server.getLevel(Level.OVERWORLD), gameprofile, ClientInformation.createDefault());
++ Player player = entity.getBukkitEntity();
++ PlayerLoginEvent event = new PlayerLoginEvent(player, loginlistener.connection.hostname, ((java.net.InetSocketAddress) socketaddress).getAddress());
++
++ if (getBans().isBanned(gameprofile) && !getBans().get(gameprofile).hasExpired()) {
++ UserBanListEntry gameprofilebanentry = (UserBanListEntry) this.bans.get(gameprofile);
++
++ ichatmutablecomponent = Component.translatable("multiplayer.disconnect.banned.reason", gameprofilebanentry.getReason());
++ if (gameprofilebanentry.getExpires() != null) {
++ ichatmutablecomponent.append((Component) Component.translatable("multiplayer.disconnect.banned.expiration", PlayerList.BAN_DATE_FORMAT.format(gameprofilebanentry.getExpires())));
+ }
+
+- return mutableComponent;
++ // return chatmessage;
++ event.disallow(PlayerLoginEvent.Result.KICK_BANNED, CraftChatMessage.fromComponent(ichatmutablecomponent));
++ } else if (!this.isWhiteListed(gameprofile)) {
++ ichatmutablecomponent = Component.translatable("multiplayer.disconnect.not_whitelisted");
++ event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, CraftChatMessage.fromComponent(ichatmutablecomponent));
++ } else if (getIpBans().isBanned(socketaddress) && !getIpBans().get(socketaddress).hasExpired()) {
++ IpBanListEntry ipbanentry = this.ipBans.get(socketaddress);
++
++ ichatmutablecomponent = Component.translatable("multiplayer.disconnect.banned_ip.reason", ipbanentry.getReason());
++ if (ipbanentry.getExpires() != null) {
++ ichatmutablecomponent.append((Component) Component.translatable("multiplayer.disconnect.banned_ip.expiration", PlayerList.BAN_DATE_FORMAT.format(ipbanentry.getExpires())));
++ }
++
++ // return chatmessage;
++ event.disallow(PlayerLoginEvent.Result.KICK_BANNED, CraftChatMessage.fromComponent(ichatmutablecomponent));
+ } else {
+- return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameProfile)
+- ? Component.translatable("multiplayer.disconnect.server_full")
+- : null;
++ // return this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameprofile) ? IChatBaseComponent.translatable("multiplayer.disconnect.server_full") : null;
++ if (this.players.size() >= this.maxPlayers && !this.canBypassPlayerLimit(gameprofile)) {
++ event.disallow(PlayerLoginEvent.Result.KICK_FULL, "The server is full");
++ }
+ }
++
++ cserver.getPluginManager().callEvent(event);
++ if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
++ loginlistener.disconnect(event.getKickMessage());
++ return null;
++ }
++ return entity;
+ }
+
+- public ServerPlayer getPlayerForLogin(GameProfile gameProfile, ClientInformation clientInformation) {
+- return new ServerPlayer(this.server, this.server.overworld(), gameProfile, clientInformation);
++ // CraftBukkit start - added EntityPlayer
++ public ServerPlayer getPlayerForLogin(GameProfile gameprofile, ClientInformation clientinformation, ServerPlayer player) {
++ player.updateOptions(clientinformation);
++ return player;
++ // CraftBukkit end
+ }
+
+- public boolean disconnectAllPlayersWithProfile(GameProfile gameProfile) {
+- UUID id = gameProfile.getId();
+- Set<ServerPlayer> set = Sets.newIdentityHashSet();
++ public boolean disconnectAllPlayersWithProfile(GameProfile gameprofile, ServerPlayer player) { // CraftBukkit - added EntityPlayer
++ /* CraftBukkit startMoved up
++ UUID uuid = gameprofile.getId();
++ Set<EntityPlayer> set = Sets.newIdentityHashSet();
++ Iterator iterator = this.players.iterator();
+
+- for (ServerPlayer serverPlayer : this.players) {
+- if (serverPlayer.getUUID().equals(id)) {
+- set.add(serverPlayer);
++ while (iterator.hasNext()) {
++ EntityPlayer entityplayer = (EntityPlayer) iterator.next();
++
++ if (entityplayer.getUUID().equals(uuid)) {
++ set.add(entityplayer);
+ }
+ }
+
+- ServerPlayer serverPlayer1 = this.playersByUUID.get(gameProfile.getId());
+- if (serverPlayer1 != null) {
+- set.add(serverPlayer1);
++ EntityPlayer entityplayer1 = (EntityPlayer) this.playersByUUID.get(gameprofile.getId());
++
++ if (entityplayer1 != null) {
++ set.add(entityplayer1);
+ }
+
+- for (ServerPlayer serverPlayer2 : set) {
+- serverPlayer2.connection.disconnect(DUPLICATE_LOGIN_DISCONNECT_MESSAGE);
++ Iterator iterator1 = set.iterator();
++
++ while (iterator1.hasNext()) {
++ EntityPlayer entityplayer2 = (EntityPlayer) iterator1.next();
++
++ entityplayer2.connection.disconnect(PlayerList.DUPLICATE_LOGIN_DISCONNECT_MESSAGE);
+ }
+
+ return !set.isEmpty();
++ */
++ return player == null;
++ // CraftBukkit end
+ }
+
+- public ServerPlayer respawn(ServerPlayer player, boolean keepEverything) {
+- this.players.remove(player);
+- player.serverLevel().removePlayerImmediately(player, Entity.RemovalReason.DISCARDED);
+- BlockPos respawnPosition = player.getRespawnPosition();
+- float respawnAngle = player.getRespawnAngle();
+- boolean isRespawnForced = player.isRespawnForced();
+- ServerLevel level = this.server.getLevel(player.getRespawnDimension());
+- Optional<Vec3> optional;
+- if (level != null && respawnPosition != null) {
+- optional = Player.findRespawnPositionAndUseSpawnBlock(level, respawnPosition, respawnAngle, isRespawnForced, keepEverything);
++ // CraftBukkit start
++ public ServerPlayer respawn(ServerPlayer entityplayer, boolean flag, RespawnReason reason) {
++ return this.respawn(entityplayer, this.server.getLevel(entityplayer.getRespawnDimension()), flag, null, true, reason);
++ }
++
++ public ServerPlayer respawn(ServerPlayer entityplayer, ServerLevel worldserver, boolean flag, Location location, boolean avoidSuffocation, RespawnReason reason) {
++ entityplayer.stopRiding(); // CraftBukkit
++ this.players.remove(entityplayer);
++ entityplayer.serverLevel().removePlayerImmediately(entityplayer, Entity.RemovalReason.DISCARDED);
++ BlockPos blockposition = entityplayer.getRespawnPosition();
++ float f = entityplayer.getRespawnAngle();
++ boolean flag1 = entityplayer.isRespawnForced();
++ /* CraftBukkit start
++ WorldServer worldserver = this.server.getLevel(entityplayer.getRespawnDimension());
++ Optional optional;
++
++ if (worldserver != null && blockposition != null) {
++ optional = EntityHuman.findRespawnPositionAndUseSpawnBlock(worldserver, blockposition, f, flag1, flag);
+ } else {
+ optional = Optional.empty();
+ }
+
+- ServerLevel serverLevel = level != null && optional.isPresent() ? level : this.server.overworld();
+- ServerPlayer serverPlayer = new ServerPlayer(this.server, serverLevel, player.getGameProfile(), player.clientInformation());
+- serverPlayer.connection = player.connection;
+- serverPlayer.restoreFrom(player, keepEverything);
+- serverPlayer.setId(player.getId());
+- serverPlayer.setMainArm(player.getMainArm());
++ WorldServer worldserver1 = worldserver != null && optional.isPresent() ? worldserver : this.server.overworld();
++ EntityPlayer entityplayer1 = new EntityPlayer(this.server, worldserver1, entityplayer.getGameProfile(), entityplayer.clientInformation());
++ // */
++ ServerPlayer entityplayer1 = entityplayer;
++ org.bukkit.World fromWorld = entityplayer.getBukkitEntity().getWorld();
++ entityplayer.wonGame = false;
++ // CraftBukkit end
+
+- for (String string : player.getTags()) {
+- serverPlayer.addTag(string);
++ entityplayer1.connection = entityplayer.connection;
++ entityplayer1.restoreFrom(entityplayer, flag);
++ entityplayer1.setId(entityplayer.getId());
++ entityplayer1.setMainArm(entityplayer.getMainArm());
++ Iterator iterator = entityplayer.getTags().iterator();
++
++ while (iterator.hasNext()) {
++ String s = (String) iterator.next();
++
++ entityplayer1.addTag(s);
+ }
+
+- boolean flag = false;
+- if (optional.isPresent()) {
+- BlockState blockState = serverLevel.getBlockState(respawnPosition);
+- boolean isRespawnAnchor = blockState.is(Blocks.RESPAWN_ANCHOR);
+- Vec3 vec3 = optional.get();
+- float f;
+- if (!blockState.is(BlockTags.BEDS) && !isRespawnAnchor) {
+- f = respawnAngle;
+- } else {
+- Vec3 vec31 = Vec3.atBottomCenterOf(respawnPosition).subtract(vec3).normalize();
+- f = (float)Mth.wrapDegrees(Mth.atan2(vec31.z, vec31.x) * 180.0F / (float)Math.PI - 90.0);
++ boolean flag2 = false;
++
++ // CraftBukkit start - fire PlayerRespawnEvent
++ if (location == null) {
++ boolean isBedSpawn = false;
++ ServerLevel worldserver1 = this.server.getLevel(entityplayer.getRespawnDimension());
++ if (worldserver1 != null) {
++ Optional optional;
++
++ if (blockposition != null) {
++ optional = net.minecraft.world.entity.player.Player.findRespawnPositionAndUseSpawnBlock(worldserver1, blockposition, f, flag1, flag);
++ } else {
++ optional = Optional.empty();
++ }
++
++ if (optional.isPresent()) {
++ IBlockData iblockdata = worldserver1.getBlockState(blockposition);
++ boolean flag3 = iblockdata.is(Blocks.RESPAWN_ANCHOR);
++ Vec3 vec3d = (Vec3) optional.get();
++ float f1;
++
++ if (!iblockdata.is(BlockTags.BEDS) && !flag3) {
++ f1 = f;
++ } else {
++ Vec3 vec3d1 = Vec3.atBottomCenterOf(blockposition).subtract(vec3d).normalize();
++
++ f1 = (float) Mth.wrapDegrees(Mth.atan2(vec3d1.z, vec3d1.x) * 57.2957763671875D - 90.0D);
++ }
++
++ // entityplayer1.setRespawnPosition(worldserver1.dimension(), blockposition, f, flag1, false); // CraftBukkit - not required, just copies old location into reused entity
++ flag2 = !flag && flag3;
++ isBedSpawn = true;
++ location = CraftLocation.toBukkit(vec3d, worldserver1.getWorld(), f1, 0.0F);
++ } else if (blockposition != null) {
++ entityplayer1.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.NO_RESPAWN_BLOCK_AVAILABLE, 0.0F));
++ entityplayer1.setRespawnPosition(null, null, 0f, false, false, PlayerSpawnChangeEvent.Cause.RESET); // CraftBukkit - SPIGOT-5988: Clear respawn location when obstructed
++ }
+ }
+
+- serverPlayer.moveTo(vec3.x, vec3.y, vec3.z, f, 0.0F);
+- serverPlayer.setRespawnPosition(serverLevel.dimension(), respawnPosition, respawnAngle, isRespawnForced, false);
+- flag = !keepEverything && isRespawnAnchor;
+- } else if (respawnPosition != null) {
+- serverPlayer.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.NO_RESPAWN_BLOCK_AVAILABLE, 0.0F));
++ if (location == null) {
++ worldserver1 = this.server.getLevel(Level.OVERWORLD);
++ blockposition = entityplayer1.getSpawnPoint(worldserver1);
++ location = CraftLocation.toBukkit(blockposition, worldserver1.getWorld()).add(0.5F, 0.1F, 0.5F);
++ }
++
++ Player respawnPlayer = entityplayer1.getBukkitEntity();
++ PlayerRespawnEvent respawnEvent = new PlayerRespawnEvent(respawnPlayer, location, isBedSpawn && !flag2, flag2, reason);
++ cserver.getPluginManager().callEvent(respawnEvent);
++
++ location = respawnEvent.getRespawnLocation();
++ if (!flag) entityplayer.reset(); // SPIGOT-4785
++ } else {
++ location.setWorld(worldserver.getWorld());
+ }
++ ServerLevel worldserver1 = ((CraftWorld) location.getWorld()).getHandle();
++ entityplayer1.spawnIn(worldserver1);
++ entityplayer1.unsetRemoved();
++ entityplayer1.setShiftKeyDown(false);
++ entityplayer1.forceSetPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
+
+- while (!serverLevel.noCollision(serverPlayer) && serverPlayer.getY() < (double)serverLevel.getMaxBuildHeight()) {
+- serverPlayer.setPos(serverPlayer.getX(), serverPlayer.getY() + 1.0, serverPlayer.getZ());
++ while (avoidSuffocation && !worldserver1.noCollision((Entity) entityplayer1) && entityplayer1.getY() < (double) worldserver1.getMaxBuildHeight()) {
++ // CraftBukkit end
++ entityplayer1.setPos(entityplayer1.getX(), entityplayer1.getY() + 1.0D, entityplayer1.getZ());
+ }
+
+- byte b = (byte)(keepEverything ? 1 : 0);
+- ServerLevel serverLevel1 = serverPlayer.serverLevel();
+- LevelData levelData = serverLevel1.getLevelData();
+- serverPlayer.connection.send(new ClientboundRespawnPacket(serverPlayer.createCommonSpawnInfo(serverLevel1), b));
+- serverPlayer.connection.teleport(serverPlayer.getX(), serverPlayer.getY(), serverPlayer.getZ(), serverPlayer.getYRot(), serverPlayer.getXRot());
+- serverPlayer.connection.send(new ClientboundSetDefaultSpawnPositionPacket(serverLevel.getSharedSpawnPos(), serverLevel.getSharedSpawnAngle()));
+- serverPlayer.connection.send(new ClientboundChangeDifficultyPacket(levelData.getDifficulty(), levelData.isDifficultyLocked()));
+- serverPlayer.connection
+- .send(new ClientboundSetExperiencePacket(serverPlayer.experienceProgress, serverPlayer.totalExperience, serverPlayer.experienceLevel));
+- this.sendLevelInfo(serverPlayer, serverLevel);
+- this.sendPlayerPermissionLevel(serverPlayer);
+- serverLevel.addRespawnedPlayer(serverPlayer);
+- this.players.add(serverPlayer);
+- this.playersByUUID.put(serverPlayer.getUUID(), serverPlayer);
+- serverPlayer.initInventoryMenu();
+- serverPlayer.setHealth(serverPlayer.getHealth());
+- if (flag) {
+- serverPlayer.connection
+- .send(
+- new ClientboundSoundPacket(
+- SoundEvents.RESPAWN_ANCHOR_DEPLETE,
+- SoundSource.BLOCKS,
+- (double)respawnPosition.getX(),
+- (double)respawnPosition.getY(),
+- (double)respawnPosition.getZ(),
+- 1.0F,
+- 1.0F,
+- serverLevel.getRandom().nextLong()
+- )
+- );
++ int i = flag ? 1 : 0;
++ ServerLevel worldserver2 = entityplayer1.serverLevel();
++ LevelData worlddata = worldserver2.getLevelData();
++
++ entityplayer1.connection.send(new ClientboundRespawnPacket(entityplayer1.createCommonSpawnInfo(worldserver2), (byte) i));
++ entityplayer1.connection.teleport(CraftLocation.toBukkit(entityplayer1.position(), worldserver2.getWorld(), entityplayer1.getYRot(), entityplayer1.getXRot())); // CraftBukkit
++ entityplayer1.connection.send(new ClientboundSetDefaultSpawnPositionPacket(worldserver1.getSharedSpawnPos(), worldserver1.getSharedSpawnAngle()));
++ entityplayer1.connection.send(new ClientboundChangeDifficultyPacket(worlddata.getDifficulty(), worlddata.isDifficultyLocked()));
++ entityplayer1.connection.send(new ClientboundSetExperiencePacket(entityplayer1.experienceProgress, entityplayer1.totalExperience, entityplayer1.experienceLevel));
++ this.sendLevelInfo(entityplayer1, worldserver1);
++ this.sendPlayerPermissionLevel(entityplayer1);
++ if (!entityplayer.connection.isDisconnected()) {
++ worldserver1.addRespawnedPlayer(entityplayer1);
++ this.players.add(entityplayer1);
++ this.playersByUUID.put(entityplayer1.getUUID(), entityplayer1);
+ }
++ // entityplayer1.initInventoryMenu();
++ entityplayer1.setHealth(entityplayer1.getHealth());
++ if (flag2) {
++ entityplayer1.connection.send(new ClientboundSoundPacket(SoundEvents.RESPAWN_ANCHOR_DEPLETE, SoundSource.BLOCKS, (double) blockposition.getX(), (double) blockposition.getY(), (double) blockposition.getZ(), 1.0F, 1.0F, worldserver1.getRandom().nextLong()));
++ }
++ // Added from changeDimension
++ sendAllPlayerInfo(entityplayer); // Update health, etc...
++ entityplayer.onUpdateAbilities();
++ for (MobEffectInstance mobEffect : entityplayer.getActiveEffects()) {
++ entityplayer.connection.send(new ClientboundUpdateMobEffectPacket(entityplayer.getId(), mobEffect));
++ }
+
+- return serverPlayer;
++ // Fire advancement trigger
++ entityplayer.triggerDimensionChangeTriggers(((CraftWorld) fromWorld).getHandle());
++
++ // Don't fire on respawn
++ if (fromWorld != location.getWorld()) {
++ PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(entityplayer.getBukkitEntity(), fromWorld);
++ server.server.getPluginManager().callEvent(event);
++ }
++
++ // Save player file again if they were disconnected
++ if (entityplayer.connection.isDisconnected()) {
++ this.save(entityplayer);
++ }
++ // CraftBukkit end
++ return entityplayer1;
+ }
+
+ public void sendPlayerPermissionLevel(ServerPlayer player) {
+- GameProfile gameProfile = player.getGameProfile();
+- int profilePermissions = this.server.getProfilePermissions(gameProfile);
+- this.sendPlayerPermissionLevel(player, profilePermissions);
++ GameProfile gameprofile = player.getGameProfile();
++ int i = this.server.getProfilePermissions(gameprofile);
++
++ this.sendPlayerPermissionLevel(player, i);
+ }
+
+ public void tick() {
+ if (++this.sendAllPlayerInfoIn > 600) {
+- this.broadcastAll(new ClientboundPlayerInfoUpdatePacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY), this.players));
++ // CraftBukkit start
++ for (int i = 0; i < this.players.size(); ++i) {
++ final ServerPlayer target = (ServerPlayer) this.players.get(i);
++
++ target.connection.send(new ClientboundPlayerInfoUpdatePacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY), this.players.stream().filter(new Predicate<ServerPlayer>() {
++ @Override
++ public boolean test(ServerPlayer input) {
++ return target.getBukkitEntity().canSee(input.getBukkitEntity());
++ }
++ }).collect(Collectors.toList())));
++ }
++ // CraftBukkit end
+ this.sendAllPlayerInfoIn = 0;
+ }
++
+ }
+
+ public void broadcastAll(Packet<?> packet) {
+- for (ServerPlayer serverPlayer : this.players) {
+- serverPlayer.connection.send(packet);
++ Iterator iterator = this.players.iterator();
++
++ while (iterator.hasNext()) {
++ ServerPlayer entityplayer = (ServerPlayer) iterator.next();
++
++ entityplayer.connection.send(packet);
+ }
++
+ }
+
++ // CraftBukkit start - add a world/entity limited version
++ public void broadcastAll(Packet packet, net.minecraft.world.entity.player.Player entityhuman) {
++ for (int i = 0; i < this.players.size(); ++i) {
++ ServerPlayer entityplayer = this.players.get(i);
++ if (entityhuman != null && !entityplayer.getBukkitEntity().canSee(entityhuman.getBukkitEntity())) {
++ continue;
++ }
++ ((ServerPlayer) this.players.get(i)).connection.send(packet);
++ }
++ }
++
++ public void broadcastAll(Packet packet, Level world) {
++ for (int i = 0; i < world.players().size(); ++i) {
++ ((ServerPlayer) world.players().get(i)).connection.send(packet);
++ }
++
++ }
++ // CraftBukkit end
++
+ public void broadcastAll(Packet<?> packet, ResourceKey<Level> dimension) {
+- for (ServerPlayer serverPlayer : this.players) {
+- if (serverPlayer.level().dimension() == dimension) {
+- serverPlayer.connection.send(packet);
++ Iterator iterator = this.players.iterator();
++
++ while (iterator.hasNext()) {
++ ServerPlayer entityplayer = (ServerPlayer) iterator.next();
++
++ if (entityplayer.level().dimension() == dimension) {
++ entityplayer.connection.send(packet);
+ }
+ }
++
+ }
+
+- public void broadcastSystemToTeam(Player player, Component message) {
+- Team team = player.getTeam();
+- if (team != null) {
+- for (String string : team.getPlayers()) {
+- ServerPlayer playerByName = this.getPlayerByName(string);
+- if (playerByName != null && playerByName != player) {
+- playerByName.sendSystemMessage(message);
++ public void broadcastSystemToTeam(net.minecraft.world.entity.player.Player player, Component message) {
++ PlayerTeam scoreboardteam = player.getTeam();
++
++ if (scoreboardteam != null) {
++ Collection<String> collection = scoreboardteam.getPlayers();
++ Iterator iterator = collection.iterator();
++
++ while (iterator.hasNext()) {
++ String s = (String) iterator.next();
++ ServerPlayer entityplayer = this.getPlayerByName(s);
++
++ if (entityplayer != null && entityplayer != player) {
++ entityplayer.sendSystemMessage(message);
+ }
+ }
++
+ }
+ }
+
+- public void broadcastSystemToAllExceptTeam(Player player, Component message) {
+- Team team = player.getTeam();
+- if (team == null) {
++ public void broadcastSystemToAllExceptTeam(net.minecraft.world.entity.player.Player player, Component message) {
++ PlayerTeam scoreboardteam = player.getTeam();
++
++ if (scoreboardteam == null) {
+ this.broadcastSystemMessage(message, false);
+ } else {
+- for (int i = 0; i < this.players.size(); i++) {
+- ServerPlayer serverPlayer = this.players.get(i);
+- if (serverPlayer.getTeam() != team) {
+- serverPlayer.sendSystemMessage(message);
++ for (int i = 0; i < this.players.size(); ++i) {
++ ServerPlayer entityplayer = (ServerPlayer) this.players.get(i);
++
++ if (entityplayer.getTeam() != scoreboardteam) {
++ entityplayer.sendSystemMessage(message);
+ }
+ }
++
+ }
+ }
+
+ public String[] getPlayerNamesArray() {
+- String[] strings = new String[this.players.size()];
++ String[] astring = new String[this.players.size()];
+
+- for (int i = 0; i < this.players.size(); i++) {
+- strings[i] = this.players.get(i).getGameProfile().getName();
++ for (int i = 0; i < this.players.size(); ++i) {
++ astring[i] = ((ServerPlayer) this.players.get(i)).getGameProfile().getName();
+ }
+
+- return strings;
++ return astring;
+ }
+
+ public UserBanList getBans() {
+@@ -593,34 +939,40 @@
+
+ public void op(GameProfile profile) {
+ this.ops.add(new ServerOpListEntry(profile, this.server.getOperatorUserPermissionLevel(), this.ops.canBypassPlayerLimit(profile)));
+- ServerPlayer player = this.getPlayer(profile.getId());
+- if (player != null) {
+- this.sendPlayerPermissionLevel(player);
++ ServerPlayer entityplayer = this.getPlayer(profile.getId());
++
++ if (entityplayer != null) {
++ this.sendPlayerPermissionLevel(entityplayer);
+ }
++
+ }
+
+ public void deop(GameProfile profile) {
+- this.ops.remove(profile);
+- ServerPlayer player = this.getPlayer(profile.getId());
+- if (player != null) {
+- this.sendPlayerPermissionLevel(player);
++ this.ops.remove(profile); // CraftBukkit - decompile error
++ ServerPlayer entityplayer = this.getPlayer(profile.getId());
++
++ if (entityplayer != null) {
++ this.sendPlayerPermissionLevel(entityplayer);
+ }
++
+ }
+
+ private void sendPlayerPermissionLevel(ServerPlayer player, int permLevel) {
+ if (player.connection != null) {
+- byte b;
++ byte b0;
++
+ if (permLevel <= 0) {
+- b = 24;
++ b0 = 24;
+ } else if (permLevel >= 4) {
+- b = 28;
++ b0 = 28;
+ } else {
+- b = (byte)(24 + permLevel);
++ b0 = (byte) (24 + permLevel);
+ }
+
+- player.connection.send(new ClientboundEntityEventPacket(player, b));
++ player.connection.send(new ClientboundEntityEventPacket(player, b0));
+ }
+
++ player.getBukkitEntity().recalculatePermissions(); // CraftBukkit
+ this.server.getCommands().sendCommands(player);
+ }
+
+@@ -629,43 +981,52 @@
+ }
+
+ public boolean isOp(GameProfile profile) {
+- return this.ops.contains(profile)
+- || this.server.isSingleplayerOwner(profile) && this.server.getWorldData().getAllowCommands()
+- || this.allowCheatsForAllPlayers;
++ return this.ops.contains(profile) || this.server.isSingleplayerOwner(profile) && this.server.getWorldData().getAllowCommands() || this.allowCheatsForAllPlayers;
+ }
+
+ @Nullable
+ public ServerPlayer getPlayerByName(String username) {
+- int size = this.players.size();
++ int i = this.players.size();
+
+- for (int i = 0; i < size; i++) {
+- ServerPlayer serverPlayer = this.players.get(i);
+- if (serverPlayer.getGameProfile().getName().equalsIgnoreCase(username)) {
+- return serverPlayer;
++ for (int j = 0; j < i; ++j) {
++ ServerPlayer entityplayer = (ServerPlayer) this.players.get(j);
++
++ if (entityplayer.getGameProfile().getName().equalsIgnoreCase(username)) {
++ return entityplayer;
+ }
+ }
+
+ return null;
+ }
+
+- public void broadcast(@Nullable Player except, double x, double y, double z, double radius, ResourceKey<Level> dimension, Packet<?> packet) {
+- for (int i = 0; i < this.players.size(); i++) {
+- ServerPlayer serverPlayer = this.players.get(i);
+- if (serverPlayer != except && serverPlayer.level().dimension() == dimension) {
+- double d = x - serverPlayer.getX();
+- double d1 = y - serverPlayer.getY();
+- double d2 = z - serverPlayer.getZ();
+- if (d * d + d1 * d1 + d2 * d2 < radius * radius) {
+- serverPlayer.connection.send(packet);
++ public void broadcast(@Nullable net.minecraft.world.entity.player.Player except, double x, double d1, double y, double d3, ResourceKey<Level> z, Packet<?> packet) {
++ for (int i = 0; i < this.players.size(); ++i) {
++ ServerPlayer entityplayer = (ServerPlayer) this.players.get(i);
++
++ // CraftBukkit start - Test if player receiving packet can see the source of the packet
++ if (except != null && !entityplayer.getBukkitEntity().canSee(except.getBukkitEntity())) {
++ continue;
++ }
++ // CraftBukkit end
++
++ if (entityplayer != except && entityplayer.level().dimension() == z) {
++ double d4 = x - entityplayer.getX();
++ double d5 = d1 - entityplayer.getY();
++ double d6 = y - entityplayer.getZ();
++
++ if (d4 * d4 + d5 * d5 + d6 * d6 < d3 * d3) {
++ entityplayer.connection.send(packet);
+ }
+ }
+ }
++
+ }
+
+ public void saveAll() {
+- for (int i = 0; i < this.players.size(); i++) {
+- this.save(this.players.get(i));
++ for (int i = 0; i < this.players.size(); ++i) {
++ this.save((ServerPlayer) this.players.get(i));
+ }
++
+ }
+
+ public UserWhiteList getWhiteList() {
+@@ -684,18 +1045,22 @@
+ return this.ops.getUserList();
+ }
+
+- public void reloadWhiteList() {
+- }
++ public void reloadWhiteList() {}
+
+ public void sendLevelInfo(ServerPlayer player, ServerLevel level) {
+- WorldBorder worldBorder = this.server.overworld().getWorldBorder();
+- player.connection.send(new ClientboundInitializeBorderPacket(worldBorder));
++ WorldBorder worldborder = player.level().getWorldBorder(); // CraftBukkit
++
++ player.connection.send(new ClientboundInitializeBorderPacket(worldborder));
+ player.connection.send(new ClientboundSetTimePacket(level.getGameTime(), level.getDayTime(), level.getGameRules().getBoolean(GameRules.RULE_DAYLIGHT)));
+ player.connection.send(new ClientboundSetDefaultSpawnPositionPacket(level.getSharedSpawnPos(), level.getSharedSpawnAngle()));
+ if (level.isRaining()) {
+- player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.START_RAINING, 0.0F));
+- player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.RAIN_LEVEL_CHANGE, level.getRainLevel(1.0F)));
+- player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, level.getThunderLevel(1.0F)));
++ // CraftBukkit start - handle player weather
++ // entityplayer.connection.send(new PacketPlayOutGameStateChange(PacketPlayOutGameStateChange.START_RAINING, 0.0F));
++ // entityplayer.connection.send(new PacketPlayOutGameStateChange(PacketPlayOutGameStateChange.RAIN_LEVEL_CHANGE, worldserver.getRainLevel(1.0F)));
++ // entityplayer.connection.send(new PacketPlayOutGameStateChange(PacketPlayOutGameStateChange.THUNDER_LEVEL_CHANGE, worldserver.getThunderLevel(1.0F)));
++ player.setPlayerWeather(org.bukkit.WeatherType.DOWNFALL, false);
++ player.updateWeather(-level.rainLevel, level.rainLevel, -level.thunderLevel, level.thunderLevel);
++ // CraftBukkit end
+ }
+
+ player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.LEVEL_CHUNKS_LOAD_START, 0.0F));
+@@ -704,8 +1069,16 @@
+
+ public void sendAllPlayerInfo(ServerPlayer player) {
+ player.inventoryMenu.sendAllDataToRemote();
+- player.resetSentInfo();
++ // entityplayer.resetSentInfo();
++ player.getBukkitEntity().updateScaledHealth(); // CraftBukkit - Update scaled health on respawn and worldchange
++ player.getEntityData().refresh(player); // CraftBukkkit - SPIGOT-7218: sync metadata
+ player.connection.send(new ClientboundSetCarriedItemPacket(player.getInventory().selected));
++ // CraftBukkit start - from GameRules
++ int i = player.level().getGameRules().getBoolean(GameRules.RULE_REDUCEDDEBUGINFO) ? 22 : 23;
++ player.connection.send(new ClientboundEntityEventPacket(player, (byte) i));
++ float immediateRespawn = player.level().getGameRules().getBoolean(GameRules.RULE_DO_IMMEDIATE_RESPAWN) ? 1.0F: 0.0F;
++ player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.IMMEDIATE_RESPAWN, immediateRespawn));
++ // CraftBukkit end
+ }
+
+ public int getPlayerCount() {
+@@ -726,10 +1099,13 @@
+
+ public List<ServerPlayer> getPlayersWithAddress(String address) {
+ List<ServerPlayer> list = Lists.newArrayList();
++ Iterator iterator = this.players.iterator();
+
+- for (ServerPlayer serverPlayer : this.players) {
+- if (serverPlayer.getIpAddress().equals(address)) {
+- list.add(serverPlayer);
++ while (iterator.hasNext()) {
++ ServerPlayer entityplayer = (ServerPlayer) iterator.next();
++
++ if (entityplayer.getIpAddress().equals(address)) {
++ list.add(entityplayer);
+ }
+ }
+
+@@ -758,111 +1134,153 @@
+ }
+
+ public void removeAll() {
+- for (int i = 0; i < this.players.size(); i++) {
+- this.players.get(i).connection.disconnect(Component.translatable("multiplayer.disconnect.server_shutdown"));
++ // CraftBukkit start - disconnect safely
++ for (ServerPlayer player : this.players) {
++ player.connection.disconnect(this.server.server.getShutdownMessage()); // CraftBukkit - add custom shutdown message
+ }
++ // CraftBukkit end
++
+ }
+
++ // CraftBukkit start
++ public void broadcastMessage(Component[] iChatBaseComponents) {
++ for (Component component : iChatBaseComponents) {
++ broadcastSystemMessage(component, false);
++ }
++ }
++ // CraftBukkit end
++
+ public void broadcastSystemMessage(Component message, boolean bypassHiddenChat) {
+- this.broadcastSystemMessage(message, serverPlayer -> message, bypassHiddenChat);
++ this.broadcastSystemMessage(message, (entityplayer) -> {
++ return message;
++ }, bypassHiddenChat);
+ }
+
+ public void broadcastSystemMessage(Component serverMessage, Function<ServerPlayer, Component> playerMessageFactory, boolean bypassHiddenChat) {
+ this.server.sendSystemMessage(serverMessage);
++ Iterator iterator = this.players.iterator();
+
+- for (ServerPlayer serverPlayer : this.players) {
+- Component component = playerMessageFactory.apply(serverPlayer);
+- if (component != null) {
+- serverPlayer.sendSystemMessage(component, bypassHiddenChat);
++ while (iterator.hasNext()) {
++ ServerPlayer entityplayer = (ServerPlayer) iterator.next();
++ Component ichatbasecomponent1 = (Component) playerMessageFactory.apply(entityplayer);
++
++ if (ichatbasecomponent1 != null) {
++ entityplayer.sendSystemMessage(ichatbasecomponent1, bypassHiddenChat);
+ }
+ }
++
+ }
+
+ public void broadcastChatMessage(PlayerChatMessage message, CommandSourceStack sender, ChatType.Bound boundChatType) {
++ Objects.requireNonNull(sender);
+ this.broadcastChatMessage(message, sender::shouldFilterMessageTo, sender.getPlayer(), boundChatType);
+ }
+
+ public void broadcastChatMessage(PlayerChatMessage message, ServerPlayer sender, ChatType.Bound boundChatType) {
++ Objects.requireNonNull(sender);
+ this.broadcastChatMessage(message, sender::shouldFilterMessageTo, sender, boundChatType);
+ }
+
+- private void broadcastChatMessage(
+- PlayerChatMessage message, Predicate<ServerPlayer> shouldFilterMessageTo, @Nullable ServerPlayer sender, ChatType.Bound boundChatType
+- ) {
++ private void broadcastChatMessage(PlayerChatMessage message, Predicate<ServerPlayer> shouldFilterMessageTo, @Nullable ServerPlayer sender, ChatType.Bound boundChatType) {
+ boolean flag = this.verifyChatTrusted(message);
++
+ this.server.logChatMessage(message.decoratedContent(), boundChatType, flag ? null : "Not Secure");
+- OutgoingChatMessage outgoingChatMessage = OutgoingChatMessage.create(message);
++ OutgoingChatMessage outgoingchatmessage = OutgoingChatMessage.create(message);
+ boolean flag1 = false;
+
+- for (ServerPlayer serverPlayer : this.players) {
+- boolean flag2 = shouldFilterMessageTo.test(serverPlayer);
+- serverPlayer.sendChatMessage(outgoingChatMessage, flag2, boundChatType);
+- flag1 |= flag2 && message.isFullyFiltered();
++ boolean flag2;
++
++ for (Iterator iterator = this.players.iterator(); iterator.hasNext(); flag1 |= flag2 && message.isFullyFiltered()) {
++ ServerPlayer entityplayer1 = (ServerPlayer) iterator.next();
++
++ flag2 = shouldFilterMessageTo.test(entityplayer1);
++ entityplayer1.sendChatMessage(outgoingchatmessage, flag2, boundChatType);
+ }
+
+ if (flag1 && sender != null) {
+- sender.sendSystemMessage(CHAT_FILTERED_FULL);
++ sender.sendSystemMessage(PlayerList.CHAT_FILTERED_FULL);
+ }
++
+ }
+
+ private boolean verifyChatTrusted(PlayerChatMessage message) {
+ return message.hasSignature() && !message.hasExpiredServer(Instant.now());
+ }
+
+- public ServerStatsCounter getPlayerStats(Player player) {
+- UUID uUID = player.getUUID();
+- ServerStatsCounter serverStatsCounter = this.stats.get(uUID);
+- if (serverStatsCounter == null) {
++ // CraftBukkit start
++ public ServerStatsCounter getPlayerStats(ServerPlayer entityhuman) {
++ ServerStatsCounter serverstatisticmanager = entityhuman.getStats();
++ return serverstatisticmanager == null ? getPlayerStats(entityhuman.getUUID(), entityhuman.getDisplayName().getString()) : serverstatisticmanager;
++ }
++
++ public ServerStatsCounter getPlayerStats(UUID uuid, String displayName) {
++ ServerPlayer entityhuman = this.getPlayer(uuid);
++ ServerStatsCounter serverstatisticmanager = entityhuman == null ? null : (ServerStatsCounter) entityhuman.getStats();
++ // CraftBukkit end
++
++ if (serverstatisticmanager == null) {
+ File file = this.server.getWorldPath(LevelResource.PLAYER_STATS_DIR).toFile();
+- File file1 = new File(file, uUID + ".json");
++ File file1 = new File(file, uuid + ".json");
++
+ if (!file1.exists()) {
+- File file2 = new File(file, player.getName().getString() + ".json");
++ File file2 = new File(file, displayName + ".json"); // CraftBukkit
+ Path path = file2.toPath();
++
+ if (FileUtil.isPathNormalized(path) && FileUtil.isPathPortable(path) && path.startsWith(file.getPath()) && file2.isFile()) {
+ file2.renameTo(file1);
+ }
+ }
+
+- serverStatsCounter = new ServerStatsCounter(this.server, file1);
+- this.stats.put(uUID, serverStatsCounter);
++ serverstatisticmanager = new ServerStatsCounter(this.server, file1);
++ // this.stats.put(uuid, serverstatisticmanager); // CraftBukkit
+ }
+
+- return serverStatsCounter;
++ return serverstatisticmanager;
+ }
+
+ public PlayerAdvancements getPlayerAdvancements(ServerPlayer player) {
+- UUID uUID = player.getUUID();
+- PlayerAdvancements playerAdvancements = this.advancements.get(uUID);
+- if (playerAdvancements == null) {
+- Path path = this.server.getWorldPath(LevelResource.PLAYER_ADVANCEMENTS_DIR).resolve(uUID + ".json");
+- playerAdvancements = new PlayerAdvancements(this.server.getFixerUpper(), this, this.server.getAdvancements(), path, player);
+- this.advancements.put(uUID, playerAdvancements);
++ UUID uuid = player.getUUID();
++ PlayerAdvancements advancementdataplayer = (PlayerAdvancements) player.getAdvancements(); // CraftBukkit
++
++ if (advancementdataplayer == null) {
++ Path path = this.server.getWorldPath(LevelResource.PLAYER_ADVANCEMENTS_DIR).resolve(uuid + ".json");
++
++ advancementdataplayer = new PlayerAdvancements(this.server.getFixerUpper(), this, this.server.getAdvancements(), path, player);
++ // this.advancements.put(uuid, advancementdataplayer); // CraftBukkit
+ }
+
+- playerAdvancements.setPlayer(player);
+- return playerAdvancements;
++ advancementdataplayer.setPlayer(player);
++ return advancementdataplayer;
+ }
+
+ public void setViewDistance(int viewDistance) {
+ this.viewDistance = viewDistance;
+ this.broadcastAll(new ClientboundSetChunkCacheRadiusPacket(viewDistance));
++ Iterator iterator = this.server.getAllLevels().iterator();
+
+- for (ServerLevel serverLevel : this.server.getAllLevels()) {
+- if (serverLevel != null) {
+- serverLevel.getChunkSource().setViewDistance(viewDistance);
++ while (iterator.hasNext()) {
++ ServerLevel worldserver = (ServerLevel) iterator.next();
++
++ if (worldserver != null) {
++ worldserver.getChunkSource().setViewDistance(viewDistance);
+ }
+ }
++
+ }
+
+ public void setSimulationDistance(int simulationDistance) {
+ this.simulationDistance = simulationDistance;
+ this.broadcastAll(new ClientboundSetSimulationDistancePacket(simulationDistance));
++ Iterator iterator = this.server.getAllLevels().iterator();
+
+- for (ServerLevel serverLevel : this.server.getAllLevels()) {
+- if (serverLevel != null) {
+- serverLevel.getChunkSource().setSimulationDistance(simulationDistance);
++ while (iterator.hasNext()) {
++ ServerLevel worldserver = (ServerLevel) iterator.next();
++
++ if (worldserver != null) {
++ worldserver.getChunkSource().setSimulationDistance(simulationDistance);
+ }
+ }
++
+ }
+
+ public List<ServerPlayer> getPlayers() {
+@@ -871,7 +1289,7 @@
+
+ @Nullable
+ public ServerPlayer getPlayer(UUID playerUUID) {
+- return this.playersByUUID.get(playerUUID);
++ return (ServerPlayer) this.playersByUUID.get(playerUUID);
+ }
+
+ public boolean canBypassPlayerLimit(GameProfile profile) {
+@@ -879,17 +1297,32 @@
+ }
+
+ public void reloadResources() {
+- for (PlayerAdvancements playerAdvancements : this.advancements.values()) {
+- playerAdvancements.reload(this.server.getAdvancements());
++ // CraftBukkit start
++ /*Iterator iterator = this.advancements.values().iterator();
++
++ while (iterator.hasNext()) {
++ AdvancementDataPlayer advancementdataplayer = (AdvancementDataPlayer) iterator.next();
++
++ advancementdataplayer.reload(this.server.getAdvancements());
++ }*/
++
++ for (ServerPlayer player : players) {
++ player.getAdvancements().reload(this.server.getAdvancements());
++ player.getAdvancements().flushDirty(player); // CraftBukkit - trigger immediate flush of advancements
+ }
++ // CraftBukkit end
+
+ this.broadcastAll(new ClientboundUpdateTagsPacket(TagNetworkSerialization.serializeTagsToNetwork(this.registries)));
+- ClientboundUpdateRecipesPacket clientboundUpdateRecipesPacket = new ClientboundUpdateRecipesPacket(this.server.getRecipeManager().getRecipes());
++ ClientboundUpdateRecipesPacket packetplayoutrecipeupdate = new ClientboundUpdateRecipesPacket(this.server.getRecipeManager().getRecipes());
++ Iterator iterator1 = this.players.iterator();
+
+- for (ServerPlayer serverPlayer : this.players) {
+- serverPlayer.connection.send(clientboundUpdateRecipesPacket);
+- serverPlayer.getRecipeBook().sendInitialRecipeBook(serverPlayer);
++ while (iterator1.hasNext()) {
++ ServerPlayer entityplayer = (ServerPlayer) iterator1.next();
++
++ entityplayer.connection.send(packetplayoutrecipeupdate);
++ entityplayer.getRecipeBook().sendInitialRecipeBook(entityplayer);
+ }
++
+ }
+
+ public boolean isAllowCheatsForAllPlayers() {