|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: MidnightTale < [email protected]> |
| 3 | +Date: Sun, 20 Jul 2025 22:46:45 +0700 |
| 4 | +Subject: [PATCH] Waypoint thread safe |
| 5 | + |
| 6 | + |
| 7 | +diff --git a/net/minecraft/server/waypoints/ServerWaypointManager.java b/net/minecraft/server/waypoints/ServerWaypointManager.java |
| 8 | +index 7dc3f46c82dcdfa30438bc4016fb9fc068680341..75c248892b0ff7b49a623612c0bd546c906989c8 100644 |
| 9 | +--- a/net/minecraft/server/waypoints/ServerWaypointManager.java |
| 10 | ++++ b/net/minecraft/server/waypoints/ServerWaypointManager.java |
| 11 | +@@ -6,6 +6,8 @@ import com.google.common.collect.Sets; |
| 12 | + import com.google.common.collect.Table; |
| 13 | + import com.google.common.collect.Tables; |
| 14 | + import com.google.common.collect.Sets.SetView; |
| 15 | ++ |
| 16 | ++import java.util.Collections; |
| 17 | + import java.util.HashSet; |
| 18 | + import java.util.Map; |
| 19 | + import java.util.Set; |
| 20 | +@@ -16,32 +18,46 @@ import net.minecraft.world.waypoints.WaypointManager; |
| 21 | + import net.minecraft.world.waypoints.WaypointTransmitter; |
| 22 | + |
| 23 | + public class ServerWaypointManager implements WaypointManager<WaypointTransmitter> { |
| 24 | +- private final Set<WaypointTransmitter> waypoints = new HashSet<>(); |
| 25 | +- private final Set<ServerPlayer> players = new HashSet<>(); |
| 26 | +- private final Table<ServerPlayer, WaypointTransmitter, WaypointTransmitter.Connection> connections = HashBasedTable.create(); |
| 27 | ++ // atDeprecated start - Thread safe collections |
| 28 | ++ private final Set<WaypointTransmitter> waypoints = Collections.synchronizedSet(new HashSet<>()); |
| 29 | ++ private final Set<ServerPlayer> players = Collections.synchronizedSet(new HashSet<>()); |
| 30 | ++ private final Table<ServerPlayer, WaypointTransmitter, WaypointTransmitter.Connection> connections = Tables.synchronizedTable(HashBasedTable.create()); |
| 31 | ++ // atDeprecated end |
| 32 | + |
| 33 | + @Override |
| 34 | + public void trackWaypoint(WaypointTransmitter waypoint) { |
| 35 | + // atDeprecated start - Force enabled waypoint |
| 36 | +- this.waypoints.add(waypoint); |
| 37 | +- for (ServerPlayer serverPlayer : this.players) {this.createConnection(serverPlayer, waypoint);} |
| 38 | ++ synchronized (this) { |
| 39 | ++ this.waypoints.add(waypoint); |
| 40 | ++ for (ServerPlayer serverPlayer : this.players) { |
| 41 | ++ this.createConnection(serverPlayer, waypoint); |
| 42 | ++ } |
| 43 | ++ } |
| 44 | + // atDeprecated end - Force enabled waypoint |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void updateWaypoint(WaypointTransmitter waypoint) { |
| 49 | +- if (this.waypoints.contains(waypoint)) { |
| 50 | +- Map<ServerPlayer, WaypointTransmitter.Connection> map = Tables.transpose(this.connections).row(waypoint); |
| 51 | +- SetView<ServerPlayer> set = Sets.difference(this.players, map.keySet()); |
| 52 | +- |
| 53 | +- for (Entry<ServerPlayer, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) { |
| 54 | +- this.updateConnection(entry.getKey(), waypoint, entry.getValue()); |
| 55 | +- } |
| 56 | ++ // atDeprecated start - Thread safe |
| 57 | ++ synchronized (this) { |
| 58 | ++ if (this.waypoints.contains(waypoint)) { |
| 59 | ++ Map<ServerPlayer, WaypointTransmitter.Connection> map = Tables.transpose(this.connections).row(waypoint); |
| 60 | ++ Set<ServerPlayer> currentPlayers = ImmutableSet.copyOf(this.players); |
| 61 | ++ SetView<ServerPlayer> set = Sets.difference(currentPlayers, map.keySet()); |
| 62 | ++ |
| 63 | ++ // Update existing connections |
| 64 | ++ Set<Entry<ServerPlayer, WaypointTransmitter.Connection>> entries = ImmutableSet.copyOf(map.entrySet()); |
| 65 | ++ for (Entry<ServerPlayer, WaypointTransmitter.Connection> entry : entries) { |
| 66 | ++ this.updateConnection(entry.getKey(), waypoint, entry.getValue()); |
| 67 | ++ } |
| 68 | + |
| 69 | +- for (ServerPlayer serverPlayer : set) { |
| 70 | +- this.createConnection(serverPlayer, waypoint); |
| 71 | ++ // Create new connections |
| 72 | ++ for (ServerPlayer serverPlayer : set) { |
| 73 | ++ this.createConnection(serverPlayer, waypoint); |
| 74 | ++ } |
| 75 | + } |
| 76 | + } |
| 77 | ++ // atDeprecated end |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | +@@ -53,41 +69,65 @@ public class ServerWaypointManager implements WaypointManager<WaypointTransmitte |
| 82 | + |
| 83 | + public void addPlayer(ServerPlayer player) { |
| 84 | + // atDeprecated start - Force enabled waypoint |
| 85 | +- this.players.add(player); |
| 86 | +- for (WaypointTransmitter waypointTransmitter : this.waypoints) {this.createConnection(player, waypointTransmitter);} |
| 87 | +- if (player.isTransmittingWaypoint()) {this.trackWaypoint((WaypointTransmitter)player);} |
| 88 | ++ synchronized (this) { |
| 89 | ++ this.players.add(player); |
| 90 | ++ for (WaypointTransmitter waypointTransmitter : this.waypoints) { |
| 91 | ++ this.createConnection(player, waypointTransmitter); |
| 92 | ++ } |
| 93 | ++ if (player.isTransmittingWaypoint()) { |
| 94 | ++ this.trackWaypoint((WaypointTransmitter)player); |
| 95 | ++ } |
| 96 | ++ } |
| 97 | + // atDeprecated end - Force enabled waypoint |
| 98 | + } |
| 99 | + |
| 100 | + public void updatePlayer(ServerPlayer player) { |
| 101 | + // atDeprecated start - Force enabled waypoint |
| 102 | +- Map<WaypointTransmitter, WaypointTransmitter.Connection> map = this.connections.row(player); |
| 103 | +- SetView<WaypointTransmitter> set = Sets.difference(this.waypoints, map.keySet()); |
| 104 | +- for (Entry<WaypointTransmitter, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) {this.updateConnection(player, entry.getKey(), entry.getValue());} |
| 105 | +- for (WaypointTransmitter waypointTransmitter : set) {this.createConnection(player, waypointTransmitter);} |
| 106 | ++ synchronized (this) { |
| 107 | ++ Map<WaypointTransmitter, WaypointTransmitter.Connection> map = this.connections.row(player); |
| 108 | ++ SetView<WaypointTransmitter> set = Sets.difference(this.waypoints, map.keySet()); |
| 109 | ++ for (Entry<WaypointTransmitter, WaypointTransmitter.Connection> entry : ImmutableSet.copyOf(map.entrySet())) { |
| 110 | ++ this.updateConnection(player, entry.getKey(), entry.getValue()); |
| 111 | ++ } |
| 112 | ++ for (WaypointTransmitter waypointTransmitter : set) { |
| 113 | ++ this.createConnection(player, waypointTransmitter); |
| 114 | ++ } |
| 115 | ++ } |
| 116 | + // atDeprecated end - Force enabled waypoint |
| 117 | + } |
| 118 | + |
| 119 | + public void removePlayer(ServerPlayer player) { |
| 120 | + // atDeprecated start - Force enabled waypoint |
| 121 | +- this.connections.row(player).values().removeIf(connection -> { |
| 122 | +- connection.disconnect(); |
| 123 | +- return true; |
| 124 | +- }); |
| 125 | +- this.untrackWaypoint((WaypointTransmitter)player); |
| 126 | +- this.players.remove(player); |
| 127 | ++ synchronized (this) { |
| 128 | ++ this.connections.row(player).values().removeIf(connection -> { |
| 129 | ++ connection.disconnect(); |
| 130 | ++ return true; |
| 131 | ++ }); |
| 132 | ++ this.untrackWaypoint((WaypointTransmitter)player); |
| 133 | ++ this.players.remove(player); |
| 134 | ++ } |
| 135 | + // atDeprecated end - Force enabled waypoint |
| 136 | + } |
| 137 | + |
| 138 | + public void breakAllConnections() { |
| 139 | +- this.connections.values().forEach(WaypointTransmitter.Connection::disconnect); |
| 140 | +- this.connections.clear(); |
| 141 | ++ // atDeprecated start - Thread safe |
| 142 | ++ synchronized (this) { |
| 143 | ++ Set<WaypointTransmitter.Connection> currentConnections = ImmutableSet.copyOf(this.connections.values()); |
| 144 | ++ currentConnections.forEach(WaypointTransmitter.Connection::disconnect); |
| 145 | ++ this.connections.clear(); |
| 146 | ++ } |
| 147 | ++ // atDeprecated end |
| 148 | + } |
| 149 | + |
| 150 | + public void remakeConnections(WaypointTransmitter waypoint) { |
| 151 | +- for (ServerPlayer serverPlayer : this.players) { |
| 152 | +- this.createConnection(serverPlayer, waypoint); |
| 153 | ++ // atDeprecated start - Thread safe |
| 154 | ++ synchronized (this) { |
| 155 | ++ Set<ServerPlayer> currentPlayers = ImmutableSet.copyOf(this.players); |
| 156 | ++ for (ServerPlayer serverPlayer : currentPlayers) { |
| 157 | ++ this.createConnection(serverPlayer, waypoint); |
| 158 | ++ } |
| 159 | + } |
| 160 | ++ // atDeprecated end |
| 161 | + } |
| 162 | + |
| 163 | + public Set<WaypointTransmitter> transmitters() { |
| 164 | +@@ -132,3 +172,4 @@ public class ServerWaypointManager implements WaypointManager<WaypointTransmitte |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | ++ |
0 commit comments