Half revert back to storing uuids in CommonBoss

Fixes #2547
This commit is contained in:
KennyTV 2021-06-14 22:47:13 +02:00
parent 7a82ded94b
commit 389c5adaa3
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B

View File

@ -32,15 +32,14 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.stream.Collectors;
public class CommonBoss implements BossBar { public class CommonBoss implements BossBar {
private final UUID uuid; private final UUID uuid;
private final Set<UserConnection> connections; private final Map<UUID, UserConnection> connections;
private final Set<BossFlag> flags; private final Set<BossFlag> flags;
private String title; private String title;
private float health; private float health;
@ -57,7 +56,7 @@ public class CommonBoss implements BossBar {
this.health = health; this.health = health;
this.color = color == null ? BossColor.PURPLE : color; this.color = color == null ? BossColor.PURPLE : color;
this.style = style == null ? BossStyle.SOLID : style; this.style = style == null ? BossStyle.SOLID : style;
this.connections = Collections.newSetFromMap(new WeakHashMap<>()); this.connections = new WeakHashMap<>();
this.flags = new HashSet<>(); this.flags = new HashSet<>();
visible = true; visible = true;
} }
@ -101,12 +100,16 @@ public class CommonBoss implements BossBar {
@Override @Override
public BossBar addPlayer(UUID player) { public BossBar addPlayer(UUID player) {
return addConnection(Via.getManager().getConnectionManager().getConnectedClient(player)); UserConnection client = Via.getManager().getConnectionManager().getConnectedClient(player);
if (client != null) {
addConnection(client);
}
return this;
} }
@Override @Override
public BossBar addConnection(UserConnection conn) { public BossBar addConnection(UserConnection conn) {
if (connections.add(conn) && visible) { if (connections.put(conn.getProtocolInfo().getUuid(), conn) == null && visible) {
sendPacketConnection(conn, getPacket(CommonBoss.UpdateAction.ADD, conn)); sendPacketConnection(conn, getPacket(CommonBoss.UpdateAction.ADD, conn));
} }
return this; return this;
@ -114,14 +117,16 @@ public class CommonBoss implements BossBar {
@Override @Override
public BossBar removePlayer(UUID uuid) { public BossBar removePlayer(UUID uuid) {
return removeConnection(Via.getManager().getConnectionManager().getConnectedClient(uuid)); UserConnection client = connections.remove(uuid);
if (client != null) {
sendPacketConnection(client, getPacket(UpdateAction.REMOVE, client));
}
return this;
} }
@Override @Override
public BossBar removeConnection(UserConnection conn) { public BossBar removeConnection(UserConnection conn) {
if (connections.remove(conn)) { removePlayer(conn.getProtocolInfo().getUuid());
sendPacketConnection(conn, getPacket(UpdateAction.REMOVE, conn));
}
return this; return this;
} }
@ -153,13 +158,12 @@ public class CommonBoss implements BossBar {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return connections.stream().map(conn -> Via.getManager().getConnectionManager().getConnectedClientId(conn)).filter(Objects::nonNull) return Collections.unmodifiableSet(connections.keySet());
.collect(Collectors.toSet());
} }
@Override @Override
public Set<UserConnection> getConnections() { public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections); return Collections.unmodifiableSet(new HashSet<>(connections.values()));
} }
@Override @Override
@ -215,7 +219,7 @@ public class CommonBoss implements BossBar {
} }
private void sendPacket(UpdateAction action) { private void sendPacket(UpdateAction action) {
for (UserConnection conn : new ArrayList<>(connections)) { for (UserConnection conn : new ArrayList<>(connections.values())) {
PacketWrapper wrapper = getPacket(action, conn); PacketWrapper wrapper = getPacket(action, conn);
sendPacketConnection(conn, wrapper); sendPacketConnection(conn, wrapper);
} }
@ -223,7 +227,7 @@ public class CommonBoss implements BossBar {
private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) { private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) {
if (conn.getProtocolInfo() == null || !conn.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) { if (conn.getProtocolInfo() == null || !conn.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
connections.remove(conn); connections.remove(conn.getProtocolInfo().getUuid());
return; return;
} }
try { try {