use of com.denizenscript.denizencore.objects.core.ScriptTag 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.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class MapCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("map-id");
WorldTag create = scriptEntry.getObjectTag("new");
ElementTag reset = scriptEntry.getElement("reset");
LocationTag resetLoc = scriptEntry.getObjectTag("reset-loc");
ElementTag image = scriptEntry.getElement("image");
ScriptTag script = scriptEntry.getObjectTag("script");
ElementTag resize = scriptEntry.getElement("resize");
ElementTag width = scriptEntry.getElement("width");
ElementTag height = scriptEntry.getElement("height");
ElementTag scale = scriptEntry.getElement("scale");
ElementTag tracking = scriptEntry.getElement("tracking");
ElementTag x = scriptEntry.getElement("x-value");
ElementTag y = scriptEntry.getElement("y-value");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, create, reset, resetLoc, image, script, resize, width, height, x, y);
}
MapView map;
if (create != null) {
map = Bukkit.getServer().createMap(create.getWorld());
scriptEntry.addObject("created_map", new ElementTag(map.getId()));
Debug.echoDebug(scriptEntry, "Created map with id " + map.getId() + ".");
} else if (id != null) {
map = Bukkit.getServer().getMap((short) id.asInt());
if (map == null) {
Debug.echoError("No map found for ID '" + id.asInt() + "'!");
return;
}
} else {
// not possible
return;
}
if (reset.asBoolean()) {
if (tracking != null) {
map.setTrackingPosition(true);
}
if (scale != null) {
map.setScale(MapView.Scale.valueOf(scale.asString().toUpperCase()));
}
List<MapRenderer> oldRenderers = DenizenMapManager.removeDenizenRenderers(map);
for (MapRenderer renderer : oldRenderers) {
map.addRenderer(renderer);
}
if (resetLoc != null) {
map.setCenterX(resetLoc.getBlockX());
map.setCenterZ(resetLoc.getBlockZ());
map.setWorld(resetLoc.getWorld());
}
}
if (script != null) {
DenizenMapManager.removeDenizenRenderers(map);
((MapScriptContainer) script.getContainer()).applyTo(map);
}
if (image != null) {
DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
int wide = width != null ? width.asInt() : resize.asBoolean() ? 128 : 0;
int high = height != null ? height.asInt() : resize.asBoolean() ? 128 : 0;
if (CoreUtilities.toLowerCase(image.asString()).endsWith(".gif")) {
dmr.autoUpdate = true;
}
dmr.addObject(new MapImage(dmr, x.asString(), y.asString(), "true", false, image.asString(), wide, high));
dmr.hasChanged = true;
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ResetCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
// We allow players to be a single player or multiple players
ObjectTag player = scriptEntry.getObjectTag("players");
ListTag players;
if (player instanceof PlayerTag) {
players = new ListTag(player);
} else {
players = scriptEntry.getObjectTag("players");
}
Type type = (Type) scriptEntry.getObject("type");
ScriptTag script = scriptEntry.getObjectTag("script");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), players, db("type", type), script);
}
// Deal with GLOBAL_COOLDOWN reset first, since there's no player/players involved
if (type == Type.GLOBAL_COOLDOWN) {
CooldownCommand.setCooldown(null, new DurationTag(0), script.getName(), true);
return;
}
// Now deal with the rest
for (String object : players) {
PlayerTag resettable = PlayerTag.valueOf(object, scriptEntry.context);
if (resettable.isValid()) {
switch(type) {
case PLAYER_COOLDOWN:
CooldownCommand.setCooldown(resettable, new DurationTag(0), script.getName(), false);
return;
}
}
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class CooldownCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ScriptTag script = scriptEntry.getObjectTag("script");
DurationTag duration = scriptEntry.getObjectTag("duration");
Type type = (scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : Type.PLAYER);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("Type", type.name()), script, (type.name().equalsIgnoreCase("player") ? Utilities.getEntryPlayer(scriptEntry) : null), duration);
}
switch(type) {
case PLAYER:
setCooldown(Utilities.getEntryPlayer(scriptEntry), duration, script.getName(), false);
break;
case GLOBAL:
setCooldown(null, duration, script.getName(), true);
break;
}
}
use of com.denizenscript.denizencore.objects.core.ScriptTag in project Denizen-For-Bukkit by DenizenScript.
the class ActionBarCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
String text = scriptEntry.getElement("text").asString();
ScriptTag formatObj = scriptEntry.getObjectTag("format");
ElementTag perPlayerObj = scriptEntry.getElement("per_player");
boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
if (!perPlayer) {
text = TagManager.tag(text, context);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("message", text), db("targets", targets), formatObj, perPlayerObj);
}
FormatScriptContainer format = formatObj == null ? null : (FormatScriptContainer) formatObj.getContainer();
for (PlayerTag player : targets) {
if (player != null) {
if (!player.isOnline()) {
Debug.echoDebug(scriptEntry, "Player is offline, can't send actionbar to them. Skipping.");
continue;
}
String personalText = text;
if (perPlayer) {
context.player = player;
personalText = TagManager.tag(personalText, context);
}
player.getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE));
} else {
Debug.echoError("Sent actionbar to non-existent player!?");
}
}
}
Aggregations