use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class HealCommand method parseArgs.
// <--[command]
// @Name Heal
// @Syntax heal (<#.#>) ({player}/<entity>|...)
// @Required 0
// @Maximum 2
// @Short Heals the player or list of entities.
// @Group entity
//
// @Description
// This command heals a player, list of players, entity or list of entities.
//
// If no amount is specified it will heal the specified player(s)/entity(s) fully.
//
// @Tags
// <EntityTag.health>
// <EntityTag.health_max>
//
// @Usage
// Use to fully heal a player.
// - heal
//
// @Usage
// Use to heal a player 5 hearts.
// - heal 10
//
// @Usage
// Use to heal a defined player fully.
// - heal <[someplayer]>
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean specified_targets = false;
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("amount") && arg.matchesFloat()) {
scriptEntry.addObject("amount", arg.asElement());
} else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentType(ListTag.class)) {
// Entity arg
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
specified_targets = true;
} else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentType(EntityTag.class)) {
// Entity arg
scriptEntry.addObject("entities", Collections.singletonList(arg.asType(EntityTag.class)));
specified_targets = true;
} else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", new ElementTag(-1));
}
if (!specified_targets) {
List<EntityTag> entities = new ArrayList<>();
if (Utilities.getEntryPlayer(scriptEntry) != null) {
entities.add(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity());
} else if (Utilities.getEntryNPC(scriptEntry) != null) {
entities.add(Utilities.getEntryNPC(scriptEntry).getDenizenEntity());
} else {
throw new InvalidArgumentsException("No valid target entities found.");
}
scriptEntry.addObject("entities", entities);
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class PushCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
EntityTag originEntity = scriptEntry.getObjectTag("origin_entity");
LocationTag originLocation = scriptEntry.hasObject("origin_location") ? (LocationTag) scriptEntry.getObject("origin_location") : new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()).subtract(0, 0.4, 0));
boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();
final boolean no_damage = scriptEntry.hasObject("no_damage") && scriptEntry.getElement("no_damage").asBoolean();
// If there is no destination set, but there is a shooter, get a point in front of the shooter and set it as the destination
final LocationTag destination = scriptEntry.hasObject("destination") ? (LocationTag) scriptEntry.getObject("destination") : (originEntity != null ? new LocationTag(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection().multiply(30))) : null);
// TODO: Should this be checked in argument parsing?
if (destination == null) {
Debug.echoError("No destination specified!");
scriptEntry.setFinished(true);
return;
}
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final ScriptTag script = scriptEntry.getObjectTag("script");
final ListTag definitions = scriptEntry.getObjectTag("definitions");
ElementTag speedElement = scriptEntry.getElement("speed");
DurationTag duration = (DurationTag) scriptEntry.getObject("duration");
ElementTag force_along = scriptEntry.getElement("force_along");
ElementTag precision = scriptEntry.getElement("precision");
ElementTag ignore_collision = scriptEntry.getElement("ignore_collision");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("origin", originEntity != null ? originEntity : originLocation), db("entities", entities), destination, speedElement, duration, script, force_along, precision, (no_rotate ? db("no_rotate", "true") : ""), (no_damage ? db("no_damage", "true") : ""), ignore_collision, definitions);
}
final boolean ignoreCollision = ignore_collision != null && ignore_collision.asBoolean();
final double speed = speedElement.asDouble();
final int maxTicks = duration.getTicksAsInt();
final boolean forceAlong = force_along.asBoolean();
// Keep a ListTag of entities that can be called using <entry[name].pushed_entities> later in the script queue
final ListTag entityList = new ListTag();
for (EntityTag entity : entities) {
entity.spawnAt(originLocation);
entityList.addObject(entity);
if (!no_rotate) {
NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
}
if (entity.isProjectile() && originEntity != null) {
entity.setShooter(originEntity);
}
}
scriptEntry.addObject("pushed_entities", entityList);
Position.mount(Conversion.convertEntities(entities));
final EntityTag lastEntity = entities.get(entities.size() - 1);
final Vector v2 = destination.toVector();
final Vector Origin = originLocation.toVector();
final int prec = precision.asInt();
BukkitRunnable task = new BukkitRunnable() {
int runs = 0;
LocationTag lastLocation;
@Override
public void run() {
if (runs < maxTicks && lastEntity.isValid()) {
Vector v1 = lastEntity.getLocation().toVector();
Vector v3 = v2.clone().subtract(v1).normalize();
if (forceAlong) {
Vector newDest = v2.clone().subtract(Origin).normalize().multiply(runs * speed).add(Origin);
lastEntity.teleport(new Location(lastEntity.getLocation().getWorld(), newDest.getX(), newDest.getY(), newDest.getZ(), lastEntity.getLocation().getYaw(), lastEntity.getLocation().getPitch()));
}
runs += prec;
// Check if the entity is close to its destination
if (Math.abs(v2.getX() - v1.getX()) < 1.5f && Math.abs(v2.getY() - v1.getY()) < 1.5f && Math.abs(v2.getZ() - v1.getZ()) < 1.5f) {
runs = maxTicks;
return;
}
Vector newVel = v3.multiply(speed);
lastEntity.setVelocity(newVel);
if (!ignoreCollision && lastEntity.isValid()) {
BoundingBox box = lastEntity.getBukkitEntity().getBoundingBox().expand(newVel);
Location ref = lastEntity.getLocation().clone();
for (int x = (int) Math.floor(box.getMinX()); x < Math.ceil(box.getMaxX()); x++) {
ref.setX(x);
for (int y = (int) Math.floor(box.getMinY()); y < Math.ceil(box.getMaxY()); y++) {
ref.setY(y);
for (int z = (int) Math.floor(box.getMinZ()); z < Math.ceil(box.getMaxZ()); z++) {
ref.setZ(z);
if (!isSafeBlock(ref)) {
runs = maxTicks;
}
}
}
}
}
if (no_damage && lastEntity.isLivingEntity()) {
lastEntity.getLivingEntity().setFallDistance(0);
}
// Record the location in case the entity gets lost (EG, if a pushed arrow hits a mob)
lastLocation = lastEntity.getLocation();
} else {
this.cancel();
if (script != null) {
Consumer<ScriptQueue> configure = (queue) -> {
if (lastEntity.getLocation() != null) {
queue.addDefinition("location", lastEntity.getLocation());
} else {
queue.addDefinition("location", lastLocation);
}
queue.addDefinition("pushed_entities", entityList);
queue.addDefinition("last_entity", lastEntity);
};
ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, configure, null, null, definitions, scriptEntry);
}
scriptEntry.setFinished(true);
}
}
};
task.runTaskTimer(Denizen.getInstance(), 0, prec);
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class RenameCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
final ElementTag name = scriptEntry.getElement("name");
ElementTag perPlayer = scriptEntry.getElement("per_player");
ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
ListTag targets = scriptEntry.getObjectTag("targets");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (perPlayer != null && perPlayer.asBoolean()) {
NetworkInterceptHelper.enable();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
}
for (ObjectTag target : targets.objectForms) {
EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity != null) {
Entity bukkitEntity = entity.getBukkitEntity();
if (bukkitEntity == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (name.asString().equals("cancel")) {
customNames.remove(bukkitEntity.getUniqueId());
if (bukkitEntity.isCustomNameVisible()) {
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
}
}
} else {
bukkitEntity.setCustomNameVisible(true);
// Force a metadata update
bukkitEntity.setCustomNameVisible(false);
}
} else {
final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
Function<Player, String> nameGetter = p -> {
originalContext.player = new PlayerTag(p);
return TagManager.tag(name.asString(), originalContext);
};
RenameData renamer = new RenameData();
renamer.nameFunction = nameGetter;
renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
if (players == null) {
playerToFuncMap.put(null, renamer);
} else {
for (PlayerTag player : players) {
playerToFuncMap.put(player.getUUID(), renamer);
}
}
if (players == null) {
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
}
} else {
for (PlayerTag player : players) {
NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
}
}
}
}
}
return;
}
String nameString = TagManager.tag(name.asString(), scriptEntry.context);
if (nameString.length() > 256) {
nameString = nameString.substring(0, 256);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("name", nameString), listNameOnly, targets);
}
for (ObjectTag target : targets.objectForms) {
EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
if (entity == null) {
entity = target.asType(NPCTag.class, scriptEntry.context);
} else {
entity = ((EntityTag) entity).getDenizenObject();
}
if (entity == null) {
Debug.echoError("Invalid entity in rename command.");
continue;
}
if (entity instanceof NPCTag) {
NPC npc = ((NPCTag) entity).getCitizen();
if (npc.isSpawned()) {
Location prev = npc.getStoredLocation().clone();
npc.despawn(DespawnReason.PENDING_RESPAWN);
npc.setName(nameString);
npc.spawn(prev);
} else {
npc.setName(nameString);
}
} else if (entity instanceof PlayerTag) {
if (listNameOnly != null && listNameOnly.asBoolean()) {
AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
} else {
String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
}
} else {
Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
customNames.remove(bukkitEntity.getUniqueId());
bukkitEntity.setCustomName(nameString);
bukkitEntity.setCustomNameVisible(true);
}
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class AnimateCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for");
PlayerAnimation animation = scriptEntry.hasObject("animation") ? (PlayerAnimation) scriptEntry.getObject("animation") : null;
EntityEffect effect = scriptEntry.hasObject("effect") ? (EntityEffect) scriptEntry.getObject("effect") : null;
String nmsAnimation = scriptEntry.hasObject("nms_animation") ? (String) scriptEntry.getObject("nms_animation") : null;
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (animation != null ? db("animation", animation.name()) : effect != null ? db("effect", effect.name()) : db("animation", nmsAnimation)), db("entities", entities), db("for", forPlayers));
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
try {
if (animation != null && entity.getBukkitEntity() instanceof Player) {
Player player = (Player) entity.getBukkitEntity();
animation.play(player);
} else if (effect != null) {
if (forPlayers != null) {
for (PlayerTag player : forPlayers) {
NMSHandler.getPacketHelper().sendEntityEffect(player.getPlayerEntity(), entity.getBukkitEntity(), effect.getData());
}
} else {
entity.getBukkitEntity().playEffect(effect);
}
} else if (nmsAnimation != null) {
EntityAnimation entityAnimation = NMSHandler.getAnimationHelper().getEntityAnimation(nmsAnimation);
entityAnimation.play(entity.getBukkitEntity());
} else {
Debug.echoError("No way to play the given animation on entity '" + entity + "'");
}
} catch (Exception e) {
Debug.echoError(scriptEntry, "Error playing that animation!");
Debug.echoError(e);
}
}
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class AttackCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
EntityTag target = scriptEntry.getObjectTag("target");
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", "true") : ""), db("entities", entities), db("target", target));
}
for (EntityTag entity : entities) {
if (entity.isCitizensNPC()) {
Navigator nav = entity.getDenizenNPC().getCitizen().getNavigator();
if (!cancel) {
nav.setTarget(target.getBukkitEntity(), true);
} else {
// Only cancel navigation if the NPC is attacking something
if (nav.isNavigating() && nav.getTargetType().equals(TargetType.ENTITY) && nav.getEntityTarget().isAggressive()) {
nav.cancelNavigation();
}
}
} else {
if (!cancel) {
entity.target(target.getLivingEntity());
} else {
entity.target(null);
}
}
}
}
Aggregations