Skip to content

Commit

Permalink
zap command: use NPC link to make better script guesses
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 25, 2021
1 parent d50a331 commit 4eda734
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
Expand Up @@ -239,15 +239,16 @@ public String getName() {
return getCitizen().getName();
}

public InteractScriptContainer getInteractScript() {
return InteractScriptHelper.getInteractScript(this);
}

public InteractScriptContainer getInteractScript(PlayerTag player, Class<? extends AbstractTrigger> triggerType) {
return InteractScriptHelper.getInteractScript(this, player, triggerType);
return InteractScriptHelper.getInteractScript(this, player, true, triggerType);
}

public InteractScriptContainer getInteractScriptQuietly(PlayerTag player, Class<? extends AbstractTrigger> triggerType) {
InteractScriptHelper.debugGet = false;
InteractScriptContainer script = InteractScriptHelper.getInteractScript(this, player, triggerType);
InteractScriptHelper.debugGet = true;
return script;
return InteractScriptHelper.getInteractScript(this, player, true, triggerType);
}

public void destroy() {
Expand Down
@@ -1,5 +1,7 @@
package com.denizenscript.denizen.scripts.commands.core;

import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer;
import com.denizenscript.denizen.scripts.containers.core.InteractScriptHelper;
import com.denizenscript.denizen.utilities.Utilities;
Expand All @@ -12,6 +14,7 @@
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.Listener;

public class ZapCommand extends AbstractCommand implements Listener {
Expand Down Expand Up @@ -39,6 +42,7 @@ public ZapCommand() {
// The step name can be '*' to automatically zap to the default step.
//
// If used inside an interact script, will default to the current interact script.
// If used elsewhere, but there is a linked NPC with an assignment and interact, that NPC's interact script will be used.
// For anywhere else, you must specify the script by name.
//
// Optionally specify a duration. When the duration is up, the script will zap back to the step it was previously on.
Expand Down Expand Up @@ -79,7 +83,11 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(ScriptTag.class)
&& !arg.matchesPrefix("step")) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
ScriptTag script = arg.asType(ScriptTag.class);
if (!CoreUtilities.toLowerCase(script.getType()).equals("interact")) {
throw new InvalidArgumentsException("Script specified must be an 'interact' script!");
}
scriptEntry.addObject("script", script);
}
else if (!scriptEntry.hasObject("step")) {
scriptEntry.addObject("step", arg.asElement());
Expand All @@ -92,10 +100,27 @@ else if (!scriptEntry.hasObject("duration")
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("script", scriptEntry.getScript());
if (!Utilities.entryHasPlayer(scriptEntry) || !Utilities.getEntryPlayer(scriptEntry).isValid()) {
PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
if (player == null || !player.isValid()) {
throw new InvalidArgumentsException("Must have player context!");
}
if (!scriptEntry.hasObject("script")) {
ScriptTag script = scriptEntry.getScript();
if (script == null || !CoreUtilities.toLowerCase(script.getType()).equals("interact")) {
script = null;
NPCTag npc = Utilities.getEntryNPC(scriptEntry);
if (npc != null) {
InteractScriptContainer scriptContainer = npc.getInteractScript();
if (scriptContainer != null) {
script = new ScriptTag(scriptContainer);
}
}
}
if (script == null) {
throw new InvalidArgumentsException("No script to zap! Must be in an interact script, or have a linked NPC with an associated interact script.");
}
scriptEntry.addObject("script", script);
}
}

@Override
Expand Down
Expand Up @@ -15,19 +15,8 @@

public class InteractScriptHelper {

public static boolean debugGet = true;

/**
* Gets the InteractScript from an NPC and returns the appropriate ScriptContainer.
* Returns null if no script found.
*
* @param npc the NPC involved
* @param player the Player involved
* @param trigger the class of the trigger being used
* @return the highest priority InteractScriptContainer that meets requirements, if any.
*/
public static InteractScriptContainer getInteractScript(NPCTag npc, PlayerTag player, Class<? extends AbstractTrigger> trigger) {
if (npc == null || player == null || trigger == null) {
public static InteractScriptContainer getInteractScript(NPCTag npc) {
if (npc == null) {
return null;
}
AssignmentScriptContainer assignmentScript = npc.getAssignmentTrait().getAssignment();
Expand All @@ -41,9 +30,6 @@ public static InteractScriptContainer getInteractScript(NPCTag npc, PlayerTag pl
if (assignedScripts.isEmpty()) {
return null;
}
if (Debug.shouldDebug(assignmentScript) && debugGet) {
Debug.log(DebugElement.Header, "Getting interact script: n@" + npc.getName() + "/p@" + player.getName());
}
String script = assignedScripts.get(0);
if (script.contains(" ") && Character.isDigit(script.charAt(0))) {
Deprecations.interactScriptPriority.warn(assignmentScript);
Expand All @@ -55,24 +41,46 @@ public static InteractScriptContainer getInteractScript(NPCTag npc, PlayerTag pl
return null;
}
}
InteractScriptContainer interactScript = ScriptRegistry.getScriptContainer(script);
InteractScriptContainer container = ScriptRegistry.getScriptContainer(script);
if (container == null) {
Debug.echoError("'" + script + "' is not a valid Interact Script. Is there a duplicate script by this name, or is it missing?");
}
return container;
}

/**
* Gets the InteractScript from an NPC and returns the appropriate ScriptContainer.
* Returns null if no script found.
*
* @param npc the NPC involved
* @param player the Player involved
* @param trigger the class of the trigger being used
* @return the highest priority InteractScriptContainer that meets requirements, if any.
*/
public static InteractScriptContainer getInteractScript(NPCTag npc, PlayerTag player, boolean showDebug, Class<? extends AbstractTrigger> trigger) {
if (player == null || trigger == null) {
return null;
}
InteractScriptContainer interactScript = getInteractScript(npc);
if (interactScript == null) {
Debug.echoError("'" + script + "' is not a valid Interact Script. Is there a duplicate script by this name?");
return null;
}
if (Debug.shouldDebug(interactScript) && showDebug) {
Debug.log(DebugElement.Header, "Getting interact script: n@" + npc.getName() + "/p@" + player.getName());
}
if (!CooldownCommand.checkCooldown(player, interactScript.getName())) {
if (Debug.shouldDebug(interactScript) && debugGet) {
if (Debug.shouldDebug(interactScript) && showDebug) {
Debug.log(ChatColor.GOLD + " ...but, isn't cooled down, yet! Skipping.");
return null;
}
}
if (Debug.shouldDebug(assignmentScript) && debugGet) {
if (Debug.shouldDebug(interactScript) && showDebug) {
Debug.log(DebugElement.Spacer, null);
}
if (Debug.shouldDebug(assignmentScript) && debugGet) {
Debug.log("Interact script is " + script + ". Current step for this script is: " + getCurrentStep(player, script));
if (Debug.shouldDebug(interactScript) && showDebug) {
Debug.log("Interact script is " + interactScript.getName() + ". Current step for this script is: " + getCurrentStep(player, interactScript.getName()));
}
if (Debug.shouldDebug(interactScript) && debugGet) {
if (Debug.shouldDebug(interactScript) && showDebug) {
Debug.log(DebugElement.Footer, "");
}
return interactScript;
Expand Down
Expand Up @@ -83,7 +83,6 @@ public class DamageTrigger extends AbstractTrigger implements Listener {
public void damageTrigger(EntityDamageByEntityEvent event) {
Map<String, ObjectTag> context = new HashMap<>();
context.put("damage", new ElementTag(event.getDamage()));

if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
if (npc == null) {
Expand All @@ -92,44 +91,35 @@ public void damageTrigger(EntityDamageByEntityEvent event) {
if (npc.getCitizen() == null) {
return;
}

EntityTag damager = new EntityTag(event.getDamager());
if (damager.isProjectile() && damager.hasShooter()) {
damager = damager.getShooter();
}
context.put("damager", damager.getDenizenObject());

String determ = npc.action("damaged", null, context);
if (determ != null && determ.equalsIgnoreCase("CANCELLED")) {
event.setCancelled(true);
return;
}

if (!damager.isPlayer()) {
return;
}
PlayerTag dplayer = damager.getDenizenPlayer();

if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return;
}
if (!npc.getTriggerTrait().isEnabled(name)) {
return;
}

TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);

if (!trigger.wasTriggered()) {
return;
}

if (trigger.hasDetermination() && trigger.getDetermination().equalsIgnoreCase("cancelled")) {
event.setCancelled(true);
return;
}

InteractScriptContainer script = InteractScriptHelper.getInteractScript(npc, dplayer, getClass());

InteractScriptContainer script = InteractScriptHelper.getInteractScript(npc, dplayer, true, getClass());
String id = null;
if (script != null) {
Map<String, String> idMap = script.getIdMapFor(this.getClass(), dplayer);
Expand All @@ -142,7 +132,6 @@ public void damageTrigger(EntityDamageByEntityEvent event) {
}
}
}

if (!parse(npc, dplayer, script, id, context)) {
npc.action("no damage trigger", dplayer);
}
Expand Down

0 comments on commit 4eda734

Please sign in to comment.