use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class FakeEquipCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
NetworkInterceptHelper.enable();
EquipmentOverride equipment = (EquipmentOverride) scriptEntry.getObject("equipment");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> playersFor = (List<PlayerTag>) scriptEntry.getObject("for");
ElementTag reset = scriptEntry.getElement("reset");
DurationTag duration = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment), reset, duration, db("for", playersFor));
}
boolean isReset = reset != null && reset.asBoolean();
for (PlayerTag player : playersFor) {
HashMap<UUID, EquipmentOverride> playersMap = overrides.computeIfAbsent(player.getUUID(), (k) -> new HashMap<>());
for (EntityTag entity : entities) {
if (entity.isGeneric()) {
Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
continue;
}
LivingEntity livingEntity = entity.getLivingEntity();
if (livingEntity == null) {
Debug.echoError(scriptEntry, "Cannot equip invalid/non-living entity " + entity.identify() + "!");
continue;
}
EquipmentOverride entityData;
if (isReset) {
entityData = playersMap.remove(entity.getUUID());
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
} else {
entityData = playersMap.computeIfAbsent(entity.getUUID(), (k) -> new EquipmentOverride());
entityData.copyFrom(equipment);
}
if (entityData != null) {
if (entityData.cancelTask != null && !entityData.cancelTask.isCancelled()) {
entityData.cancelTask.cancel();
entityData.cancelTask = null;
}
if (duration != null && duration.getTicks() > 0) {
entityData.cancelTask = new BukkitRunnable() {
@Override
public void run() {
entityData.cancelTask = null;
HashMap<UUID, EquipmentOverride> playersMap = overrides.get(player.getUUID());
if (playersMap != null) {
if (playersMap.remove(entity.getUUID()) != null) {
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
}.runTaskLater(Denizen.getInstance(), duration.getTicks());
}
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class FlyCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
LocationTag origin = scriptEntry.getObjectTag("origin");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final List<LocationTag> destinations = scriptEntry.hasObject("destinations") ? (List<LocationTag>) scriptEntry.getObject("destinations") : new ArrayList<>();
// Set freeflight to true only if there are no destinations
final boolean freeflight = destinations.size() < 1;
EntityTag controller = scriptEntry.getObjectTag("controller");
// If freeflight is on, we need to do some checks
if (freeflight) {
// flying entities, so try to find a player in the entity list
if (controller == null) {
for (EntityTag entity : entities) {
if (entity.isPlayer()) {
// the controller
if (entities.get(entities.size() - 1) != entity) {
controller = entity;
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, "Flight control defaulting to " + controller);
}
break;
}
}
}
// If the controller is still null, we cannot continue
if (controller == null) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, "There is no one to control the flight's path!");
}
return;
}
} else // Else, if the controller was set, we need to make sure
// it is among the flying entities, and add it if it is not
{
boolean found = false;
for (EntityTag entity : entities) {
if (entity.identify().equals(controller.identify())) {
found = true;
break;
}
}
// Add the controller to the entity list
if (!found) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, "Adding controller " + controller + " to flying entities.");
}
entities.add(0, controller);
}
}
}
final double speed = ((ElementTag) scriptEntry.getObject("speed")).asDouble();
final float rotationThreshold = ((ElementTag) scriptEntry.getObject("rotation_threshold")).asFloat();
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), origin, db("entities", entities), db("speed", speed), db("rotation threshold degrees", rotationThreshold), (freeflight ? controller : db("destinations", destinations)));
}
if (!cancel) {
for (EntityTag entity : entities) {
entity.spawnAt(origin);
}
Position.mount(Conversion.convertEntities(entities));
} else {
Position.dismount(Conversion.convertEntities(entities));
return;
}
final Entity entity = entities.get(entities.size() - 1).getBukkitEntity();
final LivingEntity finalController = controller != null ? controller.getLivingEntity() : null;
BukkitRunnable task = new BukkitRunnable() {
Location location = null;
Boolean flying = true;
public void run() {
if (freeflight) {
// flying where the controller is looking
if (!entity.isEmpty() && finalController.isInsideVehicle()) {
location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
} else {
flying = false;
}
} else {
// as there are destinations left
if (destinations.size() > 0) {
location = destinations.get(0);
} else {
flying = false;
}
}
if (flying && entity.isValid()) {
// when it really needs to
if (!NMSHandler.getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
NMSHandler.getEntityHelper().faceLocation(entity, location);
}
Vector v1 = entity.getLocation().toVector();
Vector v2 = location.toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
entity.setVelocity(v3);
// to be the case
if (!freeflight) {
if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2 && Math.abs(v2.getZ() - v1.getZ()) < 2) {
destinations.remove(0);
}
}
} else {
flying = false;
this.cancel();
}
}
};
task.runTaskTimer(Denizen.getInstance(), 0, 3);
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class HealthCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag quantity = scriptEntry.getElement("quantity");
ElementTag action = scriptEntry.getElement("action");
ElementTag heal = scriptEntry.getElement("heal");
List<EntityTag> targets = (List<EntityTag>) scriptEntry.getObject("target");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), quantity, action, heal, db("target", targets));
}
if (quantity == null && action == null) {
Debug.echoError(scriptEntry, "Null quantity!");
}
if (action == null) {
action = new ElementTag(true);
}
for (EntityTag target : targets) {
if (target.isCitizensNPC()) {
if (action.asString().equalsIgnoreCase("true")) {
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
} else if (action.asString().equalsIgnoreCase("false")) {
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
} else if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
} else {
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
}
}
if (quantity != null) {
if (target.isCitizensNPC()) {
if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
HealthTrait trait = target.getDenizenNPC().getCitizen().getOrAddTrait(HealthTrait.class);
trait.setMaxhealth(quantity.asInt());
if (heal.asBoolean()) {
trait.setHealth(quantity.asDouble());
}
} else {
Debug.echoError(scriptEntry, "NPC doesn't have health trait!");
}
} else if (target.isLivingEntity()) {
target.getLivingEntity().setMaxHealth(quantity.asDouble());
if (heal.asBoolean()) {
target.getLivingEntity().setHealth(quantity.asDouble());
}
} else {
Debug.echoError(scriptEntry, "Entity '" + target.identify() + "'is not alive!");
}
}
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class LeashCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
EntityTag holder = null;
LocationTag holderLoc = null;
Entity Holder = null;
Object holderObject = scriptEntry.getObject("holder");
if (holderObject instanceof EntityTag) {
holder = scriptEntry.getObjectTag("holder");
Holder = holder.getBukkitEntity();
} else if (holderObject instanceof LocationTag) {
holderLoc = scriptEntry.getObjectTag("holder");
Material material = holderLoc.getBlock().getType();
if (material.name().endsWith("_FENCE")) {
Holder = holderLoc.getWorld().spawn(holderLoc, LeashHitch.class);
} else {
Debug.echoError(scriptEntry, "Bad holder location specified - only fences are permitted!");
return;
}
}
boolean cancel = scriptEntry.hasObject("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", "true") : ""), db("entities", entities), db("holder", holder), db("holder", holderLoc));
}
for (EntityTag entity : entities) {
if (entity.isSpawned() && entity.isLivingEntity()) {
if (cancel) {
entity.getLivingEntity().setLeashHolder(null);
} else {
entity.getLivingEntity().setLeashHolder(Holder);
}
}
}
}
use of com.denizenscript.denizen.objects.EntityTag in project Denizen-For-Bukkit by DenizenScript.
the class SneakCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag fake = scriptEntry.getElement("fake");
ElementTag stopfake = scriptEntry.getElement("stopfake");
ElementTag mode = scriptEntry.getElement("mode");
List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for_players");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), mode, db("entities", entities), db("for_players", forPlayers), fake, stopfake);
}
boolean shouldSneak = mode.asString().equalsIgnoreCase("start");
boolean shouldFake = fake != null && fake.asBoolean();
boolean shouldStopFake = stopfake != null && stopfake.asBoolean();
for (EntityTag entity : entities) {
if (shouldFake || shouldStopFake) {
if (forPlayers == null) {
updateFakeSneak(entity.getUUID(), null, shouldSneak, shouldFake);
for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(entity.getBukkitEntity())) {
NMSHandler.getPacketHelper().sendEntityMetadataFlagsUpdate(player, entity.getBukkitEntity());
}
} else {
for (PlayerTag player : forPlayers) {
updateFakeSneak(entity.getUUID(), player.getUUID(), shouldSneak, shouldFake);
NMSHandler.getPacketHelper().sendEntityMetadataFlagsUpdate(player.getPlayerEntity(), entity.getBukkitEntity());
}
}
} else if (entity.isCitizensNPC()) {
SneakingTrait trait = entity.getDenizenNPC().getCitizen().getOrAddTrait(SneakingTrait.class);
if (shouldSneak) {
trait.sneak();
} else {
trait.stand();
}
} else if (entity.isSpawned()) {
NMSHandler.getEntityHelper().setSneaking(entity.getBukkitEntity(), shouldSneak);
} else {
Debug.echoError("Cannot make unspawned entity sneak.");
}
}
}
Aggregations