3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-08 17:20:24 +01:00

dont use uuids for bossbars, use userconnection

Dieser Commit ist enthalten in:
creeper123123321 2020-06-25 10:50:34 -03:00
Ursprung d86ac64ac9
Commit 7b200ad264
3 geänderte Dateien mit 74 neuen und 12 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,7 @@
package us.myles.ViaVersion.api.boss; package us.myles.ViaVersion.api.boss;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -80,13 +81,21 @@ public abstract class BossBar<T> {
} }
/** /**
* Show the bossbar to a player (uuid) * Show the bossbar to a player (uuid). You can retrieve it later with #getPlayers()
* *
* @param player uuid of the player * @param player uuid of the player
* @return The BossBar object * @return The BossBar object
*/ */
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()
*
* @param conn UserConnection of the connection
* @return The BossBar object
*/
public abstract BossBar addConnection(UserConnection conn);
/** /**
* add multiple players * add multiple players
* *
@ -112,13 +121,21 @@ public abstract class BossBar<T> {
} }
/** /**
* Removes the bossbar from a player * Removes the bossbar from a player. You shouldn't use this with #addConnection
* *
* @param uuid The platers YYUD * @param uuid The players UUID
* @return The BossBar object * @return The BossBar object
*/ */
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
*
* @param conn The UserConnection
* @return The BossBar object
*/
public abstract BossBar removeConnection(UserConnection conn);
/** /**
* Add flags * Add flags
* *
@ -142,12 +159,19 @@ public abstract class BossBar<T> {
public abstract boolean hasFlag(BossFlag flag); public abstract boolean hasFlag(BossFlag flag);
/** /**
* Get players * Get players. The storage is different from #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()
*
* @return UserConnection from players
*/
public abstract Set<UserConnection> getConnections();
/** /**
* Show the bossbar to everyone (In the getPlayer set) * Show the bossbar to everyone (In the getPlayer set)
* *

Datei anzeigen

@ -12,11 +12,7 @@ import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.api.type.Type; 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.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public abstract class CommonBoss<T> extends BossBar<T> { public abstract class CommonBoss<T> extends BossBar<T> {
private final UUID uuid; private final UUID uuid;
@ -25,6 +21,7 @@ public abstract class CommonBoss<T> extends BossBar<T> {
private BossColor color; private BossColor color;
private BossStyle style; private BossStyle style;
private final Set<UUID> players; private final Set<UUID> players;
private final Set<UserConnection> connections;
private boolean visible; private boolean visible;
private final Set<BossFlag> flags; private final Set<BossFlag> flags;
@ -38,6 +35,7 @@ public abstract class CommonBoss<T> extends BossBar<T> {
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.players = new HashSet<>();
this.connections = Collections.newSetFromMap(new WeakHashMap<>());
this.flags = new HashSet<>(); this.flags = new HashSet<>();
visible = true; visible = true;
} }
@ -91,6 +89,17 @@ public abstract class CommonBoss<T> extends BossBar<T> {
return this; return this;
} }
@Override
public BossBar addConnection(UserConnection conn) {
if (!connections.contains(conn)) {
connections.add(conn);
if (visible) {
sendPacketConnection(conn, getPacket(CommonBoss.UpdateAction.ADD, conn));
}
}
return this;
}
@Override @Override
public BossBar removePlayer(UUID uuid) { public BossBar removePlayer(UUID uuid) {
if (players.contains(uuid)) { if (players.contains(uuid)) {
@ -101,6 +110,15 @@ public abstract class CommonBoss<T> extends BossBar<T> {
return this; return this;
} }
@Override
public BossBar removeConnection(UserConnection conn) {
if (connections.contains(conn)) {
connections.remove(conn);
sendPacketConnection(conn, getPacket(UpdateAction.REMOVE, conn));
}
return this;
}
@Override @Override
public BossBar addFlag(BossFlag flag) { public BossBar addFlag(BossFlag flag) {
Preconditions.checkNotNull(flag); Preconditions.checkNotNull(flag);
@ -130,6 +148,11 @@ public abstract class CommonBoss<T> extends BossBar<T> {
return Collections.unmodifiableSet(players); return Collections.unmodifiableSet(players);
} }
@Override
public Set<UserConnection> getConnections() {
return Collections.unmodifiableSet(connections);
}
@Override @Override
public BossBar show() { public BossBar show() {
setVisible(true); setVisible(true);
@ -188,6 +211,10 @@ public abstract class CommonBoss<T> extends BossBar<T> {
PacketWrapper wrapper = getPacket(action, connection); PacketWrapper wrapper = getPacket(action, connection);
sendPacket(uuid, wrapper); sendPacket(uuid, wrapper);
} }
for (UserConnection conn : new ArrayList<>(connections)) {
PacketWrapper wrapper = getPacket(action, conn);
sendPacketConnection(conn, wrapper);
}
} }
private void sendPacket(UUID uuid, PacketWrapper wrapper) { private void sendPacket(UUID uuid, PacketWrapper wrapper) {
@ -202,6 +229,18 @@ public abstract class CommonBoss<T> extends BossBar<T> {
} }
} }
private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) {
if (conn.getProtocolInfo() == null || conn.getProtocolInfo().getProtocolVersion() >= ProtocolVersion.v1_9.getId()) {
connections.remove(conn);
return;
}
try {
wrapper.send(Protocol1_9To1_8.class);
} catch (Exception e) {
e.printStackTrace();
}
}
private PacketWrapper getPacket(UpdateAction action, UserConnection connection) { private PacketWrapper getPacket(UpdateAction action, UserConnection connection) {
try { try {
PacketWrapper wrapper = new PacketWrapper(0x0C, null, connection); // TODO don't use fixed packet ids for future support PacketWrapper wrapper = new PacketWrapper(0x0C, null, connection); // TODO don't use fixed packet ids for future support

Datei anzeigen

@ -191,7 +191,6 @@ public class EntityTracker1_9 extends EntityTracker {
} }
} }
} }
UUID uuid = getUser().getProtocolInfo().getUuid();
// Boss bar // Boss bar
if (Via.getConfig().isBossbarPatch()) { if (Via.getConfig().isBossbarPatch()) {
if (type == EntityType.ENDER_DRAGON || type == EntityType.WITHER) { if (type == EntityType.ENDER_DRAGON || type == EntityType.WITHER) {
@ -202,7 +201,7 @@ public class EntityTracker1_9 extends EntityTracker {
if (bar == null) { if (bar == null) {
bar = Via.getAPI().createBossBar(title, BossColor.PINK, BossStyle.SOLID); bar = Via.getAPI().createBossBar(title, BossColor.PINK, BossStyle.SOLID);
bossBarMap.put(entityId, bar); bossBarMap.put(entityId, bar);
bar.addPlayer(uuid); bar.addConnection(getUser());
bar.show(); bar.show();
// Send to provider // Send to provider
@ -219,7 +218,7 @@ public class EntityTracker1_9 extends EntityTracker {
String title = type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither"; String title = type == EntityType.ENDER_DRAGON ? "Ender Dragon" : "Wither";
bar = Via.getAPI().createBossBar(title, health, BossColor.PINK, BossStyle.SOLID); bar = Via.getAPI().createBossBar(title, health, BossColor.PINK, BossStyle.SOLID);
bossBarMap.put(entityId, bar); bossBarMap.put(entityId, bar);
bar.addPlayer(uuid); bar.addConnection(getUser());
bar.show(); bar.show();
// Send to provider // Send to provider
Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(getUser(), bar.getId()); Via.getManager().getProviders().get(BossBarProvider.class).handleAdd(getUser(), bar.getId());