use of com.palmergames.bukkit.towny.exceptions.NotRegisteredException in project Towny by ElgarL.
the class TownyEntityMonitorListener method onEntityDeath.
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(EntityDeathEvent event) {
Entity defenderEntity = event.getEntity();
TownyWorld World = null;
try {
World = TownyUniverse.getDataSource().getWorld(defenderEntity.getLocation().getWorld().getName());
if (!World.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// World not registered with Towny.
return;
}
// Was this a player death?
if (defenderEntity instanceof Player) {
// Killed by another entity?
if (defenderEntity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) defenderEntity.getLastDamageCause();
Entity attackerEntity = damageEvent.getDamager();
Player defenderPlayer = (Player) defenderEntity;
Player attackerPlayer = null;
Resident attackerResident = null;
Resident defenderResident = null;
try {
defenderResident = TownyUniverse.getDataSource().getResident(defenderPlayer.getName());
} catch (NotRegisteredException e) {
return;
}
// Was this a missile?
if (attackerEntity instanceof Projectile) {
Projectile projectile = (Projectile) attackerEntity;
if (projectile.getShooter() instanceof Player) {
attackerPlayer = (Player) projectile.getShooter();
try {
attackerResident = TownyUniverse.getDataSource().getResident(attackerPlayer.getName());
} catch (NotRegisteredException e) {
}
}
} else if (attackerEntity instanceof Player) {
// This was a player kill
attackerPlayer = (Player) attackerEntity;
try {
attackerResident = TownyUniverse.getDataSource().getResident(attackerPlayer.getName());
} catch (NotRegisteredException e) {
}
}
/*
* If attackerPlayer or attackerResident are null at this point
* it was a natural death, not PvP.
*/
deathPayment(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
wartimeDeathPoints(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
if (TownySettings.isRemovingOnMonarchDeath())
monarchDeath(attackerPlayer, defenderPlayer, attackerResident, defenderResident);
}
}
}
use of com.palmergames.bukkit.towny.exceptions.NotRegisteredException 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.exceptions.NotRegisteredException in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerInteractEntity.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractAtEntityEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
if (event.getRightClicked() != null) {
TownyWorld World = null;
try {
World = TownyUniverse.getDataSource().getWorld(event.getPlayer().getWorld().getName());
if (!World.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// World not registered with Towny.
e.printStackTrace();
return;
}
Player player = event.getPlayer();
boolean bBuild = true;
int blockID = 0;
/*
* Protect specific entity interactions.
*/
switch(event.getRightClicked().getType()) {
case ARMOR_STAND:
TownyMessaging.sendDebugMsg("ArmorStand Right Clicked");
blockID = 416;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
break;
default:
break;
}
if (blockID != 0) {
// Allow the removal if we are permitted
if (bBuild)
return;
event.setCancelled(true);
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
if (cache.hasBlockErrMsg())
TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
return;
}
/*
* Item_use protection.
*/
if (event.getPlayer().getItemInHand() != null) {
/*
* Info Tool
*/
if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
Entity entity = event.getRightClicked();
TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Entity Info"), ChatTools.formatCommand("", "Entity Class", "", entity.getType().getEntityClass().getSimpleName())));
event.setCancelled(true);
}
if (TownySettings.isItemUseMaterial(event.getPlayer().getItemInHand().getType().name())) {
event.setCancelled(onPlayerInteract(event.getPlayer(), null, event.getPlayer().getItemInHand()));
return;
}
}
}
}
use of com.palmergames.bukkit.towny.exceptions.NotRegisteredException in project Towny by ElgarL.
the class TownyPlayerListener method onPlayerInteractEntity.
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (plugin.isError()) {
event.setCancelled(true);
return;
}
if (event.getRightClicked() != null) {
TownyWorld World = null;
try {
World = TownyUniverse.getDataSource().getWorld(event.getPlayer().getWorld().getName());
if (!World.isUsingTowny())
return;
} catch (NotRegisteredException e) {
// World not registered with Towny.
e.printStackTrace();
return;
}
Player player = event.getPlayer();
boolean bBuild = true;
int blockID = 0;
/*
* Protect specific entity interactions.
*/
switch(event.getRightClicked().getType()) {
case ITEM_FRAME:
TownyMessaging.sendDebugMsg("ItemFrame Right Clicked");
blockID = 389;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
break;
case PAINTING:
blockID = 321;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.DESTROY);
break;
case MINECART:
if (event.getRightClicked() instanceof org.bukkit.entity.minecart.StorageMinecart) {
blockID = 342;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.RideableMinecart) {
blockID = 328;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.PoweredMinecart) {
blockID = 343;
} else if (event.getRightClicked() instanceof org.bukkit.entity.minecart.HopperMinecart) {
blockID = 408;
} else {
blockID = 321;
}
if ((blockID != 0) && (!TownySettings.isSwitchMaterial(BukkitTools.getMaterial(blockID).name())))
return;
// Get permissions (updates if none exist)
bBuild = PlayerCacheUtil.getCachePermission(player, event.getRightClicked().getLocation(), blockID, (byte) 0, TownyPermission.ActionType.SWITCH);
break;
default:
break;
}
if (blockID != 0) {
// Allow the removal if we are permitted
if (bBuild)
return;
event.setCancelled(true);
/*
* Fetch the players cache
*/
PlayerCache cache = plugin.getCache(player);
if (cache.hasBlockErrMsg())
TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
return;
}
/*
* Item_use protection.
*/
if (event.getPlayer().getItemInHand() != null) {
/*
* Info Tool
*/
if (event.getPlayer().getItemInHand().getType() == Material.getMaterial(TownySettings.getTool())) {
Entity entity = event.getRightClicked();
TownyMessaging.sendMessage(player, Arrays.asList(ChatTools.formatTitle("Entity Info"), ChatTools.formatCommand("", "Entity Class", "", entity.getType().getEntityClass().getSimpleName())));
event.setCancelled(true);
}
if (TownySettings.isItemUseMaterial(event.getPlayer().getItemInHand().getType().name())) {
event.setCancelled(onPlayerInteract(event.getPlayer(), null, event.getPlayer().getItemInHand()));
return;
}
}
}
}
use of com.palmergames.bukkit.towny.exceptions.NotRegisteredException in project Towny by ElgarL.
the class TownyWarCustomListener method onCellDefendedEvent.
@EventHandler(priority = EventPriority.LOWEST)
public void onCellDefendedEvent(CellDefendedEvent event) {
Player player = event.getPlayer();
CellUnderAttack cell = event.getCell().getAttackData();
TownyUniverse universe = plugin.getTownyUniverse();
WorldCoord worldCoord = new WorldCoord(cell.getWorldName(), cell.getX(), cell.getZ());
universe.removeWarZone(worldCoord);
plugin.updateCache(worldCoord);
String playerName;
if (player == null) {
playerName = "Greater Forces";
} else {
playerName = player.getName();
try {
playerName = TownyUniverse.getDataSource().getResident(player.getName()).getFormattedName();
} catch (TownyException e) {
}
}
plugin.getServer().broadcastMessage(String.format(TownySettings.getLangString("msg_enemy_war_area_defended"), playerName, cell.getCellString()));
// Also doesn't take into account of paying as much as the attacker can afford (Eg: cost=10 and balance=9).
if (TownySettings.isUsingEconomy()) {
try {
Resident attackingPlayer, defendingPlayer = null;
attackingPlayer = TownyUniverse.getDataSource().getResident(cell.getNameOfFlagOwner());
if (player != null) {
try {
defendingPlayer = TownyUniverse.getDataSource().getResident(player.getName());
} catch (NotRegisteredException e) {
}
}
String formattedMoney = TownyEconomyHandler.getFormattedBalance(TownyWarConfig.getDefendedAttackReward());
if (defendingPlayer == null) {
if (attackingPlayer.pay(TownyWarConfig.getDefendedAttackReward(), "War - Attack Was Defended (Greater Forces)"))
try {
TownyMessaging.sendResidentMessage(attackingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_greater_forces"), formattedMoney));
} catch (TownyException e) {
}
} else {
if (attackingPlayer.payTo(TownyWarConfig.getDefendedAttackReward(), defendingPlayer, "War - Attack Was Defended")) {
try {
TownyMessaging.sendResidentMessage(attackingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_attacker"), defendingPlayer.getFormattedName(), formattedMoney));
} catch (TownyException e) {
}
try {
TownyMessaging.sendResidentMessage(defendingPlayer, String.format(TownySettings.getLangString("msg_enemy_war_area_defended_defender"), attackingPlayer.getFormattedName(), formattedMoney));
} catch (TownyException e) {
}
}
}
} catch (EconomyException e) {
e.printStackTrace();
} catch (NotRegisteredException e) {
e.printStackTrace();
}
}
}
Aggregations