use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.
the class TownyBlockListener method onBlockPlace.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
Player player = event.getPlayer();
Block block = event.getBlock();
WorldCoord worldCoord;
try {
TownyWorld world = TownyUniverse.getDataSource().getWorld(block.getWorld().getName());
worldCoord = new WorldCoord(world.getName(), Coord.parseCoord(block));
//Get build permissions (updates if none exist)
boolean bBuild = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.BUILD);
// Allow build if we are permitted
if (bBuild)
return;
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
TownBlockStatus status = cache.getStatus();
/*
* Flag war
*/
if (((status == TownBlockStatus.ENEMY) && TownyWarConfig.isAllowingAttacks()) && (event.getBlock().getType() == TownyWarConfig.getFlagBaseMaterial())) {
try {
if (TownyWar.callAttackCellEvent(plugin, player, block, worldCoord))
return;
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
}
event.setBuild(false);
event.setCancelled(true);
} else if (status == TownBlockStatus.WARZONE) {
if (!TownyWarConfig.isEditableMaterialInWarZone(block.getType())) {
event.setBuild(false);
event.setCancelled(true);
TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_warzone_cannot_edit_material"), "build", block.getType().toString().toLowerCase()));
}
return;
} else {
event.setBuild(false);
event.setCancelled(true);
}
/*
* display any error recorded for this plot
*/
if ((cache.hasBlockErrMsg()) && (event.isCancelled()))
TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
} catch (NotRegisteredException e1) {
TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
event.setCancelled(true);
}
}
use of com.palmergames.bukkit.towny.object.TownyWorld 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.TownyWorld in project Towny by ElgarL.
the class TownyEntityListener method onEntityInteract.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityInteract(EntityInteractEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
Block block = event.getBlock();
Entity entity = event.getEntity();
Entity passenger = entity.getPassenger();
TownyWorld World = null;
try {
World = TownyUniverse.getDataSource().getWorld(block.getLocation().getWorld().getName());
if (!World.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// World not registered with Towny.
e.printStackTrace();
return;
}
try {
TownyWorld townyWorld = TownyUniverse.getDataSource().getWorld(block.getLocation().getWorld().getName());
if (townyWorld.isUsingTowny()) {
// Prevent creatures trampling crops
if (townyWorld.isDisableCreatureTrample()) {
if ((block.getType() == Material.SOIL) || (block.getType() == Material.CROPS)) {
if (entity instanceof Creature) {
event.setCancelled(true);
return;
}
}
}
/*
* Allow players in vehicles to activate pressure plates if they
* are permitted.
*/
if (passenger != null && passenger instanceof Player) {
if (TownySettings.isSwitchMaterial(block.getType().name())) {
if (!plugin.getPlayerListener().onPlayerSwitchEvent((Player) passenger, block, null, World))
return;
}
}
// Prevent creatures triggering stone pressure plates
if (TownySettings.isCreatureTriggeringPressurePlateDisabled()) {
if (block.getType() == Material.STONE_PLATE) {
if (entity instanceof Creature) {
event.setCancelled(true);
return;
}
}
}
}
} catch (NotRegisteredException e) {
// Failed to fetch world
e.printStackTrace();
}
}
use of com.palmergames.bukkit.towny.object.TownyWorld 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.TownyWorld in project Towny by ElgarL.
the class TownyEntityListener method onEntityDamageByEntityEvent.
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
if (plugin.isError()) {
return;
}
TownyWorld townyWorld = null;
Entity entity = event.getEntity();
if (entity instanceof ArmorStand) {
String damager = event.getDamager().getType().name();
if (damager == "PRIMED_TNT" || damager == "WITHER_SKULL" || damager == "FIREBALL" || damager == "SMALL_FIREBALL" || damager == "LARGE_FIREBALL" || damager == "WITHER") {
try {
townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!locationCanExplode(townyWorld, entity.getLocation())) {
event.setCancelled(true);
return;
}
}
if (event.getDamager() instanceof Projectile) {
try {
townyWorld = TownyUniverse.getDataSource().getWorld(entity.getWorld().getName());
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object remover = event.getDamager();
remover = ((Projectile) remover).getShooter();
if (remover instanceof Monster) {
event.setCancelled(true);
} else if (remover instanceof Player) {
Player player = (Player) remover;
// Get destroy permissions (updates if none exist)
boolean bDestroy = PlayerCacheUtil.getCachePermission(player, entity.getLocation(), 416, (byte) 0, TownyPermission.ActionType.DESTROY);
// Allow the removal if we are permitted
if (bDestroy)
return;
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
event.setCancelled(true);
}
}
}
}
Aggregations