mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-09-30 08:08:12 +02:00
Allow /npc shop to be performed without an NPC selected
This commit is contained in:
parent
f5dcdb8a1d
commit
e7e2efd518
@ -284,6 +284,10 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
|
|||||||
return new File(getDataFolder(), "scripts");
|
return new File(getDataFolder(), "scripts");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StoredShops getShops() {
|
||||||
|
return shops;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SkullMetaProvider getSkullMetaProvider() {
|
public SkullMetaProvider getSkullMetaProvider() {
|
||||||
return skullMetaProvider;
|
return skullMetaProvider;
|
||||||
|
@ -21,6 +21,25 @@ public class StoredShops {
|
|||||||
this.storage = storage;
|
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() {
|
public void load() {
|
||||||
PersistenceLoader.load(this, storage.getKey(""));
|
PersistenceLoader.load(this, storage.getKey(""));
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,7 @@ import com.google.common.collect.Lists;
|
|||||||
|
|
||||||
import net.citizensnpcs.Citizens;
|
import net.citizensnpcs.Citizens;
|
||||||
import net.citizensnpcs.Settings.Setting;
|
import net.citizensnpcs.Settings.Setting;
|
||||||
|
import net.citizensnpcs.StoredShops;
|
||||||
import net.citizensnpcs.api.CitizensAPI;
|
import net.citizensnpcs.api.CitizensAPI;
|
||||||
import net.citizensnpcs.api.ai.speech.SpeechContext;
|
import net.citizensnpcs.api.ai.speech.SpeechContext;
|
||||||
import net.citizensnpcs.api.ai.tree.StatusMapper;
|
import net.citizensnpcs.api.ai.tree.StatusMapper;
|
||||||
@ -146,10 +147,12 @@ import net.citizensnpcs.util.Util;
|
|||||||
public class NPCCommands {
|
public class NPCCommands {
|
||||||
private final CommandHistory history;
|
private final CommandHistory history;
|
||||||
private final NPCSelector selector;
|
private final NPCSelector selector;
|
||||||
|
private final StoredShops shops;
|
||||||
private final NPCRegistry temporaryRegistry;
|
private final NPCRegistry temporaryRegistry;
|
||||||
|
|
||||||
public NPCCommands(Citizens plugin) {
|
public NPCCommands(Citizens plugin) {
|
||||||
selector = plugin.getNPCSelector();
|
selector = plugin.getNPCSelector();
|
||||||
|
shops = plugin.getShops();
|
||||||
temporaryRegistry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
|
temporaryRegistry = CitizensAPI.createCitizensBackedNPCRegistry(new MemoryNPCDataStore());
|
||||||
history = new CommandHistory(selector);
|
history = new CommandHistory(selector);
|
||||||
}
|
}
|
||||||
@ -2325,31 +2328,37 @@ public class NPCCommands {
|
|||||||
min = 1,
|
min = 1,
|
||||||
max = 3,
|
max = 3,
|
||||||
permission = "citizens.npc.shop")
|
permission = "citizens.npc.shop")
|
||||||
|
@Requirements(selected = false, ownership = true)
|
||||||
public void shop(CommandContext args, Player sender, NPC npc,
|
public void shop(CommandContext args, Player sender, NPC npc,
|
||||||
@Arg(value = 1, completions = { "edit", "show", "delete" }) String action) throws CommandException {
|
@Arg(value = 1, completions = { "edit", "show", "delete" }) String action) throws CommandException {
|
||||||
ShopTrait trait = npc.getOrAddTrait(ShopTrait.class);
|
if (args.argsLength() == 1) {
|
||||||
NPCShop shop = trait.getDefaultShop();
|
if (npc != null) {
|
||||||
if (args.argsLength() > 1) {
|
npc.getOrAddTrait(ShopTrait.class).getDefaultShop().display(sender);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
|
shops.deleteShop(args.getString(2).toLowerCase());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (shop == null)
|
||||||
|
throw new CommandUsageException();
|
||||||
if (action.equalsIgnoreCase("edit")) {
|
if (action.equalsIgnoreCase("edit")) {
|
||||||
if (!sender.hasPermission("citizens.npc.shop.edit"))
|
if (!sender.hasPermission("citizens.admin") && (!sender.hasPermission("citizens.npc.shop.edit")
|
||||||
|
|| !sender.hasPermission("citizens.npc.shop.edit." + shop.getName())))
|
||||||
throw new NoPermissionsException();
|
throw new NoPermissionsException();
|
||||||
shop.displayEditor(trait, sender);
|
shop.displayEditor(npc == null ? null : npc.getOrAddTrait(ShopTrait.class), sender);
|
||||||
} else if (action.equalsIgnoreCase("show")) {
|
} else if (action.equalsIgnoreCase("show")) {
|
||||||
shop.display(sender);
|
shop.display(sender);
|
||||||
} else if (action.equalsIgnoreCase("delete")) {
|
|
||||||
trait.deleteShop(args.getString(2));
|
|
||||||
} else {
|
} else {
|
||||||
throw new CommandUsageException();
|
throw new CommandUsageException();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
shop.display(sender);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
|
@ -70,14 +70,6 @@ public class ShopTrait extends Trait {
|
|||||||
this.shops = shops;
|
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() {
|
public NPCShop getDefaultShop() {
|
||||||
return shops.npcShops.computeIfAbsent(npc.getUniqueId().toString(), (s) -> new NPCShop(s));
|
return shops.npcShops.computeIfAbsent(npc.getUniqueId().toString(), (s) -> new NPCShop(s));
|
||||||
}
|
}
|
||||||
@ -88,7 +80,7 @@ public class ShopTrait extends Trait {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRemove() {
|
public void onRemove() {
|
||||||
deleteShop(npc.getUniqueId().toString());
|
shops.deleteShop(npc.getUniqueId().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onRightClick(Player player) {
|
public void onRightClick(Player player) {
|
||||||
@ -113,7 +105,7 @@ public class ShopTrait extends Trait {
|
|||||||
private NPCShop() {
|
private NPCShop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private NPCShop(String name) {
|
public NPCShop(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,8 +527,10 @@ public class ShopTrait extends Trait {
|
|||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
ctx.getSlot(2).setDescription("<f>Edit shop view permission<br>" + shop.getRequiredPermission());
|
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(6).setDescription("<f>Edit shop title<br>" + shop.title);
|
||||||
ctx.getSlot(8)
|
if (trait != null) {
|
||||||
.setDescription("<f>Show shop on right click<br>" + shop.getName().equals(trait.rightClickShop));
|
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")
|
@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)
|
@MenuSlot(slot = { 0, 8 }, material = Material.COMMAND_BLOCK, amount = 1)
|
||||||
public void onToggleRightClick(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
public void onToggleRightClick(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
if (trait == null)
|
||||||
|
return;
|
||||||
if (shop.getName().equals(trait.rightClickShop)) {
|
if (shop.getName().equals(trait.rightClickShop)) {
|
||||||
trait.rightClickShop = null;
|
trait.rightClickShop = null;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user