use of com.palmergames.bukkit.towny.object.Coord in project Towny by ElgarL.
the class TownyEntityListener method onEntityExplode.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
TownyWorld townyWorld;
/**
* Perform this test outside the block loop so we only get the world
* once per explosion.
*/
try {
townyWorld = TownyUniverse.getDataSource().getWorld(event.getLocation().getWorld().getName());
if (!townyWorld.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// failed to get world so abort
return;
}
Coord coord;
List<Block> blocks = event.blockList();
Entity entity = event.getEntity();
int count = 0;
// Sort blocks by height (lowest to highest).
Collections.sort(blocks, ArraySort.getInstance());
for (Block block : blocks) {
coord = Coord.parseCoord(block.getLocation());
count++;
// Warzones
if (townyWorld.isWarZone(coord)) {
if (!TownyWarConfig.isAllowingExplosionsInWarZone()) {
if (event.getEntity() != null)
TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
event.setCancelled(true);
return;
} else {
if (TownyWarConfig.explosionsBreakBlocksInWarZone()) {
if (TownyWarConfig.regenBlocksAfterExplosionInWarZone()) {
// ***********************************
// TODO
// On completion, remove TODO from config.yml
// comments.
/*
* if
* (!plugin.getTownyUniverse().hasProtectionRegenTask
* (new BlockLocation(block.getLocation()))) {
* ProtectionRegenTask task = new
* ProtectionRegenTask(plugin.getTownyUniverse(),
* block, false);
* task.setTaskId(plugin.getServer().getScheduler().
* scheduleSyncDelayedTask(plugin, task,
* ((TownySettings.getPlotManagementWildRegenDelay()
* + count)*20)));
* plugin.getTownyUniverse().addProtectionRegenTask
* (task ); }
*/
// TODO
// ***********************************
}
// Break the block
} else {
event.blockList().remove(block);
}
}
return;
}
// TODO: expand to protect neutrals during a war
try {
TownBlock townBlock = townyWorld.getTownBlock(coord);
// and the towns has no nation
if (townyWorld.isUsingTowny() && !townyWorld.isForceExpl()) {
if ((!townBlock.getPermissions().explosion) || (TownyUniverse.isWarTime() && TownySettings.isAllowWarBlockGriefing() && !townBlock.getTown().hasNation() && !townBlock.getTown().isBANG())) {
if (event.getEntity() != null)
TownyMessaging.sendDebugMsg("onEntityExplode: Canceled " + event.getEntity().getEntityId() + " from exploding within " + coord.toString() + ".");
event.setCancelled(true);
return;
}
}
} catch (TownyException x) {
// Wilderness explosion regeneration
if (townyWorld.isUsingTowny())
if (townyWorld.isExpl()) {
if (townyWorld.isUsingPlotManagementWildRevert() && (entity != null)) {
TownyMessaging.sendDebugMsg("onEntityExplode: Testing entity: " + entity.getType().getEntityClass().getSimpleName().toLowerCase() + " @ " + coord.toString() + ".");
if (townyWorld.isProtectingExplosionEntity(entity)) {
if ((!TownyRegenAPI.hasProtectionRegenTask(new BlockLocation(block.getLocation()))) && (block.getType() != Material.TNT)) {
ProtectionRegenTask task = new ProtectionRegenTask(plugin, block, false);
task.setTaskId(plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, ((TownySettings.getPlotManagementWildRegenDelay() + count) * 20)));
TownyRegenAPI.addProtectionRegenTask(task);
event.setYield((float) 0.0);
block.getDrops().clear();
}
}
}
} else {
event.setCancelled(true);
return;
}
}
}
}
use of com.palmergames.bukkit.towny.object.Coord in project Towny by ElgarL.
the class TownyEntityListener method onCreatureSpawn.
/**
* @param event
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
if (event.getEntity() instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) event.getEntity();
Location loc = event.getLocation();
Coord coord = Coord.parseCoord(loc);
TownyWorld townyWorld = null;
try {
townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
} catch (NotRegisteredException e) {
// Failed to fetch a world
return;
}
// remove from world if set to remove mobs globally
if (townyWorld.isUsingTowny())
if (!townyWorld.hasWorldMobs() && ((MobRemovalTimerTask.isRemovingWorldEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesWorld()))))) {
if (plugin.isCitizens2()) {
if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
// TownyMessaging.sendDebugMsg("onCreatureSpawn world: Canceled "
// + event.getEntityType().name() +
// " from spawning within "+coord.toString()+".");
event.setCancelled(true);
}
} else
event.setCancelled(true);
}
// remove from towns if in the list and set to remove
try {
TownBlock townBlock = townyWorld.getTownBlock(coord);
if (townyWorld.isUsingTowny() && !townyWorld.isForceTownMobs()) {
if (!townBlock.getTown().hasMobs() && !townBlock.getPermissions().mobs) {
if ((MobRemovalTimerTask.isRemovingTownEntity(livingEntity) || ((livingEntity instanceof Villager) && !((Villager) livingEntity).isAdult() && (TownySettings.isRemovingVillagerBabiesTown())))) {
if (plugin.isCitizens2()) {
if (!CitizensAPI.getNPCRegistry().isNPC(livingEntity)) {
// TownyMessaging.sendDebugMsg("onCreatureSpawn town: Canceled "
// + event.getEntityType().name() +
// " from spawning within "+coord.toString()+".");
event.setCancelled(true);
}
} else
event.setCancelled(true);
}
}
}
} catch (TownyException x) {
}
}
}
use of com.palmergames.bukkit.towny.object.Coord in project Towny by ElgarL.
the class CombatUtil method preventDamageCall.
/**
* Tests the attacker against defender to see if we need to cancel
* the damage event due to world PvP, Plot PvP or Friendly Fire settings.
* Only allow a Wolves owner to cause it damage, and residents with destroy
* permissions to damage passive animals and villagers while in a town.
*
* @param world
* @param attackingEntity
* @param defendingEntity
* @param attackingPlayer
* @param defendingPlayer
* @return true if we should cancel.
*/
public static boolean preventDamageCall(Towny plugin, TownyWorld world, Entity attackingEntity, Entity defendingEntity, Player attackingPlayer, Player defendingPlayer) {
// World using Towny
if (!world.isUsingTowny())
return false;
/*
* We have an attacking player
*/
if (attackingPlayer != null) {
Coord coord = Coord.parseCoord(defendingEntity);
TownBlock defenderTB = null;
TownBlock attackerTB = null;
try {
attackerTB = world.getTownBlock(Coord.parseCoord(attackingEntity));
} catch (NotRegisteredException ex) {
}
try {
defenderTB = world.getTownBlock(coord);
} catch (NotRegisteredException ex) {
}
/*
* If another player is the target
* or
* The target is in a TownBlock and...
* the target is a tame wolf and we are not it's owner
*/
if ((defendingPlayer != null) || ((defenderTB != null) && ((defendingEntity instanceof Wolf) && ((Wolf) defendingEntity).isTamed() && !((Wolf) defendingEntity).getOwner().equals((AnimalTamer) attackingEntity)))) {
/*
* Defending player is in a warzone
*/
if (world.isWarZone(coord))
return false;
/*
* Check for special pvp plots (arena)
*/
if (isPvPPlot(attackingPlayer, defendingPlayer))
return false;
/*
* Check if we are preventing friendly fire between allies
* Check the attackers TownBlock and it's Town for their PvP
* status, else the world.
* Check the defenders TownBlock and it's Town for their PvP
* status, else the world.
*/
if (preventFriendlyFire(attackingPlayer, defendingPlayer) || preventPvP(world, attackerTB) || preventPvP(world, defenderTB)) {
DisallowedPVPEvent event = new DisallowedPVPEvent(attackingPlayer, defendingPlayer);
plugin.getServer().getPluginManager().callEvent(event);
return !event.isCancelled();
}
} else {
/*
* Defender is not a player so check for PvM
*/
if (defenderTB != null) {
List<Class<?>> prots = EntityTypeUtil.parseLivingEntityClassNames(TownySettings.getEntityTypes(), "TownMobPVM:");
if (EntityTypeUtil.isInstanceOfAny(prots, defendingEntity)) {
/*
* Only allow the player to kill protected entities etc,
* if they are from the same town
* and have destroy permissions (grass) in the defending
* TownBlock
*/
if (!PlayerCacheUtil.getCachePermission(attackingPlayer, attackingPlayer.getLocation(), 3, (byte) 0, ActionType.DESTROY))
return true;
}
}
/*
* Remove prevention end
*/
/*
* Protect specific entity interactions (faked with block ID's).
*/
int blockID = 0;
switch(defendingEntity.getType()) {
case ITEM_FRAME:
blockID = 389;
break;
case PAINTING:
blockID = 321;
break;
case MINECART:
if (defendingEntity instanceof org.bukkit.entity.minecart.StorageMinecart) {
blockID = 342;
} else if (defendingEntity instanceof org.bukkit.entity.minecart.RideableMinecart) {
blockID = 328;
} else if (defendingEntity instanceof org.bukkit.entity.minecart.PoweredMinecart) {
blockID = 343;
} else if (defendingEntity instanceof org.bukkit.entity.minecart.HopperMinecart) {
blockID = 408;
} else {
blockID = 321;
}
default:
break;
}
if (blockID != 0) {
// Get permissions (updates if none exist)
boolean bDestroy = PlayerCacheUtil.getCachePermission(attackingPlayer, defendingEntity.getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
if (!bDestroy) {
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(attackingPlayer);
if (cache.hasBlockErrMsg())
TownyMessaging.sendErrorMsg(attackingPlayer, cache.getBlockErrMsg());
return true;
}
}
}
}
return false;
}
use of com.palmergames.bukkit.towny.object.Coord in project Towny by ElgarL.
the class TownyBlockListener method testBlockMove.
private boolean testBlockMove(Block block, BlockFace direction) {
Block blockTo = block.getRelative(direction);
Location loc = block.getLocation();
Location locTo = blockTo.getLocation();
Coord coord = Coord.parseCoord(loc);
Coord coordTo = Coord.parseCoord(locTo);
TownyWorld townyWorld = null;
TownBlock CurrentTownBlock = null, destinationTownBlock = null;
try {
townyWorld = TownyUniverse.getDataSource().getWorld(loc.getWorld().getName());
CurrentTownBlock = townyWorld.getTownBlock(coord);
} catch (NotRegisteredException e) {
// System.out.print("Failed to fetch TownBlock");
}
try {
destinationTownBlock = townyWorld.getTownBlock(coordTo);
} catch (NotRegisteredException e1) {
// System.out.print("Failed to fetch TownBlockTo");
}
if (CurrentTownBlock != destinationTownBlock) {
// Cancel if either is not null, but other is (wild to town).
if (((CurrentTownBlock == null) && (destinationTownBlock != null)) || ((CurrentTownBlock != null) && (destinationTownBlock == null))) {
// event.setCancelled(true);
return true;
}
// If both blocks are owned by the town.
if (!CurrentTownBlock.hasResident() && !destinationTownBlock.hasResident())
return false;
try {
if ((!CurrentTownBlock.hasResident() && destinationTownBlock.hasResident()) || (CurrentTownBlock.hasResident() && !destinationTownBlock.hasResident()) || (CurrentTownBlock.getResident() != destinationTownBlock.getResident()) || (CurrentTownBlock.getPlotPrice() != -1) || (destinationTownBlock.getPlotPrice() != -1)) {
return true;
}
} catch (NotRegisteredException e) {
// Failed to fetch a resident
return true;
}
}
return false;
}
use of com.palmergames.bukkit.towny.object.Coord in project Towny by ElgarL.
the class TownyAsciiMap method generateAndSend.
public static void generateAndSend(Towny plugin, Player player, int lineHeight) {
// Collect Sample Data
boolean hasTown = false;
Resident resident;
try {
resident = TownyUniverse.getDataSource().getResident(player.getName());
if (resident.hasTown())
hasTown = true;
} catch (TownyException x) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
return;
}
TownyWorld world;
try {
world = TownyUniverse.getDataSource().getWorld(player.getWorld().getName());
} catch (NotRegisteredException e1) {
TownyMessaging.sendErrorMsg(player, "You are not in a registered world.");
return;
}
if (!world.isUsingTowny()) {
TownyMessaging.sendErrorMsg(player, "This world is not using towny.");
return;
}
Coord pos = Coord.parseCoord(plugin.getCache(player).getLastLocation());
// Generate Map
int halfLineHeight = lineHeight / 2;
String[][] townyMap = new String[lineWidth][lineHeight];
int x, y = 0;
for (int tby = pos.getX() + (lineWidth - halfLineWidth - 1); tby >= pos.getX() - halfLineWidth; tby--) {
x = 0;
for (int tbx = pos.getZ() - halfLineHeight; tbx <= pos.getZ() + (lineHeight - halfLineHeight - 1); tbx++) {
try {
TownBlock townblock = world.getTownBlock(tby, tbx);
// TODO: possibly claim outside of towns
if (!townblock.hasTown())
throw new TownyException();
if (x == halfLineHeight && y == halfLineWidth)
// location
townyMap[y][x] = Colors.Gold;
else if (hasTown) {
if (resident.getTown() == townblock.getTown()) {
// own town
townyMap[y][x] = Colors.LightGreen;
try {
if (resident == townblock.getResident())
// own plot
townyMap[y][x] = Colors.Yellow;
} catch (NotRegisteredException e) {
}
} else if (resident.hasNation()) {
if (resident.getTown().getNation().hasTown(townblock.getTown()))
// towns
townyMap[y][x] = Colors.Green;
else if (townblock.getTown().hasNation()) {
Nation nation = resident.getTown().getNation();
if (nation.hasAlly(townblock.getTown().getNation()))
townyMap[y][x] = Colors.Green;
else if (nation.hasEnemy(townblock.getTown().getNation()))
// towns
townyMap[y][x] = Colors.Red;
else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
} else
townyMap[y][x] = Colors.White;
// Registered town block
if (townblock.getPlotPrice() != -1) {
// override the colour if it's a shop plot for sale
if (townblock.getType().equals(TownBlockType.COMMERCIAL))
townyMap[y][x] = Colors.Blue;
townyMap[y][x] += "$";
} else if (townblock.isHomeBlock())
townyMap[y][x] += "H";
else
townyMap[y][x] += townblock.getType().getAsciiMapKey();
} catch (TownyException e) {
if (x == halfLineHeight && y == halfLineWidth)
townyMap[y][x] = Colors.Gold;
else
townyMap[y][x] = Colors.Gray;
// Unregistered town block
townyMap[y][x] += "-";
}
x++;
}
y++;
}
String[] compass = generateCompass(player);
// Output
player.sendMessage(ChatTools.formatTitle("Towny Map " + Colors.White + "(" + pos.toString() + ")"));
String line;
int lineCount = 0;
// Variables have been rotated to fit N/S/E/W properly
for (int my = 0; my < lineHeight; my++) {
line = compass[0];
if (lineCount < compass.length)
line = compass[lineCount];
for (int mx = lineWidth - 1; mx >= 0; mx--) line += townyMap[mx][my];
if (lineCount < help.length)
line += help[lineCount];
player.sendMessage(line);
lineCount++;
}
// Current town block data
try {
TownBlock townblock = world.getTownBlock(pos);
TownyMessaging.sendMsg(player, ("Town: " + (townblock.hasTown() ? townblock.getTown().getName() : "None") + " : " + "Owner: " + (townblock.hasResident() ? townblock.getResident().getName() : "None")));
} catch (TownyException e) {
// plugin.sendErrorMsg(player, e.getError());
// Send a blank line instead of an error, to keep the map position tidy.
player.sendMessage("");
}
}
Aggregations