@@ -239,3 +239,166 @@ index 0f8cacbb8fe55a60e2f0c98bf36c005b29f41a4b..7dc3f46c82dcdfa30438bc4016fb9fc0
239239 }
240240
241241 public void breakAllConnections() {
242+
243+ diff --git a/net/minecraft/server/waypoints/ServerWaypointManager.java b/net/minecraft/server/waypoints/ServerWaypointManager.java
244+ index 7dc3f46c82dcdfa30438bc4016fb9fc068680341..75c248892b0ff7b49a623612c0bd546c906989c8 100644
245+ --- a/net/minecraft/server/waypoints/ServerWaypointManager.java
246+ +++ b/net/minecraft/server/waypoints/ServerWaypointManager.java
247+ @@ -6,6 +6,8 @@ import com.google.common.collect.Sets;
248+ import com.google.common.collect.Table;
249+ import com.google.common.collect.Tables;
250+ import com.google.common.collect.Sets.SetView;
251+ +
252+ + import java.util.Collections;
253+ import java.util.HashSet;
254+ import java.util.Map;
255+ import java.util.Set;
256+ @@ -16,32 +18,46 @@ import net.minecraft.world.waypoints.WaypointManager;
257+ import net.minecraft.world.waypoints.WaypointTransmitter;
258+
259+ public class ServerWaypointManager implements WaypointManager<WaypointTransmitter> {
260+ - private final Set<WaypointTransmitter> waypoints = new HashSet<>();
261+ - private final Set<ServerPlayer> players = new HashSet<>();
262+ - private final Table<ServerPlayer, WaypointTransmitter, WaypointTransmitter.Connection> connections = HashBasedTable.create();
263+ + // atDeprecated start - Thread safe collections
264+ + private final Set<WaypointTransmitter> waypoints = Collections.synchronizedSet(new HashSet<>());
265+ + private final Set<ServerPlayer> players = Collections.synchronizedSet(new HashSet<>());
266+ + private final Table<ServerPlayer, WaypointTransmitter, WaypointTransmitter.Connection> connections = Tables.synchronizedTable(HashBasedTable.create());
267+ + // atDeprecated end
268+
269+ @Override
270+ public void trackWaypoint(WaypointTransmitter waypoint) {
271+ // atDeprecated start - Force enabled waypoint
272+ - this.waypoints.add(waypoint);
273+ - for (ServerPlayer serverPlayer : this.players) {this.createConnection(serverPlayer, waypoint);}
274+ + synchronized (this) {
275+ + this.waypoints.add(waypoint);
276+ + for (ServerPlayer serverPlayer : this.players) {
277+ + this.createConnection(serverPlayer, waypoint);
278+ + }
279+ + }
280+ // atDeprecated end - Force enabled waypoint
281+ }
282+
283+ @Override
284+ public void updateWaypoint(WaypointTransmitter waypoint) {
285+ - if (this.waypoints.contains(waypoint)) {
286+ - Map<ServerPlayer, WaypointTransmitter.Connection> map = Tables.transpose(this.connections).row(waypoint);
287+ - SetView<ServerPlayer> set = Sets.difference(this.players, map.keySet());
288+ -
289+ - for (Entry<ServerPlayer, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) {
290+ - this.updateConnection(entry.getKey(), waypoint, entry.getValue());
291+ - }
292+ + // atDeprecated start - Thread safe
293+ + synchronized (this) {
294+ + if (this.waypoints.contains(waypoint)) {
295+ + Map<ServerPlayer, WaypointTransmitter.Connection> map = Tables.transpose(this.connections).row(waypoint);
296+ + Set<ServerPlayer> currentPlayers = ImmutableSet.copyOf(this.players);
297+ + SetView<ServerPlayer> set = Sets.difference(currentPlayers, map.keySet());
298+ +
299+ + // Update existing connections
300+ + Set<Entry<ServerPlayer, WaypointTransmitter.Connection>> entries = ImmutableSet.copyOf(map.entrySet());
301+ + for (Entry<ServerPlayer, WaypointTransmitter.Connection> entry : entries) {
302+ + this.updateConnection(entry.getKey(), waypoint, entry.getValue());
303+ + }
304+
305+ - for (ServerPlayer serverPlayer : set) {
306+ - this.createConnection(serverPlayer, waypoint);
307+ + // Create new connections
308+ + for (ServerPlayer serverPlayer : set) {
309+ + this.createConnection(serverPlayer, waypoint);
310+ + }
311+ }
312+ }
313+ + // atDeprecated end
314+ }
315+
316+ @Override
317+ @@ -53,41 +69,65 @@ public class ServerWaypointManager implements WaypointManager<WaypointTransmitte
318+
319+ public void addPlayer(ServerPlayer player) {
320+ // atDeprecated start - Force enabled waypoint
321+ - this.players.add(player);
322+ - for (WaypointTransmitter waypointTransmitter : this.waypoints) {this.createConnection(player, waypointTransmitter);}
323+ - if (player.isTransmittingWaypoint()) {this.trackWaypoint((WaypointTransmitter)player);}
324+ + synchronized (this) {
325+ + this.players.add(player);
326+ + for (WaypointTransmitter waypointTransmitter : this.waypoints) {
327+ + this.createConnection(player, waypointTransmitter);
328+ + }
329+ + if (player.isTransmittingWaypoint()) {
330+ + this.trackWaypoint((WaypointTransmitter)player);
331+ + }
332+ + }
333+ // atDeprecated end - Force enabled waypoint
334+ }
335+
336+ public void updatePlayer(ServerPlayer player) {
337+ // atDeprecated start - Force enabled waypoint
338+ - Map<WaypointTransmitter, WaypointTransmitter.Connection> map = this.connections.row(player);
339+ - SetView<WaypointTransmitter> set = Sets.difference(this.waypoints, map.keySet());
340+ - for (Entry<WaypointTransmitter, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) {this.updateConnection(player, entry.getKey(), entry.getValue());}
341+ - for (WaypointTransmitter waypointTransmitter : set) {this.createConnection(player, waypointTransmitter);}
342+ + synchronized (this) {
343+ + Map<WaypointTransmitter, WaypointTransmitter.Connection> map = this.connections.row(player);
344+ + SetView<WaypointTransmitter> set = Sets.difference(this.waypoints, map.keySet());
345+ + for (Entry<WaypointTransmitter, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) {
346+ + this.updateConnection(player, entry.getKey(), entry.getValue());
347+ + }
348+ + for (WaypointTransmitter waypointTransmitter : ImmutableSet.copyOf(set)) {
349+ + this.createConnection(player, waypointTransmitter);
350+ + }
351+ + }
352+ // atDeprecated end - Force enabled waypoint
353+ }
354+
355+ public void removePlayer(ServerPlayer player) {
356+ // atDeprecated start - Force enabled waypoint
357+ - this.connections.row(player).values().removeIf(connection -> {
358+ - connection.disconnect();
359+ - return true;
360+ - });
361+ - this.untrackWaypoint((WaypointTransmitter)player);
362+ - this.players.remove(player);
363+ + synchronized (this) {
364+ + this.connections.row(player).values().removeIf(connection -> {
365+ + connection.disconnect();
366+ + return true;
367+ + });
368+ + this.untrackWaypoint((WaypointTransmitter)player);
369+ + this.players.remove(player);
370+ + }
371+ // atDeprecated end - Force enabled waypoint
372+ }
373+
374+ public void breakAllConnections() {
375+ - this.connections.values().forEach(WaypointTransmitter.Connection::disconnect);
376+ - this.connections.clear();
377+ + // atDeprecated start - Thread safe
378+ + synchronized (this) {
379+ + Set<WaypointTransmitter.Connection> currentConnections = ImmutableSet.copyOf(this.connections.values());
380+ + currentConnections.forEach(WaypointTransmitter.Connection::disconnect);
381+ + this.connections.clear();
382+ + }
383+ + // atDeprecated end
384+ }
385+
386+ public void remakeConnections(WaypointTransmitter waypoint) {
387+ - for (ServerPlayer serverPlayer : this.players) {
388+ - this.createConnection(serverPlayer, waypoint);
389+ + // atDeprecated start - Thread safe
390+ + synchronized (this) {
391+ + Set<ServerPlayer> currentPlayers = ImmutableSet.copyOf(this.players);
392+ + for (ServerPlayer serverPlayer : currentPlayers) {
393+ + this.createConnection(serverPlayer, waypoint);
394+ + }
395+ }
396+ + // atDeprecated end
397+ }
398+
399+ public Set<WaypointTransmitter> transmitters() {
400+ @@ -132,3 +172,4 @@ public class ServerWaypointManager implements WaypointManager<WaypointTransmitte
401+ }
402+ }
403+ }
404+ +
0 commit comments