Search in sources :

Example 41 with NotRegisteredException

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

Example 42 with NotRegisteredException

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

the class NationCommand method nationRank.

public void nationRank(Player player, String[] split) {
    if (split.length == 0) {
        // Help output.
        player.sendMessage(ChatTools.formatTitle("/nation rank"));
        player.sendMessage(ChatTools.formatCommand("", "/nation rank", "add/remove [resident] rank", ""));
    } else {
        Resident resident, target;
        Town town = null;
        Town targetTown = null;
        String rank;
        /*
			 * Does the command have enough arguments?
			 */
        if (split.length < 3) {
            TownyMessaging.sendErrorMsg(player, "Eg: /town rank add/remove [resident] [rank]");
            return;
        }
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
            target = TownyUniverse.getDataSource().getResident(split[1]);
            town = resident.getTown();
            targetTown = target.getTown();
            if (town.getNation() != targetTown.getNation())
                throw new TownyException("This resident is not a member of your Town!");
        } catch (TownyException x) {
            TownyMessaging.sendErrorMsg(player, x.getMessage());
            return;
        }
        rank = split[2].toLowerCase();
        /*
			 * Is this a known rank?
			 */
        if (!TownyPerms.getNationRanks().contains(rank)) {
            TownyMessaging.sendErrorMsg(player, "Unknown rank '" + rank + "'. Permissible ranks are :- " + StringMgmt.join(TownyPerms.getNationRanks(), ",") + ".");
            return;
        }
        /*
			 * Only allow the player to assign ranks if they have the grant perm
			 * for it.
			 */
        if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_NATION_RANK.getNode(rank))) {
            TownyMessaging.sendErrorMsg(player, "You do not have permission to modify this rank.");
            return;
        }
        if (split[0].equalsIgnoreCase("add")) {
            try {
                if (target.addNationRank(rank)) {
                    TownyMessaging.sendMsg(target, "You have been granted the Nation rank of '" + rank + "'.");
                    TownyMessaging.sendMsg(player, "You have granted the Nation rank of '" + rank + "' to " + target.getName() + ".");
                } else {
                    // Not in a nation or Rank doesn't exist
                    TownyMessaging.sendErrorMsg(player, "That resident isn't a member of a town!");
                    return;
                }
            } catch (AlreadyRegisteredException e) {
                // Must already have this rank
                TownyMessaging.sendMsg(player, target.getName() + " already holds this Nation rank.");
                return;
            }
        } else if (split[0].equalsIgnoreCase("remove")) {
            try {
                if (target.removeNationRank(rank)) {
                    TownyMessaging.sendMsg(target, "You have been demoted from the Nation rank of '" + rank + "'.");
                    TownyMessaging.sendMsg(player, "You have removed the Nation rank of '" + rank + "' from " + target.getName() + ".");
                }
            } catch (NotRegisteredException e) {
                // Must already have this rank
                TownyMessaging.sendMsg(player, target.getName() + " doesn't hold this Nation rank.");
                return;
            }
        } else {
            TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
            return;
        }
        /*
			 * If we got here we have made a change Save the altered resident
			 * data.
			 */
        TownyUniverse.getDataSource().saveResident(target);
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Town(com.palmergames.bukkit.towny.object.Town) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 43 with NotRegisteredException

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

the class ResidentCommand method parseResidentCommand.

public void parseResidentCommand(Player player, String[] split) {
    try {
        if (split.length == 0) {
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(resident, player));
            } catch (NotRegisteredException x) {
                throw new TownyException(TownySettings.getLangString("msg_err_not_registered"));
            }
        } else if (split[0].equalsIgnoreCase("?") || split[0].equalsIgnoreCase("help")) {
            for (String line : output) player.sendMessage(line);
        } else if (split[0].equalsIgnoreCase("list")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_LIST.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            listResidents(player);
        } else if (split[0].equalsIgnoreCase("tax")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_TAX.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(player.getName());
                TownyMessaging.sendMessage(player, TownyFormatter.getTaxStatus(resident));
            } catch (NotRegisteredException x) {
                throw new TownyException(TownySettings.getLangString("msg_err_not_registered"));
            }
        } else if (split[0].equalsIgnoreCase("set")) {
            /*
				 * perms checked in method.
				 */
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentSet(player, newSplit);
        } else if (split[0].equalsIgnoreCase("toggle")) {
            /*
				 * 
				 */
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentToggle(player, newSplit);
        } else if (split[0].equalsIgnoreCase("friend")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_FRIEND.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            String[] newSplit = StringMgmt.remFirstArg(split);
            residentFriend(player, newSplit);
        } else if (split[0].equalsIgnoreCase("spawn")) {
            if (!TownyUniverse.getPermissionSource().testPermission(player, PermissionNodes.TOWNY_COMMAND_RESIDENT_SPAWN.getNode()))
                throw new TownyException(TownySettings.getLangString("msg_err_command_disable"));
            residentSpawn(player);
        } else {
            try {
                Resident resident = TownyUniverse.getDataSource().getResident(split[0]);
                TownyMessaging.sendMessage(player, TownyFormatter.getStatus(resident, player));
            } catch (NotRegisteredException x) {
                throw new TownyException(String.format(TownySettings.getLangString("msg_err_not_registered_1"), split[0]));
            }
        }
    } catch (Exception x) {
        TownyMessaging.sendErrorMsg(player, x.getMessage());
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) Resident(com.palmergames.bukkit.towny.object.Resident) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 44 with NotRegisteredException

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

the class TownyPerms method assignPermissions.

/**
	 * Register a specific residents permissions with Bukkit.
	 * 
	 * @param resident
	 */
public static void assignPermissions(Resident resident, Player player) {
    PermissionAttachment playersAttachment = null;
    if (resident == null) {
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
        } catch (NotRegisteredException e) {
            // failed to get resident
            e.printStackTrace();
            return;
        }
    } else {
        player = BukkitTools.getPlayer(resident.getName());
    }
    if ((player == null) || !player.isOnline()) {
        attachments.remove(resident.getName());
        return;
    }
    TownyWorld World = null;
    try {
        World = TownyUniverse.getDataSource().getWorld(player.getLocation().getWorld().getName());
    } catch (NotRegisteredException e) {
        // World not registered with Towny.
        e.printStackTrace();
        return;
    }
    if (attachments.containsKey(resident.getName())) {
        playersAttachment = attachments.get(resident.getName());
    } else
        playersAttachment = BukkitTools.getPlayer(resident.getName()).addAttachment(plugin);
    try {
        synchronized (playersAttachment) {
            @SuppressWarnings("unchecked") Map<String, Boolean> orig = (Map<String, Boolean>) permissions.get(playersAttachment);
            /*
				 * Clear the map (faster than removing the attachment and
				 * recalculating)
				 */
            orig.clear();
            if (World.isUsingTowny()) {
                /*
					 * Fill with the fresh perm nodes
					 */
                orig.putAll(TownyPerms.getResidentPerms(resident));
            // System.out.print("Perms set for: " + resident.getName());
            }
            /*
				 * Tell bukkit to update it's permissions
				 */
            playersAttachment.getPermissible().recalculatePermissions();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    /*
		 * Store the attachment for future reference
		 */
    attachments.put(resident.getName(), playersAttachment);
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PermissionAttachment(org.bukkit.permissions.PermissionAttachment) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 45 with NotRegisteredException

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

the class TownyRegenAPI method regenChunk.

/**
	 * Regenerate the chunk the player is stood in and store the block data so it can be undone later.
	 * 
	 * @param player
	 */
public static void regenChunk(Player player) {
    try {
        Coord coord = Coord.parseCoord(player);
        World world = player.getWorld();
        Chunk chunk = world.getChunkAt(player.getLocation());
        int maxHeight = world.getMaxHeight();
        Object[][][] snapshot = new Object[16][maxHeight][16];
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = 0; y < maxHeight; y++) {
                    //Current block to save
                    BlockState state = chunk.getBlock(x, y, z).getState();
                    if (state instanceof org.bukkit.block.Sign) {
                        BlockSign sign = new BlockSign(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((org.bukkit.block.Sign) state).getLines());
                        sign.setLocation(state.getLocation());
                        snapshot[x][y][z] = sign;
                    } else if (state instanceof CreatureSpawner) {
                        BlockMobSpawner spawner = new BlockMobSpawner(((CreatureSpawner) state).getSpawnedType());
                        spawner.setLocation(state.getLocation());
                        spawner.setDelay(((CreatureSpawner) state).getDelay());
                        snapshot[x][y][z] = spawner;
                    } else if ((state instanceof InventoryHolder) && !(state instanceof Player)) {
                        BlockInventoryHolder holder = new BlockInventoryHolder(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), ((InventoryHolder) state).getInventory().getContents());
                        holder.setLocation(state.getLocation());
                        snapshot[x][y][z] = holder;
                    } else {
                        snapshot[x][y][z] = new BlockObject(BukkitTools.getTypeId(state), BukkitTools.getDataData(state), state.getLocation());
                    }
                }
            }
        }
        TownyUniverse.getDataSource().getResident(player.getName()).addUndo(snapshot);
        Bukkit.getWorld(player.getWorld().getName()).regenerateChunk(coord.getX(), coord.getZ());
    } catch (NotRegisteredException e) {
    // Failed to get resident
    }
}
Also used : Player(org.bukkit.entity.Player) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) World(org.bukkit.World) Chunk(org.bukkit.Chunk) CreatureSpawner(org.bukkit.block.CreatureSpawner) WorldCoord(com.palmergames.bukkit.towny.object.WorldCoord) Coord(com.palmergames.bukkit.towny.object.Coord) BlockMobSpawner(com.palmergames.bukkit.towny.regen.block.BlockMobSpawner) BlockState(org.bukkit.block.BlockState) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject) Sign(org.bukkit.block.Sign) BlockSign(com.palmergames.bukkit.towny.regen.block.BlockSign) InventoryHolder(org.bukkit.inventory.InventoryHolder) BlockInventoryHolder(com.palmergames.bukkit.towny.regen.block.BlockInventoryHolder) BlockObject(com.palmergames.bukkit.towny.regen.block.BlockObject)

Aggregations

NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)80 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)38 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)31 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)27 Resident (com.palmergames.bukkit.towny.object.Resident)24 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)19 Player (org.bukkit.entity.Player)18 Town (com.palmergames.bukkit.towny.object.Town)17 ArrayList (java.util.ArrayList)17 EventHandler (org.bukkit.event.EventHandler)17 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)14 Nation (com.palmergames.bukkit.towny.object.Nation)11 EconomyException (com.palmergames.bukkit.towny.exceptions.EconomyException)9 PlayerCache (com.palmergames.bukkit.towny.object.PlayerCache)9 IOException (java.io.IOException)9 Location (org.bukkit.Location)9 Entity (org.bukkit.entity.Entity)9 LivingEntity (org.bukkit.entity.LivingEntity)8 Coord (com.palmergames.bukkit.towny.object.Coord)7 BlockLocation (com.palmergames.bukkit.towny.regen.block.BlockLocation)7