19 lines
1.9 KiB
Java
19 lines
1.9 KiB
Java
package com.dunerealms.menus;
|
|
import java.util.*;
|
|
import java.util.function.IntConsumer;
|
|
import org.bukkit.entity.Player;
|
|
/** Uses the existing installed MC GUI canvas transport, with the supplied menu definitions. */
|
|
final class CanvasProvider implements FullscreenProvider {
|
|
private final DuneMenusPlugin plugin;
|
|
private final MenuArt art;private boolean prepared;
|
|
CanvasProvider(DuneMenusPlugin plugin){this.plugin=plugin;this.art=new MenuArt(plugin);var services=plugin.getServer().getPluginManager().getPlugin("DunerealmsGUI");if(services!=null)try{art.prepare(services.getDataFolder().toPath().resolve("mcgui"));prepared=true;}catch(Exception failure){plugin.getLogger().warning("Menu artwork preparation failed: "+failure.getClass().getSimpleName());}}
|
|
public boolean open(Player player,Screen screen,IntConsumer select,Runnable close){
|
|
var services=plugin.getServer().getPluginManager().getPlugin("DunerealmsGUI");
|
|
if(services==null||!services.isEnabled())return false;
|
|
Map<Integer,String> images=new HashMap<>();
|
|
try{if(prepared)for(Card card:screen.cards()){var inventory=screen.contents();if(card.slot()>=0&&card.slot()<inventory.length){String image=art.image(inventory[card.slot()]);if(!image.isEmpty())images.put(card.slot(),image);}}}catch(Exception failure){plugin.getLogger().warning("Menu artwork unavailable: "+failure.getClass().getSimpleName());}
|
|
Map<Integer,Runnable> actions=new HashMap<>();for(Card card:screen.cards())actions.put(card.slot(),()->select.accept(card.slot()));
|
|
try{return Boolean.TRUE.equals(services.getClass().getMethod("presentSuppliedMenu",Player.class,String.class,String.class,Map.class,Runnable.class).invoke(services,player,screen.title(),SuppliedDocument.html(screen.id(),screen.title(),screen.subtitle(),screen.cards(),screen.info(),images),actions,close));}
|
|
catch(ReflectiveOperationException failure){plugin.getLogger().warning("Menu canvas unavailable: "+failure.getClass().getSimpleName());return false;}
|
|
}
|
|
}
|

Staff