From 4f9506ce7a292461fd9759b8c9418e3b4b38e956 Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 8 Apr 2023 02:35:55 +0800 Subject: [PATCH] Add /npc painting --- .../citizensnpcs/commands/NPCCommands.java | 46 ++++++++++++++----- .../commands/gui/NPCConfigurator.java | 1 - .../net/citizensnpcs/npc/CitizensNPC.java | 2 +- .../npc/CitizensTraitFactory.java | 2 + .../net/citizensnpcs/trait/PaintingTrait.java | 38 +++++++++++++++ .../net/citizensnpcs/trait/ShopTrait.java | 2 + .../java/net/citizensnpcs/util/Messages.java | 1 + .../src/main/resources/messages_en.properties | 1 + 8 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 main/src/main/java/net/citizensnpcs/trait/PaintingTrait.java diff --git a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 2a6257f3b..7dd707a22 100644 --- a/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.UUID; import java.util.stream.Collectors; +import org.bukkit.Art; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.DyeColor; @@ -122,6 +123,7 @@ import net.citizensnpcs.trait.MirrorTrait; import net.citizensnpcs.trait.MountTrait; import net.citizensnpcs.trait.OcelotModifiers; import net.citizensnpcs.trait.PacketNPC; +import net.citizensnpcs.trait.PaintingTrait; import net.citizensnpcs.trait.PausePathfindingTrait; import net.citizensnpcs.trait.Poses; import net.citizensnpcs.trait.Powered; @@ -556,6 +558,18 @@ public class NPCCommands { } } + @Command( + aliases = { "npc" }, + usage = "configgui", + desc = "Display NPC configuration GUI", + modifiers = { "configgui" }, + min = 1, + max = 1, + permission = "citizens.npc.configgui") + public void configgui(CommandContext args, Player sender, NPC npc) { + InventoryMenu.createSelfRegistered(new NPCConfigurator(npc)).present(sender); + } + @Command( aliases = { "npc" }, usage = "controllable|control (-m(ount),-y,-n,-o(wner required))", @@ -1036,18 +1050,6 @@ public class NPCCommands { Messaging.sendTr(sender, key, npc.getName()); } - @Command( - aliases = { "npc" }, - usage = "gui", - desc = "Display NPC configuration GUI", - modifiers = { "gui" }, - min = 1, - max = 1, - permission = "citizens.npc.gui") - public void gui(CommandContext args, Player sender, NPC npc) { - InventoryMenu.createSelfRegistered(new NPCConfigurator(npc)).present(sender); - } - @Command( aliases = { "npc" }, usage = "hitbox --scale [scale] --width/height [value]", @@ -1873,6 +1875,26 @@ public class NPCCommands { } } + @Command( + aliases = { "npc" }, + usage = "painting (--art art)", + desc = "Set painting modifiers", + modifiers = { "painting" }, + min = 1, + max = 1, + permission = "citizens.npc.painting") + @Requirements(selected = true, ownership = true, types = { EntityType.PAINTING }) + public void painting(CommandContext args, CommandSender sender, NPC npc, @Flag("art") Art art) + throws CommandException { + PaintingTrait trait = npc.getOrAddTrait(PaintingTrait.class); + if (art != null) { + trait.setArt(art); + Messaging.sendTr(sender, Messages.PAINTING_ART_SET, npc.getName(), Util.prettyEnum(art)); + return; + } + throw new CommandUsageException(); + } + @Command( aliases = { "npc" }, usage = "passive (--set [true|false])", diff --git a/main/src/main/java/net/citizensnpcs/commands/gui/NPCConfigurator.java b/main/src/main/java/net/citizensnpcs/commands/gui/NPCConfigurator.java index 455bfe97b..af0955b69 100644 --- a/main/src/main/java/net/citizensnpcs/commands/gui/NPCConfigurator.java +++ b/main/src/main/java/net/citizensnpcs/commands/gui/NPCConfigurator.java @@ -74,6 +74,5 @@ public class NPCConfigurator extends InventoryMenuPage { InputMenus.stringSetter(() -> evt.npc.getName(), (input) -> evt.npc.setName(input))); } })); - } } diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java index 3b2f6e0d9..69423e40f 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java @@ -103,7 +103,7 @@ public class CitizensNPC extends AbstractNPC { unloadEvents(); } for (Trait trait : new ArrayList(traits.values())) { - trait.onDespawn(); + trait.onDespawn(reason); } Messaging.debug("Despawned", this, "DespawnReason." + reason); diff --git a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index d958c2974..3a382ca1b 100644 --- a/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/main/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -42,6 +42,7 @@ import net.citizensnpcs.trait.MirrorTrait; import net.citizensnpcs.trait.MountTrait; import net.citizensnpcs.trait.OcelotModifiers; import net.citizensnpcs.trait.PacketNPC; +import net.citizensnpcs.trait.PaintingTrait; import net.citizensnpcs.trait.PausePathfindingTrait; import net.citizensnpcs.trait.Poses; import net.citizensnpcs.trait.Powered; @@ -89,6 +90,7 @@ public class CitizensTraitFactory implements TraitFactory { registerTrait(TraitInfo.create(HologramTrait.class)); registerTrait(TraitInfo.create(Inventory.class)); registerTrait(TraitInfo.create(LookClose.class)); + registerTrait(TraitInfo.create(PaintingTrait.class)); registerTrait(TraitInfo.create(MirrorTrait.class)); registerTrait(TraitInfo.create(MountTrait.class)); registerTrait(TraitInfo.create(MobType.class).asDefaultTrait()); diff --git a/main/src/main/java/net/citizensnpcs/trait/PaintingTrait.java b/main/src/main/java/net/citizensnpcs/trait/PaintingTrait.java new file mode 100644 index 000000000..bb7756cf9 --- /dev/null +++ b/main/src/main/java/net/citizensnpcs/trait/PaintingTrait.java @@ -0,0 +1,38 @@ +package net.citizensnpcs.trait; + +import org.bukkit.Art; +import org.bukkit.entity.Painting; + +import net.citizensnpcs.api.persistence.Persist; +import net.citizensnpcs.api.trait.Trait; +import net.citizensnpcs.api.trait.TraitName; + +/** + * Persists {@link Painting} metadata. + */ +@TraitName("paintingtrait") +public class PaintingTrait extends Trait { + @Persist("art") + private Art art; + + public PaintingTrait() { + super("paintingtrait"); + } + + @Override + public void run() { + if (!(npc.getEntity() instanceof Painting)) + return; + Painting painting = (Painting) npc.getEntity(); + if (art != null) { + painting.setArt(art); + } + } + + /** + * @see Painting#setArt(Art) + */ + public void setArt(Art art) { + this.art = art; + } +} diff --git a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java index 3290a6f54..1cc8fbba5 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java @@ -357,6 +357,8 @@ public class ShopTrait extends Trait { max = Math.min(max, r); } } + if (max == 0) + return; } final int repeats = max == Integer.MAX_VALUE ? 1 : max; List take = apply(cost, action -> action.take(event.getWhoClicked(), repeats)); diff --git a/main/src/main/java/net/citizensnpcs/util/Messages.java b/main/src/main/java/net/citizensnpcs/util/Messages.java index 77137a85f..4d3676cb8 100644 --- a/main/src/main/java/net/citizensnpcs/util/Messages.java +++ b/main/src/main/java/net/citizensnpcs/util/Messages.java @@ -288,6 +288,7 @@ public class Messages { public static final String OVER_NPC_LIMIT = "citizens.limits.over-npc-limit"; public static final String OWNER_SET = "citizens.commands.npc.owner.set"; public static final String OWNER_SET_SERVER = "citizens.commands.npc.owner.set-server"; + public static final String PAINTING_ART_SET = "citizens.commands.npc.painting.art-set"; public static final String PANDA_HIDDEN_GENE_SET = "citizens.commands.npc.panda.hidden-gene-set"; public static final String PANDA_MAIN_GENE_SET = "citizens.commands.npc.panda.main-gene-set"; public static final String PANDA_ROLLING = "citizens.commands.npc.panda.rolling"; diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index ebed85cea..7bf6d35c2 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -221,6 +221,7 @@ citizens.commands.npc.pausepathfinding.pause-range-set=[{0}]] will now pause pat citizens.commands.npc.pausepathfinding.pause-ticks-set=[[{0}]] will now pause pathfinding for [[{1}]] ticks at a time. citizens.commands.npc.pausepathfinding.rightclick-set=[[{0}]] will now pause pathfinding on right click. citizens.commands.npc.pausepathfinding.rightclick-unset=[[{0}]] will no longer pause pathfinding on right click. +citizens.commands.npc.painting.art-set=[[{0}]]''s art set to [[{1}]]. 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.playerfilter.cleared=[[{0}]]''s filter cleared.