Converge all Exchange menu entry points on the native service
All checks were successful
DuneRealms Resourcepack CI / resourcepack (push) Successful in 3m19s
DuneRealms Plugin CI / plugins (push) Successful in 4m49s

This commit is contained in:
unimatrixgit 2026-11-29 11:31:33 +01:00
commit 313e6c712b
3 changed files with 16 additions and 80 deletions

View file

@ -75,6 +75,12 @@ public final class DuneMenusPlugin extends JavaPlugin implements TabExecutor {
Backend backend() { return backend; }
void openNativeExchange(Player player) {
var gui=getServer().getPluginManager().getPlugin("DunerealmsGUI");
if(gui!=null&&gui.isEnabled())try{gui.getClass().getMethod("openExchangeMenu",Player.class).invoke(gui,player);return;}catch(ReflectiveOperationException failure){getLogger().warning("Native Exchange entry unavailable: "+failure.getClass().getSimpleName());}
player.sendMessage("The native Exchange service is temporarily unavailable. Please retry later.");
}
public void openMenu(Player player, String menu) {
if (player.hasPermission("dunemenus.use")) create(menu, player, null).open();
}

View file

@ -1,82 +1,9 @@
package com.dunerealms.menus;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.entity.Player;
/** 6 rows. Vertical tabs on the left, a 7x4 shelf of goods, toolbar underneath. "Sell" is a price stall. */
/** Every public menu entry uses the already working native Exchange flow. */
final class ExchangeMenu extends Menu {
private static final List<Integer> SHELF = new ArrayList<>();
static { for (int r = 1; r <= 4; r++) for (int c = 2; c <= 8; c++) SHELF.add(r * 9 + c); }
private String tab = "buy";
private int page = 0;
private long price = 10_000;
ExchangeMenu(DuneMenusPlugin plugin, Player p, Menu parent) { super(plugin, p, parent); }
String id() { return "exchange"; }
Skin skin() { return Skin.MARKET; }
String title() { return "Exchange · Merchant Hall"; }
String subtitle() { return "“Coin, cargo and rumour change hands here.”"; }
int rows() { return 6; }
private void tabButton(int slot, Material m, String label, String key) {
var item = Ui.item(m, (tab.equals(key) ? "<gold><b>" : "<gray>") + label);
if (tab.equals(key)) Ui.glow(item);
put(slot, item, t -> { tab = key; page = 0; redraw(); });
}
void draw() {
backdrop(Material.BROWN_STAINED_GLASS_PANE);
tabButton(9, Material.EMERALD, "Goods", "buy");
tabButton(18, Material.NETHERITE_CHESTPLATE, "Kits", "kits");
tabButton(27, Material.ENDER_CHEST, "My listings", "mine");
tabButton(36, Material.GOLD_NUGGET, "Sell", "sell");
back(46);
put(51, Ui.item(Material.CHEST, "<green>Collect purchases", "<gray>Move paid items to your inventory"),
t -> act(backend().collectPurchases(player)));
load(backend().balance(player), b -> put(47, Ui.item(Material.GOLD_INGOT, "<gold>Postbank available", "<white>" + Ui.dm(b))));
if (tab.equals("sell")) drawStall(); else drawShelf();
}
private void drawShelf() {
load(backend().listings(player, tab, page), pg -> {
boolean mine = tab.equals("mine");
for (int i = 0; i < SHELF.size() && i < pg.items().size(); i++) {
var l = pg.items().get(i);
put(SHELF.get(i), Ui.item(l.icon(), l.quantity(), "<white>" + l.title() + " <gray>· <gold>" + Ui.dm(l.priceMinor()),
"<gray>Seller: <white>" + l.seller(), "<gray>In stock: <white>" + l.quantity(), "",
mine ? "<yellow>Click to withdraw this listing"
: "<yellow>Click <gray>buy 1 <dark_gray>· <yellow>Shift-click <gray>buy " + Math.min(16, l.quantity())),
t -> {
if (mine) act(backend().withdraw(player, l));
else if (l.purchasable()) act(backend().buy(player, l, t.isShiftClick() && l.kitVersion() == null ? Math.min(16, l.quantity()) : 1));
});
}
put(49, Ui.item(Material.PAPER, "<gray>Page " + (page + 1)));
if (page > 0) put(45, Ui.item(Material.ARROW, "<gray>◀ Previous"), t -> { page--; redraw(); });
if (pg.hasNext()) put(53, Ui.item(Material.ARROW, "<gray>Next ▶"), t -> { page++; redraw(); });
});
}
private void chip(int slot, long delta) {
boolean up = delta > 0;
put(slot, Ui.item(up ? Material.LIME_DYE : Material.RED_DYE, (up ? "<green>+" : "<red>−") + Ui.dm(Math.abs(delta))),
t -> { price = Math.max(100, price + delta); redraw(); });
}
private void drawStall() {
var held = player.getInventory().getItemInMainHand().clone();
int heldSlot = player.getInventory().getHeldItemSlot();
long shownPrice = price;
boolean has = !held.getType().isAir();
put(13, has ? held.clone() : Ui.item(Material.BARRIER, "<red>Nothing in your hand"));
put(22, Ui.item(Material.OAK_SIGN, "<gray>Hold the item you want to sell", "<gray>in your main hand, set a price,", "<gray>then list it."));
chip(28, -10_000); chip(29, -1_000); chip(30, -100);
chip(32, 100); chip(33, 1_000); chip(34, 10_000);
put(31, Ui.item(Material.GOLD_NUGGET, "<gold><b>" + Ui.dm(price), "<gray>Price per item"));
put(40, Ui.glow(Ui.item(Material.EMERALD, "<green><b>List for " + Ui.dm(price))),
t -> { if (!has) { player.sendMessage(Ui.text("<red>Hold an item first.")); return; } act(backend().sell(player, held, heldSlot, shownPrice)); });
}
ExchangeMenu(DuneMenusPlugin plugin,Player player,Menu parent){super(plugin,player,parent);}
@Override boolean launchNative(){plugin.openNativeExchange(player);return true;}
String id(){return "exchange";}Skin skin(){return Skin.MARKET;}String title(){return "Exchange";}int rows(){return 6;}
void draw(){throw new IllegalStateException("Exchange must use the native service");}
}

View file

@ -43,12 +43,14 @@ abstract class Menu implements InventoryHolder {
abstract int rows();
abstract void draw();
String subtitle() { return ""; }
boolean launchNative() { return false; }
@Override public Inventory getInventory() { return inv; }
protected Backend backend() { return plugin.backend(); }
// ---------------------------------------------------------------- lifecycle
final void open() {
if (launchNative()) return;
if (!available()) return;
active = true;
plugin.opened(player, this);