Skip to content

Commit

Permalink
teleport command: autoExecute and offline player support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 6, 2022
1 parent d49c25c commit 0a1249e
Showing 1 changed file with 44 additions and 48 deletions.
@@ -1,29 +1,29 @@
package com.denizenscript.denizen.scripts.commands.entity;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.citizensnpcs.trait.CurrentLocation;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.Collections;
import java.util.List;

public class TeleportCommand extends AbstractCommand {

public TeleportCommand() {
setName("teleport");
setSyntax("teleport (<entity>|...) [<location>] (cause:<cause>)");
setRequiredArguments(1, 3);
isProcedural = false;
autoCompile();
}

// <--[command]
Expand All @@ -40,6 +40,8 @@ public TeleportCommand() {
// Entities can be teleported between worlds using this command.
// You may optionally specify a teleport cause for player entities, allowing proper teleport event handling. When not specified, this is "PLUGIN". See <@link language teleport cause> for causes.
//
// Instead of a valid entity, an unspawned NPC or an offline player may also be used.
//
// @Tags
// <EntityTag.location>
//
Expand Down Expand Up @@ -73,56 +75,50 @@ public void addCustomTabCompletions(TabCompletionsBuilder tab) {
tab.addNotesOfType(LocationTag.class);
}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
// NPC arg for compatibility with old scripts
else if (arg.matches("npc") && Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("entities", Collections.singletonList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()));
}
else if (!scriptEntry.hasObject("cause")
&& arg.matchesEnum(PlayerTeleportEvent.TeleportCause.class)) {
scriptEntry.addObject("cause", arg.asElement());
}
else {
arg.reportUnhandled();
}
public static void autoExecute(ScriptEntry scriptEntry,
@ArgLinear @ArgName("entities") ObjectTag entityList,
@ArgLinear @ArgName("location") @ArgDefaultNull ObjectTag locationRaw,
@ArgPrefixed @ArgName("cause") @ArgDefaultText("PLUGIN") PlayerTeleportEvent.TeleportCause cause) {
if (locationRaw == null) { // Compensate for legacy "- teleport <loc>" default fill
locationRaw = entityList;
entityList = Utilities.entryDefaultEntity(scriptEntry, true);
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
else if (entityList.identify().startsWith("l@")) { // Compensate for legacy entity/location out-of-order support
ObjectTag swap = locationRaw;
locationRaw = entityList;
entityList = swap;
}
if (!scriptEntry.hasObject("entities")) {
scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, true));
LocationTag location = locationRaw.asType(LocationTag.class, scriptEntry.context);
ListTag entities = entityList.asType(ListTag.class, scriptEntry.context);
if (location == null || entities == null) {
throw new InvalidArgumentsRuntimeException("Location or entity list missing or invalid for Teleport command");
}
}

@Override
public void execute(final ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
if (entities == null) {
throw new InvalidArgumentsRuntimeException("Missing entity target input");
}
ElementTag cause = scriptEntry.getElement("cause");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, db("entities", entities), cause);
}
PlayerTeleportEvent.TeleportCause causeEnum = cause == null ? PlayerTeleportEvent.TeleportCause.PLUGIN : PlayerTeleportEvent.TeleportCause.valueOf(cause.asString().toUpperCase());
for (EntityTag entity : entities) {
for (ObjectTag entityObj : entities.objectForms) {
if (entityObj.shouldBeType(PlayerTag.class)) {
PlayerTag player = entityObj.asType(PlayerTag.class, scriptEntry.context);
if (player != null && !player.isOnline()) {
player.setLocation(location);
continue;
}
}
if (entityObj.shouldBeType(NPCTag.class)) {
NPCTag npc = entityObj.asType(NPCTag.class, scriptEntry.context);
if (npc != null && !npc.isSpawned()) {
npc.getCitizen().getOrAddTrait(CurrentLocation.class).setLocation(location.clone());
continue;
}
}
EntityTag entity = entityObj.asType(EntityTag.class, scriptEntry.context);
if (entity == null) {
Debug.echoError("Cannot interpret object '" + entityObj + "' as an EntityTag.");
continue;
}
if (entity.isFake && entity.getWorld().equals(location.getWorld())) {
NMSHandler.entityHelper.snapPositionTo(entity.getBukkitEntity(), location.toVector());
NMSHandler.entityHelper.look(entity.getBukkitEntity(), location.getYaw(), location.getPitch());
return;
}
entity.teleport(location, causeEnum);
entity.teleport(location, cause);
}
}
}

0 comments on commit 0a1249e

Please sign in to comment.