diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java index b4762cc94..e68d9c091 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java @@ -225,8 +225,8 @@ public void onEnable() { var contactsManager = new ContactsManager(); contactsManager.runTaskTimerAsynchronously(this, 0, 20); getServer().getPluginManager().registerEvents(contactsManager, this); - getCommand("contacts").setExecutor(new ContactsCommand(contactsManager)); getServer().getPluginManager().registerEvents(new ContactsSign(contactsManager), this); + getCommand("contacts").setExecutor(new ContactsCommand()); logger.info("[V " + getDescription().getVersion() + "] has been enabled."); } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsCommand.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsCommand.java index bdbe19089..3307022c8 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsCommand.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsCommand.java @@ -13,12 +13,6 @@ import org.jetbrains.annotations.NotNull; public class ContactsCommand implements CommandExecutor { - private final ContactsManager contactsManager; - - public ContactsCommand(ContactsManager contactsManager) { - this.contactsManager = contactsManager; - } - @Override public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, String[] args) { if (!command.getName().equalsIgnoreCase("contacts")) @@ -63,7 +57,7 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command ComponentPaginator paginator = new ComponentPaginator( I18nSupport.getInternationalisedComponent("Contacts"), (pageNumber) -> "/contacts " + pageNumber); - for (Craft target : contactsManager.get(base)) { + for (Craft target : base.getDataTag(ContactsManager.CONTACTS)) { if (target.getHitBox().isEmpty()) continue; diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java index e4184d779..2097b4a1b 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsManager.java @@ -2,6 +2,8 @@ import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.*; +import net.countercraft.movecraft.craft.datatag.CraftDataTagContainer; +import net.countercraft.movecraft.craft.datatag.CraftDataTagKey; import net.countercraft.movecraft.craft.type.CraftType; import net.countercraft.movecraft.events.*; import net.countercraft.movecraft.exception.EmptyHitBoxException; @@ -13,6 +15,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -24,8 +27,8 @@ import java.util.*; public class ContactsManager extends BukkitRunnable implements Listener { - private final Map> contactsMap = new WeakHashMap<>(); - private final Map> recentContacts = new WeakHashMap<>(); + public static final CraftDataTagKey> CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "contacts"), craft -> new ArrayList<>(0)); + private static final CraftDataTagKey> RECENT_CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>()); @Override public void run() { @@ -48,8 +51,8 @@ private void runContacts() { } } - private void update(Craft base, @NotNull Set craftsInWorld) { - List previousContacts = contactsMap.get(base); + private void update(@NotNull Craft base, @NotNull Set craftsInWorld) { + List previousContacts = base.getDataTag(CONTACTS); if (previousContacts == null) previousContacts = new ArrayList<>(0); List futureContacts = get(base, craftsInWorld); @@ -68,7 +71,7 @@ private void update(Craft base, @NotNull Set craftsInWorld) { Bukkit.getServer().getPluginManager().callEvent(event); } - contactsMap.put(base, futureContacts); + base.setDataTag(CONTACTS, futureContacts); } private @NotNull List get(Craft base, @NotNull Set craftsInWorld) { @@ -123,12 +126,9 @@ private void runRecentContacts() { if (base.getHitBox().isEmpty()) continue; - if (!recentContacts.containsKey(base)) - recentContacts.put(base, new WeakHashMap<>()); - - for (Craft target : contactsMap.get(base)) { + for (Craft target : base.getDataTag(CONTACTS)) { // has the craft not been seen in the last minute? - if (System.currentTimeMillis() - recentContacts.get(base).getOrDefault(target, 0L) <= 60000) + if (System.currentTimeMillis() - base.getDataTag(RECENT_CONTACTS).getOrDefault(target, 0L) <= 60000) continue; Component message = contactMessage(false, base, target); @@ -136,7 +136,7 @@ private void runRecentContacts() { continue; base.getAudience().sendMessage(message); - recentContacts.get(base).put(target, System.currentTimeMillis()); + base.getDataTag(RECENT_CONTACTS).put(target, System.currentTimeMillis()); } } } @@ -234,29 +234,25 @@ public void onCraftSink(@NotNull CraftSinkEvent e) { } private void remove(Craft base) { - contactsMap.remove(base); - for (Craft key : contactsMap.keySet()) { - if (!contactsMap.get(key).contains(base)) + for (Craft other : CraftManager.getInstance().getCrafts()) { + List contacts = other.getDataTag(CONTACTS); + if (contacts.contains(base)) continue; - contactsMap.get(key).remove(base); + contacts.remove(base); + other.setDataTag(CONTACTS, contacts); } - if (base instanceof PlayerCraft) - recentContacts.remove(base); - - for (PlayerCraft key : recentContacts.keySet()) { - if (!recentContacts.containsKey(key)) + for (Craft other : CraftManager.getInstance().getCrafts()) { + Map recentContacts = other.getDataTag(RECENT_CONTACTS); + if (!recentContacts.containsKey(other)) continue; - recentContacts.get(key).remove(base); + recentContacts.remove(base); + other.setDataTag(RECENT_CONTACTS, recentContacts); } } - public List get(Craft base) { - return contactsMap.get(base); - } - @EventHandler public void onNewContact(@NotNull NewContactEvent e) { Craft base = e.getCraft(); @@ -270,11 +266,10 @@ public void onNewContact(@NotNull NewContactEvent e) { throw new IllegalStateException("COLLISION_SOUND must be of type Sound"); base.getAudience().playSound(sound); - if (base instanceof PlayerCraft playerCraft) { - if (!recentContacts.containsKey(base)) - recentContacts.put(playerCraft, new WeakHashMap<>()); - - recentContacts.get(playerCraft).put(target, System.currentTimeMillis()); + if (base instanceof PlayerCraft) { + Map recentContacts = base.getDataTag(RECENT_CONTACTS); + recentContacts.put(target, System.currentTimeMillis()); + base.setDataTag(RECENT_CONTACTS, recentContacts); } } } diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java index 11999bbc7..4bcef2711 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/features/contacts/ContactsSign.java @@ -55,7 +55,7 @@ public final void onSignTranslateEvent(@NotNull SignTranslateEvent event) { Craft base = event.getCraft(); int line = 1; - for (Craft target : contactsManager.get(base)) { + for (Craft target : base.getDataTag(ContactsManager.CONTACTS)) { if (line > 3) break;