Search in sources :

Example 6 with WorldCoord

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

the class BorderUtil method getPlotBorder.

public static List<CellBorder> getPlotBorder(List<WorldCoord> worldCoords) {
    List<CellBorder> borderCoords = new ArrayList<CellBorder>();
    for (WorldCoord worldCoord : worldCoords) {
        CellBorder border = getPlotBorder(worldCoord);
        borderCoords.add(border);
    }
    return borderCoords;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) CellBorder(com.palmergames.bukkit.towny.object.CellBorder) ArrayList(java.util.ArrayList)

Example 7 with WorldCoord

use of com.palmergames.bukkit.towny.object.WorldCoord 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 8 with WorldCoord

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

the class TownyPlayerListener method onPlayerBedEnter.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
    if (!TownySettings.getBedUse())
        return;
    boolean isOwner = false;
    boolean isInnPlot = false;
    try {
        Resident resident = TownyUniverse.getDataSource().getResident(event.getPlayer().getName());
        WorldCoord worldCoord = new WorldCoord(event.getPlayer().getWorld().getName(), Coord.parseCoord(event.getBed().getLocation()));
        TownBlock townblock = worldCoord.getTownBlock();
        isOwner = townblock.isOwner(resident);
        isInnPlot = townblock.getType() == TownBlockType.INN;
        if (resident.hasNation() && townblock.getTown().hasNation()) {
            Nation residentNation = resident.getTown().getNation();
            Nation townblockNation = townblock.getTown().getNation();
            if (townblockNation.hasEnemy(residentNation)) {
                event.setCancelled(true);
                TownyMessaging.sendErrorMsg(event.getPlayer(), "You cannot sleep in an enemy's Inn.");
                return;
            }
        }
    } catch (NotRegisteredException e) {
    // Wilderness as it error'd getting a townblock.
    }
    if (!isOwner && !isInnPlot) {
        event.setCancelled(true);
        TownyMessaging.sendErrorMsg(event.getPlayer(), "You do not own the land this bed occupies and it is not an Inn plot.");
    }
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) EventHandler(org.bukkit.event.EventHandler)

Example 9 with WorldCoord

use of com.palmergames.bukkit.towny.object.WorldCoord 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 10 with WorldCoord

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

the class TownyFlatFileSource method saveSnapshotList.

@Override
public boolean saveSnapshotList() {
    BufferedWriter fout = null;
    try {
        fout = new BufferedWriter(new FileWriter(rootFolder + dataFolder + FileMgmt.fileSeparator() + "snapshot_queue.txt"));
        while (TownyRegenAPI.hasWorldCoords()) {
            WorldCoord worldCoord = TownyRegenAPI.getWorldCoord();
            fout.write(worldCoord.getWorldName() + "," + worldCoord.getX() + "," + worldCoord.getZ() + newLine);
        }
    } catch (Exception e) {
        TownyMessaging.sendErrorMsg("Saving Error: Exception while saving snapshot_queue file");
        e.printStackTrace();
        return false;
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException ignore) {
            }
        }
    }
    return true;
}
Also used : WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) FileWriter(java.io.FileWriter) IOException(java.io.IOException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) InvalidNameException(javax.naming.InvalidNameException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) BufferedWriter(java.io.BufferedWriter)

Aggregations

WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)19 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)12 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)8 Resident (com.palmergames.bukkit.towny.object.Resident)5 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)5 ArrayList (java.util.ArrayList)5 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)4 Player (org.bukkit.entity.Player)4 EventHandler (org.bukkit.event.EventHandler)4 TownBlockStatus (com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus)3 Town (com.palmergames.bukkit.towny.object.Town)3 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)2 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)2 CellBorder (com.palmergames.bukkit.towny.object.CellBorder)2 Nation (com.palmergames.bukkit.towny.object.Nation)2 EOFException (java.io.EOFException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InvalidNameException (javax.naming.InvalidNameException)2