| 1 | package net.minecraft.server.waypoints;
|
| 2 |
|
| 3 | import com.google.common.collect.HashBasedTable;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import com.google.common.collect.Table;
|
| 7 | import com.google.common.collect.Tables;
|
| 8 | import com.google.common.collect.Sets.SetView;
|
| 9 | import java.util.HashSet;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import net.minecraft.server.level.ServerPlayer;
|
| 14 | import net.minecraft.world.level.gamerules.GameRules;
|
| 15 | import net.minecraft.world.waypoints.WaypointManager;
|
| 16 | import net.minecraft.world.waypoints.WaypointTransmitter;
|
| 17 |
|
| 18 | public class ServerWaypointManager implements WaypointManager<WaypointTransmitter> {
|
| 19 | private final Set<WaypointTransmitter> waypoints = new HashSet<>();
|
| 20 | private final Set<ServerPlayer> players = new HashSet<>();
|
| 21 | private final Table<ServerPlayer, WaypointTransmitter, WaypointTransmitter.Connection> connections = HashBasedTable.create();
|
| 22 |
|
| 23 | public void trackWaypoint(final WaypointTransmitter waypoint) {
|
| 24 | this.waypoints.add(waypoint);
|
| 25 |
|
| 26 | for (ServerPlayer player : this.players) {
|
| 27 | this.createConnection(player, waypoint);
|
| 28 | }
|
| 29 | }
|
| 30 |
|
| 31 | public void updateWaypoint(final WaypointTransmitter waypoint) {
|
| 32 | if (this.waypoints.contains(waypoint)) {
|
| 33 | Map<ServerPlayer, WaypointTransmitter.Connection> playerConnection = Tables.transpose(this.connections).row(waypoint);
|
| 34 | SetView<ServerPlayer> potentialPlayers = Sets.difference(this.players, playerConnection.keySet());
|
| 35 |
|
| 36 | for (Entry<ServerPlayer, WaypointTransmitter.Connection> waypointConnection : ImmutableSet.copyOf(playerConnection.entrySet())) {
|
| 37 | this.updateConnection(waypointConnection.getKey(), waypoint, waypointConnection.getValue());
|
| 38 | }
|
| 39 |
|
| 40 | for (ServerPlayer player : potentialPlayers) {
|
| 41 | this.createConnection(player, waypoint);
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | public void untrackWaypoint(final WaypointTransmitter waypoint) {
|
| 47 | this.connections.column(waypoint).forEach((player, connection) -> connection.disconnect());
|
| 48 | Tables.transpose(this.connections).row(waypoint).clear();
|
| 49 | this.waypoints.remove(waypoint);
|
| 50 | }
|
| 51 |
|
| 52 | public void addPlayer(final ServerPlayer player) {
|
| 53 | this.players.add(player);
|
| 54 |
|
| 55 | for (WaypointTransmitter waypoint : this.waypoints) {
|
| 56 | this.createConnection(player, waypoint);
|
| 57 | }
|
| 58 |
|
| 59 | if (player.isTransmittingWaypoint()) {
|
| 60 | this.trackWaypoint(player);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | public void updatePlayer(final ServerPlayer player) {
|
| 65 | Map<WaypointTransmitter, WaypointTransmitter.Connection> waypointConnections = this.connections.row(player);
|
| 66 | SetView<WaypointTransmitter> potentialWaypoints = Sets.difference(this.waypoints, waypointConnections.keySet());
|
| 67 |
|
| 68 | for (Entry<WaypointTransmitter, WaypointTransmitter.Connection> waypointConnection : ImmutableSet.copyOf(waypointConnections.entrySet())) {
|
| 69 | this.updateConnection(player, waypointConnection.getKey(), waypointConnection.getValue());
|
| 70 | }
|
| 71 |
|
| 72 | for (WaypointTransmitter waypoint : potentialWaypoints) {
|
| 73 | this.createConnection(player, waypoint);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | public void removePlayer(final ServerPlayer player) {
|
| 78 | this.connections.row(player).values().removeIf(connection -> {
|
| 79 | connection.disconnect();
|
| 80 | return true;
|
| 81 | });
|
| 82 | this.untrackWaypoint(player);
|
| 83 | this.players.remove(player);
|
| 84 | }
|
| 85 |
|
| 86 | public void breakAllConnections() {
|
| 87 | this.connections.values().forEach(WaypointTransmitter.Connection::disconnect);
|
| 88 | this.connections.clear();
|
| 89 | }
|
| 90 |
|
| 91 | public void remakeConnections(final WaypointTransmitter waypoint) {
|
| 92 | for (ServerPlayer player : this.players) {
|
| 93 | this.createConnection(player, waypoint);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | public Set<WaypointTransmitter> transmitters() {
|
| 98 | return this.waypoints;
|
| 99 | }
|
| 100 |
|
| 101 | private static boolean isLocatorBarEnabledFor(final ServerPlayer player) {
|
| 102 | return player.level().getGameRules().get(GameRules.LOCATOR_BAR);
|
| 103 | }
|
| 104 |
|
| 105 | private void createConnection(final ServerPlayer player, final WaypointTransmitter waypoint) {
|
| 106 | if (player != waypoint) {
|
| 107 | if (isLocatorBarEnabledFor(player)) {
|
| 108 | waypoint.makeWaypointConnectionWith(player).ifPresentOrElse(connection -> {
|
| 109 | this.connections.put(player, waypoint, connection);
|
| 110 | connection.connect();
|
| 111 | }, () -> {
|
| 112 | WaypointTransmitter.Connection connection = this.connections.remove(player, waypoint);
|
| 113 | if (connection != null) {
|
| 114 | connection.disconnect();
|
| 115 | }
|
| 116 | });
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | private void updateConnection(final ServerPlayer player, final WaypointTransmitter waypoint, final WaypointTransmitter.Connection connection) {
|
| 122 | if (player != waypoint) {
|
| 123 | if (isLocatorBarEnabledFor(player)) {
|
| 124 | if (!connection.isBroken()) {
|
| 125 | connection.update();
|
| 126 | } else {
|
| 127 | waypoint.makeWaypointConnectionWith(player).ifPresentOrElse(newConnection -> {
|
| 128 | newConnection.connect();
|
| 129 | this.connections.put(player, waypoint, newConnection);
|
| 130 | }, () -> {
|
| 131 | connection.disconnect();
|
| 132 | this.connections.remove(player, waypoint);
|
| 133 | });
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|