Search in sources :

Example 11 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 12 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)

Example 13 with TownyWorld

use of com.palmergames.bukkit.towny.object.TownyWorld 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());
}
Also used : Player(org.bukkit.entity.Player) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PlayerTeleportEvent(org.bukkit.event.player.PlayerTeleportEvent) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) Location(org.bukkit.Location) BlockLocation(com.palmergames.bukkit.towny.regen.block.BlockLocation) EventHandler(org.bukkit.event.EventHandler)

Example 14 with TownyWorld

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

the class TownyWorldListener method newWorld.

private void newWorld(String worldName) {
    //String worldName = event.getWorld().getName();
    try {
        TownyUniverse.getDataSource().newWorld(worldName);
        TownyWorld world = TownyUniverse.getDataSource().getWorld(worldName);
        if (world == null)
            TownyMessaging.sendErrorMsg("Could not create data for " + worldName);
        else {
            if (!TownyUniverse.getDataSource().loadWorld(world)) {
                // First time world has been noticed
                TownyUniverse.getDataSource().saveWorld(world);
            }
        }
    } catch (AlreadyRegisteredException e) {
    // Allready loaded			
    } catch (NotRegisteredException e) {
        TownyMessaging.sendErrorMsg("Could not create data for " + worldName);
        e.printStackTrace();
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld)

Example 15 with TownyWorld

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

the class TownyAsciiMap method generateAndSend.

public static void generateAndSend(Towny plugin, Player player, int lineHeight) {
    // Collect Sample Data
    boolean hasTown = false;
    Resident resident;
    try {
        resident = TownyUniverse.getDataSource().getResident(player.getName());
        if (resident.hasTown())
            hasTown = true;
    } catch (TownyException x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
        return;
    }
    TownyWorld world;
    try {
        world = TownyUniverse.getDataSource().getWorld(player.getWorld().getName());
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, "You are not in a registered world.");
        return;
    }
    if (!world.isUsingTowny()) {
        TownyMessaging.sendErrorMsg(player, "This world is not using towny.");
        return;
    }
    Coord pos = Coord.parseCoord(plugin.getCache(player).getLastLocation());
    // Generate Map 
    int halfLineHeight = lineHeight / 2;
    String[][] townyMap = new String[lineWidth][lineHeight];
    int x, y = 0;
    for (int tby = pos.getX() + (lineWidth - halfLineWidth - 1); tby >= pos.getX() - halfLineWidth; tby--) {
        x = 0;
        for (int tbx = pos.getZ() - halfLineHeight; tbx <= pos.getZ() + (lineHeight - halfLineHeight - 1); tbx++) {
            try {
                TownBlock townblock = world.getTownBlock(tby, tbx);
                //TODO: possibly claim outside of towns
                if (!townblock.hasTown())
                    throw new TownyException();
                if (x == halfLineHeight && y == halfLineWidth)
                    // location
                    townyMap[y][x] = Colors.Gold;
                else if (hasTown) {
                    if (resident.getTown() == townblock.getTown()) {
                        // own town
                        townyMap[y][x] = Colors.LightGreen;
                        try {
                            if (resident == townblock.getResident())
                                //own plot
                                townyMap[y][x] = Colors.Yellow;
                        } catch (NotRegisteredException e) {
                        }
                    } else if (resident.hasNation()) {
                        if (resident.getTown().getNation().hasTown(townblock.getTown()))
                            // towns
                            townyMap[y][x] = Colors.Green;
                        else if (townblock.getTown().hasNation()) {
                            Nation nation = resident.getTown().getNation();
                            if (nation.hasAlly(townblock.getTown().getNation()))
                                townyMap[y][x] = Colors.Green;
                            else if (nation.hasEnemy(townblock.getTown().getNation()))
                                // towns
                                townyMap[y][x] = Colors.Red;
                            else
                                townyMap[y][x] = Colors.White;
                        } else
                            townyMap[y][x] = Colors.White;
                    } else
                        townyMap[y][x] = Colors.White;
                } else
                    townyMap[y][x] = Colors.White;
                // Registered town block
                if (townblock.getPlotPrice() != -1) {
                    // override the colour if it's a shop plot for sale
                    if (townblock.getType().equals(TownBlockType.COMMERCIAL))
                        townyMap[y][x] = Colors.Blue;
                    townyMap[y][x] += "$";
                } else if (townblock.isHomeBlock())
                    townyMap[y][x] += "H";
                else
                    townyMap[y][x] += townblock.getType().getAsciiMapKey();
            } catch (TownyException e) {
                if (x == halfLineHeight && y == halfLineWidth)
                    townyMap[y][x] = Colors.Gold;
                else
                    townyMap[y][x] = Colors.Gray;
                // Unregistered town block
                townyMap[y][x] += "-";
            }
            x++;
        }
        y++;
    }
    String[] compass = generateCompass(player);
    // Output
    player.sendMessage(ChatTools.formatTitle("Towny Map " + Colors.White + "(" + pos.toString() + ")"));
    String line;
    int lineCount = 0;
    // Variables have been rotated to fit N/S/E/W properly
    for (int my = 0; my < lineHeight; my++) {
        line = compass[0];
        if (lineCount < compass.length)
            line = compass[lineCount];
        for (int mx = lineWidth - 1; mx >= 0; mx--) line += townyMap[mx][my];
        if (lineCount < help.length)
            line += help[lineCount];
        player.sendMessage(line);
        lineCount++;
    }
    // Current town block data
    try {
        TownBlock townblock = world.getTownBlock(pos);
        TownyMessaging.sendMsg(player, ("Town: " + (townblock.hasTown() ? townblock.getTown().getName() : "None") + " : " + "Owner: " + (townblock.hasResident() ? townblock.getResident().getName() : "None")));
    } catch (TownyException e) {
        //plugin.sendErrorMsg(player, e.getError());
        // Send a blank line instead of an error, to keep the map position tidy.
        player.sendMessage("");
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Coord(com.palmergames.bukkit.towny.object.Coord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Aggregations

TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)33 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)27 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 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 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 Coord (com.palmergames.bukkit.towny.object.Coord)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Resident (com.palmergames.bukkit.towny.object.Resident)5 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)5 Block (org.bukkit.block.Block)5 Town (com.palmergames.bukkit.towny.object.Town)3 Projectile (org.bukkit.entity.Projectile)3