permission observer should not stop when updater is off

This commit is contained in:
rockyhawk64 2025-10-12 13:35:48 +11:00
parent 334856a421
commit 7d27cc133b
2 changed files with 66 additions and 62 deletions

View File

@ -1,5 +1,5 @@
name: CommandPanels name: CommandPanels
version: 4.1.0 version: 4.1.1
api-version: 1.21.10 api-version: 1.21.10
main: me.rockyhawk.commandpanels.CommandPanels main: me.rockyhawk.commandpanels.CommandPanels

View File

@ -17,52 +17,72 @@ import java.util.Map;
public class InventoryPanelUpdater { public class InventoryPanelUpdater {
private ScheduledTask checkTask; private ScheduledTask heartbeatTask;
private ScheduledTask updateTask; private ScheduledTask updateTask;
// List of permission states for observed permissions
private final Map<String, Boolean> lastObservedPermStates = new HashMap<>(); private final Map<String, Boolean> lastObservedPermStates = new HashMap<>();
/**
* Panel updater will maintain itself with a checkTask that will end the updater
* If it finds the panel has been closed it will end the updater tasks
*/
public void start(Context ctx, Player p, InventoryPanel panel) { public void start(Context ctx, Player p, InventoryPanel panel) {
// Stop existing tasks if any stop(); // always clean slate
stop();
// Determine update delay startHeartbeat(ctx, p, panel);
int updateDelay = 20;
if (panel.getUpdateDelay().matches("\\d+")) { int updateDelay = parseUpdateDelay(panel.getUpdateDelay());
updateDelay = Integer.parseInt(panel.getUpdateDelay()); if (updateDelay > 0) {
startUpdater(ctx, p, panel, updateDelay);
} }
}
// If update delay is 0 then do not run the updater private void startHeartbeat(Context ctx, Player p, InventoryPanel panel) {
if (updateDelay == 0) { final boolean isUsingPermObserver = ctx.fileHandler.config.getBoolean("permission-observer");
this.updateTask = null;
return;
}
InventoryPanelBuilder panelBuilder = new InventoryPanelBuilder(ctx, p); heartbeatTask = p.getScheduler().runAtFixedRate(
ItemBuilder builder = new ItemBuilder(ctx, panelBuilder);
// Main update task
this.updateTask = p.getScheduler().runAtFixedRate(
ctx.plugin, ctx.plugin,
(scheduledTask) -> { (task) -> {
Inventory inv = p.getOpenInventory().getTopInventory(); Inventory inv = p.getOpenInventory().getTopInventory();
InventoryHolder holder = inv.getHolder(); InventoryHolder holder = inv.getHolder();
// Stop everything if the panel is closed
if (!(holder instanceof InventoryPanel) || holder != panel) { if (!(holder instanceof InventoryPanel) || holder != panel) {
stop(); stop();
return; return;
} }
NamespacedKey itemIdKey = new NamespacedKey(ctx.plugin, "item_id"); // Handle permission observer
NamespacedKey baseIdKey = new NamespacedKey(ctx.plugin, "base_item_id"); if (!isUsingPermObserver) return;
NamespacedKey fillItem = new NamespacedKey(ctx.plugin, "fill_item"); for (String node : panel.getObservedPerms()) {
boolean current = p.hasPermission(node);
Boolean previous = lastObservedPermStates.put(node, current);
if (previous != null && previous != current) {
panel.open(ctx, p, false);
return;
}
}
},
null,
2,
2
);
}
private void startUpdater(Context ctx, Player p, InventoryPanel panel, int updateDelay) {
InventoryPanelBuilder panelBuilder = new InventoryPanelBuilder(ctx, p);
ItemBuilder builder = new ItemBuilder(ctx, panelBuilder);
NamespacedKey itemIdKey = new NamespacedKey(ctx.plugin, "item_id");
NamespacedKey baseIdKey = new NamespacedKey(ctx.plugin, "base_item_id");
NamespacedKey fillItem = new NamespacedKey(ctx.plugin, "fill_item");
updateTask = p.getScheduler().runAtFixedRate(
ctx.plugin,
(task) -> {
Inventory inv = p.getOpenInventory().getTopInventory();
InventoryHolder holder = inv.getHolder();
if (!(holder instanceof InventoryPanel) || holder != panel) {
stopUpdater(); // only stop this task, heartbeat may continue
return;
}
// Loop through items in the panel and update their state
for (int slot = 0; slot < inv.getSize(); slot++) { for (int slot = 0; slot < inv.getSize(); slot++) {
ItemStack item = inv.getItem(slot); ItemStack item = inv.getItem(slot);
if (item == null || item.getType().isAir()) continue; if (item == null || item.getType().isAir()) continue;
@ -99,44 +119,28 @@ public class InventoryPanelUpdater {
updateDelay, updateDelay,
updateDelay updateDelay
); );
}
final boolean isUsingPermObserver = ctx.fileHandler.config.getBoolean("permission-observer"); private int parseUpdateDelay(String delayStr) {
if (delayStr != null && delayStr.matches("\\d+")) {
// Fast heartbeat check task, should run frequently return Integer.parseInt(delayStr);
this.checkTask = p.getScheduler().runAtFixedRate( }
ctx.plugin, return 20; // default
(scheduledTask) -> {
Inventory inv = p.getOpenInventory().getTopInventory();
InventoryHolder holder = inv.getHolder();
if (!(holder instanceof InventoryPanel) || holder != panel) {
stop();
return;
}
// Permission Observer: Refresh if an observed perms state changes
if(!isUsingPermObserver) return; // Skip if disabled
for (String node : panel.getObservedPerms()) {
boolean currentState = p.hasPermission(node);
Boolean previousState = lastObservedPermStates.get(node);
lastObservedPermStates.put(node, currentState);
if (previousState != null && previousState != currentState) {
panel.open(ctx, p, false);
return;
}
}
},
null,
2,
2
);
} }
public void stop() { public void stop() {
if (checkTask != null) { stopHeartbeat();
checkTask.cancel(); stopUpdater();
checkTask = null; }
private void stopHeartbeat() {
if (heartbeatTask != null) {
heartbeatTask.cancel();
heartbeatTask = null;
} }
}
private void stopUpdater() {
if (updateTask != null) { if (updateTask != null) {
updateTask.cancel(); updateTask.cancel();
updateTask = null; updateTask = null;