use of com.palmergames.bukkit.towny.object.PlayerCache 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);
}
}
}
}
use of com.palmergames.bukkit.towny.object.PlayerCache 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.PlayerCache 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.object.PlayerCache 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.PlayerCache in project Towny by ElgarL.
the class PlayerCacheUtil method cacheSwitch.
/**
* Update the player cache for Switch rights at this WorldCoord.
*
* @param player
* @param worldCoord
* @param id
* @param data
* @param switchRight
*/
private static void cacheSwitch(Player player, WorldCoord worldCoord, Integer id, byte data, Boolean switchRight) {
PlayerCache cache = plugin.getCache(player);
cache.updateCoord(worldCoord);
cache.setSwitchPermission(id, data, switchRight);
TownyMessaging.sendDebugMsg(player.getName() + " (" + worldCoord.toString() + ") Cached Switch: " + switchRight);
}
Aggregations