mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Use a lock + weak map
This commit is contained in:
parent
e185bca468
commit
73f125763c
@ -233,7 +233,7 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<Viewable, ViewableStorage> VIEWABLE_STORAGE_MAP = new ConcurrentHashMap<>();
|
private static final Map<Viewable, ViewableStorage> VIEWABLE_STORAGE_MAP = new WeakHashMap<>();
|
||||||
|
|
||||||
private static final class ViewableStorage {
|
private static final class ViewableStorage {
|
||||||
private final Viewable viewable;
|
private final Viewable viewable;
|
||||||
@ -257,9 +257,7 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void process() {
|
private synchronized void process() {
|
||||||
final Set<Player> viewers = viewable.getViewers();
|
for (Player player : viewable.getViewers()) {
|
||||||
if (viewers.isEmpty()) return;
|
|
||||||
for (Player player : viewers) {
|
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
PlayerConnection connection = player.getPlayerConnection();
|
||||||
Consumer<ByteBuffer> writer = connection instanceof PlayerSocketConnection
|
Consumer<ByteBuffer> writer = connection instanceof PlayerSocketConnection
|
||||||
? ((PlayerSocketConnection) connection)::write :
|
? ((PlayerSocketConnection) connection)::write :
|
||||||
@ -294,6 +292,8 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Object VIEWABLE_PACKET_LOCK = new Object();
|
||||||
|
|
||||||
public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket,
|
public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket,
|
||||||
@Nullable Player player) {
|
@Nullable Player player) {
|
||||||
if (player != null && !player.isAutoViewable()) {
|
if (player != null && !player.isAutoViewable()) {
|
||||||
@ -302,8 +302,10 @@ public final class PacketUtils {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final PlayerConnection playerConnection = player != null ? player.getPlayerConnection() : null;
|
final PlayerConnection playerConnection = player != null ? player.getPlayerConnection() : null;
|
||||||
ViewableStorage viewableStorage = VIEWABLE_STORAGE_MAP.computeIfAbsent(viewable, c -> new ViewableStorage(viewable));
|
synchronized (VIEWABLE_PACKET_LOCK) {
|
||||||
viewableStorage.append(playerConnection, serverPacket);
|
ViewableStorage viewableStorage = VIEWABLE_STORAGE_MAP.computeIfAbsent(viewable, ViewableStorage::new);
|
||||||
|
viewableStorage.append(playerConnection, serverPacket);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) {
|
public static void prepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) {
|
||||||
@ -311,9 +313,10 @@ public final class PacketUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void flush() {
|
public static void flush() {
|
||||||
// TODO clear map
|
synchronized (VIEWABLE_PACKET_LOCK) {
|
||||||
for (ViewableStorage viewableStorage : VIEWABLE_STORAGE_MAP.values()) {
|
for (ViewableStorage viewableStorage : VIEWABLE_STORAGE_MAP.values()) {
|
||||||
viewableStorage.process();
|
viewableStorage.process();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user