Redirect UUID to UserConnection bossbars

This commit is contained in:
creeper123123321 2020-06-25 11:25:59 -03:00
parent 8bd982a412
commit 3f93bb051d
4 changed files with 36 additions and 41 deletions

View File

@ -156,6 +156,10 @@ public class ViaManager {
return platform.getConnectionManager().getConnectedClients(); return platform.getConnectionManager().getConnectedClients();
} }
public UUID getConnectedClientId(UserConnection conn) {
return platform.getConnectionManager().getConnectedClientId(conn);
}
/** /**
* @see ViaConnectionManager#isClientConnected(UUID) * @see ViaConnectionManager#isClientConnected(UUID)
*/ */

View File

@ -81,7 +81,7 @@ public abstract class BossBar<T> {
} }
/** /**
* Show the bossbar to a player (uuid). You can retrieve it later with #getPlayers() * Show the bossbar to a player (uuid).
* *
* @param player uuid of the player * @param player uuid of the player
* @return The BossBar object * @return The BossBar object
@ -89,7 +89,7 @@ public abstract class BossBar<T> {
public abstract BossBar addPlayer(UUID player); public abstract BossBar addPlayer(UUID player);
/** /**
* Show the bossbar to a player connection. You may retrieve it later with #getConnections() * Show the bossbar to a player connection.
* *
* @param conn UserConnection of the connection * @param conn UserConnection of the connection
* @return The BossBar object * @return The BossBar object
@ -121,7 +121,7 @@ public abstract class BossBar<T> {
} }
/** /**
* Removes the bossbar from a player. You shouldn't use this with #addConnection * Removes the bossbar from a player.
* *
* @param uuid The players UUID * @param uuid The players UUID
* @return The BossBar object * @return The BossBar object
@ -129,7 +129,7 @@ public abstract class BossBar<T> {
public abstract BossBar removePlayer(UUID uuid); public abstract BossBar removePlayer(UUID uuid);
/** /**
* Removes the bossbar from a player connection. You shouldn't use this with #addPlayer * Removes the bossbar from a player connection.
* *
* @param conn The UserConnection * @param conn The UserConnection
* @return The BossBar object * @return The BossBar object
@ -159,14 +159,14 @@ public abstract class BossBar<T> {
public abstract boolean hasFlag(BossFlag flag); public abstract boolean hasFlag(BossFlag flag);
/** /**
* Get players. The storage is different from #getConnections() * Get players. Only returns UUIDs which are front-end. For all connections, use #getConnections()
* *
* @return UUIDS from players (sorry I lied) * @return UUIDS from players (sorry I lied)
*/ */
public abstract Set<UUID> getPlayers(); public abstract Set<UUID> getPlayers();
/** /**
* Get UserConnections. The storage is different from #getPlayers() * Get UserConnections.
* *
* @return UserConnection from players * @return UserConnection from players
*/ */

View File

@ -57,6 +57,26 @@ public class ViaConnectionManager {
return clients.get(clientIdentifier); return clients.get(clientIdentifier);
} }
/**
* Returns the UUID from the frontend connection to this proxy server
* Returns null when there isn't a server or frontend id was not found
* When ViaVersion is reloaded, this method may not return some players.
* May not return ProtocolSupport players.
* <p>
* Note that connections are removed as soon as their channel is closed,
* so avoid using this method during player quits for example.
*/
@Nullable
public UUID getConnectedClientId(UserConnection conn) {
if (conn.getProtocolInfo() == null) return null;
UUID uuid = conn.getProtocolInfo().getUuid();
if (clients.get(uuid).equals(conn)) {
// This is frontend
return uuid;
}
return null;
}
/** /**
* Returns all UserConnections which are registered * Returns all UserConnections which are registered
* May contain duplicated UUIDs on multiple ProtocolInfo. * May contain duplicated UUIDs on multiple ProtocolInfo.

View File

@ -13,6 +13,7 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public abstract class CommonBoss<T> extends BossBar<T> { public abstract class CommonBoss<T> extends BossBar<T> {
private final UUID uuid; private final UUID uuid;
@ -20,7 +21,6 @@ public abstract class CommonBoss<T> extends BossBar<T> {
private float health; private float health;
private BossColor color; private BossColor color;
private BossStyle style; private BossStyle style;
private final Set<UUID> players;
private final Set<UserConnection> connections; private final Set<UserConnection> connections;
private boolean visible; private boolean visible;
private final Set<BossFlag> flags; private final Set<BossFlag> flags;
@ -34,7 +34,6 @@ public abstract class CommonBoss<T> extends BossBar<T> {
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.players = new HashSet<>();
this.connections = Collections.newSetFromMap(new WeakHashMap<>()); this.connections = Collections.newSetFromMap(new WeakHashMap<>());
this.flags = new HashSet<>(); this.flags = new HashSet<>();
visible = true; visible = true;
@ -79,14 +78,7 @@ public abstract class CommonBoss<T> extends BossBar<T> {
@Override @Override
public BossBar addPlayer(UUID player) { public BossBar addPlayer(UUID player) {
if (!players.contains(player)) { return addConnection(Via.getManager().getConnection(player));
players.add(player);
if (visible) {
UserConnection user = Via.getManager().getConnection(player);
sendPacket(player, getPacket(CommonBoss.UpdateAction.ADD, user));
}
}
return this;
} }
@Override @Override
@ -102,12 +94,7 @@ public abstract class CommonBoss<T> extends BossBar<T> {
@Override @Override
public BossBar removePlayer(UUID uuid) { public BossBar removePlayer(UUID uuid) {
if (players.contains(uuid)) { return removeConnection(Via.getManager().getConnection(uuid));
players.remove(uuid);
UserConnection user = Via.getManager().getConnection(uuid);
sendPacket(uuid, getPacket(UpdateAction.REMOVE, user));
}
return this;
} }
@Override @Override
@ -145,7 +132,8 @@ public abstract class CommonBoss<T> extends BossBar<T> {
@Override @Override
public Set<UUID> getPlayers() { public Set<UUID> getPlayers() {
return Collections.unmodifiableSet(players); return connections.stream().map(conn -> Via.getManager().getConnectedClientId(conn)).filter(Objects::nonNull)
.collect(Collectors.toSet());
} }
@Override @Override
@ -206,31 +194,14 @@ public abstract class CommonBoss<T> extends BossBar<T> {
} }
private void sendPacket(UpdateAction action) { private void sendPacket(UpdateAction action) {
for (UUID uuid : new ArrayList<>(players)) {
UserConnection connection = Via.getManager().getConnection(uuid);
PacketWrapper wrapper = getPacket(action, connection);
sendPacket(uuid, wrapper);
}
for (UserConnection conn : new ArrayList<>(connections)) { for (UserConnection conn : new ArrayList<>(connections)) {
PacketWrapper wrapper = getPacket(action, conn); PacketWrapper wrapper = getPacket(action, conn);
sendPacketConnection(conn, wrapper); sendPacketConnection(conn, wrapper);
} }
} }
private void sendPacket(UUID uuid, PacketWrapper wrapper) {
if (!Via.getAPI().isInjected(uuid) || !(Via.getAPI().getPlayerVersion(uuid) >= ProtocolVersion.v1_9.getId())) {
players.remove(uuid);
return;
}
try {
wrapper.send(Protocol1_9To1_8.class);
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) { private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) {
if (conn.getProtocolInfo() == null || conn.getProtocolInfo().getProtocolVersion() < ProtocolVersion.v1_9.getId()) { if (conn.getProtocolInfo() == null || conn.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
connections.remove(conn); connections.remove(conn);
return; return;
} }