From 40c6b4b85e0ea4db0fa4c6c517461ac61416323f Mon Sep 17 00:00:00 2001 From: fullwall Date: Sun, 3 Mar 2024 14:57:51 +0800 Subject: [PATCH] Add toggle trait command back --- .../citizensnpcs/commands/TraitCommands.java | 42 +++++++++++++++++++ main/src/main/resources/en.json | 4 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/commands/TraitCommands.java b/main/src/main/java/net/citizensnpcs/commands/TraitCommands.java index 6be5bd621..7dccd4154 100644 --- a/main/src/main/java/net/citizensnpcs/commands/TraitCommands.java +++ b/main/src/main/java/net/citizensnpcs/commands/TraitCommands.java @@ -132,4 +132,46 @@ public class TraitCommands { Bukkit.getPluginManager().callEvent(new NPCTraitCommandDetachEvent(npc, clazz, sender)); npc.removeTrait(clazz); } + + @Command( + aliases = { "trait" }, + usage = "[trait name], [trait name]...", + desc = "", + modifiers = { "*" }, + min = 1, + permission = "citizens.npc.trait") + public void toggle(CommandContext args, CommandSender sender, NPC npc) throws CommandException { + List added = Lists.newArrayList(); + List removed = Lists.newArrayList(); + List failed = Lists.newArrayList(); + for (String traitName : Splitter.on(',').split(args.getJoinedStrings(0))) { + if (!sender.hasPermission("citizens.npc.trait." + traitName) + && !sender.hasPermission("citizens.npc.trait.*")) { + failed.add(String.format("%s: No permission", traitName)); + continue; + } + Class clazz = CitizensAPI.getTraitFactory().getTraitClass(traitName); + if (clazz == null) { + failed.add(String.format("%s: Trait not found", traitName)); + continue; + } + boolean remove = npc.hasTrait(clazz); + if (remove) { + removeTrait(npc, clazz, sender); + removed.add(StringHelper.wrap(traitName)); + continue; + } + addTrait(npc, clazz, sender); + added.add(StringHelper.wrap(traitName)); + } + if (added.size() > 0) { + Messaging.sendTr(sender, Messages.TRAITS_ADDED, Joiner.on(", ").join(added)); + } + if (removed.size() > 0) { + Messaging.sendTr(sender, Messages.TRAITS_REMOVED, Joiner.on(", ").join(removed)); + } + if (failed.size() > 0) { + Messaging.send(sender, "Failed to toggle traits", Joiner.on(", ").join(failed)); + } + } } diff --git a/main/src/main/resources/en.json b/main/src/main/resources/en.json index 1b7b8c2c4..69abade1b 100644 --- a/main/src/main/resources/en.json +++ b/main/src/main/resources/en.json @@ -618,8 +618,8 @@ "citizens.commands.trait.remove.description" : "Removes traits on the NPC", "citizens.commands.trait.remove.help" : "", "citizens.commands.trait.removed" : "Removed {0} successfully.", - "citizens.commands.trait.toggle.description" : "Toggles traits on the NPC", - "citizens.commands.trait.toggle.help" : "", + "citizens.commands.trait.*.description" : "Toggles traits on the NPC", + "citizens.commands.trait.*.help" : "", "citizens.commands.traitc.*.description" : "Configures a trait", "citizens.commands.traitc.*.help" : "", "citizens.commands.traitc.missing" : "Trait not found.",