Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contacts data tag #672

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -24,8 +27,8 @@
import java.util.*;

public class ContactsManager extends BukkitRunnable implements Listener {
private final Map<Craft, List<Craft>> contactsMap = new WeakHashMap<>();
private final Map<PlayerCraft, Map<Craft, Long>> recentContacts = new WeakHashMap<>();
public static final CraftDataTagKey<List<Craft>> CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "contacts"), craft -> new ArrayList<>(0));
private static final CraftDataTagKey<Map<Craft, Long>> RECENT_CONTACTS = CraftDataTagContainer.tryRegisterTagKey(new NamespacedKey("movecraft", "recent-contacts"), craft -> new WeakHashMap<>());

@Override
public void run() {
Expand All @@ -48,8 +51,8 @@ private void runContacts() {
}
}

private void update(Craft base, @NotNull Set<Craft> craftsInWorld) {
List<Craft> previousContacts = contactsMap.get(base);
private void update(@NotNull Craft base, @NotNull Set<Craft> craftsInWorld) {
List<Craft> previousContacts = base.getDataTag(CONTACTS);
if (previousContacts == null)
previousContacts = new ArrayList<>(0);
List<Craft> futureContacts = get(base, craftsInWorld);
Expand All @@ -68,7 +71,7 @@ private void update(Craft base, @NotNull Set<Craft> craftsInWorld) {
Bukkit.getServer().getPluginManager().callEvent(event);
}

contactsMap.put(base, futureContacts);
base.setDataTag(CONTACTS, futureContacts);
}

private @NotNull List<Craft> get(Craft base, @NotNull Set<Craft> craftsInWorld) {
Expand Down Expand Up @@ -123,20 +126,17 @@ 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);
if (message == null)
continue;

base.getAudience().sendMessage(message);
recentContacts.get(base).put(target, System.currentTimeMillis());
base.getDataTag(RECENT_CONTACTS).put(target, System.currentTimeMillis());
}
}
}
Expand Down Expand Up @@ -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<Craft> 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<Craft, Long> 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<Craft> get(Craft base) {
return contactsMap.get(base);
}

@EventHandler
public void onNewContact(@NotNull NewContactEvent e) {
Craft base = e.getCraft();
Expand All @@ -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<Craft, Long> recentContacts = base.getDataTag(RECENT_CONTACTS);
recentContacts.put(target, System.currentTimeMillis());
base.setDataTag(RECENT_CONTACTS, recentContacts);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down