Skip to content

Commit

Permalink
Jobs integration cleanup & fixes (#414)
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
tal5 committed Jul 30, 2023
1 parent 5019cf4 commit 2fc7df4
Show file tree
Hide file tree
Showing 10 changed files with 308 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 <jobs>
Expand All @@ -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]
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
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 {

public JobsCommand() {
setName("jobs");
setSyntax("jobs [promote/demote/join/quit] [<job>] (<#>)");
setRequiredArguments(2, 3);
autoCompile();
}

// <--[command]
Expand All @@ -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
// <PlayerTag.job[<job>]>
Expand All @@ -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());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
// <context.job> Returns the job that the player is gaining exp for.
// <context.experience> Returns the amount of exp the player will earn.
// <context.action> 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>.
// <context.job> Returns a JobsJobTag of the job that the player is gaining exp for.
// <context.experience> Returns an ElementTag(Decimal) of the amount of exp the player will earn.
// <context.action> 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:<ElementTag(Decimal)>" to change the amount of Jobs exp this action should provide.
Expand All @@ -45,15 +43,21 @@ public class JobsJobsExpGainScriptEvent extends BukkitScriptEvent implements Lis
public JobsJobsExpGainScriptEvent() {
registerCouldMatcher("jobs player earns exp for <'job'>");
registerSwitches("action");
this.<JobsJobsExpGainScriptEvent, ElementTag>registerOptionalDetermination("exp", ElementTag.class, (evt, context, determination) -> {
if (determination.isDouble()) {
evt.event.setExp(determination.asDouble());
return true;
}
return false;
});
}

public JobsExpGainEvent event;
public JobsJobTag job;

@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())) {
Expand All @@ -64,36 +68,19 @@ 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
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ 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);
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ 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);
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ 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);
}

@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
Expand Down
Loading

0 comments on commit 2fc7df4

Please sign in to comment.