Skip to content

Commit

Permalink
Work on /npc shop
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 21, 2022
1 parent 805cd35 commit 8c004fc
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 24 deletions.
4 changes: 1 addition & 3 deletions main/src/main/java/net/citizensnpcs/Citizens.java
Expand Up @@ -360,9 +360,7 @@ public void onEnable() {

Bukkit.getPluginManager().registerEvents(new EventListen(storedRegistries), this);

if (Setting.NPC_COST.asDouble() > 0) {
setupEconomy();
}
setupEconomy();

registerCommands();
enableSubPlugins();
Expand Down
17 changes: 17 additions & 0 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -1684,6 +1684,23 @@ public void pathto(CommandContext args, CommandSender sender, NPC npc) throws Co
npc.getNavigator().setTarget(loc);
}

@Command(
aliases = { "npc" },
usage = "pickupitems (--set [true|false])",
desc = "Allow NPC to pick up items",
modifiers = { "pickupitems" },
min = 1,
max = 1,
permission = "citizens.npc.pickupitems")
public void pickupitems(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
boolean pickup = !npc.data().get(NPC.Metadata.PICKUP_ITEMS, !npc.isProtected());
if (args.hasValueFlag("set")) {
pickup = Boolean.parseBoolean(args.getFlag("set").toLowerCase());
}
npc.data().set(NPC.Metadata.PICKUP_ITEMS, pickup);
Messaging.send(sender, pickup ? Messages.PICKUP_ITEMS_SET : Messages.PICKUP_ITEMS_UNSET, npc.getName());
}

@Command(
aliases = { "npc" },
usage = "panimate [animation]",
Expand Down
5 changes: 3 additions & 2 deletions main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -444,9 +444,10 @@ public void update() {

if (isLiving) {
NMS.setKnockbackResistance((LivingEntity) getEntity(), isProtected() ? 1D : 0D);
if (isProtected() && SUPPORT_PICKUP_ITEMS) {
if (SUPPORT_PICKUP_ITEMS) {
try {
((LivingEntity) getEntity()).setCanPickupItems(false);
((LivingEntity) getEntity())
.setCanPickupItems(data().get(NPC.Metadata.PICKUP_ITEMS, !isProtected()));
} catch (Throwable t) {
SUPPORT_PICKUP_ITEMS = false;
}
Expand Down
23 changes: 12 additions & 11 deletions main/src/main/java/net/citizensnpcs/trait/CommandTrait.java
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

Expand Down Expand Up @@ -55,11 +56,11 @@

@TraitName("commandtrait")
public class CommandTrait extends Trait {
@Persist
@Persist(keyType = Integer.class)
@DelegatePersistence(NPCCommandPersister.class)
private final Map<String, NPCCommand> commands = Maps.newHashMap();
@Persist(reify = true)
private final Map<String, PlayerNPCCommand> cooldowns = Maps.newHashMap();
private final Map<Integer, NPCCommand> commands = Maps.newHashMap();
@Persist(keyType = UUID.class, reify = true)
private final Map<UUID, PlayerNPCCommand> cooldowns = Maps.newHashMap();
@Persist
private double cost = -1;
private final Map<String, Set<CommandTraitMessages>> executionErrors = Maps.newHashMap();
Expand All @@ -82,7 +83,7 @@ public CommandTrait() {

public int addCommand(NPCCommandBuilder builder) {
int id = getNewId();
commands.put(String.valueOf(id), builder.build(id));
commands.put(id, builder.build(id));
return id;
}

Expand Down Expand Up @@ -228,7 +229,7 @@ public int compare(NPCCommand o1, NPCCommand o2) {
}
for (NPCCommand command : commandList) {
if (executionMode == ExecutionMode.SEQUENTIAL) {
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
PlayerNPCCommand info = cooldowns.get(player.getUniqueId());
if (info != null && info.lastUsedHand != hand) {
info.lastUsedHand = hand;
info.lastUsedId = -1;
Expand All @@ -252,10 +253,10 @@ private void runCommand(final Player player, NPCCommand command) {
Runnable runnable = new Runnable() {
@Override
public void run() {
PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString());
PlayerNPCCommand info = cooldowns.get(player.getUniqueId());
if (info == null && (executionMode == ExecutionMode.SEQUENTIAL
|| PlayerNPCCommand.requiresTracking(command))) {
cooldowns.put(player.getUniqueId().toString(), info = new PlayerNPCCommand());
cooldowns.put(player.getUniqueId(), info = new PlayerNPCCommand());
}
if (info != null && !info.canUse(CommandTrait.this, player, command)) {
return;
Expand Down Expand Up @@ -305,22 +306,22 @@ public float getExperienceCost() {

private int getNewId() {
int i = 0;
while (commands.containsKey(String.valueOf(i))) {
while (commands.containsKey(i)) {
i++;
}
return i;
}

public boolean hasCommandId(int id) {
return commands.containsKey(String.valueOf(id));
return commands.containsKey(id);
}

public boolean isHideErrorMessages() {
return hideErrorMessages;
}

public void removeCommandById(int id) {
commands.remove(String.valueOf(id));
commands.remove(id);
}

private void sendErrorMessage(Player player, CommandTraitMessages msg, Function<String, String> transform,
Expand Down
2 changes: 1 addition & 1 deletion main/src/main/java/net/citizensnpcs/trait/ScriptTrait.java
Expand Up @@ -27,7 +27,7 @@
@TraitName("scripttrait")
public class ScriptTrait extends Trait {
@Persist
public List<String> files = new ArrayList<String>();
private final List<String> files = new ArrayList<String>();
private final List<RunnableScript> runnableScripts = new ArrayList<RunnableScript>();

public ScriptTrait() {
Expand Down
29 changes: 22 additions & 7 deletions main/src/main/java/net/citizensnpcs/trait/ShopTrait.java
Expand Up @@ -31,6 +31,7 @@
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.trait.shop.NPCShopAction;

/**
* Shop trait for NPC GUI shops.
Expand All @@ -50,7 +51,7 @@ public NPCShop getShop(String name) {
}

public static class NPCShop {
@Persist
@Persist(value = "")
private final String name;
@Persist(reify = true)
private final List<NPCShopPage> pages = Lists.newArrayList();
Expand Down Expand Up @@ -123,7 +124,7 @@ public void changePage(int newPage) {
NPCShopPage sp = shop.getOrCreatePage(page);
for (int i = 0; i < ctx.getInventory().getSize(); i++) {
ctx.getSlot(i).clear();
NPCShopItem item = sp.items.get(i);
NPCShopItem item = sp.getItem(i);
final int idx = i;
ctx.getSlot(i).addClickHandler(evt -> {
ctx.clearSlots();
Expand All @@ -137,9 +138,9 @@ public void changePage(int newPage) {

ctx.getMenu().transition(new NPCShopItemEditor(display, modified -> {
if (modified == null) {
sp.items.remove(idx);
sp.removeItem(idx);
} else {
sp.items.put(idx, modified);
sp.setItem(idx, modified);
}
}));
});
Expand Down Expand Up @@ -229,9 +230,11 @@ public void onShopTypeChange(InventoryMenuSlot slot, CitizensInventoryClickEvent

public static class NPCShopItem implements Cloneable {
@Persist
private int cost;
private List<NPCShopAction> cost;
@Persist
private ItemStack display;
@Persist
private List<NPCShopAction> result;

@Override
public NPCShopItem clone() {
Expand Down Expand Up @@ -334,14 +337,26 @@ public void onSave(InventoryMenuSlot slot, CitizensInventoryClickEvent event) {
public static class NPCShopPage {
@Persist("")
private int index;
@Persist(reify = true)
@Persist(keyType = Integer.class, reify = true)
private final Map<Integer, NPCShopItem> items = Maps.newHashMap();
@Persist
private String title;

public NPCShopPage(int page) {
this.index = page;
}

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);
}
}

@Menu(title = "NPC Shop Page Editor", type = InventoryType.CHEST, dimensions = { 5, 9 })
Expand Down Expand Up @@ -383,6 +398,6 @@ public enum ShopType {

@Persist(value = "npcShops", reify = true, namespace = "shopstrait")
private static Map<String, NPCShop> NPC_SHOPS = Maps.newHashMap();
@Persist(value = "namedShops", reify = true, namespace = "shopstrait")
@Persist(value = "globalShops", reify = true, namespace = "shopstrait")
private static Map<String, NPCShop> SHOPS = Maps.newHashMap();
}
105 changes: 105 additions & 0 deletions main/src/main/java/net/citizensnpcs/trait/shop/NPCShopAction.java
@@ -0,0 +1,105 @@
package net.citizensnpcs.trait.shop;

import java.util.Arrays;
import java.util.List;

import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.ItemStack;

import com.google.common.collect.Lists;

import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.persistence.PersisterRegistry;

public abstract class NPCShopAction implements Cloneable {
@Override
public NPCShopAction clone() {
try {
return (NPCShopAction) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}

public abstract boolean grant(HumanEntity entity);

public abstract boolean take(HumanEntity entity);

public static class ItemAction extends NPCShopAction {
@Persist
public List<ItemStack> items = Lists.newArrayList();

public ItemAction() {
}

public ItemAction(ItemStack... items) {
this(Arrays.asList(items));
}

public ItemAction(List<ItemStack> items) {
this.items = items;
}

@Override
public boolean grant(HumanEntity entity) {
return false;
}

@Override
public boolean take(HumanEntity entity) {
return false;
}
}

public static class MoneyAction extends NPCShopAction {
@Persist
public int money;

public MoneyAction() {
}

public MoneyAction(int money) {
this.money = money;
}

@Override
public boolean grant(HumanEntity entity) {
return false;
}

@Override
public boolean take(HumanEntity entity) {
return false;
}
}

public static class PermissionAction extends NPCShopAction {
@Persist
public List<String> permissions = Lists.newArrayList();

public PermissionAction() {
}

public PermissionAction(List<String> permissions) {
this.permissions = permissions;
}

@Override
public boolean grant(HumanEntity entity) {
return false;
}

@Override
public boolean take(HumanEntity entity) {
return false;
}
}

public static PersisterRegistry<NPCShopAction> REGISTRY = PersistenceLoader.createRegistry(NPCShopAction.class);
static {
REGISTRY.register("items", ItemAction.class);
REGISTRY.register("permissions", PermissionAction.class);
REGISTRY.register("money", MoneyAction.class);
}
}
2 changes: 2 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/Messages.java
Expand Up @@ -275,6 +275,8 @@ public class Messages {
public static final String PATHFINDING_OPTIONS_USE_NEW_FINDER = "citizens.commands.npc.pathopt.use-new-finder";
public static final String PATHFINDING_RANGE_SET = "citizens.commands.npc.pathfindingrange.set";
public static final String PHANTOM_STATE_SET = "citizens.commands.npc.phantom.phantom-set";
public static final String PICKUP_ITEMS_SET = "citizens.commands.npc.pickupitems.set";
public static final String PICKUP_ITEMS_UNSET = "citizens.commands.npc.pickupitems.unset";
public static final String PIGLIN_DANCING_SET = "citizens.commands.npc.piglin.dancing-set";
public static final String PIGLIN_DANCING_UNSET = "citizens.commands.npc.piglin.dancing-unset";
public static final String PLAYER_NOT_FOUND_FOR_SPAWN = "citizens.commands.npc.create.no-player-for-spawn";
Expand Down
2 changes: 2 additions & 0 deletions main/src/main/resources/messages_en.properties
Expand Up @@ -181,6 +181,8 @@ citizens.commands.npc.playerlist.added=Added [[{0}]] to the player list.
citizens.commands.npc.playerlist.removed=Removed [[{0}]] from the player list.
citizens.commands.npc.polarbear.rearing-set=[[{0}]] is now rearing.
citizens.commands.npc.polarbear.rearing-unset=[[{0}]] is no longer rearing.
citizens.commands.npc.pickupitems.set=[[{0}]] will now pickup items.
citizens.commands.npc.pickupitems.unset=[[{0}]] will no longer pickup items.
citizens.commands.npc.piglin.dancing-set=[[{0}]] is now dancing.
citizens.commands.npc.piglin.dancing-unset=[[{0}]] is no longer dancing.
citizens.commands.npc.pose.added=Pose added.
Expand Down

0 comments on commit 8c004fc

Please sign in to comment.