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-04-20 05:55:15 +02:00
|
|
|
import org.bukkit.Material;
|
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.InventoryAction;
|
|
|
|
import org.bukkit.event.inventory.InventoryType;
|
2022-02-19 13:27:41 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
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-04-20 05:55:15 +02:00
|
|
|
import net.citizensnpcs.api.gui.CitizensInventoryClickEvent;
|
|
|
|
import net.citizensnpcs.api.gui.ClickHandler;
|
|
|
|
import net.citizensnpcs.api.gui.InputMenu;
|
|
|
|
import net.citizensnpcs.api.gui.InventoryMenu;
|
|
|
|
import net.citizensnpcs.api.gui.InventoryMenuPage;
|
|
|
|
import net.citizensnpcs.api.gui.InventoryMenuSlot;
|
|
|
|
import net.citizensnpcs.api.gui.Menu;
|
|
|
|
import net.citizensnpcs.api.gui.MenuContext;
|
|
|
|
import net.citizensnpcs.api.gui.MenuSlot;
|
2021-07-14 15:23:55 +02:00
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
|
|
|
import net.citizensnpcs.api.trait.Trait;
|
|
|
|
import net.citizensnpcs.api.trait.TraitName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shop trait for NPC GUI shops.
|
|
|
|
*/
|
|
|
|
@TraitName("shop")
|
|
|
|
public class ShopTrait extends Trait {
|
|
|
|
public ShopTrait() {
|
|
|
|
super("shop");
|
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getDefaultShop() {
|
2022-02-19 13:27:41 +01:00
|
|
|
return NPC_SHOPS.computeIfAbsent(npc.getUniqueId().toString(), NPCShop::new);
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getShop(String name) {
|
2022-02-19 13:27:41 +01:00
|
|
|
return SHOPS.computeIfAbsent(name, NPCShop::new);
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class NPCShop {
|
|
|
|
@Persist
|
2022-02-19 13:27:41 +01:00
|
|
|
private final String name;
|
|
|
|
@Persist(reify = true)
|
|
|
|
private final List<NPCShopPage> pages = Lists.newArrayList();
|
|
|
|
@Persist
|
2022-04-20 05:55:15 +02:00
|
|
|
private final ShopType type = ShopType.VIEW;
|
2021-07-14 15:23:55 +02:00
|
|
|
|
|
|
|
private NPCShop(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display(Player sender) {
|
|
|
|
}
|
2022-01-06 12:25:01 +01:00
|
|
|
|
|
|
|
public void displayEditor(Player sender) {
|
2022-04-20 05:55:15 +02:00
|
|
|
InventoryMenu.create(new NPCShopEditor(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-04-20 05:55:15 +02:00
|
|
|
@Menu(title = "NPC Equipment", type = InventoryType.HOPPER, dimensions = { 0, 5 })
|
|
|
|
@MenuSlot(slot = { 0, 0 }, material = Material.BOOK, amount = 1, lore = "Edit shop type")
|
|
|
|
@MenuSlot(slot = { 0, 2 }, material = Material.OAK_SIGN, amount = 1, lore = "Edit shop permission")
|
|
|
|
public static class NPCShopEditor extends InventoryMenuPage {
|
|
|
|
private MenuContext ctx;
|
|
|
|
private final NPCShop shop;
|
|
|
|
|
|
|
|
public NPCShopEditor(NPCShop shop) {
|
|
|
|
this.shop = shop;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialise(MenuContext ctx) {
|
|
|
|
this.ctx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ClickHandler(slot = { 0, 2 }, filter = { InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_ONE })
|
|
|
|
public void onPermissionChange(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
ctx.getMenu().transition(new InputMenu());
|
|
|
|
}
|
|
|
|
|
|
|
|
@ClickHandler(slot = { 0, 0 }, filter = { InventoryAction.PICKUP_ALL, InventoryAction.PICKUP_ONE })
|
|
|
|
public void onShopTypeChange(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 13:27:41 +01:00
|
|
|
public static class NPCShopItem {
|
|
|
|
@Persist
|
|
|
|
private int cost;
|
|
|
|
@Persist
|
|
|
|
private ItemStack display;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class NPCShopPage {
|
|
|
|
@Persist("")
|
|
|
|
private int index;
|
|
|
|
@Persist(reify = true)
|
|
|
|
private final Map<Integer, NPCShopItem> items = Maps.newHashMap();
|
|
|
|
@Persist
|
|
|
|
private String title;
|
2021-07-14 15:23:55 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 05:55:15 +02:00
|
|
|
public enum ShopType {
|
|
|
|
BUY,
|
|
|
|
SELL,
|
|
|
|
VIEW;
|
|
|
|
}
|
|
|
|
|
2022-03-04 15:14:28 +01:00
|
|
|
@Persist(value = "npcShops", reify = true, namespace = "shopstrait")
|
2021-07-14 15:23:55 +02:00
|
|
|
private static Map<String, NPCShop> NPC_SHOPS = Maps.newHashMap();
|
2022-03-04 15:14:28 +01:00
|
|
|
@Persist(value = "namedShops", reify = true, namespace = "shopstrait")
|
2021-07-14 15:23:55 +02:00
|
|
|
private static Map<String, NPCShop> SHOPS = Maps.newHashMap();
|
|
|
|
}
|