2021-07-14 15:23:55 +02:00
|
|
|
package net.citizensnpcs.trait;
|
|
|
|
|
2022-02-19 13:27:41 +01:00
|
|
|
import java.util.List;
|
2021-07-14 15:23:55 +02:00
|
|
|
import java.util.Map;
|
2022-07-17 16:29:11 +02:00
|
|
|
import java.util.function.Consumer;
|
2022-07-26 17:50:28 +02:00
|
|
|
import java.util.function.Function;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
2022-07-21 18:43:40 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2022-07-17 17:01:59 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2022-04-20 05:55:15 +02:00
|
|
|
import org.bukkit.Material;
|
2022-07-17 16:29:11 +02:00
|
|
|
import org.bukkit.entity.HumanEntity;
|
2021-07-14 15:23:55 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2022-04-20 05:55:15 +02:00
|
|
|
import org.bukkit.event.inventory.InventoryType;
|
2022-07-21 18:43:40 +02:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2022-02-19 13:27:41 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2022-07-17 16:29:11 +02:00
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
2022-07-17 16:29:11 +02:00
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
import com.google.common.base.Splitter;
|
2022-02-19 13:27:41 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2021-07-14 15:23:55 +02:00
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
2022-07-21 18:43:40 +02:00
|
|
|
import net.citizensnpcs.api.CitizensAPI;
|
2022-04-20 05:55:15 +02:00
|
|
|
import net.citizensnpcs.api.gui.CitizensInventoryClickEvent;
|
|
|
|
import net.citizensnpcs.api.gui.ClickHandler;
|
2022-06-14 15:26:18 +02:00
|
|
|
import net.citizensnpcs.api.gui.InputMenus;
|
|
|
|
import net.citizensnpcs.api.gui.InputMenus.Choice;
|
2022-04-20 05:55:15 +02:00
|
|
|
import net.citizensnpcs.api.gui.InventoryMenu;
|
|
|
|
import net.citizensnpcs.api.gui.InventoryMenuPage;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.api.gui.InventoryMenuPattern;
|
2022-04-20 05:55:15 +02:00
|
|
|
import net.citizensnpcs.api.gui.InventoryMenuSlot;
|
|
|
|
import net.citizensnpcs.api.gui.Menu;
|
|
|
|
import net.citizensnpcs.api.gui.MenuContext;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.api.gui.MenuPattern;
|
2022-04-20 05:55:15 +02:00
|
|
|
import net.citizensnpcs.api.gui.MenuSlot;
|
2021-07-14 15:23:55 +02:00
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.api.persistence.PersistenceLoader;
|
2021-07-14 15:23:55 +02:00
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.trait.TraitName;
|
2022-07-17 17:01:00 +02:00
|
|
|
import net.citizensnpcs.api.util.Colorizer;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.api.util.DataKey;
|
2022-07-21 17:13:51 +02:00
|
|
|
import net.citizensnpcs.trait.shop.NPCShopAction;
|
2022-07-26 17:50:28 +02:00
|
|
|
import net.citizensnpcs.trait.shop.NPCShopAction.GUI;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shop trait for NPC GUI shops.
|
|
|
|
*/
|
|
|
|
@TraitName("shop")
|
|
|
|
public class ShopTrait extends Trait {
|
|
|
|
public ShopTrait() {
|
|
|
|
super("shop");
|
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getDefaultShop() {
|
2022-07-26 17:50:28 +02:00
|
|
|
return StoredShops.NPC_SHOPS.computeIfAbsent(npc.getUniqueId().toString(), NPCShop::new);
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getShop(String name) {
|
2022-07-26 17:50:28 +02:00
|
|
|
return StoredShops.GLOBAL_SHOPS.computeIfAbsent(name, NPCShop::new);
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class NPCShop {
|
2022-07-21 17:13:51 +02:00
|
|
|
@Persist(value = "")
|
2022-02-19 13:27:41 +01:00
|
|
|
private final String name;
|
|
|
|
@Persist(reify = true)
|
|
|
|
private final List<NPCShopPage> pages = Lists.newArrayList();
|
|
|
|
@Persist
|
2022-07-17 16:29:11 +02:00
|
|
|
private String title;
|
|
|
|
@Persist
|
|
|
|
private ShopType type = ShopType.COMMAND;
|
2022-07-16 20:10:38 +02:00
|
|
|
@Persist
|
|
|
|
private String viewPermission;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
|
|
|
private NPCShop(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display(Player sender) {
|
2022-07-16 20:10:38 +02:00
|
|
|
if (viewPermission != null && !sender.hasPermission(viewPermission))
|
2022-06-11 19:37:38 +02:00
|
|
|
return;
|
2022-07-21 18:43:40 +02:00
|
|
|
if (pages.size() == 0) {
|
|
|
|
sender.sendMessage(ChatColor.RED + "Empty shop");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
InventoryMenu.createSelfRegistered(new NPCShopViewer(this)).present(sender);
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
2022-01-06 12:25:01 +01:00
|
|
|
|
|
|
|
public void displayEditor(Player sender) {
|
2022-07-21 18:43:40 +02:00
|
|
|
InventoryMenu.createSelfRegistered(new NPCShopSettings(this)).present(sender);
|
2022-01-06 12:25:01 +01:00
|
|
|
}
|
2022-02-19 13:27:41 +01:00
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
2022-06-11 19:37:38 +02:00
|
|
|
|
2022-07-17 16:29:11 +02:00
|
|
|
public NPCShopPage getOrCreatePage(int page) {
|
|
|
|
while (pages.size() <= page) {
|
|
|
|
pages.add(new NPCShopPage(page));
|
|
|
|
}
|
|
|
|
return pages.get(page);
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:37:38 +02:00
|
|
|
public String getRequiredPermission() {
|
2022-07-16 20:10:38 +02:00
|
|
|
return viewPermission;
|
2022-06-11 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 16:29:11 +02:00
|
|
|
public void removePage(int index) {
|
|
|
|
for (int i = 0; i < pages.size(); i++) {
|
|
|
|
if (pages.get(i).index == index) {
|
|
|
|
pages.remove(i--);
|
|
|
|
index = -1;
|
|
|
|
} else if (index == -1) {
|
|
|
|
pages.get(i).index--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 19:37:38 +02:00
|
|
|
public void setPermission(String permission) {
|
2022-07-16 20:10:38 +02:00
|
|
|
this.viewPermission = permission;
|
|
|
|
if (viewPermission != null && viewPermission.isEmpty()) {
|
|
|
|
viewPermission = null;
|
2022-06-11 19:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 13:27:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-17 16:29:11 +02:00
|
|
|
@Menu(title = "NPC Shop Contents Editor", type = InventoryType.CHEST, dimensions = { 5, 9 })
|
|
|
|
public static class NPCShopContentsEditor extends InventoryMenuPage {
|
|
|
|
private MenuContext ctx;
|
|
|
|
private int page = 0;
|
|
|
|
private final NPCShop shop;
|
|
|
|
|
|
|
|
public NPCShopContentsEditor(NPCShop shop) {
|
|
|
|
this.shop = shop;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changePage(int newPage) {
|
|
|
|
this.page = newPage;
|
|
|
|
NPCShopPage sp = shop.getOrCreatePage(page);
|
|
|
|
for (int i = 0; i < ctx.getInventory().getSize(); i++) {
|
|
|
|
ctx.getSlot(i).clear();
|
2022-07-21 17:13:51 +02:00
|
|
|
NPCShopItem item = sp.getItem(i);
|
2022-07-17 16:29:11 +02:00
|
|
|
final int idx = i;
|
|
|
|
ctx.getSlot(i).addClickHandler(evt -> {
|
|
|
|
ctx.clearSlots();
|
|
|
|
NPCShopItem display = item;
|
|
|
|
if (display == null) {
|
|
|
|
display = new NPCShopItem();
|
|
|
|
if (evt.getCursor() != null) {
|
|
|
|
display.display = evt.getCursor().clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.getMenu().transition(new NPCShopItemEditor(display, modified -> {
|
|
|
|
if (modified == null) {
|
2022-07-21 17:13:51 +02:00
|
|
|
sp.removeItem(idx);
|
2022-07-17 16:29:11 +02:00
|
|
|
} else {
|
2022-07-21 17:13:51 +02:00
|
|
|
sp.setItem(idx, modified);
|
2022-07-17 16:29:11 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ctx.getSlot(i).setItemStack(item.display);
|
|
|
|
}
|
|
|
|
InventoryMenuSlot prev = ctx.getSlot(4 * 9 + 3);
|
|
|
|
InventoryMenuSlot edit = ctx.getSlot(4 * 9 + 4);
|
|
|
|
InventoryMenuSlot next = ctx.getSlot(4 * 9 + 5);
|
|
|
|
prev.clear();
|
|
|
|
if (page > 0) {
|
|
|
|
prev.setItemStack(new ItemStack(Material.FEATHER, 1), "Previous page (" + (page) + ")");
|
|
|
|
prev.addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
changePage(page - 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
next.clear();
|
|
|
|
next.setItemStack(new ItemStack(Material.FEATHER, 1),
|
|
|
|
page + 1 >= shop.pages.size() ? "New page" : "Next page (" + (page + 1) + ")");
|
|
|
|
next.addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
changePage(page + 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
edit.clear();
|
|
|
|
edit.setItemStack(new ItemStack(Material.BOOK), "Edit page");
|
|
|
|
edit.addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
2022-07-21 18:43:40 +02:00
|
|
|
ctx.getMenu().transition(new NPCShopPageSettings(shop.getOrCreatePage(page)));
|
2022-07-17 16:29:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
if (ctx.data().containsKey("removePage")) {
|
|
|
|
int index = (int) ctx.data().remove("removePage");
|
|
|
|
shop.removePage(index);
|
|
|
|
page = Math.max(page - 1, 0);
|
|
|
|
}
|
|
|
|
changePage(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class NPCShopItem implements Cloneable {
|
2022-02-19 13:27:41 +01:00
|
|
|
@Persist
|
2022-07-26 17:50:28 +02:00
|
|
|
private final List<NPCShopAction> cost = Lists.newArrayList();
|
2022-02-19 13:27:41 +01:00
|
|
|
@Persist
|
|
|
|
private ItemStack display;
|
2022-07-21 17:13:51 +02:00
|
|
|
@Persist
|
2022-07-26 17:50:28 +02:00
|
|
|
private final List<NPCShopAction> result = Lists.newArrayList();
|
|
|
|
|
|
|
|
private void changeAction(List<NPCShopAction> source, Function<NPCShopAction, Boolean> filter,
|
|
|
|
NPCShopAction delta) {
|
|
|
|
for (int i = 0; i < source.size(); i++) {
|
|
|
|
if (filter.apply(source.get(i))) {
|
|
|
|
if (delta == null) {
|
|
|
|
source.remove(i);
|
|
|
|
} else {
|
|
|
|
source.set(i, delta);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (delta != null) {
|
|
|
|
source.add(delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeCost(Function<NPCShopAction, Boolean> filter, NPCShopAction cost) {
|
|
|
|
changeAction(this.cost, filter, cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeResult(Function<NPCShopAction, Boolean> filter, NPCShopAction result) {
|
|
|
|
changeAction(this.result, filter, result);
|
|
|
|
}
|
2022-07-17 16:29:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public NPCShopItem clone() {
|
|
|
|
try {
|
|
|
|
return (NPCShopItem) super.clone();
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-21 18:43:40 +02:00
|
|
|
|
|
|
|
public void onClick(NPCShop shop, CitizensInventoryClickEvent event) {
|
|
|
|
}
|
2022-02-19 13:27:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-25 17:14:00 +02:00
|
|
|
@Menu(title = "NPC Shop Item Editor", type = InventoryType.CHEST, dimensions = { 6, 9 })
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuSlot(slot = { 3, 4 }, material = Material.DISPENSER, amount = 1, title = "<f>Place display item below")
|
2022-07-16 20:10:38 +02:00
|
|
|
public static class NPCShopItemEditor extends InventoryMenuPage {
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuPattern(
|
|
|
|
offset = { 0, 0 },
|
|
|
|
slots = { @MenuSlot(pat = 'x', material = Material.AIR) },
|
|
|
|
value = "x x\n x \nx x")
|
|
|
|
private InventoryMenuPattern actionItems;
|
2022-07-24 18:23:44 +02:00
|
|
|
private final Consumer<NPCShopItem> callback;
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuPattern(
|
|
|
|
offset = { 0, 6 },
|
|
|
|
slots = { @MenuSlot(pat = 'x', material = Material.AIR) },
|
|
|
|
value = "x x\n x \nx x")
|
|
|
|
private InventoryMenuPattern costItems;
|
2022-07-16 20:10:38 +02:00
|
|
|
private MenuContext ctx;
|
2022-07-17 16:29:11 +02:00
|
|
|
private final NPCShopItem modified;
|
|
|
|
private NPCShopItem original;
|
2022-07-16 20:10:38 +02:00
|
|
|
|
2022-07-17 17:01:00 +02:00
|
|
|
public NPCShopItemEditor(NPCShopItem item, Consumer<NPCShopItem> consumer) {
|
2022-07-17 16:29:11 +02:00
|
|
|
this.original = item;
|
|
|
|
this.modified = original.clone();
|
2022-07-24 18:23:44 +02:00
|
|
|
this.callback = consumer;
|
2022-07-16 20:10:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
2022-07-17 16:29:11 +02:00
|
|
|
if (modified.display != null) {
|
|
|
|
ctx.getSlot(9 + 4).setItemStack(modified.display);
|
|
|
|
}
|
2022-07-26 17:50:28 +02:00
|
|
|
int pos = 0;
|
|
|
|
for (GUI template : NPCShopAction.getGUIs()) {
|
|
|
|
ItemStack item = template.createMenuItem();
|
|
|
|
if (item == null)
|
|
|
|
continue;
|
|
|
|
costItems.getSlots().get(pos).setItemStack(item);
|
|
|
|
costItems.getSlots().get(pos).addClickHandler(event -> {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu()
|
|
|
|
.transition(template.createEditor(
|
|
|
|
modified.cost.stream().filter(template::manages).findFirst().orElse(null),
|
|
|
|
cost -> modified.changeCost(template::manages, cost)));
|
|
|
|
});
|
|
|
|
|
|
|
|
actionItems.getSlots().get(pos).setItemStack(item);
|
|
|
|
actionItems.getSlots().get(pos).addClickHandler(event -> {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu()
|
|
|
|
.transition(template.createEditor(
|
|
|
|
modified.result.stream().filter(template::manages).findFirst().orElse(null),
|
|
|
|
result -> modified.changeResult(template::manages, result)));
|
|
|
|
});
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
2022-07-17 16:29:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuSlot(slot = { 5, 3 }, material = Material.REDSTONE_BLOCK, amount = 1, title = "<7>Cancel")
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onCancel(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transitionBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClose(HumanEntity who) {
|
2022-07-17 17:01:00 +02:00
|
|
|
if (original != null && original.display == null) {
|
2022-07-17 16:29:11 +02:00
|
|
|
original = null;
|
|
|
|
}
|
2022-07-24 18:23:44 +02:00
|
|
|
callback.accept(original);
|
2022-07-17 16:29:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuSlot(slot = { 4, 5 }, material = Material.BOOK, amount = 1, title = "<f>Set description")
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onEditDescription(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
2022-07-17 17:01:00 +02:00
|
|
|
if (modified.display == null)
|
|
|
|
return;
|
2022-07-26 17:50:28 +02:00
|
|
|
ctx.getMenu()
|
|
|
|
.transition(InputMenus.stringSetter(() -> modified.display.getItemMeta().hasLore()
|
|
|
|
? Joiner.on("<br>").skipNulls().join(modified.display.getItemMeta().getLore())
|
|
|
|
: "", description -> {
|
|
|
|
ItemMeta meta = modified.display.getItemMeta();
|
|
|
|
meta.setLore(Lists
|
|
|
|
.newArrayList(Splitter.on("<br>").split(Colorizer.parseColors(description))));
|
|
|
|
modified.display.setItemMeta(meta);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 4, 3 }, material = Material.FEATHER, amount = 1, title = "<f>Set name")
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onEditName(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
2022-07-17 17:01:00 +02:00
|
|
|
if (modified.display == null)
|
|
|
|
return;
|
|
|
|
ctx.getMenu().transition(InputMenus.stringSetter(modified.display.getItemMeta()::getDisplayName, name -> {
|
|
|
|
ItemMeta meta = modified.display.getItemMeta();
|
|
|
|
meta.setDisplayName(ChatColor.RESET + Colorizer.parseColors(name));
|
|
|
|
modified.display.setItemMeta(meta);
|
|
|
|
}));
|
2022-07-17 16:29:11 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
@ClickHandler(slot = { 4, 4 })
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onModifyDisplayItem(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
if (event.getCursor() != null) {
|
|
|
|
event.setCurrentItem(event.getCursor());
|
|
|
|
modified.display = event.getCursor().clone();
|
|
|
|
} else {
|
|
|
|
event.setCurrentItem(null);
|
|
|
|
modified.display = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuSlot(slot = { 5, 4 }, material = Material.TNT, amount = 1, title = "<c>Remove")
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onRemove(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
original = null;
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transitionBack();
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
@MenuSlot(slot = { 5, 5 }, material = Material.EMERALD_BLOCK, amount = 1, title = "<a>Save")
|
2022-07-17 16:29:11 +02:00
|
|
|
public void onSave(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
original = modified;
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transitionBack();
|
2022-07-16 20:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 13:27:41 +01:00
|
|
|
public static class NPCShopPage {
|
|
|
|
@Persist("")
|
|
|
|
private int index;
|
2022-07-21 17:13:51 +02:00
|
|
|
@Persist(keyType = Integer.class, reify = true)
|
2022-02-19 13:27:41 +01:00
|
|
|
private final Map<Integer, NPCShopItem> items = Maps.newHashMap();
|
|
|
|
@Persist
|
|
|
|
private String title;
|
2022-07-17 16:29:11 +02:00
|
|
|
|
|
|
|
public NPCShopPage(int page) {
|
|
|
|
this.index = page;
|
|
|
|
}
|
2022-07-21 17:13:51 +02:00
|
|
|
|
|
|
|
public NPCShopItem getItem(int idx) {
|
|
|
|
return items.get(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeItem(int idx) {
|
|
|
|
items.remove(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setItem(int idx, NPCShopItem modified) {
|
|
|
|
items.put(idx, modified);
|
|
|
|
}
|
2022-07-17 16:29:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Menu(title = "NPC Shop Page Editor", type = InventoryType.CHEST, dimensions = { 5, 9 })
|
2022-07-21 18:43:40 +02:00
|
|
|
public static class NPCShopPageSettings extends InventoryMenuPage {
|
2022-07-17 16:29:11 +02:00
|
|
|
private MenuContext ctx;
|
|
|
|
private final NPCShopPage page;
|
|
|
|
|
2022-07-21 18:43:40 +02:00
|
|
|
public NPCShopPageSettings(NPCShopPage page) {
|
2022-07-17 16:29:11 +02:00
|
|
|
this.page = page;
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 0, 4 }, material = Material.FEATHER, amount = 1)
|
|
|
|
public void editPageTitle(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(InputMenus.stringSetter(() -> page.title, newTitle -> {
|
|
|
|
page.title = newTitle.isEmpty() ? null : newTitle;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
ctx.getSlot(4).setDescription("Set page title<br>Currently: " + page.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 4, 4 }, material = Material.TNT, amount = 1, title = "<c>Remove page")
|
|
|
|
public void removePage(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.data().put("removePage", page.index);
|
|
|
|
ctx.getMenu().transitionBack();
|
|
|
|
}
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:43:40 +02:00
|
|
|
@Menu(title = "NPC Shop Editor", type = InventoryType.CHEST, dimensions = { 1, 9 })
|
|
|
|
public static class NPCShopSettings extends InventoryMenuPage {
|
|
|
|
private MenuContext ctx;
|
|
|
|
private final NPCShop shop;
|
|
|
|
|
|
|
|
public NPCShopSettings(NPCShop shop) {
|
|
|
|
this.shop = shop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
2022-07-22 20:01:57 +02:00
|
|
|
if (shop.pages.size() > 0) {
|
|
|
|
ctx.getSlot(8).setItemStack(new ItemStack(Material.CHEST, 1), "Open shop");
|
|
|
|
ctx.getSlot(8).addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(new NPCShopViewer(shop));
|
|
|
|
});
|
|
|
|
}
|
2022-07-21 18:43:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 0, 4 }, material = Material.FEATHER, amount = 1, title = "Edit shop items")
|
|
|
|
public void onEditItems(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(new NPCShopContentsEditor(shop));
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 0, 2 }, material = Material.OAK_SIGN, amount = 1, title = "Edit shop permission")
|
|
|
|
public void onPermissionChange(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(InputMenus.stringSetter(shop::getRequiredPermission, shop::setPermission));
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 0, 6 }, material = Material.NAME_TAG, amount = 1, title = "Edit shop title")
|
|
|
|
public void onSetTitle(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(InputMenus.stringSetter(() -> shop.title, newTitle -> {
|
|
|
|
shop.title = newTitle.isEmpty() ? null : newTitle;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
@MenuSlot(slot = { 0, 0 }, material = Material.BOOK, amount = 1, title = "Edit shop type")
|
|
|
|
public void onShopTypeChange(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
ctx.getMenu().transition(InputMenus.<ShopType> picker("Edit shop type", chosen -> {
|
|
|
|
shop.type = chosen.getValue();
|
|
|
|
}, Choice.<ShopType> of(ShopType.BUY, Material.DIAMOND, "Players buy items", shop.type == ShopType.BUY),
|
|
|
|
Choice.of(ShopType.SELL, Material.EMERALD, "Players sell items", shop.type == ShopType.SELL),
|
|
|
|
Choice.of(ShopType.COMMAND, Material.ENDER_EYE, "Clicks trigger commands only",
|
|
|
|
shop.type == ShopType.COMMAND)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Menu(title = "Shop", type = InventoryType.CHEST, dimensions = { 5, 9 })
|
|
|
|
public static class NPCShopViewer extends InventoryMenuPage {
|
|
|
|
private MenuContext ctx;
|
|
|
|
private int currentPage = 0;
|
|
|
|
private final NPCShop shop;
|
|
|
|
|
|
|
|
public NPCShopViewer(NPCShop shop) {
|
|
|
|
this.shop = shop;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changePage(int newPage) {
|
|
|
|
this.currentPage = newPage;
|
|
|
|
NPCShopPage page = shop.pages.get(currentPage);
|
|
|
|
if (page.title != null && !page.title.isEmpty()) {
|
|
|
|
Bukkit.getScheduler().runTaskLater(CitizensAPI.getPlugin(), () -> {
|
|
|
|
ctx.setTitle(page.title);
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < ctx.getInventory().getSize(); i++) {
|
|
|
|
ctx.getSlot(i).clear();
|
|
|
|
NPCShopItem item = page.getItem(i);
|
|
|
|
if (item == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ctx.getSlot(i).setItemStack(item.display);
|
|
|
|
ctx.getSlot(i).addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
item.onClick(shop, evt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
InventoryMenuSlot prev = ctx.getSlot(4 * 9 + 3);
|
|
|
|
InventoryMenuSlot next = ctx.getSlot(4 * 9 + 5);
|
|
|
|
prev.clear();
|
|
|
|
if (currentPage > 0) {
|
|
|
|
prev.setItemStack(new ItemStack(Material.FEATHER, 1), "Previous page (" + (currentPage) + ")");
|
|
|
|
prev.addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
changePage(currentPage - 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
next.clear();
|
|
|
|
if (currentPage + 1 < shop.pages.size()) {
|
|
|
|
next.setItemStack(new ItemStack(Material.FEATHER, 1), "Next page (" + (currentPage + 1) + ")");
|
|
|
|
next.addClickHandler(evt -> {
|
|
|
|
evt.setCancelled(true);
|
|
|
|
changePage(currentPage + 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Inventory createInventory(String title) {
|
|
|
|
return Bukkit.createInventory(null, 45, shop.title == null || shop.title.isEmpty() ? "Shop" : shop.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
changePage(currentPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 05:55:15 +02:00
|
|
|
public enum ShopType {
|
|
|
|
BUY,
|
2022-07-17 16:29:11 +02:00
|
|
|
COMMAND,
|
|
|
|
SELL;
|
2022-04-20 05:55:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:50:28 +02:00
|
|
|
private static class StoredShops {
|
|
|
|
@Persist(value = "global", reify = true)
|
|
|
|
private static Map<String, NPCShop> GLOBAL_SHOPS = Maps.newHashMap();
|
|
|
|
@Persist(value = "npc", reify = true)
|
|
|
|
private static Map<String, NPCShop> NPC_SHOPS = Maps.newHashMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void loadShops(DataKey root) {
|
|
|
|
SAVED = PersistenceLoader.load(StoredShops.class, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void saveShops(DataKey root) {
|
|
|
|
PersistenceLoader.save(SAVED, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static StoredShops SAVED = new StoredShops();
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|