Allow /npc shop to be performed without an NPC selected

This commit is contained in:
fullwall 2022-12-19 01:44:16 +08:00
parent f5dcdb8a1d
commit e7e2efd518
4 changed files with 59 additions and 31 deletions

View File

@ -284,6 +284,10 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
return new File(getDataFolder(), "scripts");
}
public StoredShops getShops() {
return shops;
}
@Override
public SkullMetaProvider getSkullMetaProvider() {
return skullMetaProvider;

View File

@ -21,6 +21,25 @@ public class StoredShops {
this.storage = storage;
}
public void deleteShop(String name) {
if (globalShops.containsKey(name)) {
globalShops.remove(name);
} else {
npcShops.remove(name);
}
}
public NPCShop getGlobalShop(String name) {
return globalShops.computeIfAbsent(name, s -> new NPCShop(s));
}
public NPCShop getShop(String name) {
if (npcShops.containsKey(name)) {
return npcShops.get(name);
}
return getGlobalShop(name);
}
public void load() {
PersistenceLoader.load(this, storage.getKey(""));
}

View File

@ -51,6 +51,7 @@ import com.google.common.collect.Lists;
import net.citizensnpcs.Citizens;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.StoredShops;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.ai.tree.StatusMapper;
@ -146,10 +147,12 @@ import net.citizensnpcs.util.Util;
public class NPCCommands {
private final CommandHistory history;
private final NPCSelector selector;
private final StoredShops shops;
private final NPCRegistry temporaryRegistry;
public NPCCommands(Citizens plugin) {
selector = plugin.getNPCSelector();
shops = plugin.getShops();
temporaryRegistry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
history = new CommandHistory(selector);
}
@ -2325,30 +2328,36 @@ public class NPCCommands {
min = 1,
max = 3,
permission = "citizens.npc.shop")
@Requirements(selected = false, ownership = true)
public void shop(CommandContext args, Player sender, NPC npc,
@Arg(value = 1, completions = { "edit", "show", "delete" }) String action) throws CommandException {
ShopTrait trait = npc.getOrAddTrait(ShopTrait.class);
NPCShop shop = trait.getDefaultShop();
if (args.argsLength() > 1) {
if (args.argsLength() == 3) {
if (action.equalsIgnoreCase("edit")
&& !sender.hasPermission("citizens.npc.shop.edit." + args.getString(2).toLowerCase()))
throw new NoPermissionsException();
shop = trait.getShop(args.getString(2).toLowerCase());
if (args.argsLength() == 1) {
if (npc != null) {
npc.getOrAddTrait(ShopTrait.class).getDefaultShop().display(sender);
}
if (action.equalsIgnoreCase("edit")) {
if (!sender.hasPermission("citizens.npc.shop.edit"))
throw new NoPermissionsException();
shop.displayEditor(trait, sender);
} else if (action.equalsIgnoreCase("show")) {
shop.display(sender);
} else if (action.equalsIgnoreCase("delete")) {
trait.deleteShop(args.getString(2));
} else {
return;
}
NPCShop shop = npc != null ? npc.getOrAddTrait(ShopTrait.class).getDefaultShop() : null;
if (args.argsLength() == 3) {
shop = shops.getShop(args.getString(2).toLowerCase());
}
if (action.equalsIgnoreCase("delete")) {
if (args.argsLength() != 3)
throw new CommandUsageException();
}
} else {
shops.deleteShop(args.getString(2).toLowerCase());
return;
}
if (shop == null)
throw new CommandUsageException();
if (action.equalsIgnoreCase("edit")) {
if (!sender.hasPermission("citizens.admin") && (!sender.hasPermission("citizens.npc.shop.edit")
|| !sender.hasPermission("citizens.npc.shop.edit." + shop.getName())))
throw new NoPermissionsException();
shop.displayEditor(npc == null ? null : npc.getOrAddTrait(ShopTrait.class), sender);
} else if (action.equalsIgnoreCase("show")) {
shop.display(sender);
} else {
throw new CommandUsageException();
}
}

View File

@ -70,14 +70,6 @@ public class ShopTrait extends Trait {
this.shops = shops;
}
public void deleteShop(String name) {
if (shops.globalShops.containsKey(name)) {
shops.globalShops.remove(name);
} else {
shops.npcShops.remove(name);
}
}
public NPCShop getDefaultShop() {
return shops.npcShops.computeIfAbsent(npc.getUniqueId().toString(), (s) -> new NPCShop(s));
}
@ -88,7 +80,7 @@ public class ShopTrait extends Trait {
@Override
public void onRemove() {
deleteShop(npc.getUniqueId().toString());
shops.deleteShop(npc.getUniqueId().toString());
}
public void onRightClick(Player player) {
@ -113,7 +105,7 @@ public class ShopTrait extends Trait {
private NPCShop() {
}
private NPCShop(String name) {
public NPCShop(String name) {
this.name = name;
}
@ -535,8 +527,10 @@ public class ShopTrait extends Trait {
this.ctx = ctx;
ctx.getSlot(2).setDescription("<f>Edit shop view permission<br>" + shop.getRequiredPermission());
ctx.getSlot(6).setDescription("<f>Edit shop title<br>" + shop.title);
ctx.getSlot(8)
.setDescription("<f>Show shop on right click<br>" + shop.getName().equals(trait.rightClickShop));
if (trait != null) {
ctx.getSlot(8).setDescription(
"<f>Show shop on right click<br>" + shop.getName().equals(trait.rightClickShop));
}
}
@MenuSlot(slot = { 0, 4 }, material = Material.FEATHER, amount = 1, title = "<f>Edit shop items")
@ -569,6 +563,8 @@ 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);
if (trait == null)
return;
if (shop.getName().equals(trait.rightClickShop)) {
trait.rightClickShop = null;
} else {