use of org.bukkit.Chunk in project NoCheatPlus by NoCheatPlus.
the class MovingUtil method checkUntrackedLocation.
/**
* Detect if the given location is an untracked spot. This is spots for
* which a player is at the location, but the moving data has another
* "last to" position set for that player. Note that one matching player
* with "last to" being consistent is enough to let this return null, world spawn is exempted.
* <hr>
* Pre-conditions:<br>
* <li>Context-specific (e.g. activation flags for command, teleport).</li>
* <li>See MovingUtils.shouldCheckUntrackedLocation.</li>
*
* @param loc
* @return Corrected location, if loc is an "untracked location".
*/
public static Location checkUntrackedLocation(final Location loc) {
// TODO: More efficient method to get entities at the same position (might use MCAccess).
final Chunk toChunk = loc.getChunk();
final Entity[] entities = toChunk.getEntities();
MovingData untrackedData = null;
for (int i = 0; i < entities.length; i++) {
final Entity entity = entities[i];
if (entity.getType() != EntityType.PLAYER) {
continue;
}
final Location refLoc = entity.getLocation(useLoc);
// TODO: Exempt other warps -> HASH based exemption (expire by time, keep high count)?
if (TrigUtil.isSamePos(loc, refLoc) && (entity instanceof Player)) {
final Player other = (Player) entity;
final IPlayerData otherPData = DataManager.getPlayerData(other);
final MovingData otherData = otherPData.getGenericInstance(MovingData.class);
final PlayerMoveData otherLastMove = otherData.playerMoves.getFirstPastMove();
if (!otherLastMove.toIsValid) {
// TODO: Consider counting as tracked?
continue;
} else if (TrigUtil.isSamePos(refLoc, otherLastMove.to.getX(), otherLastMove.to.getY(), otherLastMove.to.getZ())) {
// Tracked.
return null;
} else {
// More leniency: allow moving inside of the same block.
if (TrigUtil.isSameBlock(loc, otherLastMove.to.getX(), otherLastMove.to.getY(), otherLastMove.to.getZ()) && !BlockProperties.isPassable(refLoc.getWorld(), otherLastMove.to.getX(), otherLastMove.to.getY(), otherLastMove.to.getZ())) {
continue;
}
untrackedData = otherData;
}
}
}
// Cleanup.
useLoc.setWorld(null);
if (untrackedData == null) {
return null;
} else {
// TODO: Count and log to TRACE_FILE, if multiple locations would match (!).
final PlayerMoveData lastMove = untrackedData.playerMoves.getFirstPastMove();
return new Location(loc.getWorld(), lastMove.to.getX(), lastMove.to.getY(), lastMove.to.getZ(), loc.getYaw(), loc.getPitch());
}
}
use of org.bukkit.Chunk in project Towny by ElgarL.
the class ResidentCommand method residentSpawn.
/**
* Attempt to send player to bed spawn.
*
* @param player
*/
public void residentSpawn(Player player) {
boolean isTownyAdmin = TownyUniverse.getPermissionSource().isTownyAdmin(player);
Resident resident;
try {
resident = TownyUniverse.getDataSource().getResident(player.getName());
Town town;
Location spawnLoc;
String notAffordMSG;
TownSpawnLevel townSpawnPermission;
// Set target town and affiliated messages.
town = resident.getTown();
notAffordMSG = TownySettings.getLangString("msg_err_cant_afford_tp");
if (TownySettings.getBedUse() && player.getBedSpawnLocation() != null) {
spawnLoc = player.getBedSpawnLocation();
} else {
spawnLoc = town.getSpawn();
}
if (isTownyAdmin) {
townSpawnPermission = TownSpawnLevel.ADMIN;
} else {
townSpawnPermission = TownSpawnLevel.TOWN_RESIDENT;
}
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("Resident 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());
}
}
Aggregations