51 lines
2.3 KiB
Java
51 lines
2.3 KiB
Java
package me.sashegdev.blackknife;
|
|
|
|
import org.bukkit.NamespacedKey;
|
|
import org.bukkit.entity.Display;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.persistence.PersistentDataType;
|
|
|
|
public class EntityTracker {
|
|
public static final NamespacedKey BK_MARK = new NamespacedKey("blackknife", "bk_entity");
|
|
private static final java.util.Set<java.util.UUID> tracked = java.util.Collections.synchronizedSet(new java.util.HashSet<>());
|
|
|
|
public static void mark(Entity e) {
|
|
e.getPersistentDataContainer().set(BK_MARK, PersistentDataType.BYTE, (byte) 1);
|
|
tracked.add(e.getUniqueId());
|
|
}
|
|
|
|
public static void untrack(Entity e) {
|
|
tracked.remove(e.getUniqueId());
|
|
}
|
|
|
|
public static void smooth(Display e) {
|
|
try { e.setTeleportDuration(1); } catch (Exception ignored) {}
|
|
}
|
|
|
|
public static int cleanup(BlackKnifePlugin plugin) {
|
|
int count = 0;
|
|
for (var world : plugin.getServer().getWorlds()) {
|
|
for (var e : world.getEntitiesByClasses(Display.class, org.bukkit.entity.Marker.class, org.bukkit.entity.TextDisplay.class)) {
|
|
boolean isBk = e.getPersistentDataContainer().has(BK_MARK, PersistentDataType.BYTE) || tracked.contains(e.getUniqueId());
|
|
if (!isBk && e instanceof org.bukkit.entity.BlockDisplay bd) {
|
|
var mat = bd.getBlock().getMaterial();
|
|
if (mat == org.bukkit.Material.DEEPSLATE_TILES || mat == org.bukkit.Material.SCULK_CATALYST || mat == org.bukkit.Material.AMETHYST_BLOCK || mat == org.bukkit.Material.MEDIUM_AMETHYST_BUD || mat == org.bukkit.Material.TNT) isBk = true;
|
|
}
|
|
if (isBk) { untrack(e); e.remove(); count++; }
|
|
}
|
|
}
|
|
for (var uuid : new java.util.HashSet<>(tracked)) {
|
|
var e = plugin.getServer().getEntity(uuid);
|
|
if (e != null && e.isValid()) { e.remove(); count++; }
|
|
}
|
|
tracked.clear();
|
|
if (plugin.getTension() != null) plugin.getTension().hideAll();
|
|
for (var p : plugin.getServer().getOnlinePlayers()) {
|
|
var copy = new java.util.ArrayList<net.kyori.adventure.bossbar.BossBar>();
|
|
for (var b : p.activeBossBars()) copy.add(b);
|
|
for (var bar : copy) p.hideBossBar(bar);
|
|
}
|
|
return count;
|
|
}
|
|
}
|