Skip to content

Commit

Permalink
Contacts rewrite (#667)
Browse files Browse the repository at this point in the history
* Remove contacts from AsyncManager

* Move contacts command

* WIP

* Deprecate TopicPaginator

* Create ComponentPaginator.java

* Update I18nSupport.java

* Switch to component paginator

* Clean up

* Add page commands

* Update ContactsCommand.java

* WIP

* Finish rewriting contracts

* Add events

* Add new contact notification

* Fix various bugs

* Update ComponentPaginator.java

* Make getCraftsInWorld async safe

* Update ContactsSign.java

* Fix paginator length

* Temporary sign patch

* Update all signs on rotation

* Remove contacts of sinking crafts

* Update SinkingCraftImpl.java

* Remove subcrafts from contacts and fix possible deadlock

* Update CraftReportCommand.java

* Update buildlogic.java-conventions.gradle.kts
  • Loading branch information
TylerS1066 authored Jul 14, 2024
1 parent 8d53e92 commit 6c2f22f
Show file tree
Hide file tree
Showing 20 changed files with 749 additions and 414 deletions.
16 changes: 12 additions & 4 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.ChunkManager;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.features.contacts.ContactsCommand;
import net.countercraft.movecraft.features.contacts.ContactsManager;
import net.countercraft.movecraft.features.contacts.ContactsSign;
import net.countercraft.movecraft.listener.BlockListener;
import net.countercraft.movecraft.listener.InteractListener;
import net.countercraft.movecraft.listener.PlayerListener;
Expand Down Expand Up @@ -195,7 +198,6 @@ public void onEnable() {
getCommand("cruise").setExecutor(new CruiseCommand());
getCommand("craftreport").setExecutor(new CraftReportCommand());
getCommand("manoverboard").setExecutor(new ManOverboardCommand());
getCommand("contacts").setExecutor(new ContactsCommand());
getCommand("scuttle").setExecutor(new ScuttleCommand());
getCommand("crafttype").setExecutor(new CraftTypeCommand());
getCommand("craftinfo").setExecutor(new CraftInfoCommand());
Expand All @@ -204,7 +206,6 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getPluginManager().registerEvents(new ChunkManager(), this);
getServer().getPluginManager().registerEvents(new AscendSign(), this);
getServer().getPluginManager().registerEvents(new ContactsSign(), this);
getServer().getPluginManager().registerEvents(new CraftSign(), this);
getServer().getPluginManager().registerEvents(new CruiseSign(), this);
getServer().getPluginManager().registerEvents(new DescendSign(), this);
Expand All @@ -221,6 +222,12 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new TeleportSign(), this);
getServer().getPluginManager().registerEvents(new ScuttleSign(), this);

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);

logger.info("[V " + getDescription().getVersion() + "] has been enabled.");
}

Expand All @@ -230,7 +237,6 @@ public void onLoad() {
instance = this;
logger = getLogger();
saveDefaultConfig();

}

private boolean initializeDatapack() {
Expand Down Expand Up @@ -314,5 +320,7 @@ public SmoothTeleport getSmoothTeleport() {
return smoothTeleport;
}

public AsyncManager getAsyncManager(){return asyncManager;}
public AsyncManager getAsyncManager() {
return asyncManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
@Deprecated
public class AsyncManager extends BukkitRunnable {
private final Map<AsyncTask, Craft> ownershipMap = new HashMap<>();
private final Map<Craft, Map<Craft, Long>> recentContactTracking = new WeakHashMap<>();
private final BlockingQueue<AsyncTask> finishedAlgorithms = new LinkedBlockingQueue<>();
private final Set<Craft> clearanceSet = new HashSet<>();
private final Map<HitBox, Long> wrecks = new HashMap<>();
Expand All @@ -78,7 +77,6 @@ public class AsyncManager extends BukkitRunnable {
private final Map<Craft, Integer> cooldownCache = new WeakHashMap<>();

private long lastFadeCheck = 0;
private long lastContactCheck = 0;

public AsyncManager() {}

Expand Down Expand Up @@ -438,113 +436,13 @@ private void processFadingBlocks() {
lastFadeCheck = System.currentTimeMillis();
}

private void processDetection() {
long ticksElapsed = (System.currentTimeMillis() - lastContactCheck) / 50;
if (ticksElapsed < 20)
return;
lastContactCheck = System.currentTimeMillis();

for (World w : Bukkit.getWorlds()) {
if (w == null)
continue;

for (Craft craft : CraftManager.getInstance().getPlayerCraftsInWorld(w)) {
MovecraftLocation craftCenter;
try {
craftCenter = craft.getHitBox().getMidPoint();
}
catch (EmptyHitBoxException e) {
continue;
}
if (!recentContactTracking.containsKey(craft))
recentContactTracking.put(craft, new WeakHashMap<>());
for (Craft target : craft.getContacts()) {
MovecraftLocation targetCenter;
try {
targetCenter = target.getHitBox().getMidPoint();
}
catch (EmptyHitBoxException e) {
continue;
}
int diffx = craftCenter.getX() - targetCenter.getX();
int diffz = craftCenter.getZ() - targetCenter.getZ();
int distsquared = craftCenter.distanceSquared(targetCenter);
// craft has been detected

// has the craft not been seen in the last
// minute, or is completely new?
if (System.currentTimeMillis()
- recentContactTracking.get(craft).getOrDefault(target, 0L) <= 60000)
continue;


Component notification = I18nSupport.getInternationalisedComponent(
"Contact - New Contact").append(Component.text( ": "));

if (target.getName().length() >= 1)
notification = notification.append(Component.text(target.getName() + " ("));
notification = notification.append(Component.text(
target.getType().getStringProperty(CraftType.NAME)));
if (target.getName().length() >= 1)
notification = notification.append(Component.text(")"));
notification = notification.append(Component.text(" "))
.append(I18nSupport.getInternationalisedComponent("Contact - Commanded By"))
.append(Component.text(" "));
if (target instanceof PilotedCraft)
notification = notification.append(Component.text(
((PilotedCraft) target).getPilot().getDisplayName()));
else
notification = notification.append(Component.text("NULL"));
notification = notification.append(Component.text(", "))
.append(I18nSupport.getInternationalisedComponent("Contact - Size"))
.append(Component.text( ": "))
.append(Component.text(target.getOrigBlockCount()))
.append(Component.text(", "))
.append(I18nSupport.getInternationalisedComponent("Contact - Range"))
.append(Component.text(": "))
.append(Component.text((int) Math.sqrt(distsquared)))
.append(Component.text(" "))
.append(I18nSupport.getInternationalisedComponent("Contact - To The"))
.append(Component.text(" "));
if (Math.abs(diffx) > Math.abs(diffz)) {
if (diffx < 0)
notification = notification.append(I18nSupport.getInternationalisedComponent(
"Contact/Subcraft Rotate - East"));
else
notification = notification.append(I18nSupport.getInternationalisedComponent(
"Contact/Subcraft Rotate - West"));
}
else if (diffz < 0)
notification = notification.append(I18nSupport.getInternationalisedComponent(
"Contact/Subcraft Rotate - South"));
else
notification = notification.append(I18nSupport.getInternationalisedComponent(
"Contact/Subcraft Rotate - North"));

notification = notification.append(Component.text("."));

craft.getAudience().sendMessage(notification);
var object = craft.getType().getObjectProperty(CraftType.COLLISION_SOUND);
if (!(object instanceof Sound))
throw new IllegalStateException("COLLISION_SOUND must be of type Sound");

craft.getAudience().playSound((Sound) object);

long timestamp = System.currentTimeMillis();
recentContactTracking.get(craft).put(target, timestamp);
}
}
}
}

public void run() {
clearAll();

processCruise();
detectSinking();
processSinking();
processFadingBlocks();
processDetection();
processAlgorithmQueue();

// now cleanup craft that are bugged and have not moved in the past 60 seconds,
Expand Down

This file was deleted.

Loading

0 comments on commit 6c2f22f

Please sign in to comment.