Add op/server toggles to shop

This commit is contained in:
fullwall 2023-01-06 23:34:47 +08:00
parent a0439a3272
commit eed9170519
2 changed files with 22 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package net.citizensnpcs.trait.shop;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
@ -21,6 +22,10 @@ import net.citizensnpcs.util.Util;
public class CommandAction extends NPCShopAction { public class CommandAction extends NPCShopAction {
@Persist @Persist
public List<String> commands = Lists.newArrayList(); public List<String> commands = Lists.newArrayList();
@Persist
public boolean op = false;
@Persist
public boolean server = false;
public CommandAction() { public CommandAction() {
} }
@ -62,13 +67,13 @@ public class CommandAction extends NPCShopAction {
Player player = (Player) entity; Player player = (Player) entity;
return Transaction.create(() -> true, () -> { return Transaction.create(() -> true, () -> {
for (String command : commands) { for (String command : commands) {
Util.runCommand(null, player, command, false, true); Util.runCommand(null, player, command, op, !server);
} }
}, () -> { }, () -> {
}); });
} }
@Menu(title = "Command editor", dimensions = { 3, 9 }) @Menu(title = "Command editor", dimensions = { 4, 9 })
public static class CommandActionEditor extends InventoryMenuPage { public static class CommandActionEditor extends InventoryMenuPage {
private CommandAction base; private CommandAction base;
private Consumer<NPCShopAction> callback; private Consumer<NPCShopAction> callback;
@ -115,6 +120,19 @@ public class CommandAction extends NPCShopAction {
})); }));
}); });
} }
ctx.getSlot(3 * 9 + 3).setItemStack(new ItemStack(Util.getFallbackMaterial("COMMAND_BLOCK", "COMMAND")),
"Run commands as server", base.server ? ChatColor.GREEN + "On" : ChatColor.RED + "OFF");
ctx.getSlot(3 * 9 + 3).addClickHandler(InputMenus.clickToggle((res) -> {
base.server = res;
return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, base.server));
ctx.getSlot(3 * 9 + 4).setItemStack(
new ItemStack(Util.getFallbackMaterial("COMPARATOR", "REDSTONE_COMPARATOR")), "Run commands as op",
base.op ? ChatColor.GREEN + "On" : ChatColor.RED + "OFF");
ctx.getSlot(3 * 9 + 4).addClickHandler(InputMenus.clickToggle((res) -> {
base.op = res;
return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, base.server));
} }
@Override @Override

View File

@ -18,7 +18,6 @@ import org.bukkit.inventory.meta.Damageable;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.BooleanSlotHandler;
import net.citizensnpcs.api.gui.InputMenus; import net.citizensnpcs.api.gui.InputMenus;
import net.citizensnpcs.api.gui.InventoryMenuPage; import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.InventoryMenuSlot; import net.citizensnpcs.api.gui.InventoryMenuSlot;
@ -206,14 +205,14 @@ public class ItemAction extends NPCShopAction {
ctx.getSlot(3 * 9 + 1).setItemStack(new ItemStack(Material.ANVIL), "Must have no damage", ctx.getSlot(3 * 9 + 1).setItemStack(new ItemStack(Material.ANVIL), "Must have no damage",
base.requireUndamaged ? ChatColor.GREEN + "On" : ChatColor.RED + "Off"); base.requireUndamaged ? ChatColor.GREEN + "On" : ChatColor.RED + "Off");
ctx.getSlot(3 * 9 + 1).addClickHandler(new BooleanSlotHandler((res) -> { ctx.getSlot(3 * 9 + 1).addClickHandler(InputMenus.clickToggle((res) -> {
base.requireUndamaged = res; base.requireUndamaged = res;
return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off"; return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, base.requireUndamaged)); }, base.requireUndamaged));
ctx.getSlot(3 * 9 + 2).setItemStack( ctx.getSlot(3 * 9 + 2).setItemStack(
new ItemStack(Util.getFallbackMaterial("COMPARATOR", "REDSTONE_COMPARATOR")), new ItemStack(Util.getFallbackMaterial("COMPARATOR", "REDSTONE_COMPARATOR")),
"Compare item similarity", base.compareSimilarity ? ChatColor.GREEN + "On" : ChatColor.RED + "Off"); "Compare item similarity", base.compareSimilarity ? ChatColor.GREEN + "On" : ChatColor.RED + "Off");
ctx.getSlot(3 * 9 + 2).addClickHandler(new BooleanSlotHandler((res) -> { ctx.getSlot(3 * 9 + 2).addClickHandler(InputMenus.clickToggle((res) -> {
base.compareSimilarity = res; base.compareSimilarity = res;
return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off"; return res ? ChatColor.GREEN + "On" : ChatColor.RED + "Off";
}, base.compareSimilarity)); }, base.compareSimilarity));