9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-30 20:39:13 +00:00

feat: optimize new packet pufferfish system

This commit is contained in:
LoJoSho
2023-06-22 13:15:44 -05:00
parent f34f5f01aa
commit b682dd7c42
2 changed files with 7 additions and 2 deletions

View File

@@ -72,11 +72,12 @@ public class CosmeticBalloonType extends Cosmetic {
userBalloonManager.setLocation(newLocation);
PacketManager.sendTeleportPacket(userBalloonManager.getPufferfishBalloonId(), newLocation, false, viewer);
PacketManager.sendLeashPacket(userBalloonManager.getPufferfishBalloonId(), entity.getEntityId(), viewer);
if (!user.getHidden() && showLead) {
List<Player> sendTo = userBalloonManager.getPufferfish().refreshViewers(newLocation);
if (sendTo.isEmpty()) return;
PacketManager.sendEntitySpawnPacket(newLocation, userBalloonManager.getPufferfishBalloonId(), EntityType.PUFFERFISH, userBalloonManager.getPufferfishBalloonUniqueId(), sendTo);
PacketManager.sendInvisibilityPacket(userBalloonManager.getPufferfishBalloonId(), sendTo);
PacketManager.sendLeashPacket(userBalloonManager.getPufferfishBalloonId(), entity.getEntityId(), viewer);
}
}

View File

@@ -1,5 +1,6 @@
package com.hibiscusmc.hmccosmetics.user.manager;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
import com.hibiscusmc.hmccosmetics.util.PlayerUtils;
import com.hibiscusmc.hmccosmetics.util.packets.PacketManager;
import org.bukkit.Location;
@@ -14,10 +15,12 @@ public class UserBalloonPufferfish {
private int id;
private UUID uuid;
private List<Player> viewers = new ArrayList<>();
private Long lastUpdate;
public UserBalloonPufferfish(int id, UUID uuid) {
this.id = id;
this.uuid = uuid;
this.lastUpdate = System.currentTimeMillis();
}
public int getId() {
@@ -29,11 +32,11 @@ public class UserBalloonPufferfish {
}
public List<Player> refreshViewers(Location location) {
if (System.currentTimeMillis() - lastUpdate <= 1000) return List.of(); //Prevents mass refreshes
ArrayList<Player> newPlayers = new ArrayList<>();
ArrayList<Player> removePlayers = new ArrayList<>();
List<Player> players = PlayerUtils.getNearbyPlayers(location);
for (Player player : players) {
if (!viewers.contains(player)) {
viewers.add(player);
@@ -49,6 +52,7 @@ public class UserBalloonPufferfish {
}
}
viewers.removeAll(removePlayers);
lastUpdate = System.currentTimeMillis();
return newPlayers;
}
}