Search in sources :

Example 1 with TownyWorld

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);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ArmorStand(org.bukkit.entity.ArmorStand) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Monster(org.bukkit.entity.Monster) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 2 with TownyWorld

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;
                }
        }
    }
}
Also used : Coord(com.palmergames.bukkit.towny.object.Coord) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) ProtectionRegenTask(com.palmergames.bukkit.towny.tasks.ProtectionRegenTask) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 3 with TownyWorld

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();
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Creature(org.bukkit.entity.Creature) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) EventHandler(org.bukkit.event.EventHandler)

Example 4 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld 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) {
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Coord(com.palmergames.bukkit.towny.object.Coord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Villager(org.bukkit.entity.Villager) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 5 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld in project Towny by ElgarL.

the class DailyTimerTask method run.

@Override
public void run() {
    long start = System.currentTimeMillis();
    TownyMessaging.sendDebugMsg("New Day");
    // Collect taxes
    if (TownyEconomyHandler.isActive() && TownySettings.isTaxingDaily()) {
        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_new_day_tax")));
        try {
            TownyMessaging.sendDebugMsg("Collecting Town Taxes");
            collectTownTaxes();
            TownyMessaging.sendDebugMsg("Collecting Nation Taxes");
            collectNationTaxes();
            TownyMessaging.sendDebugMsg("Collecting Town Costs");
            collectTownCosts();
            TownyMessaging.sendDebugMsg("Collecting Nation Costs");
            collectNationCosts();
        } catch (EconomyException e) {
        } catch (TownyException e) {
            // TODO king exception
            e.printStackTrace();
        }
    } else
        TownyMessaging.sendGlobalMessage(String.format(TownySettings.getLangString("msg_new_day")));
    // Automatically delete old residents
    if (TownySettings.isDeletingOldResidents()) {
        // Run a purge in it's own thread
        new ResidentPurge(plugin, null, TownySettings.getDeleteTime() * 1000).start();
    }
    // Backups
    TownyMessaging.sendDebugMsg("Cleaning up old backups.");
    TownyUniverse.getDataSource().cleanupBackups();
    if (TownySettings.isBackingUpDaily())
        try {
            TownyMessaging.sendDebugMsg("Making backup.");
            TownyUniverse.getDataSource().backup();
        } catch (IOException e) {
            TownyMessaging.sendErrorMsg("Could not create backup.");
            e.printStackTrace();
        }
    TownyMessaging.sendDebugMsg("Finished New Day Code");
    TownyMessaging.sendDebugMsg("Universe Stats:");
    TownyMessaging.sendDebugMsg("    Residents: " + TownyUniverse.getDataSource().getResidents().size());
    TownyMessaging.sendDebugMsg("    Towns: " + TownyUniverse.getDataSource().getTowns().size());
    TownyMessaging.sendDebugMsg("    Nations: " + TownyUniverse.getDataSource().getNations().size());
    for (TownyWorld world : TownyUniverse.getDataSource().getWorlds()) TownyMessaging.sendDebugMsg("    " + world.getName() + " (townblocks): " + world.getTownBlocks().size());
    TownyMessaging.sendDebugMsg("Memory (Java Heap):");
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (max)", Runtime.getRuntime().maxMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (total)", Runtime.getRuntime().totalMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (free)", Runtime.getRuntime().freeMemory() / 1024 / 1024));
    TownyMessaging.sendDebugMsg(String.format("%8d Mb (used=total-free)", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 / 1024));
    TownyMessaging.sendDebugMsg("newDay took " + (System.currentTimeMillis() - start) + "ms");
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) IOException(java.io.IOException) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)34 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)28 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)16 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)14 EventHandler (org.bukkit.event.EventHandler)12 Player (org.bukkit.entity.Player)11 Entity (org.bukkit.entity.Entity)9 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 Coord (com.palmergames.bukkit.towny.object.Coord)7 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)7 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)7 Location (org.bukkit.Location)7 LivingEntity (org.bukkit.entity.LivingEntity)7 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Resident (com.palmergames.bukkit.towny.object.Resident)5 Block (org.bukkit.block.Block)5 Town (com.palmergames.bukkit.towny.object.Town)4 Projectile (org.bukkit.entity.Projectile)3