use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class NBTCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dItem item = scriptEntry.getdObject("item");
Element key = scriptEntry.getElement("key");
Element value = scriptEntry.getElement("value");
dB.report(scriptEntry, getName(), item.debug() + key.debug() + value.debug());
ItemStack itemStack = item.getItemStack();
if (value.asString().equals("!")) {
itemStack = CustomNBT.removeCustomNBT(itemStack, key.asString(), CustomNBT.KEY_DENIZEN);
} else {
itemStack = CustomNBT.addCustomNBT(itemStack, key.asString(), value.asString(), CustomNBT.KEY_DENIZEN);
}
scriptEntry.addObject("new_item", new dItem(itemStack));
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class PushCommand method parseArgs.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("origin") && arg.matchesPrefix("origin", "o", "source", "shooter", "s")) {
if (arg.matchesArgumentType(dEntity.class)) {
scriptEntry.addObject("originEntity", arg.asType(dEntity.class));
} else if (arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("originLocation", arg.asType(dLocation.class));
} else {
dB.echoError("Ignoring unrecognized argument: " + arg.raw_value);
}
} else if (!scriptEntry.hasObject("destination") && arg.matchesArgumentType(dLocation.class) && arg.matchesPrefix("destination", "d")) {
scriptEntry.addObject("destination", arg.asType(dLocation.class));
} else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(Duration.class) && arg.matchesPrefix("duration", "d")) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
} else if (!scriptEntry.hasObject("speed") && arg.matchesPrimitive(aH.PrimitiveType.Double) && arg.matchesPrefix("speed", "s")) {
scriptEntry.addObject("speed", arg.asElement());
} else if (!scriptEntry.hasObject("script") && (arg.matchesArgumentType(dScript.class) || arg.matchesPrefix("script"))) {
scriptEntry.addObject("script", arg.asType(dScript.class));
} else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
} else if (!scriptEntry.hasObject("force_along") && arg.matches("force_along")) {
scriptEntry.addObject("force_along", new Element(true));
} else if (!scriptEntry.hasObject("no_rotate") && arg.matches("no_rotate")) {
scriptEntry.addObject("no_rotate", new Element(true));
} else if (!scriptEntry.hasObject("precision") && arg.matchesPrefix("precision")) {
scriptEntry.addObject("precision", arg.asElement());
} else if (!scriptEntry.hasObject("no_damage") && arg.matches("no_damage")) {
scriptEntry.addObject("no_damage", new Element(true));
} else if (arg.matchesPrefix("def", "define", "context")) {
scriptEntry.addObject("definitions", arg.asType(dList.class));
} else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("originLocation")) {
scriptEntry.defaultObject("originEntity", ((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity() : null, ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getDenizenEntity() : null);
}
scriptEntry.defaultObject("speed", new Element(1.5));
scriptEntry.defaultObject("duration", new Duration(20));
scriptEntry.defaultObject("force_along", new Element(false));
scriptEntry.defaultObject("precision", new Element(2));
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
if (!scriptEntry.hasObject("originEntity") && !scriptEntry.hasObject("originLocation")) {
throw new InvalidArgumentsException("Must specify an origin location!");
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class RemoveCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dWorld world = (dWorld) scriptEntry.getObject("world");
Element region = (Element) scriptEntry.getObject("region");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugList("entities", entities) + (region != null ? aH.debugObj("region", region) : ""));
boolean conditionsMet;
for (dEntity entity : entities) {
conditionsMet = true;
if (!entity.isGeneric()) {
if (region != null) {
dB.echoError(scriptEntry.getResidingQueue(), "Region support is deprecated!");
/*conditionsMet = WorldGuardUtilities.inRegion
(entity.getBukkitEntity().getLocation(),
region.asString());*/
}
if (conditionsMet) {
if (entity.isCitizensNPC()) {
entity.getDenizenNPC().getCitizen().destroy();
} else {
entity.remove();
}
}
} else // If this is a generic unspawned entity, remove
// all entities of this type from the world (as
// long as they meet all other conditions)
{
for (Entity worldEntity : world.getEntities()) {
if (entity.getEntityType().equals(DenizenEntityType.getByEntity(worldEntity))) {
if (region != null) {
dB.echoError(scriptEntry.getResidingQueue(), "Region support is deprecated!");
/*conditionsMet = WorldGuardUtilities.inRegion
(worldEntity.getLocation(),
region.asString());*/
}
if (conditionsMet) {
worldEntity.remove();
}
}
}
}
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class RotateCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
final List<dEntity> entities = new ArrayList<dEntity>((List<dEntity>) scriptEntry.getObject("entities"));
final Duration duration = (Duration) scriptEntry.getObject("duration");
final Duration frequency = (Duration) scriptEntry.getObject("frequency");
final Element yaw = (Element) scriptEntry.getObject("yaw");
final Element pitch = (Element) scriptEntry.getObject("pitch");
boolean cancel = scriptEntry.hasObject("cancel");
final boolean infinite = scriptEntry.hasObject("infinite");
// Report to dB
dB.report(scriptEntry, getName(), (cancel ? aH.debugObj("cancel", cancel) : "") + aH.debugObj("entities", entities.toString()) + (infinite ? aH.debugObj("duration", "infinite") : duration.debug()) + frequency.debug() + yaw.debug() + pitch.debug());
// them from it
for (dEntity entity : entities) {
if (cancel) {
rotatingEntities.remove(entity.getUUID());
} else {
rotatingEntities.add(entity.getUUID());
}
}
// Go no further if we are canceling a rotation
if (cancel) {
return;
}
// Run a task that will keep rotating the entities
BukkitRunnable task = new BukkitRunnable() {
int ticks = 0;
int maxTicks = duration.getTicksAsInt();
// Track entities that are no longer used, to remove them from
// the regular list
Collection<dEntity> unusedEntities = new LinkedList<dEntity>();
@Override
public void run() {
if (entities.isEmpty()) {
scriptEntry.setFinished(true);
this.cancel();
} else if (infinite || ticks < maxTicks) {
for (dEntity entity : entities) {
if (entity.isSpawned() && rotatingEntities.contains(entity.getUUID())) {
NMSHandler.getInstance().getEntityHelper().rotate(entity.getBukkitEntity(), NMSHandler.getInstance().getEntityHelper().normalizeYaw(entity.getLocation().getYaw() + yaw.asFloat()), entity.getLocation().getPitch() + pitch.asFloat());
} else {
rotatingEntities.remove(entity.getUUID());
unusedEntities.add(entity);
}
}
// Remove any entities that are no longer spawned
if (!unusedEntities.isEmpty()) {
for (dEntity unusedEntity : unusedEntities) {
entities.remove(unusedEntity);
}
unusedEntities.clear();
}
ticks = (int) (ticks + frequency.getTicks());
} else {
scriptEntry.setFinished(true);
this.cancel();
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, frequency.getTicks());
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class ShootCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity");
dLocation originLocation = scriptEntry.hasObject("originLocation") ? (dLocation) scriptEntry.getObject("originLocation") : new dLocation(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()));
boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").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 dLocation destination = scriptEntry.hasObject("destination") ? (dLocation) scriptEntry.getObject("destination") : (originEntity != null ? new dLocation(originEntity.getEyeLocation().clone().add(originEntity.getEyeLocation().clone().getDirection().multiply(30))) : (originLocation != null ? new dLocation(originLocation.clone().add(originLocation.getDirection().multiply(30))) : null));
// TODO: Same as PUSH -- is this the place to do this?
if (destination == null) {
dB.report(scriptEntry, getName(), "No destination specified!");
return;
}
final List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final dScript script = (dScript) scriptEntry.getObject("script");
final dList definitions = (dList) scriptEntry.getObject("definitions");
dEntity shooter = (dEntity) scriptEntry.getObject("shooter");
Element height = scriptEntry.getElement("height");
Element gravity = scriptEntry.getElement("gravity");
Element speed = scriptEntry.getElement("speed");
Element spread = scriptEntry.getElement("spread");
dLocation lead = (dLocation) scriptEntry.getObject("lead");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("origin", originEntity != null ? originEntity : originLocation) + aH.debugObj("entities", entities.toString()) + destination.debug() + height.debug() + (gravity != null ? gravity.debug() : "") + (speed != null ? speed.debug() : "") + (script != null ? script.debug() : "") + (shooter != null ? shooter.debug() : "") + (spread != null ? spread.debug() : "") + (lead != null ? lead.debug() : "") + (no_rotate ? aH.debugObj("no_rotate", "true") : "") + (definitions != null ? definitions.debug() : ""));
// Keep a dList of entities that can be called using <entry[name].shot_entities>
// later in the script queue
final dList entityList = new dList();
// Go through all the entities, spawning/teleporting and rotating them
for (dEntity entity : entities) {
if (!entity.isSpawned() || !no_rotate) {
entity.spawnAt(originLocation);
}
// Only add to entityList after the entities have been
// spawned, otherwise you'll get something like "e@skeleton"
// instead of "e@57" on it
entityList.add(entity.toString());
if (!no_rotate) {
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
}
// when applicable
if (entity.isProjectile() && (shooter != null || originEntity != null)) {
entity.setShooter(shooter != null ? shooter : originEntity);
// Also, watch for it hitting a target
arrows.put(entity.getUUID(), null);
}
}
// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("shot_entities", entityList);
if (spread == null) {
Position.mount(Conversion.convertEntities(entities));
}
// Get the entity at the bottom of the entity list, because
// only its gravity should be affected and tracked considering
// that the other entities will be mounted on it
final dEntity lastEntity = entities.get(entities.size() - 1);
if (gravity == null) {
gravity = new Element(lastEntity.getEntityType().getGravity());
}
if (speed == null) {
Vector v1 = lastEntity.getLocation().toVector();
Vector v2 = destination.toVector();
Vector v3 = Velocity.calculate(v1, v2, gravity.asDouble(), height.asDouble());
lastEntity.setVelocity(v3);
} else if (lead == null) {
Vector relative = destination.clone().subtract(originLocation).toVector();
lastEntity.setVelocity(relative.normalize().multiply(speed.asDouble()));
} else {
double g = 20;
double v = speed.asDouble();
Vector relative = destination.clone().subtract(originLocation).toVector();
double testAng = Velocity.launchAngle(originLocation, destination.toVector(), v, relative.getY(), g);
double hangTime = Velocity.hangtime(testAng, v, relative.getY(), g);
Vector to = destination.clone().add(lead.clone().multiply(hangTime)).toVector();
relative = to.clone().subtract(originLocation.toVector());
Double dist = Math.sqrt(relative.getX() * relative.getX() + relative.getZ() * relative.getZ());
if (dist == 0) {
dist = 0.1d;
}
testAng = Velocity.launchAngle(originLocation, to, v, relative.getY(), g);
relative.setY(Math.tan(testAng) * dist);
relative = relative.normalize();
v = v + (1.188 * Math.pow(hangTime, 2));
relative = relative.multiply(v / 20.0d);
lastEntity.setVelocity(relative);
}
if (spread != null) {
Vector base = lastEntity.getVelocity().clone();
float sf = spread.asFloat();
for (dEntity entity : entities) {
Vector newvel = Velocity.spread(base, (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf), (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf));
entity.setVelocity(newvel);
}
}
final dLocation start = new dLocation(lastEntity.getLocation());
final Vector start_vel = lastEntity.getVelocity();
// A task used to trigger a script if the entity is no longer
// being shot, when the script argument is used
BukkitRunnable task = new BukkitRunnable() {
boolean flying = true;
dLocation lastLocation = null;
Vector lastVelocity = null;
public void run() {
// If the entity is no longer spawned, stop the task
if (!lastEntity.isSpawned()) {
flying = false;
} else // the air, stop the task
if (lastVelocity != null) {
if (lastVelocity.distance(lastEntity.getBukkitEntity().getVelocity()) < 0.05) {
flying = false;
}
}
// are met
if (!flying) {
this.cancel();
if (script != null) {
if (lastLocation == null) {
lastLocation = start;
}
if (lastVelocity == null) {
lastVelocity = start_vel;
}
// Build a queue out of the targeted script
List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName())).addEntries(entries);
// Add relevant definitions
queue.addDefinition("location", lastLocation.identify());
queue.addDefinition("shot_entities", entityList.toString());
queue.addDefinition("last_entity", lastEntity.identify());
// Handle hit_entities definition
dList hitEntities = new dList();
for (dEntity entity : entities) {
if (arrows.containsKey(entity.getUUID())) {
dEntity hit = arrows.get(entity.getUUID());
arrows.remove(entity.getUUID());
if (hit != null) {
hitEntities.add(hit.identify());
}
}
}
queue.addDefinition("hit_entities", hitEntities.identify());
if (definitions != null) {
int x = 1;
String[] definition_names = null;
try {
definition_names = script.getContainer().getString("definitions").split("\\|");
} catch (Exception e) {
// TODO: less lazy handling
}
for (String definition : definitions) {
String name = definition_names != null && definition_names.length >= x ? definition_names[x - 1].trim() : String.valueOf(x);
queue.addDefinition(name, definition);
dB.echoDebug(scriptEntry, "Adding definition %" + name + "% as " + definition);
x++;
}
}
// Start it!
queue.start();
}
scriptEntry.setFinished(true);
} else {
// Record it's position in case the entity dies
lastLocation = lastEntity.getLocation();
lastVelocity = lastEntity.getVelocity();
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 2);
}
Aggregations