Search in sources :

Example 16 with TownBlock

use of com.palmergames.bukkit.towny.object.TownBlock 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)

Example 17 with TownBlock

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

the class PlotCommand method setPlotForSale.

/**
	 * Set the plot for sale/not for sale if permitted
	 * 
	 * @param resident
	 * @param worldCoord
	 * @param forSale
	 * @throws TownyException
	 */
public void setPlotForSale(Resident resident, WorldCoord worldCoord, double forSale) throws TownyException {
    if (resident.hasTown())
        try {
            TownBlock townBlock = worldCoord.getTownBlock();
            // Test we are allowed to work on this plot
            // ignore the return as we
            plotTestOwner(resident, townBlock);
            // exception
            if (forSale > 1000000) {
                TownyUniverse.getPlayer(resident).sendMessage("Plot price too expensive.");
            } else {
                townBlock.setPlotPrice(forSale);
                if (forSale != -1)
                    TownyMessaging.sendTownMessage(townBlock.getTown(), TownySettings.getPlotForSaleMsg(resident.getName(), worldCoord));
                else
                    TownyUniverse.getPlayer(resident).sendMessage(TownySettings.getLangString("msg_err_plot_nfs"));
                // Save this townblock so the for sale status is remembered.
                TownyUniverse.getDataSource().saveTownBlock(townBlock);
            }
        } catch (NotRegisteredException e) {
            throw new TownyException(TownySettings.getLangString("msg_err_not_part_town"));
        }
    else
        throw new TownyException(TownySettings.getLangString("msg_err_must_belong_town"));
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 18 with TownBlock

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

the class PlotCommand method parsePlotCommand.

public boolean parsePlotCommand(Player player, String[] split) throws TownyException {
    if (split.length == 0 || split[0].equalsIgnoreCase("?")) {
        for (String line : output) player.sendMessage(line);
    } else {
        Resident resident;
        String world;
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
            world = player.getWorld().getName();
        //resident.getTown();
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return true;
        }
        try {
            if (split[0].equalsIgnoreCase("claim")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_CLAIM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                if (TownyUniverse.isWarTime())
                    throw new TownyException(TownySettings.getLangString("msg_war_cannot_do"));
                List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                if (selection.size() > 0) {
                    double cost = 0;
                    // tally up costs.
                    for (WorldCoord worldCoord : new ArrayList<WorldCoord>(selection)) {
                        try {
                            double price = worldCoord.getTownBlock().getPlotPrice();
                            if (price > -1)
                                cost += worldCoord.getTownBlock().getPlotPrice();
                            else {
                                if (// ||
                                !worldCoord.getTownBlock().getTown().isMayor(resident))
                                    // worldCoord.getTownBlock().getTown().hasAssistant(resident))
                                    selection.remove(worldCoord);
                            }
                        } catch (NotRegisteredException e) {
                            selection.remove(worldCoord);
                        }
                    }
                    int maxPlots = TownySettings.getMaxResidentPlots(resident);
                    if (maxPlots >= 0 && resident.getTownBlocks().size() + selection.size() > maxPlots)
                        throw new TownyException(String.format(TownySettings.getLangString("msg_max_plot_own"), maxPlots));
                    if (TownySettings.isUsingEconomy() && (!resident.canPayFromHoldings(cost)))
                        throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_claim"), selection.size(), TownyEconomyHandler.getFormattedBalance(cost)));
                    // Start the claim task
                    new PlotClaim(plugin, player, resident, selection, true).start();
                } else {
                    player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                }
            } else if (split[0].equalsIgnoreCase("unclaim")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_UNCLAIM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                if (TownyUniverse.isWarTime())
                    throw new TownyException(TownySettings.getLangString("msg_war_cannot_do"));
                if (split.length == 2 && split[1].equalsIgnoreCase("all")) {
                    // Start the unclaim task
                    new PlotClaim(plugin, player, resident, null, false).start();
                } else {
                    List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                    selection = AreaSelectionUtil.filterOwnedBlocks(resident, selection);
                    if (selection.size() > 0) {
                        // Start the unclaim task
                        new PlotClaim(plugin, player, resident, selection, false).start();
                    } else {
                        player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                    }
                }
            } else if (split[0].equalsIgnoreCase("notforsale") || split[0].equalsIgnoreCase("nfs")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_NOTFORSALE.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                List<WorldCoord> selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.remFirstArg(split));
                selection = AreaSelectionUtil.filterOwnedBlocks(resident.getTown(), selection);
                for (WorldCoord worldCoord : selection) {
                    setPlotForSale(resident, worldCoord, -1);
                }
            } else if (split[0].equalsIgnoreCase("forsale") || split[0].equalsIgnoreCase("fs")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_FORSALE.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                WorldCoord pos = new WorldCoord(world, Coord.parseCoord(player));
                double plotPrice = pos.getTownBlock().getTown().getPlotTypePrice(pos.getTownBlock().getType());
                if (split.length > 1) {
                    int areaSelectPivot = AreaSelectionUtil.getAreaSelectPivot(split);
                    List<WorldCoord> selection;
                    if (areaSelectPivot >= 0) {
                        selection = AreaSelectionUtil.selectWorldCoordArea(resident, new WorldCoord(world, Coord.parseCoord(player)), StringMgmt.subArray(split, areaSelectPivot + 1, split.length));
                        selection = AreaSelectionUtil.filterOwnedBlocks(resident.getTown(), selection);
                        if (selection.size() == 0) {
                            player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                            return true;
                        }
                    } else {
                        selection = new ArrayList<WorldCoord>();
                        selection.add(pos);
                    }
                    // Check that it's not: /plot forsale within rect 3
                    if (areaSelectPivot != 1) {
                        try {
                            // command was 'plot fs $'
                            plotPrice = Double.parseDouble(split[1]);
                            if (plotPrice < 0) {
                                TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_negative_money"));
                                return true;
                            }
                        } catch (NumberFormatException e) {
                            player.sendMessage(String.format(TownySettings.getLangString("msg_error_must_be_num")));
                            return true;
                        }
                    }
                    for (WorldCoord worldCoord : selection) {
                        if (selection.size() > 1)
                            plotPrice = worldCoord.getTownBlock().getTown().getPlotTypePrice(worldCoord.getTownBlock().getType());
                        setPlotForSale(resident, worldCoord, plotPrice);
                    }
                } else {
                    // basic 'plot fs' command
                    setPlotForSale(resident, pos, plotPrice);
                }
            } else if (split[0].equalsIgnoreCase("perm")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_PERM.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(townBlock));
            } else if (split[0].equalsIgnoreCase("toggle")) {
                /*
					 * perm test in the plottoggle.
					 */
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                // Test we are allowed to work on this plot
                // ignore the return as
                plotTestOwner(resident, townBlock);
                // we are only checking
                // for an exception
                plotToggle(player, new WorldCoord(world, Coord.parseCoord(player)).getTownBlock(), StringMgmt.remFirstArg(split));
            } else if (split[0].equalsIgnoreCase("set")) {
                split = StringMgmt.remFirstArg(split);
                if (split.length > 0) {
                    if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_SET.getNode(split[0].toLowerCase())))
                        throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                    if (split[0].equalsIgnoreCase("perm")) {
                        // Set plot level permissions (if the plot owner) or
                        // Mayor/Assistant of the town.
                        TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                        // Test we are allowed to work on this plot
                        TownBlockOwner owner = plotTestOwner(resident, townBlock);
                        // Check we are allowed to set these perms
                        toggleTest(player, townBlock, StringMgmt.join(StringMgmt.remFirstArg(split), ""));
                        setTownBlockPermissions(player, owner, townBlock, StringMgmt.remFirstArg(split));
                        return true;
                    } else if (split[0].equalsIgnoreCase("name")) {
                        TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                        // Test we are allowed to work on this plot
                        plotTestOwner(resident, townBlock);
                        if (split.length == 1) {
                            townBlock.setName("");
                            TownyMessaging.sendMsg(player, String.format("Plot name removed"));
                            TownyUniverse.getDataSource().saveTownBlock(townBlock);
                            return true;
                        }
                        // Test if the plot name contains invalid characters.
                        if (!NameValidation.isBlacklistName(split[1])) {
                            townBlock.setName(StringMgmt.join(StringMgmt.remFirstArg(split), ""));
                            //townBlock.setChanged(true);
                            TownyUniverse.getDataSource().saveTownBlock(townBlock);
                            TownyMessaging.sendMsg(player, String.format("Plot name set to [%s]", townBlock.getName()));
                        } else {
                            TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_invalid_name"));
                        }
                        return true;
                    }
                    WorldCoord worldCoord = new WorldCoord(world, Coord.parseCoord(player));
                    setPlotType(resident, worldCoord, split[0]);
                    player.sendMessage(String.format(TownySettings.getLangString("msg_plot_set_type"), split[0]));
                } else {
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "name", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "reset", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set", "shop|embassy|arena|wilds|spleef", ""));
                    player.sendMessage(ChatTools.formatCommand("", "/plot set perm", "?", ""));
                }
            } else if (split[0].equalsIgnoreCase("clear")) {
                if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_PLOT_CLEAR.getNode()))
                    throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
                TownBlock townBlock = new WorldCoord(world, Coord.parseCoord(player)).getTownBlock();
                if (townBlock != null) {
                    /**
						 * Only allow mayors or plot owners to use this command.
						 */
                    if (townBlock.hasResident()) {
                        if (!townBlock.isOwner(resident)) {
                            player.sendMessage(TownySettings.getLangString("msg_area_not_own"));
                            return true;
                        }
                    } else if (!townBlock.getTown().equals(resident.getTown())) {
                        player.sendMessage(TownySettings.getLangString("msg_area_not_own"));
                        return true;
                    }
                    for (String material : TownyUniverse.getDataSource().getWorld(world).getPlotManagementMayorDelete()) if (Material.matchMaterial(material) != null) {
                        TownyRegenAPI.deleteTownBlockMaterial(townBlock, Material.getMaterial(material));
                        player.sendMessage(String.format(TownySettings.getLangString("msg_clear_plot_material"), material));
                    } else
                        throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_property"), material));
                    // Raise an event for the claim
                    BukkitTools.getPluginManager().callEvent(new PlotClearEvent(townBlock));
                } else {
                    // Shouldn't ever reach here as a null townBlock should
                    // be caught already in WorldCoord.
                    player.sendMessage(TownySettings.getLangString("msg_err_empty_area_selection"));
                }
            } else
                throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
        } catch (EconomyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
        }
    }
    return true;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) PlotClaim(com.palmergames.bukkit.towny.tasks.PlotClaim) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) TownBlockOwner(com.palmergames.bukkit.towny.object.TownBlockOwner) Resident(com.palmergames.bukkit.towny.object.Resident) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PlotClearEvent(com.palmergames.bukkit.towny.event.PlotClearEvent) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 19 with TownBlock

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

the class TownyFormatter method getTaxStatus.

/**
	 * Returns the tax info this resident will have to pay at the next new day.
	 * 
	 * @param resident
	 * @return tax status message
	 */
public static List<String> getTaxStatus(Resident resident) {
    List<String> out = new ArrayList<String>();
    Town town = null;
    double plotTax = 0.0;
    out.add(ChatTools.formatTitle(getFormattedName(resident) + ((BukkitTools.isOnline(resident.getName())) ? Colors.LightGreen + " (Online)" : "")));
    if (resident.hasTown()) {
        try {
            town = resident.getTown();
            out.add(Colors.Green + "Owner of: " + Colors.LightGreen + resident.getTownBlocks().size() + " plots");
            if (TownyPerms.getResidentPerms(resident).containsKey("towny.tax_exempt")) {
                out.add(Colors.Green + "Staff are exempt from paying town taxes.");
            } else {
                if (town.isTaxPercentage()) {
                    out.add(Colors.Green + "Town Tax: " + Colors.LightGreen + (resident.getHoldingBalance() * town.getTaxes() / 100));
                } else {
                    out.add(Colors.Green + "Town Tax: " + Colors.LightGreen + town.getTaxes());
                    if ((resident.getTownBlocks().size() > 0)) {
                        for (TownBlock townBlock : new ArrayList<TownBlock>(resident.getTownBlocks())) {
                            plotTax += townBlock.getType().getTax(townBlock.getTown());
                        }
                        out.add(Colors.Green + "Total Plot Taxes: " + Colors.LightGreen + plotTax);
                    }
                    out.add(Colors.Green + "Total Tax to pay: " + Colors.LightGreen + (town.getTaxes() + plotTax));
                }
            }
        } catch (NotRegisteredException e) {
        // Failed to fetch town
        } catch (EconomyException e) {
        // Economy failed
        }
    }
    return out;
}
Also used : EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Example 20 with TownBlock

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

the class TownyFlatFileSource method loadPlotData.

/**
	 * Load PlotBlockData
	 * 
	 * @param worldName
	 * @param x
	 * @param z
	 * @return PlotBlockData or null
	 */
@Override
public PlotBlockData loadPlotData(String worldName, int x, int z) {
    try {
        TownyWorld world = getWorld(worldName);
        TownBlock townBlock = new TownBlock(x, z, world);
        return loadPlotData(townBlock);
    } catch (NotRegisteredException e) {
        // Failed to get world
        e.printStackTrace();
    }
    return null;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

TownBlock (com.palmergames.bukkit.towny.object.TownBlock)34 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)28 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)16 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)12 Resident (com.palmergames.bukkit.towny.object.Resident)11 Town (com.palmergames.bukkit.towny.object.Town)8 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)7 Coord (com.palmergames.bukkit.towny.object.Coord)7 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Location (org.bukkit.Location)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 World (org.bukkit.World)4 Block (org.bukkit.block.Block)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 EventHandler (org.bukkit.event.EventHandler)3 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)2 KeyValueFile (com.palmergames.util.KeyValueFile)2 EOFException (java.io.EOFException)2