181 lines
7.6 KiB
Java
181 lines
7.6 KiB
Java
package com.dunerealms.menus;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.TreeMap;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletionException;
|
|
import java.util.function.Consumer;
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.inventory.ClickType;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.InventoryHolder;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
/**
|
|
* One menu definition, three renderers: chest inventory (Java), Floodgate form (Bedrock) and an optional
|
|
* fullscreen provider. Subclasses only describe a layout in draw(). One click does one thing: no confirm screens.
|
|
*/
|
|
abstract class Menu implements InventoryHolder {
|
|
interface Button { void click(ClickType type); }
|
|
enum Mode { INVENTORY, BEDROCK, FULLSCREEN }
|
|
|
|
protected final DuneMenusPlugin plugin;
|
|
protected final Player player;
|
|
protected final Menu parent;
|
|
private Inventory inv;
|
|
private final Map<Integer, Button> buttons = new TreeMap<>();
|
|
private Mode mode = Mode.INVENTORY;
|
|
private boolean active, redrawn;
|
|
private int gen, pending;
|
|
|
|
Menu(DuneMenusPlugin plugin, Player player, Menu parent) { this.plugin = plugin; this.player = player; this.parent = parent; }
|
|
|
|
abstract String id();
|
|
abstract Skin skin();
|
|
abstract String title();
|
|
abstract int rows();
|
|
abstract void draw();
|
|
String subtitle() { return ""; }
|
|
|
|
@Override public Inventory getInventory() { return inv; }
|
|
protected Backend backend() { return plugin.backend(); }
|
|
|
|
// ---------------------------------------------------------------- lifecycle
|
|
final void open() {
|
|
if (!available()) return;
|
|
active = true;
|
|
mode = plugin.modeFor(player);
|
|
if (inv == null) inv = Bukkit.createInventory(this, rows() * 9, plugin.titleFor(this));
|
|
redraw();
|
|
if (mode == Mode.INVENTORY) showInventory();
|
|
}
|
|
|
|
private void showInventory() {
|
|
player.openInventory(inv);
|
|
player.playSound(player.getLocation(), skin().open(), 0.6f, 1f);
|
|
}
|
|
|
|
final void closed() { active = false; }
|
|
|
|
/** Leaves the menu system entirely (travel, house choice...). */
|
|
protected void end() { active = false; player.closeInventory(); }
|
|
|
|
protected void go(Menu next) {
|
|
if (!next.available()) return;
|
|
active = false;
|
|
Bukkit.getScheduler().runTask(plugin, next::open);
|
|
}
|
|
|
|
private boolean available() {
|
|
return switch (id()) {
|
|
case "bank", "atm", "withdraw" -> {
|
|
say(Backend.Result.no("Rihla is currently unavailable in Minecraft."));
|
|
yield false;
|
|
}
|
|
default -> true;
|
|
};
|
|
}
|
|
|
|
final void redraw() {
|
|
redrawn = true; gen++; pending = 0;
|
|
inv.clear(); buttons.clear();
|
|
draw();
|
|
if (mode != Mode.INVENTORY && pending == 0) present();
|
|
}
|
|
|
|
final void handle(int slot, ClickType type) {
|
|
Button b = buttons.get(slot);
|
|
if (b == null || !active || pending != 0) return;
|
|
player.playSound(player.getLocation(), skin().click(), 0.5f, 1.2f);
|
|
redrawn = false;
|
|
b.click(type);
|
|
if (mode != Mode.INVENTORY && active && !redrawn && pending == 0) present(); // keep forms/screens alive
|
|
}
|
|
|
|
// ---------------------------------------------------------------- drawing helpers
|
|
protected void backdrop(Material m) { ItemStack p = Ui.pane(m); for (int i = 0; i < inv.getSize(); i++) inv.setItem(i, p); }
|
|
protected void put(int slot, ItemStack item) { if (slot >= 0 && slot < inv.getSize()) inv.setItem(slot, item); }
|
|
protected void put(int slot, ItemStack item, Button b) { put(slot, item); buttons.put(slot, b); }
|
|
protected void back(int slot) { if (parent != null) put(slot, Ui.item(Material.ARROW, "<gray>Back"), t -> go(parent)); }
|
|
protected void say(Backend.Result r) { player.sendMessage(Component.text(r.message(), r.ok() ? net.kyori.adventure.text.format.NamedTextColor.GREEN : net.kyori.adventure.text.format.NamedTextColor.RED)); }
|
|
|
|
/** Runs the backend call, continues on the main thread, and ignores answers for a screen that was redrawn or closed. */
|
|
protected <T> void load(CompletableFuture<T> f, Consumer<T> ok) {
|
|
final int g = gen;
|
|
pending++;
|
|
f.whenComplete((value, err) -> Bukkit.getScheduler().runTask(plugin, () -> {
|
|
if (g != gen || !active || !player.isOnline()) return;
|
|
pending--;
|
|
if (err != null) {
|
|
Throwable cause = err instanceof CompletionException && err.getCause() != null ? err.getCause() : err;
|
|
player.sendMessage(Ui.text(cause instanceof Backend.Unavailable
|
|
? "<red>This service isn't connected to the new menus yet."
|
|
: "<red>Something went wrong. Please try again."));
|
|
} else ok.accept(value);
|
|
if (g == gen && mode != Mode.INVENTORY && pending == 0) present();
|
|
}));
|
|
}
|
|
|
|
/** Backend action: tell the player the outcome, then refresh. */
|
|
protected void act(CompletableFuture<Backend.Result> f) { load(f, r -> { say(r); redraw(); }); }
|
|
|
|
// ---------------------------------------------------------------- Bedrock / fullscreen
|
|
private static String plain(ItemStack it) {
|
|
var m = it.getItemMeta();
|
|
Component c = m == null ? null : m.displayName();
|
|
return c == null ? "" : PlainTextComponentSerializer.plainText().serialize(c).trim();
|
|
}
|
|
|
|
private static List<String> loreOf(ItemStack it) {
|
|
var m = it.getItemMeta();
|
|
List<Component> l = m == null ? null : m.lore();
|
|
List<String> out = new ArrayList<>();
|
|
if (l != null) for (Component c : l) out.add(PlainTextComponentSerializer.plainText().serialize(c).trim());
|
|
return out;
|
|
}
|
|
|
|
private List<Card> snapshot() {
|
|
List<Card> out = new ArrayList<>();
|
|
for (int slot : buttons.keySet()) {
|
|
ItemStack it = inv.getItem(slot);
|
|
if (it == null || plain(it).isBlank()) continue;
|
|
out.add(new Card(slot, plain(it), loreOf(it), it.getType().name().toLowerCase()));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
private List<String> info() {
|
|
List<String> out = new ArrayList<>();
|
|
for (int i = 0; i < inv.getSize(); i++) {
|
|
ItemStack it = inv.getItem(i);
|
|
if (it == null || buttons.containsKey(i) || it.getType().name().endsWith("_PANE")) continue;
|
|
String n = plain(it);
|
|
if (n.isBlank()) continue;
|
|
List<String> lore = loreOf(it).stream().filter(s -> !s.isBlank()).toList();
|
|
out.add(lore.isEmpty() ? n : n + " — " + String.join(" · ", lore));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
private void present() {
|
|
List<Card> cards = snapshot();
|
|
List<String> info = info();
|
|
if (mode == Mode.FULLSCREEN) {
|
|
FullscreenProvider provider = plugin.fullscreen();
|
|
boolean ok = provider != null && provider.open(player,
|
|
new FullscreenProvider.Screen(id(), title(), subtitle(), cards, info),
|
|
slot -> handle(slot, ClickType.LEFT), () -> active = false);
|
|
if (ok) return;
|
|
mode = Mode.INVENTORY; // provider declined: fall back to the chest menu
|
|
showInventory();
|
|
return;
|
|
}
|
|
Bedrock.show(plugin, player, title(), subtitle(), info, cards, slot -> handle(slot, ClickType.LEFT), () -> active = false);
|
|
}
|
|
}
|

Staff