use of org.bukkit.scheduler.BukkitRunnable 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);
}
use of org.bukkit.scheduler.BukkitRunnable in project Denizen-For-Bukkit by DenizenScript.
the class FlyCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
dLocation origin = (dLocation) scriptEntry.getObject("origin");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final List<dLocation> destinations = scriptEntry.hasObject("destinations") ? (List<dLocation>) scriptEntry.getObject("destinations") : new ArrayList<dLocation>();
// Set freeflight to true only if there are no destinations
final boolean freeflight = destinations.size() < 1;
dEntity controller = (dEntity) scriptEntry.getObject("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 (dEntity entity : entities) {
if (entity.isPlayer()) {
// the controller
if (entities.get(entities.size() - 1) != entity) {
controller = entity;
dB.report(scriptEntry, getName(), "Flight control defaulting to " + controller);
break;
}
}
}
// If the controller is still null, we cannot continue
if (controller == null) {
dB.report(scriptEntry, getName(), "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 (dEntity entity : entities) {
if (entity.identify().equals(controller.identify())) {
found = true;
break;
}
}
// Add the controller to the entity list
if (!found) {
dB.report(scriptEntry, getName(), "Adding controller " + controller + " to flying entities.");
entities.add(0, controller);
}
}
}
final double speed = ((Element) scriptEntry.getObject("speed")).asDouble();
final float rotationThreshold = ((Element) scriptEntry.getObject("rotationThreshold")).asFloat();
boolean cancel = scriptEntry.hasObject("cancel");
// Report to dB
dB.report(scriptEntry, getName(), (cancel ? aH.debugObj("cancel", cancel) : "") + aH.debugObj("origin", origin) + aH.debugObj("entities", entities.toString()) + aH.debugObj("speed", speed) + aH.debugObj("rotation threshold degrees", rotationThreshold) + (freeflight ? aH.debugObj("controller", controller) : aH.debugObj("destinations", destinations.toString())));
// Mount or dismount all of the entities
if (!cancel) {
// Go through all the entities, spawning/teleporting them
for (dEntity entity : entities) {
entity.spawnAt(origin);
}
Position.mount(Conversion.convertEntities(entities));
} else {
Position.dismount(Conversion.convertEntities(entities));
// Go no further if we are dismounting entities
return;
}
// Get the last entity on the list
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) {
if (!entity.isEmpty() && finalController.isInsideVehicle()) {
location = finalController.getEyeLocation().add(finalController.getEyeLocation().getDirection().multiply(30));
} else {
flying = false;
}
} else {
if (destinations.size() > 0) {
location = destinations.get(0);
} else {
flying = false;
}
}
if (flying && entity.isValid()) {
// when it really needs to
if (!NMSHandler.getInstance().getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {
NMSHandler.getInstance().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);
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(DenizenAPI.getCurrentInstance(), 0, 3);
}
use of org.bukkit.scheduler.BukkitRunnable in project Denizen-For-Bukkit by DenizenScript.
the class FakeBlock method stopShowingTo.
public static void stopShowingTo(List<dPlayer> players, final dLocation location) {
final List<UUID> uuids = new ArrayList<UUID>();
for (dPlayer player : players) {
if (!player.isOnline() || !player.isValid()) {
continue;
}
UUID uuid = player.getPlayerEntity().getUniqueId();
uuids.add(uuid);
if (blocks.containsKey(uuid)) {
Map<dLocation, FakeBlock> playerBlocks = blocks.get(uuid);
if (playerBlocks.containsKey(location)) {
playerBlocks.get(location).cancelBlock();
}
}
}
new BukkitRunnable() {
@Override
public void run() {
for (UUID uuid : blocks.keySet()) {
if (uuids.contains(uuid)) {
continue;
}
Map<dLocation, FakeBlock> playerBlocks = blocks.get(uuid);
if (playerBlocks.containsKey(location)) {
playerBlocks.get(location).updateBlock();
}
}
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), 2);
}
use of org.bukkit.scheduler.BukkitRunnable in project Denizen-For-Bukkit by DenizenScript.
the class EntityHelper_v1_10_R1 method walkTo.
@Override
public void walkTo(final Entity entity, Location location, double speed, final Runnable callback) {
if (entity == null || location == null) {
return;
}
net.minecraft.server.v1_10_R1.Entity nmsEntityEntity = ((CraftEntity) entity).getHandle();
if (!(nmsEntityEntity instanceof EntityInsentient)) {
return;
}
final EntityInsentient nmsEntity = (EntityInsentient) nmsEntityEntity;
final NavigationAbstract entityNavigation = nmsEntity.getNavigation();
final PathEntity path;
final boolean aiDisabled = isAIDisabled(entity);
if (aiDisabled) {
toggleAI(entity, true);
nmsEntity.onGround = true;
}
path = entityNavigation.a(location.getX(), location.getY(), location.getZ());
if (path != null) {
entityNavigation.a(path, 1D);
entityNavigation.a(2D);
final double oldSpeed = nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).b();
nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
new BukkitRunnable() {
@Override
public void run() {
if (entityNavigation.n() || path.b()) {
if (callback != null) {
callback.run();
}
nmsEntity.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(oldSpeed);
if (aiDisabled) {
toggleAI(entity, false);
}
cancel();
}
}
}.runTaskTimer(NMSHandler.getJavaPlugin(), 1, 1);
} else //if (!Utilities.checkLocation(location, entity.getLocation(), 20)) {
// TODO: generate waypoints to the target location?
{
entity.teleport(location);
}
}
use of org.bukkit.scheduler.BukkitRunnable in project Denizen-For-Bukkit by DenizenScript.
the class EntityHelper_v1_10_R1 method follow.
@Override
public void follow(final Entity target, final Entity follower, final double speed, final double lead, final double maxRange, final boolean allowWander) {
if (target == null || follower == null) {
return;
}
final net.minecraft.server.v1_10_R1.Entity nmsEntityFollower = ((CraftEntity) follower).getHandle();
if (!(nmsEntityFollower instanceof EntityInsentient)) {
return;
}
final EntityInsentient nmsFollower = (EntityInsentient) nmsEntityFollower;
final NavigationAbstract followerNavigation = nmsFollower.getNavigation();
UUID uuid = follower.getUniqueId();
if (followTasks.containsKey(uuid)) {
followTasks.get(uuid).cancel();
}
final int locationNearInt = (int) Math.floor(lead);
final boolean hasMax = maxRange > lead;
followTasks.put(follower.getUniqueId(), new BukkitRunnable() {
private boolean inRadius = false;
public void run() {
if (!target.isValid() || !follower.isValid()) {
this.cancel();
}
followerNavigation.a(2F);
Location targetLocation = target.getLocation();
PathEntity path;
if (hasMax && !Utilities.checkLocation(targetLocation, follower.getLocation(), maxRange) && !target.isDead() && target.isOnGround()) {
if (!inRadius) {
follower.teleport(Utilities.getWalkableLocationNear(targetLocation, locationNearInt));
} else {
inRadius = false;
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
}
} else if (!inRadius && !Utilities.checkLocation(targetLocation, follower.getLocation(), lead)) {
path = followerNavigation.a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
if (path != null) {
followerNavigation.a(path, 1D);
followerNavigation.a(2D);
}
} else {
inRadius = true;
}
if (inRadius && !allowWander) {
followerNavigation.o();
}
nmsFollower.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
}
}.runTaskTimer(NMSHandler.getJavaPlugin(), 0, 10));
}
Aggregations