diff --git a/.forgejo/workflows/dunechat.yml b/.forgejo/workflows/dunechat.yml index f186b53c..e69deb19 100644 --- a/.forgejo/workflows/dunechat.yml +++ b/.forgejo/workflows/dunechat.yml @@ -24,14 +24,13 @@ jobs: - uses: gradle/actions/setup-gradle@v4 with: gradle-version: '9.2.1' - - name: Build and test DuneChat and its GUI identity hook + - name: Build and test DuneChat shell: bash run: | set -euo pipefail gradle --no-daemon -p DuneChat test jar - gradle --no-daemon -p DuneRealmsGUI test jar mkdir dunechat-artifacts - cp DuneChat/build/libs/DuneChat.jar DuneRealmsGUI/build/libs/DunerealmsGUI.jar dunechat-artifacts/ + cp DuneChat/build/libs/DuneChat.jar dunechat-artifacts/ printf '%s\n' "$(git rev-parse HEAD)" > dunechat-artifacts/SOURCE_COMMIT (cd dunechat-artifacts && sha256sum -- *.jar SOURCE_COMMIT > SHA256SUMS) - uses: actions/upload-artifact@v3 diff --git a/DuneChat/src/main/java/com/example/dunechat/ChatIdentitySource.java b/DuneChat/src/main/java/com/example/dunechat/ChatIdentitySource.java new file mode 100644 index 00000000..de431390 --- /dev/null +++ b/DuneChat/src/main/java/com/example/dunechat/ChatIdentitySource.java @@ -0,0 +1,28 @@ +package com.example.dunechat; + +import com.google.gson.JsonObject; +import java.lang.reflect.Method; +import java.util.UUID; +import org.bukkit.plugin.Plugin; + +/** Reuse the installed JARVIS reads without replacing the GUI or the central service. */ +final class ChatIdentitySource { + private final Plugin jarvis; + private final Method house; + private final Method organisations; + + ChatIdentitySource(Plugin jarvis) throws ReflectiveOperationException { + this.jarvis = jarvis; + if (jarvis == null || !jarvis.isEnabled()) throw new IllegalStateException("JARVIS unavailable"); + house = jarvis.getClass().getMethod("requestHouse", UUID.class, String.class, int.class, UUID.class); + organisations = jarvis.getClass().getMethod("requestOrganisations", UUID.class); + } + + JsonObject house(UUID player) throws ReflectiveOperationException { + return (JsonObject) house.invoke(jarvis, player, null, 0, null); + } + + JsonObject organisations(UUID player) throws ReflectiveOperationException { + return (JsonObject) organisations.invoke(jarvis, player); + } +} diff --git a/DuneChat/src/main/java/com/example/dunechat/ChatLogic.java b/DuneChat/src/main/java/com/example/dunechat/ChatLogic.java index 125cd848..14ab3bac 100644 --- a/DuneChat/src/main/java/com/example/dunechat/ChatLogic.java +++ b/DuneChat/src/main/java/com/example/dunechat/ChatLogic.java @@ -61,6 +61,19 @@ final class ChatLogic { "", clean(clanTag.isBlank() ? clanName : clanTag, max), "", clean(companyName, max)); } + static Identity house(JsonObject data, int max) { + if (data == null) return Identity.NONE; + String faction = text(data, "selection"); + JsonArray choices = data.getAsJsonArray("choices"); + if (!faction.isBlank() && choices != null) { + for (JsonElement element : choices) { + JsonObject choice = element.getAsJsonObject(); + if (faction.equals(text(choice, "id"))) return parse(data, faction, text(choice, "name"), max); + } + } + return parse(data, "", "", max); + } + record Membership(String clan, String company, long observedAt) { static final Membership NONE = new Membership("", "", 0); } diff --git a/DuneChat/src/main/java/com/example/dunechat/DuneChat.java b/DuneChat/src/main/java/com/example/dunechat/DuneChat.java index 31a4eb5c..d4edf650 100644 --- a/DuneChat/src/main/java/com/example/dunechat/DuneChat.java +++ b/DuneChat/src/main/java/com/example/dunechat/DuneChat.java @@ -2,8 +2,6 @@ package com.example.dunechat; import com.example.dunechat.ChatLogic.Channel; import com.example.dunechat.ChatLogic.Identity; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import io.papermc.paper.event.player.AsyncChatEvent; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -17,6 +15,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; import java.util.List; @@ -27,13 +26,15 @@ import java.util.concurrent.ConcurrentHashMap; /** * Chat prefixes (race, clan, company) and private clan, company and staff channels. - * Identity comes from the central JARVIS briefing, pushed in by DuneRealmsGUI through {@link #identity}. + * Identity and private membership come from the installed JARVIS read APIs. * Chat is per server; there is no cross-server relay. */ public final class DuneChat extends JavaPlugin implements Listener, CommandExecutor { private static final LegacyComponentSerializer LEGACY = LegacyComponentSerializer.legacyAmpersand(); private final Map identities = new ConcurrentHashMap<>(); private final Map memberships = new ConcurrentHashMap<>(); + private final Map refreshing = new ConcurrentHashMap<>(); + private ChatIdentitySource identitySource; private final Map mode = new ConcurrentHashMap<>(); private record CommandMessage(UUID sender, Channel channel) {} private final ThreadLocal commandMessage = new ThreadLocal<>(); @@ -46,6 +47,11 @@ public final class DuneChat extends JavaPlugin implements Listener, CommandExecu saveDefaultConfig(); order = getConfig().getStringList("prefix-order"); max = Math.max(4, getConfig().getInt("max-prefix-length", 16)); + try { + identitySource = new ChatIdentitySource(getServer().getPluginManager().getPlugin("JarvisTranslator")); + } catch (ReflectiveOperationException | IllegalStateException unavailable) { + getLogger().warning("JARVIS identity unavailable; organisation channels are unavailable."); + } if (getServer().getPluginManager().isPluginEnabled("DiscordSRV")) { try { discordPrivacy = new DiscordPrivacy(this); @@ -54,6 +60,7 @@ public final class DuneChat extends JavaPlugin implements Listener, CommandExecu } } getServer().getPluginManager().registerEvents(this, this); + getServer().getScheduler().runTaskTimer(this, () -> Bukkit.getOnlinePlayers().forEach(this::refresh), 20L, 400L); for (String c : List.of("clanchat", "companychat", "staffchat", "globalchat", "chatspy")) { var cmd = getCommand(c); if (cmd != null) cmd.setExecutor(this); @@ -64,25 +71,27 @@ public final class DuneChat extends JavaPlugin implements Listener, CommandExecu if (discordPrivacy != null) discordPrivacy.close(); } - /** Called by DuneRealmsGUI (by reflection) whenever the central briefing for a player changes. */ - public void identity(UUID player, String briefingJson, String faction, String race) { - Identity next = Identity.NONE; - try { - if (briefingJson != null) next = ChatLogic.parse(JsonParser.parseString(briefingJson).getAsJsonObject(), faction, race, max); - } catch (RuntimeException malformed) { getLogger().warning("Ignored malformed identity for " + player); } - identities.put(player, next); - } + @EventHandler public void onJoin(PlayerJoinEvent event) { refresh(event.getPlayer()); } - /** Called with an authenticated JARVIS organisation-list response for this player. */ - public void memberships(UUID player, String json) { - ChatLogic.Membership membership = ChatLogic.Membership.NONE; - try { - if (json != null) membership = ChatLogic.memberships( - JsonParser.parseString(json).getAsJsonObject(), player, System.currentTimeMillis()); - } catch (RuntimeException invalid) { - getLogger().warning("Ignored malformed chat membership projection"); - } - memberships.put(player, membership); + private void refresh(Player player) { + UUID id = player.getUniqueId(); + if (identitySource == null || refreshing.putIfAbsent(id, player) != null) return; + getServer().getScheduler().runTaskAsynchronously(this, () -> { + Identity display = Identity.NONE; + ChatLogic.Membership membership = ChatLogic.Membership.NONE; + try { display = ChatLogic.house(identitySource.house(id), max); } + catch (ReflectiveOperationException | RuntimeException unavailable) { /* Default display. */ } + try { membership = ChatLogic.memberships(identitySource.organisations(id), id, System.currentTimeMillis()); } + catch (ReflectiveOperationException | RuntimeException unavailable) { /* Private access fails closed. */ } + Identity nextDisplay = display; + ChatLogic.Membership nextMembership = membership; + if (!isEnabled()) return; + getServer().getScheduler().runTask(this, () -> { + if (!refreshing.remove(id, player) || Bukkit.getPlayer(id) != player || !player.isOnline()) return; + identities.put(id, nextDisplay); + memberships.put(id, nextMembership); + }); + }); } private Identity of(Player p) { @@ -103,7 +112,7 @@ public final class DuneChat extends JavaPlugin implements Listener, CommandExecu } @EventHandler public void onQuit(PlayerQuitEvent e) { - UUID id = e.getPlayer().getUniqueId(); identities.remove(id); memberships.remove(id); mode.remove(id); spies.remove(id); + UUID id = e.getPlayer().getUniqueId(); identities.remove(id); memberships.remove(id); refreshing.remove(id, e.getPlayer()); mode.remove(id); spies.remove(id); } /** Never un-cancels: if another plugin cancelled the chat (mute, filter), we leave it alone. */ diff --git a/DuneChat/src/main/resources/plugin.yml b/DuneChat/src/main/resources/plugin.yml index 4cfec2a5..787b4821 100644 --- a/DuneChat/src/main/resources/plugin.yml +++ b/DuneChat/src/main/resources/plugin.yml @@ -3,6 +3,7 @@ version: 1.0.0 main: com.example.dunechat.DuneChat api-version: '1.21.8' softdepend: [DiscordSRV] +depend: [JarvisTranslator] description: Chat prefixes for race, clan and company, plus private clan, company and staff channels. commands: clanchat: