Search in sources :

Example 11 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class TownyLoginListener method onPlayerLogin.

@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerLogin(AsyncPlayerPreLoginEvent event) {
    String npcPrefix = TownySettings.getNPCPrefix();
    String warChest = "towny-war-chest";
    String serverAccount = TownySettings.getString(ConfigNodes.ECO_CLOSED_ECONOMY_SERVER_ACCOUNT);
    boolean disallowed = false;
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    if (event.getName().startsWith(npcPrefix)) {
        Resident npcRes = townyUniverse.getResident(event.getUniqueId());
        if (npcRes != null && npcRes.isMayor()) {
            event.disallow(Result.KICK_OTHER, "Towny is preventing you from logging in using this account name.");
            disallowed = true;
        }
    } else if (event.getName().equals(warChest) || event.getName().equals(warChest.replace("-", "_"))) {
        // Deny because this is the warChest account.
        event.disallow(Result.KICK_OTHER, "Towny is preventing you from logging in using this account name.");
        disallowed = true;
    } else if (event.getName().equals(serverAccount) || event.getName().equals(serverAccount.replace("-", "_"))) {
        // Deny because this is the closed economy server account.
        event.disallow(Result.KICK_OTHER, "Towny is preventing you from logging in using this account name.");
        disallowed = true;
    } else if (event.getName().startsWith(TownySettings.getTownAccountPrefix()) || event.getName().startsWith(TownySettings.getTownAccountPrefix().replace("-", "_")) || event.getName().startsWith(TownySettings.getNationAccountPrefix()) || event.getName().startsWith(TownySettings.getNationAccountPrefix().replace("-", "_"))) {
        event.disallow(Result.KICK_OTHER, "Towny is preventing you from logging in using this account name.");
        disallowed = true;
    }
    if (disallowed) {
        String ip = event.getAddress().toString().substring(1);
        Towny.getPlugin().getLogger().warning("A player using the IP address " + ip + " tried to log in using an account name (" + event.getName() + ") which could damage your server's economy, but was prevented by Towny. Consider banning this IP address!");
        for (Player ops : Bukkit.getOnlinePlayers()) {
            if (ops.isOp() || ops.hasPermission("towny.admin"))
                TownyMessaging.sendMsg(ops, Translatable.of("msg_admin_blocked_login", ip, event.getName()));
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse) EventHandler(org.bukkit.event.EventHandler)

Example 12 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class TownyPerms method assignPermissions.

/**
 * Register a specific residents permissions with Bukkit.
 *
 * @param resident - Resident to check if player is valid
 * @param player - Player to register permission
 */
public static void assignPermissions(Resident resident, Player player) {
    PermissionAttachment playersAttachment;
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    if (resident == null) {
        if (player != null)
            resident = townyUniverse.getResident(player.getUniqueId());
        // failed to get resident
        if (resident == null)
            return;
    } else {
        player = BukkitTools.getPlayer(resident.getName());
    }
    if ((player == null) || !player.isOnline()) {
        attachments.remove(resident.getName());
        return;
    }
    TownyWorld world = TownyAPI.getInstance().getTownyWorld(player.getWorld().getName());
    if (world == null)
        return;
    if (attachments.containsKey(resident.getName()))
        playersAttachment = attachments.get(resident.getName());
    else
        // DungeonsXL sometimes moves players which aren't online out of dungeon worlds causing an error in the log to appear.
        try {
            playersAttachment = BukkitTools.getPlayer(resident.getName()).addAttachment(plugin);
        } catch (Exception e) {
            return;
        }
    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 | IllegalAccessException e) {
        e.printStackTrace();
    }
    /*
		 * Store the attachment for future reference
		 */
    attachments.put(resident.getName(), playersAttachment);
}
Also used : TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse) PermissionAttachment(org.bukkit.permissions.PermissionAttachment) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 13 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class SetDefaultModes method run.

@Override
public void run() {
    // Is the player still available
    if (!BukkitTools.isOnline(name))
        return;
    // setup default modes
    try {
        TownyUniverse townyUniverse = TownyUniverse.getInstance();
        String modeString = townyUniverse.getPermissionSource().getPlayerPermissionStringNode(name, PermissionNodes.TOWNY_DEFAULT_MODES.getNode());
        if (modeString.isEmpty()) {
            return;
        }
        String[] modes = modeString.split(",");
        Resident resident = townyUniverse.getResident(name);
        if (resident != null)
            resident.setModes(modes, notify);
    } catch (NullPointerException ignored) {
    }
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse)

Example 14 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class TownRuinUtil method evaluateRuinedTownRemovals.

/**
 * This method cycles through all towns
 * If a town is in ruins, its remaining_ruin_time_hours counter is decreased
 * If a counter hits 0, the town is deleted
 */
public static void evaluateRuinedTownRemovals() {
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    List<Town> towns = new ArrayList<>(townyUniverse.getTowns());
    ListIterator<Town> townItr = towns.listIterator();
    Town town;
    while (townItr.hasNext()) {
        town = townItr.next();
        /*
			 * Only delete ruined town if it really still
			 * exists.
			 * We are running in an Async thread so MUST verify all objects.
			 */
        if (townyUniverse.hasTown(town.getName()) && town.isRuined() && town.getRuinedTime() != 0 && getTimeSinceRuining(town) > TownySettings.getTownRuinsMaxDurationHours()) {
            // Ruin found & recently ruined end time reached. Delete town now.
            townyUniverse.getDataSource().removeTown(town, false);
        }
    }
}
Also used : Town(com.palmergames.bukkit.towny.object.Town) ArrayList(java.util.ArrayList) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse)

Example 15 with TownyUniverse

use of com.palmergames.bukkit.towny.TownyUniverse in project Towny by TownyAdvanced.

the class ResidentPurge method run.

@Override
public void run() {
    int count = 0;
    message(Translatable.of("msg_scanning_for_old_residents"));
    TownyUniverse townyUniverse = TownyUniverse.getInstance();
    List<Resident> residentList;
    if (town != null) {
        residentList = new ArrayList<>(town.getResidents());
    } else {
        residentList = new ArrayList<>(townyUniverse.getResidents());
    }
    for (Resident resident : residentList) {
        if (!resident.isNPC() && (System.currentTimeMillis() - resident.getLastOnline() > (this.deleteTime)) && !BukkitTools.isOnline(resident.getName())) {
            if (townless && resident.hasTown()) {
                continue;
            }
            count++;
            message(Translatable.of("msg_deleting_resident", resident.getName()));
            townyUniverse.getDataSource().removeResident(resident);
        }
    }
    message(Translatable.of("msg_purge_complete", count));
}
Also used : Resident(com.palmergames.bukkit.towny.object.Resident) TownyUniverse(com.palmergames.bukkit.towny.TownyUniverse)

Aggregations

TownyUniverse (com.palmergames.bukkit.towny.TownyUniverse)27 Resident (com.palmergames.bukkit.towny.object.Resident)19 ArrayList (java.util.ArrayList)8 Player (org.bukkit.entity.Player)8 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)7 Town (com.palmergames.bukkit.towny.object.Town)6 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4 EventHandler (org.bukkit.event.EventHandler)4 Nation (com.palmergames.bukkit.towny.object.Nation)3 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)3 HashSet (java.util.HashSet)3 Invite (com.palmergames.bukkit.towny.invites.Invite)2 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)2 Translator (com.palmergames.bukkit.towny.object.Translator)2 WorldCoord (com.palmergames.bukkit.towny.object.WorldCoord)2 InvalidObjectException (java.io.InvalidObjectException)2 List (java.util.List)2 UUID (java.util.UUID)2 SiegeSide (com.gmail.goosius.siegewar.enums.SiegeSide)1 Siege (com.gmail.goosius.siegewar.objects.Siege)1