forked from unimatrix/web
Remove remaining legacy renderers and isolate preserved game content from GUI assets
This commit is contained in:
parent
d4480cad6f
commit
70098d57ff
2 changed files with 183 additions and 823 deletions
|
|
@ -0,0 +1,41 @@
|
|||
package com.example.dunegui;
|
||||
|
||||
import java.util.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
/** Compatibility entry point. Presentation belongs exclusively to the supplied DuneMenus module. */
|
||||
public final class MenuPresentation {
|
||||
private MenuPresentation() {}
|
||||
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 caller,Player player,String title,Inventory model,Map<Integer,Runnable> actions){
|
||||
var owner=Bukkit.getPluginManager().getPlugin("DuneMenus");
|
||||
if(owner==null||!owner.isEnabled()){player.sendMessage("DuneMenus is currently unavailable.");return;}
|
||||
try {owner.getClass().getMethod("openSharedMenu",Player.class,String.class,Inventory.class,Map.class)
|
||||
.invoke(owner,player,title,model,Map.copyOf(actions));}
|
||||
catch(ReflectiveOperationException failure){caller.getLogger().warning("DuneMenus presentation contract unavailable");player.sendMessage("The menu is currently unavailable.");}
|
||||
}
|
||||
public static void show(JavaPlugin caller,Player player,String title,List<Choice> choices){show(caller,player,title,"",choices);}
|
||||
public static void show(JavaPlugin caller,Player player,String title,String content,List<Choice> choices){page(caller,player,title,content,choices,0);}
|
||||
private static void page(JavaPlugin caller,Player player,String title,String content,List<Choice> choices,int page){
|
||||
Inventory model=Bukkit.createInventory(null,54,Component.text(title));
|
||||
Map<Integer,Runnable> actions=new HashMap<>();
|
||||
if(!content.isBlank())model.setItem(4,item(title,content));
|
||||
int start=page*36,end=Math.min(start+36,choices.size());
|
||||
for(int i=start;i<end;i++){Choice c=choices.get(i);int slot=9+i-start;model.setItem(slot,item(c.label(),c.description()));actions.put(slot,c.action());}
|
||||
if(page>0){model.setItem(45,item("Previous",""));actions.put(45,()->page(caller,player,title,content,choices,page-1));}
|
||||
if(end<choices.size()){model.setItem(53,item("Next",""));actions.put(53,()->page(caller,player,title,content,choices,page+1));}
|
||||
inventory(caller,player,title,model,actions);
|
||||
}
|
||||
private static ItemStack item(String name,String description){
|
||||
var item=new ItemStack(Material.PAPER);var meta=item.getItemMeta();meta.displayName(Component.text(name));
|
||||
meta.lore(description.lines().map(Component::text).toList());item.setItemMeta(meta);return item;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
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 static boolean isBedrock(Player player) {
|
||||
return Bukkit.getPluginManager().isPluginEnabled("floodgate") && Bedrock.isPlayer(player);
|
||||
}
|
||||
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(isBedrock(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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff