forked from unimatrix/web
63 lines
3.5 KiB
Java
63 lines
3.5 KiB
Java
package com.dunerealms.gui;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.List;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
/** Bridges the existing menu document and its server-side callbacks to an opt-in renderer. */
|
|
final class NativeMenus {
|
|
private NativeMenus() {}
|
|
|
|
static boolean show(JavaPlugin plugin, Player player, String setting, String title, String description,
|
|
Inventory inventory, Map<Integer,Runnable> actions, GuiSession gate,
|
|
DecisionsViewTransport.Style style, MenuDocument.Branding branding) {
|
|
if (!plugin.getConfig().getStringList(setting).contains(player.getUniqueId().toString())) return false;
|
|
var transport = Bukkit.getServicesManager().load(DecisionsViewTransport.class);
|
|
if (transport == null) return false;
|
|
var cards = MCGUIMenus.cards(inventory, actions).stream().map(card ->
|
|
new DecisionsViewTransport.Card(card.slot(), card.title(), card.lore(), card.action(), card.material())).toList();
|
|
return show(player, plugin, transport, title, description, cards, actions, gate, style, branding);
|
|
}
|
|
|
|
static boolean showGuild(JavaPlugin plugin, Player player, String title, String description,
|
|
List<DecisionsViewTransport.Card> cards, Map<Integer,Runnable> actions, GuiSession gate) {
|
|
if (!plugin.getConfig().getStringList("development.fullscreen-guild-viewers").contains(player.getUniqueId().toString())) return false;
|
|
var transport = Bukkit.getServicesManager().load(DecisionsViewTransport.class);
|
|
return transport != null && show(player, plugin, transport, title, description, cards, actions, gate,
|
|
DecisionsViewTransport.Style.GUILD, null);
|
|
}
|
|
|
|
private static boolean show(Player player, JavaPlugin plugin, DecisionsViewTransport transport, String title,
|
|
String description, List<DecisionsViewTransport.Card> cards, Map<Integer,Runnable> actions,
|
|
GuiSession gate, DecisionsViewTransport.Style style, MenuDocument.Branding branding) {
|
|
var identity = branding == null ? null :
|
|
new DecisionsViewTransport.Identity(branding.logo(), branding.mentat(), branding.portrait());
|
|
var document = new DecisionsViewTransport.Document(title, description, cards, style, identity);
|
|
Map<Integer,Runnable> visibleActions = new HashMap<>();
|
|
for (var card : cards) if (card.actionable()) visibleActions.put(card.slot(), actions.get(card.slot()));
|
|
var callbacks = Map.copyOf(visibleActions);
|
|
player.closeInventory();
|
|
var world = player.getWorld().getUID();
|
|
try {
|
|
if (!transport.open(player, document, slot -> {
|
|
if (!Bukkit.isPrimaryThread()) return;
|
|
if (!player.isOnline() || !world.equals(player.getWorld().getUID())) {
|
|
gate.expire();
|
|
return;
|
|
}
|
|
var action = gate.claimAction(player.getUniqueId(), callbacks.get(slot));
|
|
gate.expire();
|
|
if (action != null) action.run();
|
|
}, gate::expire)) return false;
|
|
} catch (RuntimeException | LinkageError failure) {
|
|
plugin.getLogger().warning("Development fullscreen view unavailable: " + failure.getClass().getSimpleName());
|
|
return false;
|
|
}
|
|
gate.markFormVisible(world);
|
|
return true;
|
|
}
|
|
}
|

Staff