Share and verify the menu click action gate

This commit is contained in:
unimatrixgit 2026-11-28 08:02:17 +01:00
commit 9b78a57168
3 changed files with 16 additions and 1 deletions

View file

@ -11,6 +11,7 @@ final class GuiSession {
private final AtomicBoolean used=new AtomicBoolean();
GuiSession(UUID owner){this.owner=owner;CURRENT.put(owner,this);}
boolean claim(UUID actor){return owner.equals(actor)&&System.nanoTime()<expires&&CURRENT.get(owner)==this&&used.compareAndSet(false,true)&&CURRENT.remove(owner,this);}
Runnable claimAction(UUID actor,Runnable action){return action!=null&&claim(actor)?action:null;}
private volatile UUID formWorld;
void markFormVisible(UUID world){formWorld=world;}
static boolean hasVisibleForm(UUID owner,UUID world){

View file

@ -111,7 +111,7 @@ final class MCGUIMenus implements AutoCloseable, Listener {
switch(method.getName()){
case "canvas":return alive?canvas:null;
case "pointer":{int next=slot((int)args[1],(int)args[2]);if(next!=hovered){hovered=next;schedule();}return null;}
case "click":{int slot=slot((int)args[1],(int)args[2]);if(!current())return null;if(slot<=-100){picked=-100-slot;scroll=0;schedule();return null;}if(slot==-1){finish();sessions.remove(player.getUniqueId());return null;}Runnable action=slot==-3?()->player.performCommand("menu"):actions.get(slot);if(action!=null&&gate.claim(player.getUniqueId())){finish();sessions.remove(player.getUniqueId());Bukkit.getScheduler().runTask(plugin,()->{if(player.isOnline()&&world.equals(player.getWorld().getUID()))action.run();});}return null;}
case "click":{int slot=slot((int)args[1],(int)args[2]);if(!current())return null;if(slot<=-100){picked=-100-slot;scroll=0;schedule();return null;}if(slot==-1){finish();sessions.remove(player.getUniqueId());return null;}Runnable action=gate.claimAction(player.getUniqueId(),slot==-3?()->player.performCommand("menu"):actions.get(slot));if(action!=null){finish();sessions.remove(player.getUniqueId());Bukkit.getScheduler().runTask(plugin,()->{if(player.isOnline()&&world.equals(player.getWorld().getUID()))action.run();});}return null;}
case "scroll":{Object f=frame;if(f!=null){double max=((Number)f.getClass().getMethod("maxScroll").invoke(f)).doubleValue();scroll=Math.max(0,Math.min(max,scroll+(int)args[3]*90));schedule();}return null;}
case "closed":alive=false;gate.expire();sessions.remove(player.getUniqueId(),this);return null;
case "toString":return "DuneRealms menu view";

View file

@ -8,4 +8,18 @@ class GuiSessionTest {
GuiSession fresh=new GuiSession(player);assertFalse(old.claim(player));assertTrue(fresh.claim(player));assertFalse(fresh.claim(player));
GuiSession expired=new GuiSession(player);expired.expire();assertFalse(expired.claim(player));
}
@Test void aHitClaimsOnlyItsOwnActionAndReadOnlySlotsLeaveTheSessionUsable(){
UUID player=UUID.randomUUID();
var stale=new GuiSession(player);
var current=new GuiSession(player);
var calls=new java.util.concurrent.atomic.AtomicInteger();
Runnable action=calls::incrementAndGet;
assertNull(stale.claimAction(player,action));
assertNull(current.claimAction(player,null));
assertNull(current.claimAction(UUID.randomUUID(),action));
assertSame(action,current.claimAction(player,action));
action.run();
assertEquals(1,calls.get());
assertNull(current.claimAction(player,action));
}
}