Skip to content
Permalink
Browse files

- Create ResidentUtil for resident-related methods.

  • Loading branch information
LlmDl committed Jan 14, 2020
1 parent 3a69532 commit 7460c2187d9eb2431debe828ecbaeae86fbfc508
@@ -4073,4 +4073,4 @@ v0.92.0.11:
- When set to false the notifications will appear in the chat rather than the action bar.
- Closes #3636.
- Translated cannot-kick-that-resident strings.

- Create ResidentUtil for resident-related methods.
@@ -1,6 +1,5 @@
package com.palmergames.bukkit.towny;

import com.palmergames.bukkit.towny.command.TownCommand;
import com.palmergames.bukkit.towny.exceptions.EconomyException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
@@ -16,6 +15,7 @@
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.permissions.TownyPerms;
import com.palmergames.bukkit.towny.utils.ResidentUtil;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.ChatTools;
import com.palmergames.bukkit.util.Colors;
@@ -52,7 +52,7 @@ public static void initialize(Towny plugin) {
}

public static List<String> getFormattedOnlineResidents(String prefix, ResidentList residentList, Player player) {
List<Resident> onlineResidents = TownCommand.getOnlineResidentsViewable(player, residentList);
List<Resident> onlineResidents = ResidentUtil.getOnlineResidentsViewable(player, residentList);
return getFormattedResidents(prefix, onlineResidents);
}

@@ -41,6 +41,7 @@
import com.palmergames.bukkit.towny.object.inviteobjects.TownJoinNationInvite;
import com.palmergames.bukkit.towny.permissions.PermissionNodes;
import com.palmergames.bukkit.towny.permissions.TownyPerms;
import com.palmergames.bukkit.towny.utils.ResidentUtil;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.towny.war.flagwar.TownyWar;
import com.palmergames.bukkit.util.BukkitTools;
@@ -698,7 +699,7 @@ private void parseNationOnlineCommand(Player player, String[] split) throws Town
if (split.length > 0) {
try {
Nation nation = townyUniverse.getDataSource().getNation(split[0]);
List<Resident> onlineResidents = TownCommand.getOnlineResidentsViewable(player, nation);
List<Resident> onlineResidents = ResidentUtil.getOnlineResidentsViewable(player, nation);
if (onlineResidents.size() > 0 ) {
TownyMessaging.sendMsg(player, TownyFormatter.getFormattedOnlineResidents(TownySettings.getLangString("msg_nation_online"), nation, player));
} else {
@@ -30,7 +30,6 @@
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.ResidentList;
import com.palmergames.bukkit.towny.object.SpawnType;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
@@ -51,6 +50,7 @@
import com.palmergames.bukkit.towny.tasks.TownClaim;
import com.palmergames.bukkit.towny.utils.AreaSelectionUtil;
import com.palmergames.bukkit.towny.utils.OutpostUtil;
import com.palmergames.bukkit.towny.utils.ResidentUtil;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.towny.war.flagwar.TownyWar;
import com.palmergames.bukkit.util.BukkitTools;
@@ -910,7 +910,7 @@ private void parseTownOnlineCommand(Player player, String[] split) throws TownyE
if (split.length > 0) {
try {
Town town = townyUniverse.getDataSource().getTown(split[0]);
List<Resident> onlineResidents = getOnlineResidentsViewable(player, town);
List<Resident> onlineResidents = ResidentUtil.getOnlineResidentsViewable(player, town);
if (onlineResidents.size() > 0) {
TownyMessaging.sendMsg(player, TownyFormatter.getFormattedOnlineResidents(TownySettings.getLangString("msg_town_online"), town, player));
} else {
@@ -2362,21 +2362,13 @@ public static void townKick(Player player, String[] names) {
return;
}

townKickResidents(player, resident, town, getValidatedResidents(player, names));
townKickResidents(player, resident, town, ResidentUtil.getValidatedResidents(player, names));

// Reset everyones cache permissions as this player leaving can affect
// multiple areas.
plugin.resetCache();
}

/*
* private static List<Resident> getResidentMap(Player player, String[] names)
* { List<Resident> invited = new ArrayList<Resident>(); for (String name :
* names) try { Resident target =
* plugin.getTownyUniverse().getResident(name); invited.add(target); } catch
* (TownyException x) { TownyMessaging.sendErrorMsg(player, x.getMessage());
* } return invited; }
*/
public static void townAddResidents(Object sender, Town town, List<Resident> invited) {
String name;
if (sender instanceof Player) {
@@ -2770,7 +2762,7 @@ public static void townAdd(Object sender, Town specifiedTown, String[] names) th
}

if (names.length != 0) {
townAddResidents(sender, town, getValidatedResidents(sender, names));
townAddResidents(sender, town, ResidentUtil.getValidatedResidents(sender, names));
}

// Reset this players cached permissions
@@ -3306,38 +3298,6 @@ public static void townDeposit(Player player, Town town, int amount) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
}
}

public static List<Resident> getValidatedResidents(Object sender, String[] names) {
TownyUniverse townyUniverse = TownyUniverse.getInstance();
List<Resident> invited = new ArrayList<>();
for (String name : names) {
List<Player> matches = BukkitTools.matchPlayer(name);
if (matches.size() > 1) {
StringBuilder line = new StringBuilder("Multiple players selected: ");
for (Player p : matches)
line.append(", ").append(p.getName());
TownyMessaging.sendErrorMsg(sender, line.toString());
} else if (matches.size() == 1) {
// Match found online
try {
Resident target = townyUniverse.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 = townyUniverse.getDataSource().getResident(name);
invited.add(target);
} catch (NotRegisteredException x) {
TownyMessaging.sendErrorMsg(sender, x.getMessage());
}
}
}
return invited;
}

public static List<Resident> getValidatedResidentsForInviteRevoke(Object sender, String[] names, Town town) {
TownyUniverse townyUniverse = TownyUniverse.getInstance();
@@ -3355,24 +3315,5 @@ public static void townDeposit(Player player, Town town, int amount) {
}
return toRevoke;
}

public static List<Resident> getOnlineResidentsViewable(Player viewer, ResidentList residentList) {

List<Resident> onlineResidents = new ArrayList<>();
for (Player player : BukkitTools.getOnlinePlayers()) {
if (player != null) {
/*
* Loop town/nation resident list
*/
for (Resident resident : residentList.getResidents()) {
if (resident.getName().equalsIgnoreCase(player.getName()))
if ((viewer == null) || (viewer.canSee(BukkitTools.getPlayerExact(resident.getName())))) {
onlineResidents.add(resident);
}
}
}
}

return onlineResidents;
}

}
@@ -34,6 +34,7 @@
import com.palmergames.bukkit.towny.tasks.PlotClaim;
import com.palmergames.bukkit.towny.tasks.TownClaim;
import com.palmergames.bukkit.towny.utils.AreaSelectionUtil;
import com.palmergames.bukkit.towny.utils.ResidentUtil;
import com.palmergames.bukkit.towny.utils.SpawnUtil;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.bukkit.util.ChatTools;
@@ -601,7 +602,7 @@ public void parseAdminTownCommand(String[] split) throws TownyException {

} else if (split[1].equalsIgnoreCase("kick")) {

TownCommand.townKickResidents(getSender(), town.getMayor(), town, TownCommand.getValidatedResidents(getSender(), StringMgmt.remArgs(split, 2)));
TownCommand.townKickResidents(getSender(), town.getMayor(), town, ResidentUtil.getValidatedResidents(getSender(), StringMgmt.remArgs(split, 2)));

} else if (split[1].equalsIgnoreCase("delete")) {
if (!isConsole) {
@@ -0,0 +1,87 @@
package com.palmergames.bukkit.towny.utils;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.entity.Player;

import com.palmergames.bukkit.towny.TownyMessaging;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.ResidentList;
import com.palmergames.bukkit.util.BukkitTools;

public class ResidentUtil {

/**
* Return a list of Residents that can be seen (not vanished) by the viewer.
*
* @param viewer - Player who is looking.
* @param residentList - List of Residents which could be viewed.
* @return - List of residents that can actually be seen.
*/
public static List<Resident> getOnlineResidentsViewable(Player viewer, ResidentList residentList) {

List<Resident> onlineResidents = new ArrayList<>();
for (Player player : BukkitTools.getOnlinePlayers()) {
if (player != null) {
/*
* Loop town/nation resident list
*/
for (Resident resident : residentList.getResidents()) {
if (resident.getName().equalsIgnoreCase(player.getName()))
if ((viewer == null) || (viewer.canSee(BukkitTools.getPlayerExact(resident.getName())))) {
onlineResidents.add(resident);
}
}
}
}

return onlineResidents;
}

/**
* Transforms a String[] of names to a list of Residents.
* Uses the BukkitTools.matchPlayer() rather than BukkitTools.getPlayerExact();
* Used for:
* - Inviting
* - Kicking
*
* @param sender - CommandSender.
* @param names - Names to be converted.
* @return - List of residents to be used later.
*/
public static List<Resident> getValidatedResidents(Object sender, String[] names) {
TownyUniverse townyUniverse = TownyUniverse.getInstance();
List<Resident> residents = new ArrayList<>();
for (String name : names) {
List<Player> matches = BukkitTools.matchPlayer(name);
if (matches.size() > 1) {
StringBuilder line = new StringBuilder("Multiple players selected: ");
for (Player p : matches)
line.append(", ").append(p.getName());
TownyMessaging.sendErrorMsg(sender, line.toString());
} else if (matches.size() == 1) {
// Match found online
try {
Resident target = townyUniverse.getDataSource().getResident(matches.get(0).getName());
residents.add(target);
} catch (TownyException x) {
TownyMessaging.sendErrorMsg(sender, x.getMessage());
}
} else {
// No online matches so test for offline.
Resident target;
try {
target = townyUniverse.getDataSource().getResident(name);
residents.add(target);
} catch (NotRegisteredException x) {
TownyMessaging.sendErrorMsg(sender, x.getMessage());
}
}
}
return residents;
}
}

0 comments on commit 7460c21

Please sign in to comment.
You can’t perform that action at this time.