forked from unimatrix/web
DuneRealms V2 clean baseline
Owner-authorised history reset; current source tree preserved exactly.
This commit is contained in:
commit
6deb4a2e7d
5 changed files with 65043 additions and 0 deletions
|
|
@ -0,0 +1,38 @@
|
|||
package com.example.dunegui;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import java.util.List;
|
||||
|
||||
/** Presentation-only note: never a bearer token or a source of wallet credit. */
|
||||
public final class DunemarkItems {
|
||||
public static final NamespacedKey MODEL = new NamespacedKey("dunerealms", "dunemark_note");
|
||||
public static final String ICON_URL = "https://www.dunerealms.com/assets/dunemark-icon-128.png";
|
||||
private DunemarkItems() {}
|
||||
public static int denomination(double balance) {
|
||||
for (int value : new int[]{5000,1000,100,5,1}) if (balance >= value) return value;
|
||||
return 1;
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack icon(String name, List<String> lore) {
|
||||
return icon(100,name,lore);
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
public static ItemStack icon(int value, String name, List<String> lore) {
|
||||
if (value!=1 && value!=5 && value!=100 && value!=1000 && value!=5000) throw new IllegalArgumentException("Unsupported Dunemark denomination");
|
||||
ItemStack item = new ItemStack(Material.PAPER);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName(PublicNames.text(name));
|
||||
meta.setLore(lore);
|
||||
meta.setItemModel(new NamespacedKey("dunerealms", "dunemark_"+value));
|
||||
item.setItemMeta(meta);
|
||||
return item;
|
||||
}
|
||||
public static boolean isNote(ItemStack item) {
|
||||
if(item==null || !item.hasItemMeta()) return false;
|
||||
NamespacedKey key=item.getItemMeta().getItemModel();
|
||||
return key!=null && key.getNamespace().equals("dunerealms") && (key.equals(MODEL) || java.util.Set.of("dunemark_1","dunemark_5","dunemark_100","dunemark_1000","dunemark_5000").contains(key.getKey()));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.example.dunegui;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/** One owner, one active screen, one accepted action, and a short expiry. */
|
||||
public final class MenuSession {
|
||||
private static final ConcurrentHashMap<UUID,MenuSession> CURRENT=new ConcurrentHashMap<>();
|
||||
private final UUID owner;
|
||||
private final long expires;
|
||||
private final AtomicBoolean used=new AtomicBoolean();
|
||||
public MenuSession(UUID owner) { this.owner=owner; expires=System.nanoTime()+Duration.ofMinutes(2).toNanos(); CURRENT.put(owner,this); }
|
||||
public boolean claim(UUID actor) {
|
||||
return owner.equals(actor) && System.nanoTime()<expires && CURRENT.get(owner)==this
|
||||
&& used.compareAndSet(false,true) && CURRENT.remove(owner,this);
|
||||
}
|
||||
public void expire() { CURRENT.remove(owner,this); }
|
||||
}
|
||||
111
gui-common/src/main/java/com/example/dunegui/NativeMenus.java
Normal file
111
gui-common/src/main/java/com/example/dunegui/NativeMenus.java
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
package com.example.dunegui;
|
||||
|
||||
import io.papermc.paper.dialog.Dialog;
|
||||
import io.papermc.paper.registry.data.dialog.ActionButton;
|
||||
import io.papermc.paper.registry.data.dialog.DialogBase;
|
||||
import io.papermc.paper.registry.data.dialog.body.DialogBody;
|
||||
import io.papermc.paper.registry.data.dialog.action.DialogAction;
|
||||
import io.papermc.paper.registry.data.dialog.type.DialogType;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickCallback;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/** Shared presentation layer. All gameplay callbacks run once on the server thread. */
|
||||
public final class NativeMenus {
|
||||
private NativeMenus() {}
|
||||
public record Choice(String label, String description, Runnable action, String imageUrl) {
|
||||
public Choice(String label,String description,Runnable action) { this(label,description,action,null); }
|
||||
}
|
||||
public static void inventory(JavaPlugin plugin, Player player, String title, Inventory model, Map<Integer,Runnable> actions) {
|
||||
List<Choice> choices = new ArrayList<>();
|
||||
List<String> information = new ArrayList<>();
|
||||
List<ItemStack> illustrations = new ArrayList<>();
|
||||
for(int slot=0;slot<model.getSize();slot++) {
|
||||
var item=model.getItem(slot);
|
||||
if(item==null || !item.hasItemMeta()) continue;
|
||||
var meta=item.getItemMeta();
|
||||
String label=ChatColor.stripColor(meta.getDisplayName());
|
||||
if(label==null || label.isBlank()) continue;
|
||||
String description=meta.hasLore()?String.join("\n",meta.getLore().stream().map(ChatColor::stripColor).toList()):"";
|
||||
Runnable action=actions.get(slot);
|
||||
boolean note=DunemarkItems.isNote(item);
|
||||
boolean realmImage=meta.hasItemModel()&&meta.getItemModel().getNamespace().equals("dunerealms");
|
||||
if(note||realmImage) illustrations.add(item.clone());
|
||||
if(action==null) information.add(label+(description.isBlank()?"":"\n"+description));
|
||||
else choices.add(new Choice(label,description,action,note?DunemarkItems.ICON_URL:null));
|
||||
}
|
||||
show(plugin,player,ChatColor.stripColor(title),String.join("\n\n",information),choices,illustrations);
|
||||
}
|
||||
public static void show(JavaPlugin plugin, Player player, String title, List<Choice> choices) {
|
||||
show(plugin,player,title,"",choices);
|
||||
}
|
||||
public static void show(JavaPlugin plugin, Player player, String title, String content, List<Choice> choices) {
|
||||
show(plugin,player,title,content,choices,List.of());
|
||||
}
|
||||
private static void show(JavaPlugin plugin, Player player, String title, String content, List<Choice> choices,List<ItemStack> illustrations) {
|
||||
MenuSession session=new MenuSession(player.getUniqueId());
|
||||
Bukkit.getScheduler().runTaskLater(plugin,session::expire,2400L);
|
||||
List<Choice> guarded=choices.stream().map(c->new Choice(c.label(),c.description(),()-> {
|
||||
if(!session.claim(player.getUniqueId())) return;
|
||||
Bukkit.getScheduler().runTask(plugin,()-> { if(player.isOnline()) c.action().run(); });
|
||||
},c.imageUrl())).toList();
|
||||
if(Bukkit.getPluginManager().isPluginEnabled("floodgate") && Bedrock.isPlayer(player)) {
|
||||
Bedrock.show(player,title,content,guarded); return;
|
||||
}
|
||||
// Dialogs were introduced with protocol 771 (Java 1.21.6). Fail closed if Via cannot identify a client.
|
||||
if(protocol(player)>=771) {
|
||||
List<ActionButton> buttons=guarded.stream().map(c->ActionButton.builder(Component.text(c.label()))
|
||||
.tooltip(Component.text(c.description()))
|
||||
.action(DialogAction.customClick((view,audience)-> {
|
||||
if(audience instanceof Player actor && actor.getUniqueId().equals(player.getUniqueId())) c.action().run();
|
||||
},ClickCallback.Options.builder().uses(1).lifetime(Duration.ofMinutes(2)).build())).build()).toList();
|
||||
List<DialogBody> body=new ArrayList<>();
|
||||
if(!content.isBlank())body.add(DialogBody.plainMessage(Component.text(content)));
|
||||
for(ItemStack illustration:illustrations)body.add(DialogBody.item(illustration,null,false,true,48,48));
|
||||
player.showDialog(Dialog.create(builder->builder.empty()
|
||||
.base(DialogBase.builder(Component.text(title)).body(body).build())
|
||||
.type(DialogType.multiAction(buttons).build())));
|
||||
return;
|
||||
}
|
||||
player.sendMessage(Component.text(title+" (select an option below)"));
|
||||
if(!content.isBlank()) player.sendMessage(Component.text(content));
|
||||
for(Choice choice:guarded) player.sendMessage(Component.text("["+choice.label()+"] "+choice.description())
|
||||
.clickEvent(net.kyori.adventure.text.event.ClickEvent.callback(audience->{
|
||||
if(audience instanceof Player actor && actor.getUniqueId().equals(player.getUniqueId())) choice.action().run();
|
||||
},ClickCallback.Options.builder().uses(1).lifetime(Duration.ofMinutes(2)).build())));
|
||||
}
|
||||
static int protocol(Player player) {
|
||||
if(Bukkit.getPluginManager().isPluginEnabled("ViaVersion")) {
|
||||
try {
|
||||
Class<?> via=Class.forName("com.viaversion.viaversion.api.Via");
|
||||
Object api=via.getMethod("getAPI").invoke(null);
|
||||
return (int)Class.forName("com.viaversion.viaversion.api.ViaAPI")
|
||||
.getMethod("getPlayerVersion",UUID.class).invoke(api,player.getUniqueId());
|
||||
} catch(ReflectiveOperationException | LinkageError ex) { return -1; }
|
||||
}
|
||||
return player.getProtocolVersion();
|
||||
}
|
||||
/** Loaded only when the optional Floodgate plugin is enabled. Never shades Cumulus/API classes. */
|
||||
private static final class Bedrock {
|
||||
static boolean isPlayer(Player player) { return org.geysermc.floodgate.api.FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId()); }
|
||||
static void show(Player player,String title,String content,List<Choice> choices) {
|
||||
var form=org.geysermc.cumulus.form.SimpleForm.builder().title(title).content(content);
|
||||
for(Choice choice:choices) {
|
||||
String label=choice.label()+ (choice.description().isBlank()?"":"\n"+choice.description());
|
||||
if(choice.imageUrl()==null)form.button(label);
|
||||
else form.button(label,org.geysermc.cumulus.util.FormImage.Type.URL,choice.imageUrl());
|
||||
}
|
||||
form.validResultHandler(response->{ int index=response.clickedButtonId(); if(index>=0 && index<choices.size()) choices.get(index).action().run(); });
|
||||
if(!org.geysermc.floodgate.api.FloodgateApi.getInstance().sendForm(player.getUniqueId(),form.build()))
|
||||
player.sendMessage("The menu could not be sent. Close your current screen and try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.example.dunegui;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import java.util.Map;
|
||||
public final class PlanetSigils {
|
||||
private static final Map<String,String> IDS=Map.of("arrakis","serevan","caladan","pelareth","giedi_prime","varkesh","kaitain","orivane","sigma_draconis_iv","nivareth");
|
||||
private static final Map<String,String> LABELS=Map.of("arrakis","Arrakduin","caladan","Kaladam","giedi_prime","Giedermark","kaitain","Kaiterhof","sigma_draconis_iv","Sigmadraken IV");
|
||||
public static String label(String id){String name=LABELS.getOrDefault(id,id);return name.isEmpty()?name:Character.toUpperCase(name.charAt(0))+name.substring(1);}
|
||||
public static ItemStack apply(ItemStack item,String id){String name=IDS.get(id);if(name!=null){var meta=item.getItemMeta();meta.setItemModel(new NamespacedKey("dunerealms","planet_"+name));item.setItemMeta(meta);}return item;}
|
||||
private PlanetSigils(){}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.example.dunegui;
|
||||
import java.util.Map;
|
||||
/** Presentation only; callers retain canonical faction/world keys for persistence and commands. */
|
||||
public final class PublicNames {
|
||||
private static final Map<String,String> LABELS=Map.ofEntries(Map.entry("Atreides","Aerdrecht"),Map.entry("Harkonnen","Harkveen"),Map.entry("Corrino","Korenholt"),Map.entry("Ordos","Oordwijk"),Map.entry("Fremen","Vrijduin"),Map.entry("Spacing Guild","Vaerkring"),Map.entry("Guild Navigators","Vaerkring Routekeepers"),Map.entry("Guild Navigator","Vaerkring Routekeeper"),Map.entry("CHOAM","Koopkring"),Map.entry("Solaris","Dunemarks"),Map.entry("Steward of Dune","Specerhuis"),Map.entry("House of Water","Welvaart House"));
|
||||
private static final Map<String,String> WORLDS=Map.of("Arrakis","Arrakduin","Caladan","Kaladam","Giedi Prime","Giedermark","Kaitain","Kaiterhof","Sigma Draconis IV","Sigmadraken IV","Stillsuit","Reclaimer suit","Sietch","Rockhaven");
|
||||
private static final Map<String,String> LEGACY=Map.of("Serevan","Arrakduin","Pelareth","Kaladam","Varkesh","Giedermark","Orivane","Kaiterhof","Nivareth","Sigmadraken IV");
|
||||
public static String text(String text){if(text==null)return null;for(var pair:LABELS.entrySet())text=text.replaceAll("\\b"+java.util.regex.Pattern.quote(pair.getKey())+"\\b",java.util.regex.Matcher.quoteReplacement(pair.getValue()));for(var pair:WORLDS.entrySet())text=text.replaceAll("\\b"+java.util.regex.Pattern.quote(pair.getKey())+"\\b",java.util.regex.Matcher.quoteReplacement(pair.getValue()));for(var pair:LEGACY.entrySet())text=text.replaceAll("\\b"+java.util.regex.Pattern.quote(pair.getKey())+"\\b",java.util.regex.Matcher.quoteReplacement(pair.getValue()));return text;}
|
||||
@SuppressWarnings("unchecked") public static <T>T text(T value){return value instanceof String string?(T)text(string):value;}
|
||||
private PublicNames(){}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff