use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class SpawnCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
dLocation location = (dLocation) scriptEntry.getObject("location");
dEntity target = (dEntity) scriptEntry.getObject("target");
Element spread = scriptEntry.getElement("spread");
boolean persistent = scriptEntry.hasObject("persistent");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("entities", entities.toString()) + location.debug() + (spread != null ? spread.debug() : "") + (target != null ? target.debug() : "") + (persistent ? aH.debugObj("persistent", "true") : ""));
// Keep a dList of entities that can be called using <entry[name].spawned_entities>
// later in the script queue
dList entityList = new dList();
for (dEntity entity : entities) {
Location loc = location.clone();
if (spread != null) {
loc.add(CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt(), 0, CoreUtilities.getRandom().nextInt(spread.asInt() * 2) - spread.asInt());
}
entity.spawnAt(loc);
// 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 (persistent && entity.isLivingEntity()) {
entity.getLivingEntity().setRemoveWhenFarAway(false);
}
if (target != null && target.isLivingEntity()) {
entity.target(target.getLivingEntity());
}
}
// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("spawned_entities", entityList);
}
use of org.bukkit.Location 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.Location in project Dragonet-Legacy by DragonetMC.
the class MovePlayerPacketTranslator method handleSpecific.
@Override
public Message[] handleSpecific(MovePlayerPacket packet) {
Location loc = new Location(this.getSession().getPlayer().getWorld(), packet.x, packet.y, packet.z);
if (!this.getSession().validateMovement(loc)) {
//Revert
this.getSession().sendPosition();
System.out.println("Reverted movement! ");
return null;
}
//Hack ;P
((DragonetPlayer) this.getSession().getPlayer()).setLocation(new Location(((DragonetPlayer) this.getSession().getPlayer()).getWorld(), packet.x, packet.y, packet.z, packet.yaw, packet.pitch));
return new Message[] { new PlayerPositionLookMessage(false, (double) packet.x, (double) packet.y, (double) packet.z, packet.yaw, packet.pitch) };
}
use of org.bukkit.Location in project Towny by ElgarL.
the class TownCommand method townSpawn.
/**
* Core spawn function to allow admin use.
*
* @param player
* @param split
* @param town
* @param notAffordMSG
* @param outpost
*/
public static void townSpawn(Player player, String[] split, Town town, String notAffordMSG, Boolean outpost) {
try {
boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
Location spawnLoc;
TownSpawnLevel townSpawnPermission;
if (outpost) {
if (!town.hasOutpostSpawn())
throw new TownyException(TownySettings.getLangString("msg_err_outpost_spawn"));
Integer index;
try {
index = Integer.parseInt(split[split.length - 1]);
} catch (NumberFormatException e) {
// invalid entry so assume the first outpost
index = 1;
} catch (ArrayIndexOutOfBoundsException i) {
// Number not present so assume the first outpost.
index = 1;
}
spawnLoc = town.getOutpostSpawn(Math.max(1, index));
} else
spawnLoc = town.getSpawn();
// Determine conditions
if (isTownyAdmin) {
townSpawnPermission = TownSpawnLevel.ADMIN;
} else if ((split.length == 0) && (!outpost)) {
townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
} else {
// split.length > 1
if (!resident.hasTown()) {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
} else if (resident.getTown() == town) {
townSpawnPermission = outpost ? TownSpawnLevel.TOWN_RESIDENT_OUTPOST : TownSpawnLevel.TOWN_RESIDENT;
} else if (resident.hasNation() && town.hasNation()) {
Nation playerNation = resident.getTown().getNation();
Nation targetNation = town.getNation();
if (playerNation == targetNation) {
townSpawnPermission = TownSpawnLevel.PART_OF_NATION;
} else if (targetNation.hasEnemy(playerNation)) {
// Prevent enemies from using spawn travel.
throw new TownyException(TownySettings.getLangString("msg_err_public_spawn_enemy"));
} else if (targetNation.hasAlly(playerNation)) {
townSpawnPermission = TownSpawnLevel.NATION_ALLY;
} else {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
}
} else {
townSpawnPermission = TownSpawnLevel.UNAFFILIATED;
}
}
TownyMessaging.sendDebugMsg(townSpawnPermission.toString() + " " + townSpawnPermission.isAllowed());
townSpawnPermission.checkIfAllowed(plugin, player);
// Check the permissions
if (!(isTownyAdmin || ((townSpawnPermission == TownSpawnLevel.UNAFFILIATED) ? town.isPublic() : townSpawnPermission.hasPermissionNode(plugin, player)))) {
throw new TownyException(TownySettings.getLangString("msg_err_not_public"));
}
if (!isTownyAdmin) {
// Prevent spawn travel while in disallowed zones (if
// configured)
List<String> disallowedZones = TownySettings.getDisallowedTownSpawnZones();
if (!disallowedZones.isEmpty()) {
String inTown = null;
try {
Location loc = plugin.getCache(player).getLastLocation();
inTown = TownyUniverse.getTownName(loc);
} catch (NullPointerException e) {
inTown = TownyUniverse.getTownName(player.getLocation());
}
if (inTown == null && disallowedZones.contains("unclaimed"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "the Wilderness"));
if (inTown != null && resident.hasNation() && TownyUniverse.getDataSource().getTown(inTown).hasNation()) {
Nation inNation = TownyUniverse.getDataSource().getTown(inTown).getNation();
Nation playerNation = resident.getTown().getNation();
if (inNation.hasEnemy(playerNation) && disallowedZones.contains("enemy"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Enemy areas"));
if (!inNation.hasAlly(playerNation) && !inNation.hasEnemy(playerNation) && disallowedZones.contains("neutral"))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_town_spawn_disallowed_from"), "Neutral towns"));
}
}
}
double travelCost = townSpawnPermission.getCost();
// Check if need/can pay
if (travelCost > 0 && TownySettings.isUsingEconomy() && (resident.getHoldingBalance() < travelCost))
throw new TownyException(notAffordMSG);
// Used later to make sure the chunk we teleport to is loaded.
Chunk chunk = spawnLoc.getChunk();
// Essentials tests
boolean UsingESS = plugin.isEssentials();
if (UsingESS && !isTownyAdmin) {
try {
User user = plugin.getEssentials().getUser(player);
if (!user.isJailed()) {
Teleport teleport = user.getTeleport();
if (!chunk.isLoaded())
chunk.load();
// Cause an essentials exception if in cooldown.
teleport.cooldown(true);
teleport.teleport(spawnLoc, null);
}
} catch (Exception e) {
TownyMessaging.sendErrorMsg(player, "Error: " + e.getMessage());
// cooldown?
return;
}
}
// travel.
if (travelCost > 0 && TownySettings.isUsingEconomy() && resident.payTo(travelCost, town, String.format("Town Spawn (%s)", townSpawnPermission))) {
// +
TownyMessaging.sendMsg(player, String.format(TownySettings.getLangString("msg_cost_spawn"), TownyEconomyHandler.getFormattedBalance(travelCost)));
// TownyEconomyObject.getEconomyCurrency()));
}
// If an Admin or Essentials teleport isn't being used, use our own.
if (isTownyAdmin) {
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded())
chunk.load();
player.teleport(spawnLoc, TeleportCause.COMMAND);
return;
}
if (!UsingESS) {
if (TownyTimerHandler.isTeleportWarmupRunning()) {
// Use teleport warmup
player.sendMessage(String.format(TownySettings.getLangString("msg_town_spawn_warmup"), TownySettings.getTeleportWarmupTime()));
plugin.getTownyUniverse().requestTeleport(player, spawnLoc, travelCost);
} else {
// Don't use teleport warmup
if (player.getVehicle() != null)
player.getVehicle().eject();
if (!chunk.isLoaded())
chunk.load();
player.teleport(spawnLoc, TeleportCause.COMMAND);
}
}
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
} catch (EconomyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
}
}
use of org.bukkit.Location in project Denizen-For-Bukkit by DenizenScript.
the class ProximityTrigger method onEnable.
@Override
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
final ProximityTrigger trigger = this;
taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
//
// Iterate over all of the NPCs
//
Iterator<dNPC> it = dNPCRegistry.getSpawnedNPCs().iterator();
while (it.hasNext()) {
dNPC npc = it.next();
if (npc == null) {
continue;
}
if (npc.getCitizen() == null) {
continue;
}
//
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
continue;
}
if (!npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name)) {
continue;
}
if (!npc.isSpawned()) {
continue;
}
// Loop through all players
for (Player BukkitPlayer : Bukkit.getOnlinePlayers()) {
//
if (!npc.getWorld().equals(BukkitPlayer.getWorld()) && hasExitedProximityOf(BukkitPlayer, npc)) {
continue;
}
//
if (!isCloseEnough(BukkitPlayer, npc) && hasExitedProximityOf(BukkitPlayer, npc)) {
continue;
}
// Get the player
dPlayer player = dPlayer.mirrorBukkitPlayer(BukkitPlayer);
//
// Check to make sure the NPC has an assignment. If no assignment, a script doesn't need to be parsed,
// but it does still need to trigger for cooldown and action purposes.
//
InteractScriptContainer script = npc.getInteractScriptQuietly(player, ProximityTrigger.class);
//
// Set default ranges with information from the TriggerTrait. This allows per-npc overrides and will
// automatically check the config for defaults.
//
double entryRadius = npc.getTriggerTrait().getRadius(name);
double exitRadius = npc.getTriggerTrait().getRadius(name);
double moveRadius = npc.getTriggerTrait().getRadius(name);
//
if (script != null) {
try {
if (script.hasTriggerOptionFor(ProximityTrigger.class, player, null, "ENTRY RADIUS")) {
entryRadius = Integer.valueOf(script.getTriggerOptionFor(ProximityTrigger.class, player, null, "ENTRY RADIUS"));
}
} catch (NumberFormatException nfe) {
dB.echoDebug(script, "Entry Radius was not an integer. Assuming " + entryRadius + " as the radius.");
}
try {
if (script.hasTriggerOptionFor(ProximityTrigger.class, player, null, "EXIT RADIUS")) {
exitRadius = Integer.valueOf(script.getTriggerOptionFor(ProximityTrigger.class, player, null, "EXIT RADIUS"));
}
} catch (NumberFormatException nfe) {
dB.echoDebug(script, "Exit Radius was not an integer. Assuming " + exitRadius + " as the radius.");
}
try {
if (script.hasTriggerOptionFor(ProximityTrigger.class, player, null, "MOVE RADIUS")) {
moveRadius = Integer.valueOf(script.getTriggerOptionFor(ProximityTrigger.class, player, null, "MOVE RADIUS"));
}
} catch (NumberFormatException nfe) {
dB.echoDebug(script, "Move Radius was not an integer. Assuming " + moveRadius + " as the radius.");
}
}
Location npcLocation = npc.getLocation();
//
// If the Player switches worlds while in range of an NPC, trigger still needs to
// fire since technically they have exited proximity. Let's check that before
// trying to calculate a distance between the Player and NPC, which will throw
// an exception if worlds do not match.
//
boolean playerChangedWorlds = false;
if (npcLocation.getWorld() != player.getWorld()) {
playerChangedWorlds = true;
}
//
// If the user is outside the range, and was previously within the
// range, then execute the "Exit" script.
//
// If the user entered the range and were not previously within the
// range, then execute the "Entry" script.
//
// If the user was previously within the range and moved, then execute
// the "Move" script.
//
boolean exitedProximity = hasExitedProximityOf(BukkitPlayer, npc);
double distance = 0;
if (!playerChangedWorlds) {
distance = npcLocation.distance(player.getLocation());
}
if (!exitedProximity && (playerChangedWorlds || distance >= exitRadius)) {
if (!npc.getTriggerTrait().triggerCooldownOnly(trigger, player)) {
continue;
}
// Remember that NPC has exited proximity.
exitProximityOf(BukkitPlayer, npc);
// Exit Proximity Action
npc.action("exit proximity", player);
// Parse Interact Script
parse(npc, player, script, "EXIT");
} else if (exitedProximity && distance <= entryRadius) {
// Cooldown
if (!npc.getTriggerTrait().triggerCooldownOnly(trigger, player)) {
continue;
}
// Remember that Player has entered proximity of the NPC
enterProximityOf(BukkitPlayer, npc);
// Enter Proximity Action
npc.action("enter proximity", player);
// Parse Interact Script
parse(npc, player, script, "ENTRY");
} else if (!exitedProximity && distance <= moveRadius) {
// TODO: Remove this? Constantly cooling down on move may make
// future entry/exit proximities 'lag' behind. Temporarily removing
// cooldown on 'move proximity'.
// if (!npc.getTriggerTrait().triggerCooldownOnly(this, event.getPlayer()))
// continue;
// Move Proximity Action
npc.action("move proximity", player);
// Parse Interact Script
parse(npc, player, script, "MOVE");
}
}
}
}
}, 5, 5);
}
Aggregations