From 8743c78281466b3e893242b599c81da9e966baec Mon Sep 17 00:00:00 2001 From: Fortifier42 Date: Wed, 12 Aug 2020 10:58:57 +1000 Subject: [PATCH] A few meta changes and additions (#2244) * Add Item in Off Hand for `- take` * Add world context to * Minor Meta fixes * Update PlayerTag.java Oopsies forgot to update this. * Minor Meta updates & JavaDoc * Removed redundant spaces * Remove more redundant spaces. * Correct identify() for TradeTag * Added Villager Events Added: `on villager acquires trade` `on villager replenishes trade` `on villager changes profession` Referencing https://github.com/DenizenScript/Denizen/issues/1913 * Correct typo on event name. * Revert "Minor Meta updates & JavaDoc" This reverts commit d8cfb62f60507081b1bcdec696cf9aa2ccc71d5b. * Revert "Correct identify() for TradeTag" This reverts commit dbf69664 * Updates as per request on PR * Revert "Add Item in Off Hand for `- take`" This reverts commit 740c34f3 * Uncomplicate the PlayerTag.groups tag * Fix my stupid * Lil Fix * This should fix that * fix the idiocy again, cause idiot. --- .../denizen/events/ScriptEventRegistry.java | 3 + .../events/entity/EntityDeathScriptEvent.java | 5 - .../entity/EntityInteractScriptEvent.java | 4 - .../VillagerAcquiresTradeScriptEvent.java | 87 ++++++++++++++++ .../VillagerChangesProfessionScriptEvent.java | 99 +++++++++++++++++++ .../VillagerReplenishesTradeScriptEvent.java | 97 ++++++++++++++++++ .../denizen/objects/CuboidTag.java | 4 +- .../denizen/objects/PlayerTag.java | 24 ++++- .../bukkit/BukkitElementProperties.java | 4 +- .../commands/item/InventoryCommand.java | 4 +- .../scripts/commands/player/GroupCommand.java | 2 +- .../denizen/tags/core/TextTagBase.java | 4 +- 12 files changed, 314 insertions(+), 23 deletions(-) create mode 100644 plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerAcquiresTradeScriptEvent.java create mode 100644 plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerChangesProfessionScriptEvent.java create mode 100644 plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerReplenishesTradeScriptEvent.java diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java b/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java index 60e0a38300..7ff6336288 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/ScriptEventRegistry.java @@ -88,6 +88,9 @@ public static void registerMainEvents() { ScriptEvent.registerScriptEvent(new SheepDyedScriptEvent()); ScriptEvent.registerScriptEvent(new SheepRegrowsScriptEvent()); ScriptEvent.registerScriptEvent(new SlimeSplitsScriptEvent()); + ScriptEvent.registerScriptEvent(new VillagerAcquiresTradeScriptEvent()); + ScriptEvent.registerScriptEvent(new VillagerChangesProfessionScriptEvent()); + ScriptEvent.registerScriptEvent(new VillagerReplenishesTradeScriptEvent()); // NPC events if (Depends.citizens != null) { diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java index 08daec987f..11ccb00e81 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityDeathScriptEvent.java @@ -93,23 +93,18 @@ public boolean couldMatch(ScriptPath path) { @Override public boolean matches(ScriptPath path) { String target = path.eventArgLowerAt(0); - if (!tryEntity(entity, target)) { return false; } - if (!runInCheck(path, entity.getLocation())) { return false; } - if (path.switches.containsKey("by") && (damager == null || !tryEntity(damager, path.switches.get("by")))) { return false; } - if (!runGenericSwitchCheck(path, "cause", cause == null ? null : cause.asString())) { return false; } - return super.matches(path); } diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityInteractScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityInteractScriptEvent.java index a2d8828086..62710200f2 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityInteractScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/EntityInteractScriptEvent.java @@ -62,19 +62,15 @@ public boolean couldMatch(ScriptPath path) { @Override public boolean matches(ScriptPath path) { - if (!tryEntity(entity, path.eventArgLowerAt(0))) { return false; } - if (!tryMaterial(material, path.eventArgLowerAt(3))) { return false; } - if (!runInCheck(path, location)) { return false; } - return super.matches(path); } diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerAcquiresTradeScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerAcquiresTradeScriptEvent.java new file mode 100644 index 0000000000..795f5361b4 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerAcquiresTradeScriptEvent.java @@ -0,0 +1,87 @@ +package com.denizenscript.denizen.events.entity; + +import com.denizenscript.denizen.events.BukkitScriptEvent; +import com.denizenscript.denizen.objects.EntityTag; +import com.denizenscript.denizen.objects.TradeTag; +import com.denizenscript.denizencore.objects.ObjectTag; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.VillagerAcquireTradeEvent; + +public class VillagerAcquiresTradeScriptEvent extends BukkitScriptEvent implements Listener { + + // <--[event] + // @Events + // villager acquires trade + // + // @Regex ^on villager acquires trade$ + // + // @Group Entity + // + // @Switch in: to only process the event if it occurred within a specified area. + // + // @Cancellable true + // + // @Triggers when a villager acquires a new trade. + // + // @Context + // returns the EntityTag of the villager. + // returns the TradeTag for the new trade. + // + // @Determine + // TradeTag to change the new trade. + // --> + + public VillagerAcquiresTradeScriptEvent() { + instance = this; + } + + public static VillagerAcquiresTradeScriptEvent instance; + public EntityTag entity; + public VillagerAcquireTradeEvent event; + + @Override + public boolean couldMatch(ScriptPath path) { + return path.eventLower.startsWith("villager acquires trade"); + } + + @Override + public boolean matches(ScriptPath path) { + if (!runInCheck(path, entity.getLocation())) { + return false; + } + return super.matches(path); + } + + @Override + public String getName() { + return "VillagerAcquiresTrade"; + } + + + @Override + public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) { + if (TradeTag.matches(determinationObj.toString())) { + event.setRecipe(determinationObj.asType(TradeTag.class, getTagContext(path)).getRecipe()); + return true; + } + return super.applyDetermination(path, determinationObj); + } + + @Override + public ObjectTag getContext(String name) { + if (name.equals("entity")) { + return entity; + } + else if (name.equals("trade")) { + return new TradeTag(event.getRecipe()); + } + return super.getContext(name); + } + + @EventHandler + public void onVillagerAcquiresTrade(VillagerAcquireTradeEvent event) { + this.event = event; + fire(event); + } +} diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerChangesProfessionScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerChangesProfessionScriptEvent.java new file mode 100644 index 0000000000..a8a723ad18 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerChangesProfessionScriptEvent.java @@ -0,0 +1,99 @@ +package com.denizenscript.denizen.events.entity; + +import com.denizenscript.denizen.events.BukkitScriptEvent; +import com.denizenscript.denizen.objects.EntityTag; +import com.denizenscript.denizen.utilities.debugging.Debug; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.ElementTag; +import org.bukkit.entity.Villager; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.VillagerCareerChangeEvent; + +public class VillagerChangesProfessionScriptEvent extends BukkitScriptEvent implements Listener { + + // <--[event] + // @Events + // villager changes profession + // + // @Regex ^on villager changes profession$ + // + // @Group Entity + // + // @Switch in: to only process the event if it occurred within a specified area. + // + // @Cancellable true + // + // @Triggers when a villager changes profession. + // + // @Context + // returns the EntityTag of the villager. + // returns the name of the new profession. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Villager.Profession.html> + // returns the reason for the change. <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/VillagerCareerChangeEvent.ChangeReason.html> + // + // @Determine + // ElementTag to change the profession. + // --> + + public VillagerChangesProfessionScriptEvent() { + instance = this; + } + + public static VillagerChangesProfessionScriptEvent instance; + public EntityTag entity; + public VillagerCareerChangeEvent event; + + @Override + public boolean couldMatch(ScriptPath path) { + return path.eventLower.startsWith("villager changes profession"); + } + + @Override + public boolean matches(ScriptPath path) { + if (!runInCheck(path, entity.getLocation())) { + return false; + } + return super.matches(path); + } + + @Override + public String getName() { + return "VillagerChangesProfession"; + } + + + @Override + public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) { + String determination = determinationObj.toString(); + try { + Villager.Profession newProfession = Villager.Profession.valueOf(determination.toUpperCase()); + event.setProfession(newProfession); + return true; + } + catch (IllegalArgumentException e) { + Debug.echoError("Invalid profession specified: " + determination); + } + return super.applyDetermination(path, determinationObj); + } + + @Override + public ObjectTag getContext(String name) { + if (name.equals("entity")) { + return entity; + } + else if (name.equals("reason")) { + return new ElementTag(event.getReason().toString()); + } + else if (name.equals("profession")) { + return new ElementTag(event.getProfession().toString()); + } + return super.getContext(name); + } + + @EventHandler + public void onVillagerChangesProfession(VillagerCareerChangeEvent event) { + this.event = event; + this.entity = new EntityTag(event.getEntity()); + fire(event); + } +} diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerReplenishesTradeScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerReplenishesTradeScriptEvent.java new file mode 100644 index 0000000000..b0273c3a09 --- /dev/null +++ b/plugin/src/main/java/com/denizenscript/denizen/events/entity/VillagerReplenishesTradeScriptEvent.java @@ -0,0 +1,97 @@ +package com.denizenscript.denizen.events.entity; + +import com.denizenscript.denizen.events.BukkitScriptEvent; +import com.denizenscript.denizen.objects.EntityTag; +import com.denizenscript.denizen.objects.TradeTag; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.ElementTag; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.VillagerReplenishTradeEvent; + +public class VillagerReplenishesTradeScriptEvent extends BukkitScriptEvent implements Listener { + + // <--[event] + // @Events + // villager replenishes trade + // + // @Regex ^on villager replenishes trade$ + // + // @Group Entity + // + // @Switch in: to only process the event if it occurred within a specified area. + // + // @Cancellable true + // + // @Triggers when a villager replenishes a trade. + // + // @Context + // returns the EntityTag of the villager. + // returns the TradeTag for the trade being replenished. + // returns the number of bonus uses added. + // + // @Determine + // TradeTag to change the trade being replenished. + // ElementTag(Number) to change the number of bonus uses added. + // --> + + public VillagerReplenishesTradeScriptEvent() { + instance = this; + } + + public static VillagerReplenishesTradeScriptEvent instance; + public EntityTag entity; + public VillagerReplenishTradeEvent event; + + @Override + public boolean couldMatch(ScriptPath path) { + return path.eventLower.startsWith("villager replenishes trade"); + } + + @Override + public boolean matches(ScriptPath path) { + if (!runInCheck(path, entity.getLocation())) { + return false; + } + return super.matches(path); + } + + @Override + public String getName() { + return "VillagerReplenishesTrade"; + } + + + @Override + public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) { + if (TradeTag.matches(determinationObj.toString())) { + event.setRecipe(determinationObj.asType(TradeTag.class, getTagContext(path)).getRecipe()); + return true; + } + else if (determinationObj instanceof ElementTag && ((ElementTag) determinationObj).isInt()) { + event.setBonus(((ElementTag) determinationObj).asInt()); + return true; + } + return super.applyDetermination(path, determinationObj); + } + + @Override + public ObjectTag getContext(String name) { + if (name.equals("entity")) { + return entity; + } + else if (name.equals("trade")) { + return new TradeTag(event.getRecipe()); + } + else if (name.equals("bonus")) { + return new ElementTag(event.getBonus()); + } + return super.getContext(name); + } + + @EventHandler + public void onVillagerReplenishesTrade(VillagerReplenishTradeEvent event) { + this.event = event; + fire(event); + } +} diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/CuboidTag.java b/plugin/src/main/java/com/denizenscript/denizen/objects/CuboidTag.java index ecda1c3713..1d6e3bafbf 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/CuboidTag.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/CuboidTag.java @@ -766,7 +766,7 @@ public String toString() { public static void registerTags() { // <--[tag] - // @attribute |...]> + // @attribute |...)]> // @returns ListTag(LocationTag) // @description // Returns each block location within the CuboidTag. @@ -792,7 +792,7 @@ public static void registerTags() { }); // <--[tag] - // @attribute |...]> + // @attribute |...)]> // @returns ListTag(LocationTag) // @description // Returns each LocationTag within the CuboidTag that is safe for players or similar entities to spawn in. diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/PlayerTag.java b/plugin/src/main/java/com/denizenscript/denizen/objects/PlayerTag.java index 7f34dbc0c0..0c22c92079 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/PlayerTag.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/PlayerTag.java @@ -1228,7 +1228,7 @@ else if (ban.getExpiration() == null) { }); // <--[tag] - // @attribute + // @attribute )]> // @returns ListTag // @description // Returns a list of all groups the player is in. @@ -1242,9 +1242,16 @@ else if (ban.getExpiration() == null) { return null; } ListTag list = new ListTag(); - // TODO: optionally specify world + WorldTag world = null; + if (attribute.hasContext(1)) { + world = attribute.contextAsType(1, WorldTag.class); + if (world == null) { + Debug.echoError("Invalid world specified: " + attribute.getContext(1)); + return null; + } + } for (String group : Depends.permissions.getGroups()) { - if (Depends.permissions.playerInGroup(null, object.getOfflinePlayer(), group)) { + if (Depends.permissions.playerInGroup(world != null ? world.getName() : null, object.getOfflinePlayer(), group)) { list.add(group); } } @@ -1397,9 +1404,16 @@ else if (attribute.startsWith("source", 2)) { // Permission in certain world else if (attribute.startsWith("world", 2)) { - String world = attribute.getContext(2); + WorldTag world = null; + if (attribute.hasContext(2)) { + world = attribute.contextAsType(2, WorldTag.class); + if (world == null) { + Debug.echoError("Invalid world specified: " + attribute.getContext(2)); + return null; + } + } attribute.fulfill(1); - return new ElementTag(Depends.permissions.playerInGroup(world, object.getOfflinePlayer(), group)); + return new ElementTag(Depends.permissions.playerInGroup(world != null ? world.getName() : null, object.getOfflinePlayer(), group)); } // Permission in current world diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java index 272113a68d..3af2186182 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java @@ -391,7 +391,7 @@ public static void registerTags() { // @group text manipulation // @description // Adds a hover message to the element, which makes the element display the input hover text when the mouse is left over it. - // Optionally specify the hover type as one of: SHOW_TEXT, SHOW_ACHIEVEMENT, SHOW_ITEM, or SHOW_ENTITY. + // Available hover types: SHOW_TEXT, SHOW_ACHIEVEMENT, SHOW_ITEM, or SHOW_ENTITY. // Note: for "SHOW_ITEM", replace the text with a valid ItemTag. For "SHOW_ENTITY", replace the text with a valid spawned EntityTag (requires F3+H to see entities). // Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>. // --> @@ -427,7 +427,7 @@ public static void registerTags() { // @group text manipulation // @description // Adds a click command to the element, which makes the element execute the input command when clicked. - // Optionally specify the hover type as one of: OPEN_URL, OPEN_FILE, RUN_COMMAND, SUGGEST_COMMAND, or CHANGE_PAGE. + // Available command types: OPEN_URL, OPEN_FILE, RUN_COMMAND, SUGGEST_COMMAND, or CHANGE_PAGE. // For example: - narrate "You can to learn about Denizen!" // Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>. // --> diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java index 18515b09a1..309d69ee76 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java @@ -144,7 +144,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException isAdjust = arg.toString().equalsIgnoreCase("adjust"); } - // Check for an origin, which can be a InventoryTag, EntityTag, LocationTag + // Check for an origin, which can be an InventoryTag, EntityTag, LocationTag // or a ListTag of ItemTags else if (!scriptEntry.hasObject("origin") && arg.matchesPrefix("origin", "o", "source", "items", "item", "i", "from", "f") @@ -153,7 +153,7 @@ else if (!scriptEntry.hasObject("origin") scriptEntry.addObject("origin", Conversion.getInventory(arg, scriptEntry)); } - // Check for a destination, which can be a InventoryTag, EntityTag + // Check for a destination, which can be an InventoryTag, EntityTag // or LocationTag else if (!scriptEntry.hasObject("destination") && arg.matchesPrefix("destination", "dest", "d", "target", "to", "t") diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/GroupCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/GroupCommand.java index afb7266deb..bd612124af 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/GroupCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/GroupCommand.java @@ -41,7 +41,7 @@ public GroupCommand() { // ]> // ].global> // ].world> - // + // )]> // // // @Usage diff --git a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java index 984f130957..dc746517e2 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java +++ b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java @@ -362,7 +362,7 @@ public void run(ReplaceableTagEvent event) { // @description // Returns a special chat code that makes the following text display the input hover text when the mouse is left over it. // This tag must be followed by an <&end_hover> tag. - // Optionally specify the hover type as one of: SHOW_TEXT, SHOW_ACHIEVEMENT, SHOW_ITEM, or SHOW_ENTITY. + // Available hover types: SHOW_TEXT, SHOW_ACHIEVEMENT, SHOW_ITEM, or SHOW_ENTITY. // For example: - narrate "There is a <&hover[you found it!].type[SHOW_TEXT]>secret<&end_hover> in this message!" // Note: for "SHOW_ITEM", replace the text with a valid ItemTag. For "SHOW_ENTITY", replace the text with a valid spawned EntityTag (requires F3+H to see entities). // Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>. @@ -399,7 +399,7 @@ public void run(ReplaceableTagEvent event) { // @description // Returns a special chat code that makes the following text execute the input command when clicked. // This tag must be followed by an <&end_click> tag. - // Optionally specify the hover type as one of: OPEN_URL, OPEN_FILE, RUN_COMMAND, SUGGEST_COMMAND, or CHANGE_PAGE. + // Available command types: OPEN_URL, OPEN_FILE, RUN_COMMAND, SUGGEST_COMMAND, or CHANGE_PAGE. // For example: - narrate "You can <&click[https://denizenscript.com].type[OPEN_URL]>click here<&end_click> to learn about Denizen!" // Note that this is a magic Denizen tool - refer to <@link language Denizen Text Formatting>. // -->