Search in sources :

Example 11 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyUniverse method getValidatedResidents.

public static List<Resident> getValidatedResidents(Object sender, String[] names) {
    List<Resident> invited = new ArrayList<Resident>();
    for (String name : names) {
        List<Player> matches = BukkitTools.matchPlayer(name);
        if (matches.size() > 1) {
            String line = "Multiple players selected";
            for (Player p : matches) line += ", " + p.getName();
            TownyMessaging.sendErrorMsg(sender, line);
        } else if (matches.size() == 1) {
            // Match found online
            try {
                Resident target = getDataSource().getResident(matches.get(0).getName());
                invited.add(target);
            } catch (TownyException x) {
                TownyMessaging.sendErrorMsg(sender, x.getMessage());
            }
        } else {
            // No online matches so test for offline.
            Resident target;
            try {
                target = getDataSource().getResident(name);
                invited.add(target);
            } catch (NotRegisteredException x) {
                TownyMessaging.sendErrorMsg(sender, x.getMessage());
            }
        }
    }
    return invited;
}
Also used : Player(org.bukkit.entity.Player) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 12 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyBlockListener method onBlockPlace.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    Player player = event.getPlayer();
    Block block = event.getBlock();
    WorldCoord worldCoord;
    try {
        TownyWorld world = TownyUniverse.getDataSource().getWorld(block.getWorld().getName());
        worldCoord = new WorldCoord(world.getName(), Coord.parseCoord(block));
        // Get build permissions (updates if none exist)
        boolean bBuild = PlayerCacheUtil.getCachePermission(player, block.getLocation(), BukkitTools.getTypeId(block), BukkitTools.getData(block), TownyPermission.ActionType.BUILD);
        // Allow build if we are permitted
        if (bBuild)
            return;
        /*
			 * Fetch the players cache
			 */
        PlayerCache cache = plugin.getCache(player);
        TownBlockStatus status = cache.getStatus();
        /*
			 * Flag war
			 */
        if (((status == TownBlockStatus.ENEMY) && TownyWarConfig.isAllowingAttacks()) && (event.getBlock().getType() == TownyWarConfig.getFlagBaseMaterial())) {
            try {
                if (TownyWar.callAttackCellEvent(plugin, player, block, worldCoord))
                    return;
            } catch (TownyException e) {
                TownyMessaging.sendErrorMsg(player, e.getMessage());
            }
            event.setBuild(false);
            event.setCancelled(true);
        } else if (status == TownBlockStatus.WARZONE) {
            if (!TownyWarConfig.isEditableMaterialInWarZone(block.getType())) {
                event.setBuild(false);
                event.setCancelled(true);
                TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_warzone_cannot_edit_material"), "build", block.getType().toString().toLowerCase()));
            }
            return;
        } else {
            event.setBuild(false);
            event.setCancelled(true);
        }
        /* 
			 * display any error recorded for this plot
			 */
        if ((cache.hasBlockErrMsg()) && (event.isCancelled()))
            TownyMessaging.sendErrorMsg(player, cache.getBlockErrMsg());
    } catch (NotRegisteredException e1) {
        TownyMessaging.sendErrorMsg(player, TownySettings.getLangString("msg_err_not_configured"));
        event.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) PlayerCache(com.palmergames.bukkit.towny.object.PlayerCache) TownBlockStatus(com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 13 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyFlatFileSource method loadNation.

@Override
public boolean loadNation(Nation nation) {
    String line = "";
    String[] tokens;
    String path = getNationFilename(nation);
    File fileResident = new File(path);
    if (fileResident.exists() && fileResident.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("towns");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        TownyMessaging.sendDebugMsg("Nation Fetching Town: " + token);
                        Town town = getTown(token);
                        if (town != null)
                            nation.addTown(town);
                    }
                }
            }
            line = kvFile.get("capital");
            if (line != null)
                nation.setCapital(getTown(line));
            // line = kvFile.get("assistants");
            // if (line != null) {
            // tokens = line.split(",");
            // for (String token : tokens) {
            // if (!token.isEmpty()) {
            // Resident assistant = getResident(token);
            // if (assistant != null)
            // nation.addAssistant(assistant);
            // }
            // }
            // }
            line = kvFile.get("tag");
            if (line != null)
                try {
                    nation.setTag(line);
                } catch (TownyException e) {
                    nation.setTag("");
                }
            line = kvFile.get("allies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation friend = getNation(token);
                        if (friend != null)
                            // ("ally", friend);
                            nation.addAlly(friend);
                    }
                }
            }
            line = kvFile.get("enemies");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        Nation enemy = getNation(token);
                        if (enemy != null)
                            // ("enemy", enemy);
                            nation.addEnemy(enemy);
                    }
                }
            }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    nation.setTaxes(Double.parseDouble(line));
                } catch (Exception e) {
                    nation.setTaxes(0.0);
                }
            line = kvFile.get("neutral");
            if (line != null)
                try {
                    nation.setNeutral(Boolean.parseBoolean(line));
                } catch (Exception e) {
                }
        } catch (Exception e) {
            TownyMessaging.sendErrorMsg("Loading Error: Exception while reading nation file " + nation.getName());
            return false;
        }
        return true;
    } else
        return false;
}
Also used : Nation(com.palmergames.bukkit.towny.object.Nation) Town(com.palmergames.bukkit.towny.object.Town) KeyValueFile(com.palmergames.util.KeyValueFile) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) 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) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 14 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyFlatFileSource method saveTown.

@Override
public boolean saveTown(Town town) {
    List<String> list = new ArrayList<String>();
    // Name
    list.add("name=" + town.getName());
    // Residents
    list.add("residents=" + StringMgmt.join(town.getResidents(), ","));
    // Mayor
    if (town.hasMayor())
        list.add("mayor=" + town.getMayor().getName());
    // Nation
    if (town.hasNation())
        try {
            list.add("nation=" + town.getNation().getName());
        } catch (NotRegisteredException e) {
        }
    // Assistants
    list.add("assistants=" + StringMgmt.join(town.getAssistants(), ","));
    list.add(newLine);
    // Town Board
    list.add("townBoard=" + town.getTownBoard());
    // tag
    list.add("tag=" + town.getTag());
    // Town Protection
    list.add("protectionStatus=" + town.getPermissions().toString());
    // Bonus Blocks
    list.add("bonusBlocks=" + Integer.toString(town.getBonusBlocks()));
    // Purchased Blocks
    list.add("purchasedBlocks=" + Integer.toString(town.getPurchasedBlocks()));
    // Taxpercent
    list.add("taxpercent=" + Boolean.toString(town.isTaxPercentage()));
    // Taxes
    list.add("taxes=" + Double.toString(town.getTaxes()));
    // Plot Price
    list.add("plotPrice=" + Double.toString(town.getPlotPrice()));
    // Plot Tax
    list.add("plotTax=" + Double.toString(town.getPlotTax()));
    // Commercial Plot Price
    list.add("commercialPlotPrice=" + Double.toString(town.getCommercialPlotPrice()));
    // Commercial Tax
    list.add("commercialPlotTax=" + Double.toString(town.getCommercialPlotTax()));
    // Embassy Plot Price
    list.add("embassyPlotPrice=" + Double.toString(town.getEmbassyPlotPrice()));
    // Embassy Tax
    list.add("embassyPlotTax=" + Double.toString(town.getEmbassyPlotTax()));
    // Upkeep
    list.add("hasUpkeep=" + Boolean.toString(town.hasUpkeep()));
    // Open
    list.add("open=" + Boolean.toString(town.isOpen()));
    // PVP
    list.add("adminDisabledPvP=" + Boolean.toString(town.isAdminDisabledPVP()));
    /* // Mobs
		* fout.write("mobs=" + Boolean.toString(town.hasMobs()) + newLine);
		*/
    // Public
    list.add("public=" + Boolean.toString(town.isPublic()));
    // Home Block
    if (town.hasHomeBlock())
        try {
            list.add("homeBlock=" + town.getHomeBlock().getWorld().getName() + "," + Integer.toString(town.getHomeBlock().getX()) + "," + Integer.toString(town.getHomeBlock().getZ()));
        } catch (TownyException e) {
        }
    // Spawn
    if (town.hasSpawn())
        try {
            list.add("spawn=" + town.getSpawn().getWorld().getName() + "," + Double.toString(town.getSpawn().getX()) + "," + Double.toString(town.getSpawn().getY()) + "," + Double.toString(town.getSpawn().getZ()) + "," + Float.toString(town.getSpawn().getPitch()) + "," + Float.toString(town.getSpawn().getYaw()));
        } catch (TownyException e) {
        }
    // Outpost Spawns
    if (town.hasOutpostSpawn()) {
        String outpostArray = "outpostspawns=";
        for (Location spawn : new ArrayList<Location>(town.getAllOutpostSpawns())) {
            outpostArray += (spawn.getWorld().getName() + "," + Double.toString(spawn.getX()) + "," + Double.toString(spawn.getY()) + "," + Double.toString(spawn.getZ()) + "," + Float.toString(spawn.getPitch()) + "," + Float.toString(spawn.getYaw()) + ";");
        }
        list.add(outpostArray);
    }
    /*
		 *  Make sure we only save in async
		 */
    this.queryQueue.add(new FlatFile_Task(list, getTownFilename(town)));
    return true;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) Location(org.bukkit.Location)

Example 15 with TownyException

use of com.palmergames.bukkit.towny.exceptions.TownyException in project Towny by ElgarL.

the class TownyFlatFileSource method loadTown.

@Override
public boolean loadTown(Town town) {
    String line;
    String[] tokens;
    String path = getTownFilename(town);
    File fileTown = new File(path);
    if (fileTown.exists() && fileTown.isFile()) {
        try {
            KeyValueFile kvFile = new KeyValueFile(path);
            line = kvFile.get("residents");
            if (line != null) {
                tokens = line.split(",");
                for (String token : tokens) {
                    if (!token.isEmpty()) {
                        TownyMessaging.sendDebugMsg("Town Fetching Resident: " + token);
                        Resident resident = getResident(token);
                        if (resident != null) {
                            try {
                                town.addResident(resident);
                            } catch (AlreadyRegisteredException e) {
                                TownyMessaging.sendErrorMsg("Loading Error: " + resident.getName() + " is already a member of a town (" + resident.getTown().getName() + ").");
                            }
                        }
                    }
                }
            }
            line = kvFile.get("mayor");
            if (line != null)
                town.setMayor(getResident(line));
            // line = kvFile.get("assistants");
            // if (line != null) {
            // tokens = line.split(",");
            // for (String token : tokens) {
            // if (!token.isEmpty()) {
            // Resident assistant = getResident(token);
            // if ((assistant != null) && (town.hasResident(assistant)))
            // town.addAssistant(assistant);
            // }
            // }
            // }
            town.setTownBoard(kvFile.get("townBoard"));
            line = kvFile.get("tag");
            if (line != null)
                try {
                    town.setTag(line);
                } catch (TownyException e) {
                    town.setTag("");
                }
            line = kvFile.get("protectionStatus");
            if (line != null)
                town.setPermissions(line);
            line = kvFile.get("bonusBlocks");
            if (line != null)
                try {
                    town.setBonusBlocks(Integer.parseInt(line));
                } catch (Exception e) {
                    town.setBonusBlocks(0);
                }
            line = kvFile.get("purchasedBlocks");
            if (line != null)
                try {
                    town.setPurchasedBlocks(Integer.parseInt(line));
                } catch (Exception e) {
                    town.setPurchasedBlocks(0);
                }
            line = kvFile.get("plotPrice");
            if (line != null)
                try {
                    town.setPlotPrice(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setPlotPrice(0);
                }
            line = kvFile.get("hasUpkeep");
            if (line != null)
                try {
                    town.setHasUpkeep(Boolean.parseBoolean(line));
                } catch (NumberFormatException nfe) {
                } catch (Exception e) {
                }
            line = kvFile.get("taxpercent");
            if (line != null)
                try {
                    town.setTaxPercentage(Boolean.parseBoolean(line));
                } catch (NumberFormatException nfe) {
                } catch (Exception e) {
                }
            line = kvFile.get("taxes");
            if (line != null)
                try {
                    town.setTaxes(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setTaxes(0);
                }
            line = kvFile.get("plotTax");
            if (line != null)
                try {
                    town.setPlotTax(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setPlotTax(0);
                }
            line = kvFile.get("commercialPlotPrice");
            if (line != null)
                try {
                    town.setCommercialPlotPrice(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setCommercialPlotPrice(0);
                }
            line = kvFile.get("commercialPlotTax");
            if (line != null)
                try {
                    town.setCommercialPlotTax(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setCommercialPlotTax(0);
                }
            line = kvFile.get("embassyPlotPrice");
            if (line != null)
                try {
                    town.setEmbassyPlotPrice(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setEmbassyPlotPrice(0);
                }
            line = kvFile.get("embassyPlotTax");
            if (line != null)
                try {
                    town.setEmbassyPlotTax(Double.parseDouble(line));
                } catch (Exception e) {
                    town.setEmbassyPlotTax(0);
                }
            line = kvFile.get("adminDisabledPvP");
            if (line != null)
                try {
                    town.setAdminDisabledPVP(Boolean.parseBoolean(line));
                } catch (NumberFormatException nfe) {
                } catch (Exception e) {
                }
            /*
				 * line = kvFile.get("mobs");
				 * if (line != null)
				 * try {
				 * town.setHasMobs(Boolean.parseBoolean(line));
				 * } catch (NumberFormatException nfe) {
				 * } catch (Exception e) {
				 * }
				 */
            line = kvFile.get("open");
            if (line != null)
                try {
                    town.setOpen(Boolean.parseBoolean(line));
                } catch (NumberFormatException nfe) {
                } catch (Exception e) {
                }
            line = kvFile.get("public");
            if (line != null)
                try {
                    town.setPublic(Boolean.parseBoolean(line));
                } catch (NumberFormatException nfe) {
                } catch (Exception e) {
                }
            /*
				 * line = kvFile.get("explosion");
				 * if (line != null)
				 * try {
				 * town.setBANG(Boolean.parseBoolean(line));
				 * } catch (NumberFormatException nfe) {
				 * } catch (Exception e) {
				 * }
				 * 
				 * line = kvFile.get("fire");
				 * if (line != null)
				 * try {
				 * town.setFire(Boolean.parseBoolean(line));
				 * } catch (NumberFormatException nfe) {
				 * } catch (Exception e) {
				 * }
				 */
            line = kvFile.get("townBlocks");
            if (line != null)
                utilLoadTownBlocks(line, town, null);
            line = kvFile.get("homeBlock");
            if (line != null) {
                tokens = line.split(",");
                if (tokens.length == 3)
                    try {
                        TownyWorld world = getWorld(tokens[0]);
                        try {
                            int x = Integer.parseInt(tokens[1]);
                            int z = Integer.parseInt(tokens[2]);
                            TownBlock homeBlock = world.getTownBlock(x, z);
                            town.forceSetHomeBlock(homeBlock);
                        } catch (NumberFormatException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid location.");
                        } catch (NotRegisteredException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid TownBlock.");
                        } catch (TownyException e) {
                            TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " does not have a home block.");
                        }
                    } catch (NotRegisteredException e) {
                        TownyMessaging.sendErrorMsg("[Warning] " + town.getName() + " homeBlock tried to load invalid world.");
                    }
            }
            line = kvFile.get("spawn");
            if (line != null) {
                tokens = line.split(",");
                if (tokens.length >= 4)
                    try {
                        World world = plugin.getServerWorld(tokens[0]);
                        double x = Double.parseDouble(tokens[1]);
                        double y = Double.parseDouble(tokens[2]);
                        double z = Double.parseDouble(tokens[3]);
                        Location loc = new Location(world, x, y, z);
                        if (tokens.length == 6) {
                            loc.setPitch(Float.parseFloat(tokens[4]));
                            loc.setYaw(Float.parseFloat(tokens[5]));
                        }
                        town.forceSetSpawn(loc);
                    } catch (NumberFormatException e) {
                    } catch (NotRegisteredException e) {
                    } catch (NullPointerException e) {
                    }
            }
            // Load outpost spawns
            line = kvFile.get("outpostspawns");
            if (line != null) {
                String[] outposts = line.split(";");
                for (String spawn : outposts) {
                    tokens = spawn.split(",");
                    if (tokens.length >= 4)
                        try {
                            World world = plugin.getServerWorld(tokens[0]);
                            double x = Double.parseDouble(tokens[1]);
                            double y = Double.parseDouble(tokens[2]);
                            double z = Double.parseDouble(tokens[3]);
                            Location loc = new Location(world, x, y, z);
                            if (tokens.length == 6) {
                                loc.setPitch(Float.parseFloat(tokens[4]));
                                loc.setYaw(Float.parseFloat(tokens[5]));
                            }
                            town.forceAddOutpostSpawn(loc);
                        } catch (NumberFormatException e) {
                        } catch (NotRegisteredException e) {
                        } catch (NullPointerException e) {
                        }
                }
            }
        } catch (Exception e) {
            TownyMessaging.sendErrorMsg("Loading Error: Exception while reading town file " + town.getName());
            return false;
        }
        return true;
    } else
        return false;
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) KeyValueFile(com.palmergames.util.KeyValueFile) World(org.bukkit.World) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) 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) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) File(java.io.File) KeyValueFile(com.palmergames.util.KeyValueFile) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Location(org.bukkit.Location)

Aggregations

TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)70 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)37 Resident (com.palmergames.bukkit.towny.object.Resident)29 Town (com.palmergames.bukkit.towny.object.Town)17 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)16 Nation (com.palmergames.bukkit.towny.object.Nation)16 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)15 ArrayList (java.util.ArrayList)12 Player (org.bukkit.entity.Player)12 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)11 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)11 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)7 Location (org.bukkit.Location)7 IOException (java.io.IOException)6 EventHandler (org.bukkit.event.EventHandler)6 Coord (com.palmergames.bukkit.towny.object.Coord)5 InvalidNameException (javax.naming.InvalidNameException)5 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)4 EmptyNationException (com.palmergames.bukkit.towny.exceptions.EmptyNationException)3 EmptyTownException (com.palmergames.bukkit.towny.exceptions.EmptyTownException)3