From 2fc7df45a5a43e7b18d729bd48cf9f4a5d9d1f86 Mon Sep 17 00:00:00 2001 From: Aya <31237389+tal5@users.noreply.github.com> Date: Sun, 30 Jul 2023 01:49:27 +0100 Subject: [PATCH] Jobs integration cleanup & fixes (#414) * Initial cleanup/fix * Better handling for the UUID input * Properly input check the job name * Correct the UUID used in the meta * `JobsPlayerProperties` -> `JobsPlayerExtensions` * Remove `registerPropertyTagHandlers` call * Remove weird check in `valueOf` --- .../depenizen/bukkit/bridges/JobsBridge.java | 39 +-- .../bukkit/commands/jobs/JobsCommand.java | 99 +++---- .../jobs/JobsJobsExpGainScriptEvent.java | 47 ++- .../events/jobs/JobsJobsJoinScriptEvent.java | 13 +- .../events/jobs/JobsJobsLeaveScriptEvent.java | 13 +- .../jobs/JobsJobsLevelUpScriptEvent.java | 13 +- .../jobs/JobsJobsPaymentScriptEvent.java | 61 ++-- .../bukkit/objects/jobs/JobsJobTag.java | 273 ++++++++++-------- .../properties/jobs/JobsPlayerExtensions.java | 36 +++ .../properties/jobs/JobsPlayerProperties.java | 85 ------ 10 files changed, 308 insertions(+), 371 deletions(-) create mode 100644 src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerExtensions.java delete mode 100644 src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerProperties.java diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/bridges/JobsBridge.java b/src/main/java/com/denizenscript/depenizen/bukkit/bridges/JobsBridge.java index d5f821fe0..048e3fef5 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/bridges/JobsBridge.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/bridges/JobsBridge.java @@ -2,24 +2,26 @@ import com.denizenscript.denizencore.DenizenCore; import com.denizenscript.denizencore.events.ScriptEvent; +import com.denizenscript.denizencore.objects.ObjectFetcher; import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.ListTag; +import com.denizenscript.denizencore.tags.TagManager; import com.denizenscript.denizencore.utilities.debugging.SlowWarning; -import com.denizenscript.depenizen.bukkit.events.jobs.*; +import com.denizenscript.denizencore.utilities.debugging.Warning; import com.denizenscript.depenizen.bukkit.Bridge; import com.denizenscript.depenizen.bukkit.commands.jobs.JobsCommand; +import com.denizenscript.depenizen.bukkit.events.jobs.*; import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; +import com.denizenscript.depenizen.bukkit.properties.jobs.JobsPlayerExtensions; import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.container.Job; -import com.denizenscript.denizen.objects.PlayerTag; -import com.denizenscript.denizencore.objects.ObjectFetcher; -import com.denizenscript.denizencore.objects.core.ListTag; -import com.denizenscript.denizencore.objects.properties.PropertyParser; -import com.denizenscript.depenizen.bukkit.properties.jobs.JobsPlayerProperties; -import com.denizenscript.denizencore.tags.TagManager; public class JobsBridge extends Bridge { - public static SlowWarning deprecatedJobsConstructor = new SlowWarning("jobsDeprecatedConstructor", "The 'jobs' constructor from Depenizen/Jobs is deprecated: use 'jobs_job'"); + public static Warning jobsDeprecatedConstructor = new SlowWarning("jobsDeprecatedConstructor", "The 'jobs' constructor from Depenizen/Jobs is deprecated: use 'jobs_job'"); + public static Warning jobsNameShort = new SlowWarning("jobsNameShort", "The tag 'JobsJobTag.name.short' from Depenizen/Jobs is deprecated: use 'JobsJobTag.short_name'"); + public static Warning jobsXpLevel = new SlowWarning("jobsXpLevel", "The tag 'JobsJobTag.xp.level' from Depenizen/Jobs is deprecated: use 'JobsJobTag.level'"); + public static Warning jobsXpMax = new SlowWarning("jobsXpMax", "The tag 'JobsJobTag.xp.max' from Depenizen/Jobs is deprecated: use 'JobsJobTag.max_xp'"); + public static Warning jobsSingleLineDescription = new SlowWarning("jobsSingleLineDescription", "'JobsJobTag.description' is deprecated, as single-line descriptions are deprecated in Jobs. Use 'JobsJobTag.full_description' instead."); @Override public void init() { @@ -29,7 +31,8 @@ public void init() { ScriptEvent.registerScriptEvent(JobsJobsLeaveScriptEvent.class); ScriptEvent.registerScriptEvent(JobsJobsLevelUpScriptEvent.class); ObjectFetcher.registerWithObjectFetcher(JobsJobTag.class, JobsJobTag.tagProcessor); - PropertyParser.registerProperty(JobsPlayerProperties.class, PlayerTag.class); + DenizenCore.commandRegistry.registerCommand(JobsCommand.class); + JobsPlayerExtensions.register(); // <--[tag] // @attribute @@ -40,14 +43,10 @@ public void init() { // --> TagManager.registerTagHandler(ObjectTag.class, "jobs", (attribute) -> { if (attribute.hasParam()) { - deprecatedJobsConstructor.warn(attribute.context); + jobsDeprecatedConstructor.warn(attribute.context); return JobsJobTag.valueOf(attribute.getParam(), attribute.context); } - ListTag jobsList = new ListTag(); - for (Job job : Jobs.getJobs()) { - jobsList.addObject(new JobsJobTag(job)); - } - return jobsList; + return new ListTag(Jobs.getJobs(), JobsJobTag::new); }); // <--[tag] @@ -57,12 +56,6 @@ public void init() { // @description // Returns the job tag with the given name. // --> - TagManager.registerTagHandler(JobsJobTag.class, "jobs_job", attribute -> { - if (attribute.hasParam()) { - return JobsJobTag.valueOf(attribute.getParam(), attribute.context); - } - return null; - }); - DenizenCore.commandRegistry.registerCommand(JobsCommand.class); + TagManager.registerTagHandler(JobsJobTag.class, JobsJobTag.class, "jobs_job", (attribute, param) -> param); } } diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/commands/jobs/JobsCommand.java b/src/main/java/com/denizenscript/depenizen/bukkit/commands/jobs/JobsCommand.java index a9988dbf8..7d9bc2280 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/commands/jobs/JobsCommand.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/commands/jobs/JobsCommand.java @@ -1,14 +1,20 @@ package com.denizenscript.depenizen.bukkit.commands.jobs; -import com.denizenscript.denizencore.objects.Argument; -import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.container.JobsPlayer; import com.denizenscript.denizen.utilities.Utilities; -import com.denizenscript.denizencore.exceptions.InvalidArgumentsException; +import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException; +import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.scripts.ScriptEntry; import com.denizenscript.denizencore.scripts.commands.AbstractCommand; +import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull; +import com.denizenscript.denizencore.scripts.commands.generator.ArgLinear; +import com.denizenscript.denizencore.scripts.commands.generator.ArgName; +import com.denizenscript.denizencore.utilities.Deprecations; import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.PlayerManager; +import com.gamingmesh.jobs.container.Job; +import com.gamingmesh.jobs.container.JobsPlayer; public class JobsCommand extends AbstractCommand { @@ -16,6 +22,7 @@ public JobsCommand() { setName("jobs"); setSyntax("jobs [promote/demote/join/quit] [] (<#>)"); setRequiredArguments(2, 3); + autoCompile(); } // <--[command] @@ -28,8 +35,8 @@ public JobsCommand() { // @Short Modifies the specified job of a player. // // @Description - // This allows you to promote or demote a player's job level. This also allows you - // to force a player to join or quit a job. + // This allows you to promote or demote a player's job level. + // This also allows you to force a player to join or quit a job. // // @Tags // ]> @@ -49,64 +56,44 @@ public JobsCommand() { // // --> - private enum Action {PROMOTE, DEMOTE, JOIN, QUIT} @Override - public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - - for (Argument arg : scriptEntry) { - if (!scriptEntry.hasObject("action") - && arg.matchesEnum(Action.class)) { - scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase())); - } - else if (!scriptEntry.hasObject("job") - && arg.matchesArgumentType(JobsJobTag.class)) { - scriptEntry.addObject("job", JobsJobTag.valueOf(arg.getValue(), scriptEntry.context)); - } - else if (!scriptEntry.hasObject("number") - && arg.matchesInteger()) { - scriptEntry.addObject("number", new ElementTag(arg.getValue())); - } + public void addCustomTabCompletions(TabCompletionsBuilder tab) { + for (Job job : Jobs.getJobs()) { + tab.add(job.getName()); } + } - if (!scriptEntry.hasObject("action")) { - throw new InvalidArgumentsException("Must specify an action!"); + public enum Action {PROMOTE, DEMOTE, JOIN, QUIT} + + public static void autoExecute(ScriptEntry scriptEntry, + @ArgName("action") Action action, + @ArgLinear @ArgName("job") ObjectTag jobObject, + @ArgLinear @ArgDefaultNull @ArgName("number") ObjectTag numberObject) { + if (!Utilities.entryHasPlayer(scriptEntry)) { + throw new InvalidArgumentsRuntimeException("Missing linked player."); } - if (!scriptEntry.hasObject("job")) { - throw new InvalidArgumentsException("Must specify a job!"); + if (numberObject != null && jobObject.asElement().isInt()) { + Deprecations.outOfOrderArgs.warn(scriptEntry); + ObjectTag jobObjectSwitch = jobObject; + jobObject = numberObject; + numberObject = jobObjectSwitch; } - if (!Utilities.entryHasPlayer(scriptEntry)) { - throw new InvalidArgumentsException("Must have a player attached to the queue."); + ElementTag numberElement = numberObject != null ? numberObject.asElement() : new ElementTag(0); + if (!numberElement.isInt()) { + throw new InvalidArgumentsRuntimeException("Invalid number '" + numberElement + "' specified: must be a valid non-decimal number."); } - - } - - @Override - public void execute(ScriptEntry scriptEntry) { - - Action action = (Action) scriptEntry.getObject("action"); - JobsJobTag job = scriptEntry.getObjectTag("job"); - int number = (scriptEntry.hasObject("number") ? scriptEntry.getElement("number").asInt() : 0); - JobsPlayer player = Jobs.getPlayerManager().getJobsPlayer(Utilities.getEntryPlayer(scriptEntry).getName()); - + JobsJobTag job = jobObject.asType(JobsJobTag.class, scriptEntry.context); + if (job == null) { + throw new InvalidArgumentsRuntimeException("Invalid JobsJobTag specified: " + jobObject + '.'); + } + PlayerManager playerManager = Jobs.getPlayerManager(); + JobsPlayer player = playerManager.getJobsPlayer(Utilities.getEntryPlayer(scriptEntry).getUUID()); switch (action) { - - case PROMOTE: - player.promoteJob(job.getJob(), number); - break; - - case DEMOTE: - player.demoteJob(job.getJob(), number); - break; - - case JOIN: - player.joinJob(job.getJob()); - break; - - case QUIT: - player.leaveJob(job.getJob()); - break; - + case PROMOTE -> playerManager.promoteJob(player, job.getJob(), numberElement.asInt()); + case DEMOTE -> playerManager.demoteJob(player, job.getJob(), numberElement.asInt()); + case JOIN -> playerManager.joinJob(player, job.getJob()); + case QUIT -> playerManager.leaveJob(player, job.getJob()); } } diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsExpGainScriptEvent.java b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsExpGainScriptEvent.java index 5ebbdbd61..02f68b9e9 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsExpGainScriptEvent.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsExpGainScriptEvent.java @@ -3,11 +3,9 @@ import com.denizenscript.denizen.events.BukkitScriptEvent; import com.denizenscript.denizen.objects.PlayerTag; import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; -import com.denizenscript.denizencore.events.ScriptEvent; import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.scripts.ScriptEntryData; -import com.denizenscript.denizencore.utilities.CoreUtilities; import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.api.JobsExpGainEvent; @@ -27,9 +25,9 @@ public class JobsJobsExpGainScriptEvent extends BukkitScriptEvent implements Lis // @Triggers when a player performs an action that would cause them to earn Jobs exp for a certain job. // // @Context - // Returns the job that the player is gaining exp for. - // Returns the amount of exp the player will earn. - // Returns the name of the action being paid for, which can be any of the strings from: <@link url https://github.com/Zrips/Jobs/blob/master/src/main/java/com/gamingmesh/jobs/container/ActionType.java>. + // Returns a JobsJobTag of the job that the player is gaining exp for. + // Returns an ElementTag(Decimal) of the amount of exp the player will earn. + // Returns an ElementTag of the name of the action being paid for, which can be any of the strings from: <@link url https://github.com/Zrips/Jobs/blob/master/src/main/java/com/gamingmesh/jobs/container/ActionType.java>. // // @Determine // "EXP:" to change the amount of Jobs exp this action should provide. @@ -45,6 +43,13 @@ public class JobsJobsExpGainScriptEvent extends BukkitScriptEvent implements Lis public JobsJobsExpGainScriptEvent() { registerCouldMatcher("jobs player earns exp for <'job'>"); registerSwitches("action"); + this.registerOptionalDetermination("exp", ElementTag.class, (evt, context, determination) -> { + if (determination.isDouble()) { + evt.event.setExp(determination.asDouble()); + return true; + } + return false; + }); } public JobsExpGainEvent event; @@ -52,8 +57,7 @@ public JobsJobsExpGainScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!path.eventArgLowerAt(5).equals("job") - && !runGenericCheck(path.eventArgAt(5), job.getJob().getName())) { + if (!path.tryArgObject(5, job)) { return false; } if (!runGenericSwitchCheck(path, "action", event.getActionInfo().getType().getName())) { @@ -64,16 +68,12 @@ public boolean matches(ScriptPath path) { @Override public ObjectTag getContext(String name) { - switch (name) { - case "job": - return job; - case "experience": - return new ElementTag(event.getExp()); - case "action": - return new ElementTag(event.getActionInfo().getType().getName()); - default: - return super.getContext(name); - } + return switch (name) { + case "job" -> job; + case "experience" -> new ElementTag(event.getExp()); + case "action" -> new ElementTag(event.getActionInfo().getType().getName(), true); + default -> super.getContext(name); + }; } @Override @@ -81,19 +81,6 @@ public ScriptEntryData getScriptEntryData() { return new BukkitScriptEntryData(new PlayerTag(event.getPlayer()), null); } - @Override - public boolean applyDetermination(ScriptEvent.ScriptPath path, ObjectTag determinationObj) { - if (determinationObj instanceof ElementTag) { - String determination = determinationObj.toString(); - String lower = CoreUtilities.toLowerCase(determination); - if (lower.startsWith("exp:")) { - event.setExp(Double.parseDouble(determination.substring("exp:".length()))); - return true; - } - } - return super.applyDetermination(path, determinationObj); - } - @EventHandler public void onJobsExpGain(JobsExpGainEvent event) { job = new JobsJobTag(event.getJob(), Jobs.getPlayerManager().getJobsPlayer(event.getPlayer().getUniqueId())); diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsJoinScriptEvent.java b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsJoinScriptEvent.java index 4064adf8d..1fc7ac021 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsJoinScriptEvent.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsJoinScriptEvent.java @@ -40,8 +40,7 @@ public JobsJobsJoinScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!path.eventArgLowerAt(3).equals("job") - && !runGenericCheck(path.eventArgAt(3), job.getJob().getName())) { + if (!path.tryArgObject(3, job)) { return false; } return super.matches(path); @@ -49,12 +48,10 @@ public boolean matches(ScriptPath path) { @Override public ObjectTag getContext(String name) { - switch (name) { - case "job": - return job; - default: - return super.getContext(name); - } + return switch (name) { + case "job" -> job; + default -> super.getContext(name); + }; } @Override diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLeaveScriptEvent.java b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLeaveScriptEvent.java index 090e374c9..6449ccb80 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLeaveScriptEvent.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLeaveScriptEvent.java @@ -40,8 +40,7 @@ public JobsJobsLeaveScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!path.eventArgLowerAt(3).equals("job") - && !runGenericCheck(path.eventArgAt(3), job.getJob().getName())) { + if (!path.tryArgObject(3, job)) { return false; } return super.matches(path); @@ -49,12 +48,10 @@ public boolean matches(ScriptPath path) { @Override public ObjectTag getContext(String name) { - switch (name) { - case "job": - return job; - default: - return super.getContext(name); - } + return switch (name) { + case "job" -> job; + default -> super.getContext(name); + }; } @Override diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLevelUpScriptEvent.java b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLevelUpScriptEvent.java index 754d2ef08..14810cb3d 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLevelUpScriptEvent.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsLevelUpScriptEvent.java @@ -40,8 +40,7 @@ public JobsJobsLevelUpScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!path.eventArgLowerAt(4).equals("job") - && !runGenericCheck(path.eventArgAt(4), job.getJob().getName())) { + if (!path.tryArgObject(4, job)) { return false; } return super.matches(path); @@ -49,12 +48,10 @@ public boolean matches(ScriptPath path) { @Override public ObjectTag getContext(String name) { - switch (name) { - case "job": - return job; - default: - return super.getContext(name); - } + return switch (name) { + case "job" -> job; + default -> super.getContext(name); + }; } @Override diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsPaymentScriptEvent.java b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsPaymentScriptEvent.java index 43802d2fb..e414b39ea 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsPaymentScriptEvent.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/events/jobs/JobsJobsPaymentScriptEvent.java @@ -6,7 +6,6 @@ import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.scripts.ScriptEntryData; -import com.denizenscript.denizencore.utilities.CoreUtilities; import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.api.JobsPrePaymentEvent; @@ -26,10 +25,10 @@ public class JobsJobsPaymentScriptEvent extends BukkitScriptEvent implements Lis // @Triggers when a player performs an action that would cause them to be paid for a certain job. // // @Context - // Returns the job that the player is being paid for. - // Returns the amount of money the player will be paid. - // Returns the amount of points the player will be paid. - // Returns the name of the action being paid for, which can be any of the strings from: <@link url https://github.com/Zrips/Jobs/blob/master/src/main/java/com/gamingmesh/jobs/container/ActionType.java>. + // Returns an JobsJobTag of the job that the player is being paid for. + // Returns an ElementTag(Decimal) of the amount of money the player will be paid. + // Returns an ElementTag(Decimal) of the amount of points the player will be paid. + // Returns an ElementTag the name of the action being paid for, which can be any of the strings from: <@link url https://github.com/Zrips/Jobs/blob/master/src/main/java/com/gamingmesh/jobs/container/ActionType.java>. // // @Determine // "MONEY:" to change the amount of money this action should provide. @@ -46,6 +45,20 @@ public class JobsJobsPaymentScriptEvent extends BukkitScriptEvent implements Lis public JobsJobsPaymentScriptEvent() { registerCouldMatcher("jobs player earns money for <'job'>"); registerSwitches("action"); + this.registerOptionalDetermination("money", ElementTag.class, (evt, context, input) -> { + if (input.isDouble()) { + evt.event.setAmount(input.asDouble()); + return true; + } + return false; + }); + this.registerOptionalDetermination("points", ElementTag.class, (evt, context, input) -> { + if (input.isDouble()) { + evt.event.setPoints(input.asDouble()); + return true; + } + return false; + }); } public JobsPrePaymentEvent event; @@ -53,8 +66,7 @@ public JobsJobsPaymentScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!path.eventArgLowerAt(5).equals("job") - && !runGenericCheck(path.eventArgAt(4), job.getJob().getName())) { + if (!path.tryArgObject(5, job)) { return false; } if (!runGenericSwitchCheck(path, "action", event.getActionInfo().getType().getName())) { @@ -65,18 +77,13 @@ public boolean matches(ScriptPath path) { @Override public ObjectTag getContext(String name) { - switch (name) { - case "job": - return job; - case "money": - return new ElementTag(event.getAmount()); - case "points": - return new ElementTag(event.getPoints()); - case "action": - return new ElementTag(event.getActionInfo().getType().getName()); - default: - return super.getContext(name); - } + return switch (name) { + case "job" -> job; + case "money" -> new ElementTag(event.getAmount()); + case "points" -> new ElementTag(event.getPoints()); + case "action" -> new ElementTag(event.getActionInfo().getType().getName(), true); + default -> super.getContext(name); + }; } @Override @@ -84,22 +91,6 @@ public ScriptEntryData getScriptEntryData() { return new BukkitScriptEntryData(new PlayerTag(event.getPlayer()), null); } - @Override - public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) { - if (determinationObj instanceof ElementTag) { - String determination = determinationObj.toString(); - String lower = CoreUtilities.toLowerCase(determination); - if (lower.startsWith("money:")) { - event.setAmount(Double.parseDouble(determination.substring("money:".length()))); - return true; - } else if (lower.startsWith("points:")) { - event.setPoints(Double.parseDouble(determination.substring("points:".length()))); - return true; - } - } - return super.applyDetermination(path, determinationObj); - } - @EventHandler public void onJobsJobsPrepayment(JobsPrePaymentEvent event) { job = new JobsJobTag(event.getJob(), Jobs.getPlayerManager().getJobsPlayer(event.getPlayer().getUniqueId())); diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/objects/jobs/JobsJobTag.java b/src/main/java/com/denizenscript/depenizen/bukkit/objects/jobs/JobsJobTag.java index ca96c9b9d..67592f3c7 100644 --- a/src/main/java/com/denizenscript/depenizen/bukkit/objects/jobs/JobsJobTag.java +++ b/src/main/java/com/denizenscript/depenizen/bukkit/objects/jobs/JobsJobTag.java @@ -1,23 +1,28 @@ package com.denizenscript.depenizen.bukkit.objects.jobs; -import com.denizenscript.denizencore.objects.*; +import com.denizenscript.denizen.objects.PlayerTag; +import com.denizenscript.denizencore.events.ScriptEvent; +import com.denizenscript.denizencore.objects.Adjustable; +import com.denizenscript.denizencore.objects.Fetchable; +import com.denizenscript.denizencore.objects.Mechanism; +import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.objects.core.ListTag; +import com.denizenscript.denizencore.objects.properties.PropertyParser; +import com.denizenscript.denizencore.tags.Attribute; import com.denizenscript.denizencore.tags.ObjectTagProcessor; +import com.denizenscript.denizencore.tags.TagContext; import com.denizenscript.denizencore.utilities.CoreUtilities; import com.denizenscript.denizencore.utilities.debugging.Debug; -import com.denizenscript.denizencore.utilities.debugging.SlowWarning; +import com.denizenscript.depenizen.bukkit.bridges.JobsBridge; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.container.Job; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; -import com.denizenscript.denizen.objects.PlayerTag; -import com.denizenscript.denizencore.objects.core.ElementTag; -import com.denizenscript.denizencore.objects.properties.PropertyParser; -import com.denizenscript.denizencore.tags.Attribute; -import com.denizenscript.denizencore.tags.TagContext; + +import java.util.UUID; public class JobsJobTag implements ObjectTag, Adjustable { - public static ObjectTagProcessor tagProcessor = new ObjectTagProcessor<>(); // <--[ObjectType] // @name JobsJobTag @@ -25,13 +30,17 @@ public class JobsJobTag implements ObjectTag, Adjustable { // @base ElementTag // @format // The identity format for jobs is the player UUID (optional), followed by the job name - // For example: job@05f57b6e-77ba-4546-b214-b58dacc30356,job_name + // For example: job@460e96b9-7a0e-416d-b2c3-4508164b8b1b,job_name // Or: job@job_name // // @plugin Depenizen, Jobs // @description // A JobsJobTag represents a Jobs job, with a player's progression if specified. // + // @Matchable + // JobsJobTag matchers, sometimes identified as "": + // "job" plaintext: always matches. + // Job name: matches if the job name matches the input, using advanced matchers. // --> ///////////////////// @@ -43,32 +52,42 @@ public static JobsJobTag valueOf(String string, TagContext context) { if (string.startsWith("job@")) { string = string.substring("job@".length()); } - if (string.contains("@")) { - return null; - } int comma = string.indexOf(','); - PlayerTag player = null; + UUID playerUUID = null; if (comma > 0) { - //player = new PlayerTag(UUID.fromString(string.substring(0, comma))); - player = PlayerTag.valueOf(string.substring(0, comma), context); - string = string.substring(comma + 1); + try { + playerUUID = UUID.fromString(string.substring(0, comma)); + string = string.substring(comma + 1); + } + catch (IllegalArgumentException iae) { + if (context == null || context.showErrors()) { + Debug.echoError("valueOf JobsJobTag returning null: Invalid UUID '" + string.substring(0, comma) + "' specified."); + } + return null; + } + } + Job job = Jobs.getJob(string); + if (job == null) { + if (context == null || context.showErrors()) { + Debug.echoError("valueOf JobsJobTag returning null: Invalid job '" + string + "' specified."); + } + return null; } - JobsJobTag job = new JobsJobTag(Jobs.getJob(string)); - if (player != null) { - if (player.isValid()) { - job.setOwner(player); - } else { + JobsJobTag jobTag = new JobsJobTag(job); + if (playerUUID != null) { + jobTag.setOwner(playerUUID); + if (!jobTag.hasOwner()) { Debug.echoError("Player specified in JobsJobTag is not valid"); } } - return job; + return jobTag; } public static boolean matches(String arg) { - if (valueOf(arg, CoreUtilities.noDebugContext) != null) { + if (arg.startsWith("job@")) { return true; } - return false; + return valueOf(arg, CoreUtilities.noDebugContext) != null; } ///////////////////// @@ -106,15 +125,18 @@ public boolean hasOwner() { return jobOwner != null; } - public PlayerTag getOwner() { + public UUID getOwner() { if (jobOwner == null) { return null; } - return new PlayerTag(jobOwner.playerUUID); + return jobOwner.getUniqueId(); } - public void setOwner(PlayerTag player) { - this.jobOwner = Jobs.getPlayerManager().getJobsPlayer(player.getName()); + public void setOwner(UUID playerUUID) { + this.jobOwner = Jobs.getPlayerManager().getJobsPlayer(playerUUID); + if (jobOwner == null) { + return; + } this.jobProgression = jobOwner.getJobProgression(job); } @@ -122,58 +144,7 @@ public void setOwner(PlayerTag player) { // ObjectTag Methods ///////////////// - private String prefix = "Job"; - - @Override - public String getPrefix() { - return prefix; - } - - @Override - public ObjectTag setPrefix(String prefix) { - this.prefix = prefix; - return this; - } - - @Override - public boolean isUnique() { - return true; - } - - @Override - public String identify() { - if (jobOwner != null) { - return "job@" + jobOwner.playerUUID + "," + job.getName(); - } - return "job@" + job.getName(); - } - - @Override - public String identifySimple() { - return identify(); - } - - @Override - public String toString() { - return identify(); - } - - @Override - public ObjectTag getObjectAttribute(Attribute attribute) { - return tagProcessor.getObjectAttribute(this, attribute); - } - - public static SlowWarning nameShortTag = new SlowWarning("jobsNameShort", "The tag 'JobsJobTag.name.short' from Depenizen/Jobs is deprecated: use 'JobsJobTag.short_name'"); - public static SlowWarning xpLevelTag = new SlowWarning("jobsXpLevel", "The tag 'JobsJobTag.xp.level' from Depenizen/Jobs is deprecated: use 'JobsJobTag.level'"); - public static SlowWarning xpMaxTag = new SlowWarning("jobsXpMax", "The tag 'JobsJobTag.xp.max' from Depenizen/Jobs is deprecated: use 'JobsJobTag.max_xp'"); - - @Override - public void applyProperty(Mechanism mechanism) { - mechanism.echoError("Cannot apply Properties to a Jobs Job!"); - } - public static void register() { - PropertyParser.registerPropertyTagHandlers(JobsJobTag.class, tagProcessor); // <--[tag] // @attribute @@ -184,7 +155,8 @@ public static void register() { // Single line description is deprecated in Jobs. Use <@link tag JobsJobTag.full_description> instead. // --> tagProcessor.registerTag(ElementTag.class, "description", (attribute, object) -> { - return new ElementTag(object.getJob().getDescription()); + JobsBridge.jobsSingleLineDescription.warn(attribute.context); + return new ElementTag(object.getJob().getDescription(), true); }); // <--[tag] @@ -195,7 +167,7 @@ public static void register() { // Returns the full description of the job. // --> tagProcessor.registerTag(ListTag.class, "full_description", (attribute, object) -> { - return new ListTag(object.getJob().getFullDescription()); + return new ListTag(object.getJob().getFullDescription(), true); }); // <--[tag] @@ -216,11 +188,11 @@ public static void register() { // --> tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> { if (attribute.startsWith("short", 2)) { - nameShortTag.warn(attribute.context); + JobsBridge.jobsNameShort.warn(attribute.context); attribute.fulfill(1); - return new ElementTag(object.getJob().getShortName()); + return new ElementTag(object.getJob().getShortName(), true); } - return new ElementTag(object.getJob().getName()); + return new ElementTag(object.getJob().getName(), true); }); // <--[tag] @@ -231,7 +203,7 @@ public static void register() { // Returns the shortened name of the job. // --> tagProcessor.registerTag(ElementTag.class, "short_name", (attribute, object) -> { - return new ElementTag(object.getJob().getShortName()); + return new ElementTag(object.getJob().getShortName(), true); }); // <--[tag] @@ -261,20 +233,20 @@ public static void register() { // Returns the current experience a player has for the current level in a specified job. // --> tagProcessor.registerTag(ElementTag.class, "xp", (attribute, object) -> { - if (object.jobProgression != null) { - if (attribute.startsWith("max", 2)) { - xpMaxTag.warn(attribute.context); - attribute.fulfill(1); - return new ElementTag(object.jobProgression.getMaxExperience()); - } - if (attribute.startsWith("level", 2)) { - xpLevelTag.warn(attribute.context); - attribute.fulfill(1); - return new ElementTag(object.jobProgression.getLevel()); - } - return new ElementTag(object.jobProgression.getExperience()); + if (object.jobProgression == null) { + return null; } - return null; + if (attribute.startsWith("max", 2)) { + JobsBridge.jobsXpMax.warn(attribute.context); + attribute.fulfill(1); + return new ElementTag(object.jobProgression.getMaxExperience()); + } + if (attribute.startsWith("level", 2)) { + JobsBridge.jobsXpLevel.warn(attribute.context); + attribute.fulfill(1); + return new ElementTag(object.jobProgression.getLevel()); + } + return new ElementTag(object.jobProgression.getExperience()); }); // <--[tag] @@ -285,10 +257,7 @@ public static void register() { // Returns the current level a player has in a specified job. // --> tagProcessor.registerTag(ElementTag.class, "level", (attribute, object) -> { - if (object.jobProgression != null) { - return new ElementTag(object.jobProgression.getLevel()); - } - return null; + return object.jobProgression != null ? new ElementTag(object.jobProgression.getLevel()) : null; }); // <--[tag] @@ -300,13 +269,13 @@ public static void register() { // If the level is not specified, uses the current level of the player. // --> tagProcessor.registerTag(ElementTag.class, "max_xp", (attribute, object) -> { - if (object.jobProgression != null) { - if (attribute.hasParam()) { - return new ElementTag(object.jobProgression.getMaxExperience(attribute.getIntParam())); - } - return new ElementTag(object.jobProgression.getMaxExperience()); + if (object.jobProgression == null) { + return null; } - return null; + if (attribute.hasParam()) { + return new ElementTag(object.jobProgression.getMaxExperience(attribute.getIntParam())); + } + return new ElementTag(object.jobProgression.getMaxExperience()); }); // <--[tag] @@ -317,12 +286,9 @@ public static void register() { // Returns the player the job progression for this tag belongs to. // --> tagProcessor.registerTag(PlayerTag.class, "player", (attribute, object) -> { - return object.getOwner(); + UUID ownerUUID = object.getOwner(); + return ownerUUID != null ? new PlayerTag(ownerUUID) : null; }); - } - - @Override - public void adjust(Mechanism mechanism) { // <--[mechanism] // @object JobsJobTag @@ -334,15 +300,86 @@ public void adjust(Mechanism mechanism) { // @tags // // --> - if (!mechanism.isProperty && mechanism.matches("xp") && mechanism.requireDouble()) { - if (jobProgression == null) { + tagProcessor.registerMechanism("xp", false, ElementTag.class, (object, mechanism, input) -> { + if (!mechanism.requireDouble()) { + return; + } + if (object.jobProgression == null) { mechanism.echoError("This mechanism requires the object to be linked to a player."); return; } - jobProgression.setExperience(mechanism.getValue().asDouble()); - } + object.jobProgression.setExperience(input.asDouble()); + }); + } + public static final ObjectTagProcessor tagProcessor = new ObjectTagProcessor<>(); + + @Override + public ObjectTag getObjectAttribute(Attribute attribute) { + return tagProcessor.getObjectAttribute(this, attribute); + } + + @Override + public void adjust(Mechanism mechanism) { tagProcessor.processMechanism(this, mechanism); - CoreUtilities.autoPropertyMechanism(this, mechanism); + } + + @Override + public void applyProperty(Mechanism mechanism) { + mechanism.echoError("Cannot apply Properties to a Jobs Job!"); + } + + @Override + public String identify() { + return identify("job@", ","); + } + + @Override + public String debuggable() { + return identify("job@", ","); + } + + public String identify(String prefix, String separator) { + if (jobOwner != null) { + return prefix + jobOwner.getUniqueId() + separator + job.getName(); + } + return prefix + job.getName(); + } + + @Override + public String identifySimple() { + return identify(); + } + + @Override + public String toString() { + return identify(); + } + + @Override + public boolean advancedMatches(String matcher) { + String lowerMatcher = CoreUtilities.toLowerCase(matcher); + if (lowerMatcher.equals("job")) { + return true; + } + return ScriptEvent.createMatcher(lowerMatcher).doesMatch(getJob().getName()); + } + + @Override + public boolean isUnique() { + return true; + } + + private String prefix = "Job"; + + @Override + public String getPrefix() { + return prefix; + } + + @Override + public ObjectTag setPrefix(String prefix) { + this.prefix = prefix; + return this; } } diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerExtensions.java b/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerExtensions.java new file mode 100644 index 000000000..962cf03f8 --- /dev/null +++ b/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerExtensions.java @@ -0,0 +1,36 @@ +package com.denizenscript.depenizen.bukkit.properties.jobs; + +import com.denizenscript.denizen.objects.PlayerTag; +import com.denizenscript.denizencore.objects.core.ListTag; +import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.JobsPlayer; + +public class JobsPlayerExtensions { + + public static void register() { + + // <--[tag] + // @attribute ]> + // @returns JobsJobTag + // @plugin Depenizen, Jobs + // @description + // Returns the job specified with the player's information attached. + // --> + PlayerTag.tagProcessor.registerTag(JobsJobTag.class, JobsJobTag.class, "job", (attribute, object, job) -> { + return new JobsJobTag(job.getJob(), Jobs.getPlayerManager().getJobsPlayer(object.getUUID())); + }); + + // <--[tag] + // @attribute + // @returns ListTag(JobsJobTag) + // @plugin Depenizen, Jobs + // @description + // Returns a list of all jobs that the player is in. + // --> + PlayerTag.tagProcessor.registerTag(ListTag.class, "current_jobs", (attribute, object) -> { + JobsPlayer jobsPlayer = Jobs.getPlayerManager().getJobsPlayer(object.getUUID()); + return new ListTag(jobsPlayer.getJobProgression(), jobProgression -> new JobsJobTag(jobProgression.getJob(), jobsPlayer)); + }); + } +} diff --git a/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerProperties.java b/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerProperties.java deleted file mode 100644 index 93b37ca90..000000000 --- a/src/main/java/com/denizenscript/depenizen/bukkit/properties/jobs/JobsPlayerProperties.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.denizenscript.depenizen.bukkit.properties.jobs; - -import com.denizenscript.denizencore.objects.core.ListTag; -import com.denizenscript.denizencore.objects.properties.Property; -import com.denizenscript.denizencore.objects.Mechanism; -import com.denizenscript.denizencore.objects.properties.PropertyParser; -import com.denizenscript.depenizen.bukkit.objects.jobs.JobsJobTag; -import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.container.JobProgression; -import com.gamingmesh.jobs.container.JobsPlayer; -import com.denizenscript.denizen.objects.PlayerTag; -import com.denizenscript.denizencore.objects.ObjectTag; - -public class JobsPlayerProperties implements Property { - - @Override - public String getPropertyString() { - return null; - } - - @Override - public String getPropertyId() { - return "JobsPlayer"; - } - - @Override - public void adjust(Mechanism mechanism) { - // None - } - - public static boolean describes(ObjectTag object) { - return object instanceof PlayerTag; - } - - public static JobsPlayerProperties getFrom(ObjectTag object) { - if (!describes(object)) { - return null; - } - else { - return new JobsPlayerProperties((PlayerTag) object); - } - } - - public static final String[] handledTags = new String[] { - "job", "current_jobs" - }; - - public static final String[] handledMechs = new String[] { - }; // None - - public JobsPlayerProperties(PlayerTag player) { - this.player = Jobs.getPlayerManager().getJobsPlayer(player.getName()); - } - - JobsPlayer player; - - public static void register() { - - // <--[tag] - // @attribute ]> - // @returns JobsJobTag - // @plugin Depenizen, Jobs - // @description - // Returns the job specified with the player's information attached. - // --> - PropertyParser.registerTag(JobsPlayerProperties.class, JobsJobTag.class, JobsJobTag.class, "job", (attribute, object, job) -> { - return new JobsJobTag(job.getJob(), object.player); - }); - - // <--[tag] - // @attribute - // @returns ListTag(JobsJobTag) - // @plugin Depenizen, Jobs - // @description - // Returns a list of all jobs that the player is in. - // --> - PropertyParser.registerTag(JobsPlayerProperties.class, ListTag.class, "current_jobs", (attribute, object) -> { - ListTag response = new ListTag(); - for (JobProgression progress : object.player.getJobProgression()) { - response.addObject(new JobsJobTag(progress.getJob(), object.player)); - } - return response; - }); - } -}