use of com.palmergames.bukkit.towny.regen.block.BlockLocation in project Towny by ElgarL.
the class TownyBlockPhysicsListener method onBlockPhysics.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockPhysics(BlockPhysicsEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
/**
* Exit if the regen timer is zero.
*/
if (TownySettings.getRegenDelay() <= 0)
return;
// long start = System.currentTimeMillis();
Block block = event.getBlock();
if (block == null)
return;
BlockLocation blockLocation = new BlockLocation(block.getLocation());
// if this is a placeholder remove it, as it's no longer needed.
if (TownyRegenAPI.isPlaceholder(block)) {
TownyRegenAPI.removePlaceholder(block);
BukkitTools.setTypeId(block, 0, false);
}
if (TownyRegenAPI.hasProtectionRegenTask(blockLocation)) {
// Cancel any physics events as we will be replacing this block
event.setCancelled(true);
} else {
// Check the block below and cancel the event if that block is going to be replaced.
Block blockBelow = block.getRelative(BlockFace.DOWN);
blockLocation = new BlockLocation(blockBelow.getLocation());
if (TownyRegenAPI.hasProtectionRegenTask(blockLocation) && (NeedsPlaceholder.contains(block.getType()))) {
// System.out.print("Cancelling for Below on - " + block.getType().toString());
event.setCancelled(true);
}
}
// plugin.sendDebugMsg("onBlockPhysics took " + (System.currentTimeMillis() - start) + "ms ("+event.isCancelled() +")");
}
use of com.palmergames.bukkit.towny.regen.block.BlockLocation 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.regen.block.BlockLocation in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerMove.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
/*
* Abort if we havn't really moved
*/
if (event.getFrom().getBlockX() == event.getTo().getBlockX() && event.getFrom().getBlockZ() == event.getTo().getBlockZ() && event.getFrom().getBlockY() == event.getTo().getBlockY()) {
return;
}
Player player = event.getPlayer();
Location to = event.getTo();
Location from;
PlayerCache cache = plugin.getCache(player);
try {
from = cache.getLastLocation();
} catch (NullPointerException e) {
from = event.getFrom();
}
// Prevent fly/double jump cheats
if (!(event instanceof PlayerTeleportEvent)) {
if (TownySettings.isUsingCheatProtection() && (player.getGameMode() != GameMode.CREATIVE) && !TownyUniverse.getPermissionSource().has(player, PermissionNodes.CHEAT_BYPASS.getNode())) {
try {
if (TownyUniverse.getDataSource().getWorld(player.getWorld().getName()).isUsingTowny())
if ((from.getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) && (player.getFallDistance() == 0) && (player.getVelocity().getY() <= -0.6) && (player.getLocation().getY() > 0)) {
// plugin.sendErrorMsg(player, "Cheat Detected!");
Location blockLocation = from;
// find the first non air block below us
while ((blockLocation.getBlock().getType() == Material.AIR) && (blockLocation.getY() > 0)) blockLocation.setY(blockLocation.getY() - 1);
// set to 1 block up so we are not sunk in the
// ground
blockLocation.setY(blockLocation.getY() + 1);
// Update the cache for this location (same
// WorldCoord).
cache.setLastLocation(blockLocation);
player.teleport(blockLocation);
return;
}
} catch (NotRegisteredException e1) {
TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
return;
}
}
}
try {
TownyWorld fromWorld = TownyUniverse.getDataSource().getWorld(from.getWorld().getName());
WorldCoord fromCoord = new WorldCoord(fromWorld.getName(), Coord.parseCoord(from));
TownyWorld toWorld = TownyUniverse.getDataSource().getWorld(to.getWorld().getName());
WorldCoord toCoord = new WorldCoord(toWorld.getName(), Coord.parseCoord(to));
if (!fromCoord.equals(toCoord))
onPlayerMoveChunk(player, fromCoord, toCoord, from, to, event);
else {
// plugin.sendDebugMsg(" From: " + fromCoord);
// plugin.sendDebugMsg(" To: " + toCoord);
// plugin.sendDebugMsg(" " + from.toString());
// plugin.sendDebugMsg(" " + to.toString());
}
} catch (NotRegisteredException e) {
TownyMessaging.sendErrorMsg(player, e.getMessage());
}
// Update the cached players current location
cache.setLastLocation(to);
// plugin.updateCache(player);
// plugin.sendDebugMsg("onBlockMove: " + player.getName() + ": ");
// plugin.sendDebugMsg(" " + from.toString());
// plugin.sendDebugMsg(" " + to.toString());
}
use of com.palmergames.bukkit.towny.regen.block.BlockLocation in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerInteract.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
Player player = event.getPlayer();
Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
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;
}
if ((event.getAction() == Action.PHYSICAL)) {
if ((block.getType() == Material.SOIL) || (block.getType() == Material.CROPS))
if (World.isDisablePlayerTrample() || !PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.DESTROY)) {
event.setCancelled(true);
return;
}
}
if (event.hasItem()) {
/*
* Info Tool
*/
if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
if (TownyUniverse.getPermissionSource().isTownyAdmin(player)) {
if (event.getClickedBlock() instanceof Block) {
block = (Block) event.getClickedBlock();
TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Block Info"), ChatTools.formatCommand("", "Block Type", "", block.getType().name()), ChatTools.formatCommand("", "Data value", "", Byte.toString(BukkitTools.getData(block)))));
event.setCancelled(true);
}
}
}
if (TownySettings.isItemUseMaterial(event.getItem().getType().name())) {
event.setCancelled(onPlayerInteract(player, event.getClickedBlock(), event.getItem()));
}
}
if (event.getClickedBlock() != null) {
// Towny regen
if (TownySettings.getRegenDelay() > 0) {
if (event.getClickedBlock().getState().getData() instanceof Attachable) {
Attachable attachable = (Attachable) event.getClickedBlock().getState().getData();
BlockLocation attachedToBlock = new BlockLocation(event.getClickedBlock().getRelative(attachable.getAttachedFace()).getLocation());
// Prevent attached blocks from falling off when interacting
if (TownyRegenAPI.hasProtectionRegenTask(attachedToBlock)) {
event.setCancelled(true);
return;
}
}
}
if (TownySettings.isSwitchMaterial(event.getClickedBlock().getType().name()) || event.getAction() == Action.PHYSICAL) {
onPlayerSwitchEvent(event, null, World);
return;
}
}
}
use of com.palmergames.bukkit.towny.regen.block.BlockLocation in project Towny by ElgarL.
the class TownyBlockListener method onBlockBreak.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
Player player = event.getPlayer();
Block block = event.getBlock();
// Get build permissions (updates cache if none exist)
boolean bDestroy = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.DESTROY);
// Allow destroy if we are permitted
if (bDestroy)
return;
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
/*
* Allow destroy in a WarZone (FlagWar) if it's an editable material.
*/
if (cache.getStatus() == TownBlockStatus.WARZONE) {
if (!TownyWarConfig.isEditableMaterialInWarZone(block.getType())) {
event.setCancelled(true);
TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_warzone_cannot_edit_material"), "destroy", block.getType().toString().toLowerCase()));
}
return;
}
/*
* Queue a protectionRegenTask if we have delayed regeneration set
*/
long delay = TownySettings.getRegenDelay();
if (delay > 0) {
if (!TownyRegenAPI.isPlaceholder(block)) {
if (!TownyRegenAPI.hasProtectionRegenTask(new BlockLocation(block.getLocation()))) {
ProtectionRegenTask task = new ProtectionRegenTask(plugin, block, true);
task.setTaskId(plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, task, 20 * delay));
TownyRegenAPI.addProtectionRegenTask(task);
}
} else {
TownyRegenAPI.removePlaceholder(block);
BukkitTools.setTypeId(block, 0, false);
}
} else {
event.setCancelled(true);
}
/*
* display any error recorded for this plot
*/
if ((cache.hasBlockErrMsg()) && (event.isCancelled()))
TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
}
Aggregations