mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-12-25 09:57:39 +01:00
parent
7a82ded94b
commit
389c5adaa3
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user