From d8a4c73f585c55463b9a225c58490fe427c2e83b Mon Sep 17 00:00:00 2001 From: fullwall Date: Wed, 30 Nov 2022 05:35:31 +0800 Subject: [PATCH] Make /npc shop open on right click with named shops too --- .../citizensnpcs/commands/NPCCommands.java | 2 +- .../net/citizensnpcs/trait/ShopTrait.java | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index aef4ae87c..f5c607ef6 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -2314,7 +2314,7 @@ public class NPCCommands { if (action.equalsIgnoreCase("edit")) { if (!sender.hasPermission("citizens.npc.shop.edit")) throw new NoPermissionsException(); - shop.displayEditor(sender); + shop.displayEditor(trait, sender); } else if (action.equalsIgnoreCase("show")) { shop.display(sender); } else { diff --git a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java index 6a5029f2f..a21d25afb 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java @@ -57,6 +57,9 @@ import net.citizensnpcs.util.Util; */ @TraitName("shop") public class ShopTrait extends Trait { + @Persist + private String rightClickShop; + public ShopTrait() { super("shop"); } @@ -70,17 +73,15 @@ public class ShopTrait extends Trait { } public void onRightClick(Player player) { - NPCShop shop = getDefaultShop(); - if (shop.openOnRightClick) { - shop.display(player); - } + if (rightClickShop == null) + return; + NPCShop shop = StoredShops.GLOBAL_SHOPS.getOrDefault(rightClickShop, getDefaultShop()); + shop.display(player); } public static class NPCShop { @Persist(value = "") private String name; - @Persist - private boolean openOnRightClick; @Persist(reify = true) private final List pages = Lists.newArrayList(); @Persist @@ -107,8 +108,8 @@ public class ShopTrait extends Trait { InventoryMenu.createSelfRegistered(new NPCShopViewer(this, sender)).present(sender); } - public void displayEditor(Player sender) { - InventoryMenu.createSelfRegistered(new NPCShopSettings(this)).present(sender); + public void displayEditor(ShopTrait trait, Player sender) { + InventoryMenu.createSelfRegistered(new NPCShopSettings(trait, this)).present(sender); } public String getName() { @@ -509,8 +510,10 @@ public class ShopTrait extends Trait { public static class NPCShopSettings extends InventoryMenuPage { private MenuContext ctx; private final NPCShop shop; + private final ShopTrait trait; - public NPCShopSettings(NPCShop shop) { + public NPCShopSettings(ShopTrait trait, NPCShop shop) { + this.trait = trait; this.shop = shop; } @@ -519,7 +522,7 @@ public class ShopTrait extends Trait { this.ctx = ctx; ctx.getSlot(2).setDescription("Edit shop view permission
" + shop.getRequiredPermission()); ctx.getSlot(6).setDescription("Edit shop title
" + shop.title); - ctx.getSlot(8).setDescription("Show shop on right click
" + shop.openOnRightClick); + ctx.getSlot(8).setDescription("Show shop on right click
" + shop.name.equals(trait.rightClickShop)); } @MenuSlot(slot = { 0, 4 }, material = Material.FEATHER, amount = 1, title = "Edit shop items") @@ -556,8 +559,12 @@ public class ShopTrait extends Trait { @MenuSlot(slot = { 0, 8 }, material = Material.COMMAND_BLOCK, amount = 1) public void onToggleRightClick(InventoryMenuSlot slot, CitizensInventoryClickEvent event) { event.setCancelled(true); - shop.openOnRightClick = !shop.openOnRightClick; - ctx.getSlot(8).setDescription("Show shop on right click
" + shop.openOnRightClick); + if (shop.name.equals(trait.rightClickShop)) { + trait.rightClickShop = null; + } else { + trait.rightClickShop = shop.name; + } + ctx.getSlot(8).setDescription("Show shop on right click
" + shop.name.equals(trait.rightClickShop)); } }