Add native crossplay menus and organic Arrakis highlands
Recovered from the attached four-commit source patch (original tip 1686291). Includes native Java/Bedrock menus, minimal Essentials, organic highlands, finite Dense Spice, desert villages, Harkonnen fortress and three personal highland sietches. Original validation reports 50 passing tests on Java 21/Paper 1.21.8. Live Paper/Velocity/Bedrock/ViaVersion staging remains pending. Existing world, configuration and player data must be preserved.
This commit is contained in:
parent
c692949a78
commit
d988ea2983
1 changed files with 2005 additions and 138 deletions
|
|
@ -0,0 +1,94 @@
|
|||
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.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) {}
|
||||
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<>();
|
||||
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);
|
||||
if(action==null) information.add(label+(description.isBlank()?"":"\n"+description));
|
||||
else choices.add(new Choice(label,description,action));
|
||||
}
|
||||
show(plugin,player,ChatColor.stripColor(title),String.join("\n\n",information),choices);
|
||||
}
|
||||
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) {
|
||||
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(); });
|
||||
})).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();
|
||||
player.showDialog(Dialog.create(builder->builder.empty()
|
||||
.base(DialogBase.builder(Component.text(title)).body(content.isBlank()?List.of():List.of(DialogBody.plainMessage(Component.text(content)))).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) form.button(choice.label()+ (choice.description().isBlank()?"":"\n"+choice.description()));
|
||||
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