Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towny with less than 100 NotRegisteredException mentions. #6502

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1869,5 +1869,16 @@ msg_warn_are_you_sure_you_want_to_transfer_nation_ownership: "<red>Are you sure
msg_warn_town_already_capital: "<red>The town of %s is already the nation capital."
msg_min_adjacent_blocks: 'You are required to have at least %s adjacent town blocks. There is only %s.'


# Shown when a player with /plot evict permissions uses /plot claim on an owned but not for sale plot, please preserve comma space at the start when translating.
msg_plot_claim_consider_evict_instead: ', perhaps you are looking for /plot evict instead?'

msg_you_are_unknown: "You are not known to Towny."
msg_townyobject_unknown: "%s is not known to Towny."
msg_err_resident_unknown: "%s is not a resident that is recognized."
msg_err_town_unknown: "%s is not a town that is recognized."
msg_err_nation_unknown: "%s is not a nation that is recognized."
msg_err_townyobject_x_has_no_town: "%s does not belong to any town."
msg_err_townyobject_x_has_no_nation: "%s does not belong to any nation."

msg_err_townblock_has_no_claimedat_data: "This townblock has no data on when it was claimed."
5 changes: 3 additions & 2 deletions src/com/palmergames/bukkit/towny/TownyAsciiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ public static void generateAndSend(Towny plugin, Player player, int lineHeight)
for (int tbx = pos.getZ() - halfLineHeight; tbx <= pos.getZ() + (lineHeight - halfLineHeight - 1); tbx++) {
try {
townyMap[y][x] = Component.empty().color(NamedTextColor.WHITE);
TownBlock townblock = world.getTownBlock(tby, tbx);
if (!townblock.hasTown())
WorldCoord wc = new WorldCoord(world.getBukkitWorld(), new Coord(tby, tbx));
if (wc.isWilderness())
throw new TownyException();
TownBlock townblock = wc.getTownBlockOrNull();
Town town = townblock.getTownOrNull();
if (x == halfLineHeight && y == halfLineWidth)
// This is the player's location, colour it special.
Expand Down
40 changes: 40 additions & 0 deletions src/com/palmergames/bukkit/towny/TownyUniverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,46 @@ public void unregisterResident(@NotNull Resident resident) throws NotRegisteredE
}
}

/**
* Used internally by Towny to correct resident UUIDs which were improperly
* recorded. (Probably something to do with offline servers.)
*
* @param resident Resident that exists in the correct name maps.
* @param uuid UUID that the Resident should have.
*/
public void changeResidentUUID(@NotNull Resident resident, @NotNull UUID uuid) {
Preconditions.checkNotNull(resident, "Resident cannot be null!");
Preconditions.checkNotNull(uuid, "UUID cannot be null!");

Preconditions.checkState(residentNameMap.containsKey(resident.getName()), "ResidentNameMap must contain resident!");
Preconditions.checkState(residentUUIDMap.containsKey(uuid), "ResidentUUIDMap must contain resident!");

try {
unregisterResident(resident); // Unregister
resident.setUUID(uuid); // Set proper UUID.
registerResident(resident); // Re-register.
} catch (NotRegisteredException | AlreadyRegisteredException ignored) {} // Ignored because we know we will be successful.
}

public void changeResidentName(@NotNull Resident resident, @NotNull String name) {
Preconditions.checkNotNull(resident, "Resident cannot be null!");
Preconditions.checkNotNull(name, "Name cannot be null!");

Preconditions.checkState(residentNameMap.containsKey(resident.getName()), "ResidentNameMap must contain resident!");
try {
// Forcefully remove the resident from the Universe.
residentNameMap.remove(resident.getName().toLowerCase(Locale.ROOT));
residentsTrie.removeKey(resident.getName());
if (resident.getUUID() != null)
residentUUIDMap.remove(resident.getUUID());

resident.setName(name); // Set proper name.
registerResident(resident); // Re-register.
} catch (AlreadyRegisteredException e) {
e.printStackTrace();
}
}

@Unmodifiable
public Collection<Resident> getResidents() {
return Collections.unmodifiableCollection(residentNameMap.values());
Expand Down
34 changes: 17 additions & 17 deletions src/com/palmergames/bukkit/towny/command/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyUniverse;
import com.palmergames.bukkit.towny.exceptions.NoPermissionException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
//import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
Expand Down Expand Up @@ -273,88 +273,88 @@ protected static Optional<Boolean> parseToggleChoice(String str) {

@NotNull
protected static Town getTownFromPlayerOrThrow(Player player) throws TownyException {
return getTownFromResidentOrThrow(getResidentOrThrow(player.getUniqueId()));
return getTownFromResidentOrThrow(getResidentOrThrow(player));
}

@NotNull
protected static Town getTownFromResidentOrThrow(@NotNull Resident resident) throws TownyException {
if (!resident.hasTown())
throw new TownyException(Translatable.of("msg_err_dont_belong_town"));
throw new TownyException(Translatable.of("msg_err_townyobject_x_has_no_town", resident));
return resident.getTownOrNull();
}

@NotNull
protected static Resident getResidentOrThrow(UUID playerUUID) throws NotRegisteredException {
protected static Resident getResidentOrThrow(UUID playerUUID) throws TownyException {
Resident res = TownyUniverse.getInstance().getResident(playerUUID);

if (res == null) {
throw new NotRegisteredException(Translatable.of("msg_err_not_registered"));
throw new TownyException(Translatable.of("msg_err_not_registered"));
}

return res;
}

@NotNull
@Contract("null -> fail")
protected static Resident getResidentOrThrow(@Nullable Player player) throws NotRegisteredException {
protected static Resident getResidentOrThrow(@Nullable Player player) throws TownyException {
Resident resident = player == null ? null : TownyAPI.getInstance().getResident(player);

if (resident == null)
throw new NotRegisteredException(Translatable.of("msg_err_not_registered"));
throw new TownyException(Translatable.of("msg_err_resident_unknown", player.getName()));

return resident;
}

@NotNull
protected static Resident getResidentOrThrow(String residentName) throws NotRegisteredException {
protected static Resident getResidentOrThrow(String residentName) throws TownyException {
Resident res = TownyUniverse.getInstance().getResident(residentName);

if (res == null) {
throw new NotRegisteredException(Translatable.of("msg_err_not_registered_1", residentName));
throw new TownyException(Translatable.of("msg_err_resident_unknown", residentName));
}

return res;
}

@NotNull
protected static Town getTownOrThrow(String townName) throws NotRegisteredException {
protected static Town getTownOrThrow(String townName) throws TownyException {
Town town = TownyUniverse.getInstance().getTown(townName);

if (town == null) {
throw new NotRegisteredException(Translatable.of("msg_err_not_registered_1", townName));
throw new TownyException(Translatable.of("msg_err_town_unknown", townName));
}

return town;
}

@NotNull
protected static Nation getNationOrThrow(String nationName) throws NotRegisteredException {
protected static Nation getNationOrThrow(String nationName) throws TownyException {
Nation nation = TownyUniverse.getInstance().getNation(nationName);

if (nation == null)
throw new NotRegisteredException(Translatable.of("msg_err_not_registered_1", nationName));
throw new TownyException(Translatable.of("msg_err_nation_unknown", nationName));

return nation;
}

@NotNull
protected static Nation getNationFromPlayerOrThrow(Player player) throws TownyException {
return getNationFromResidentOrThrow(getResidentOrThrow(player.getUniqueId()));
return getNationFromResidentOrThrow(getResidentOrThrow(player));
}

@NotNull
protected static Nation getNationFromResidentOrThrow(Resident resident) throws TownyException {
if (!resident.hasNation())
throw new TownyException(Translatable.of("msg_err_dont_belong_nation"));
throw new TownyException(Translatable.of("msg_err_townyobject_x_has_no_nation", resident));
return resident.getNationOrNull();
}

@NotNull
protected static Nation getNationFromTownOrThrow(Town town) throws NotRegisteredException {
protected static Nation getNationFromTownOrThrow(Town town) throws TownyException {
Nation nation = town.getNationOrNull();

if (nation == null)
throw new NotRegisteredException(Translatable.of("msg_err_town_doesnt_belong_to_any_nation"));
throw new TownyException(Translatable.of("msg_err_townyobject_x_has_no_nation", town));

return nation;
}
Expand Down
51 changes: 25 additions & 26 deletions src/com/palmergames/bukkit/towny/command/NationCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.palmergames.bukkit.towny.event.nation.NationKingChangeEvent;
import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException;
import com.palmergames.bukkit.towny.exceptions.InvalidNameException;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.exceptions.TownyException;
import com.palmergames.bukkit.towny.invites.Invite;
import com.palmergames.bukkit.towny.invites.InviteHandler;
Expand Down Expand Up @@ -649,8 +648,8 @@ private void parseNationJoin(Player player, String[] args) {

nationName = args[0];

resident = getResidentOrThrow(player.getUniqueId());
town = resident.getTown();
resident = getResidentOrThrow(player);
town = getTownFromResidentOrThrow(resident);
nation = getNationOrThrow(nationName);

// Check if town is currently in a nation.
Expand Down Expand Up @@ -752,7 +751,7 @@ public void nationRank(Player player, String[] split) throws TownyException {
return;
}

Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Resident target = getResidentOrThrow(split[1]);
Nation nation = getNationFromResidentOrThrow(resident);

Expand Down Expand Up @@ -895,20 +894,21 @@ public void listNations(CommandSender sender, String[] split) throws TownyExcept
}

private void newNation(Player player, String[] split) throws TownyException {
Resident resident = getResidentOrThrow(player.getUniqueId());
if (TownySettings.getNumResidentsCreateNation() > 0 && resident.getTown().getNumResidents() < TownySettings.getNumResidentsCreateNation())
Resident resident = getResidentOrThrow(player);
Town town = getTownFromResidentOrThrow(resident);
if (TownySettings.getNumResidentsCreateNation() > 0 && town.getNumResidents() < TownySettings.getNumResidentsCreateNation())
throw new TownyException(Translatable.of("msg_err_not_enough_residents_new_nation"));

if (split.length == 1)
throw new TownyException(Translatable.of("msg_specify_nation_name"));

if (!resident.isMayor() && !resident.getTown().hasResidentWithRank(resident, "assistant"))
if (!resident.isMayor() && !town.hasResidentWithRank(resident, "assistant"))
throw new TownyException(Translatable.of("msg_peasant_right"));

boolean noCharge = TownySettings.getNewNationPrice() == 0.0 || !TownyEconomyHandler.isActive();

String nationName = String.join("_", StringMgmt.remFirstArg(split));
newNation(player, nationName, resident.getTown(), noCharge);
newNation(player, nationName, town, noCharge);
}

public static void newNation(Player player, String name, Town capitalTown, boolean noCharge) {
Expand Down Expand Up @@ -955,7 +955,7 @@ public static void newNation(CommandSender sender, String name, Town capitalTown
Confirmation.runOnAccept(() -> {
try {
newNation(finalName, capitalTown);
} catch (AlreadyRegisteredException | NotRegisteredException e) {
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(sender, e.getMessage(sender));
return;
}
Expand All @@ -977,7 +977,7 @@ public static void newNation(CommandSender sender, String name, Town capitalTown
}
}

public static Nation newNation(String name, Town town) throws AlreadyRegisteredException, NotRegisteredException {
public static Nation newNation(String name, Town town) throws TownyException {
TownyUniverse townyUniverse = TownyUniverse.getInstance();

UUID nationUUID = UUID.randomUUID();
Expand All @@ -987,7 +987,7 @@ public static Nation newNation(String name, Town town) throws AlreadyRegisteredE
// Should never happen
if (nation == null) {
TownyMessaging.sendErrorMsg(String.format("Error fetching new nation with name %s; it was not properly registered!", name));
throw new NotRegisteredException(Translatable.of("msg_err_not_registered_1", name));
throw new TownyException(Translatable.of("msg_err_not_registered_1", name));
}
nation.setRegistered(System.currentTimeMillis());
nation.setMapColorHexCode(MapUtil.generateRandomNationColourAsHexCode());
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public static void mergeNation(CommandSender sender, String[] split, @NotNull Na
throw new TownyException(Translatable.of("msg_specify_nation_name"));

String name = split[0];
if (!admin && sender instanceof Player player && !getResidentOrThrow(player.getUniqueId()).isKing())
if (!admin && sender instanceof Player player && !getResidentOrThrow(player).isKing())
throw new TownyException(Translatable.of("msg_err_merging_for_kings_only"));

Nation nation = TownyUniverse.getInstance().getNation(name);
Expand Down Expand Up @@ -1057,7 +1057,7 @@ public static void mergeNation(CommandSender sender, String[] split, @NotNull Na
}

public void nationLeave(Player player) throws TownyException {
Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Town town = getTownFromResidentOrThrow(resident);
Nation nation = getNationFromResidentOrThrow(resident);

Expand Down Expand Up @@ -1093,7 +1093,7 @@ public void nationDelete(Player player, String[] split) {
// Player is using "/n delete"
if (split.length == 0) {
try {
Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Town town = getTownFromResidentOrThrow(resident);
Nation nation = getNationFromResidentOrThrow(resident);
// Check that the capital wont have too many residents after deletion.
Expand Down Expand Up @@ -1165,13 +1165,12 @@ public void nationAdd(Player player, String[] names) throws TownyException {
continue;
}

Town town = null;
try {
town = getTownOrThrow(townname);
} catch (NotRegisteredException e) {
Town town = TownyAPI.getInstance().getTown(townname);
if (town == null) {
// The Town doesn't actually exist or was mis-spelled.
removeinvites.add(townname);
continue;

}

if (nation.hasTown(town) || town.hasNation()) {
Expand Down Expand Up @@ -1424,7 +1423,7 @@ private void nationAlly(Player player, String[] split) throws TownyException {
HelpMenu.ALLIES_STRING.send(player);
return;
}
Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Nation nation = getNationFromResidentOrThrow(resident);

switch (split[0].toLowerCase()) {
Expand Down Expand Up @@ -1785,7 +1784,7 @@ public void nationEnemy(Player player, String[] split) throws TownyException {
return;
}

Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Nation nation = getNationFromResidentOrThrow(resident);

ArrayList<Nation> list = new ArrayList<>();
Expand Down Expand Up @@ -1906,7 +1905,7 @@ public static void nationSet(CommandSender sender, String[] split, boolean admin
Resident resident;
try {
if (!admin && sender instanceof Player player) {
resident = getResidentOrThrow(player.getUniqueId());
resident = getResidentOrThrow(player);
nation = getNationFromResidentOrThrow(resident);
} else // treat resident as king for testing purposes.
resident = nation.getKing();
Expand Down Expand Up @@ -2023,7 +2022,7 @@ private static void nationSetBoard(CommandSender sender, Nation nation, String[]
}
}

private static void nationSetSurname(CommandSender sender, Nation nation, Resident resident, String[] split, boolean admin) throws NotRegisteredException {
private static void nationSetSurname(CommandSender sender, Nation nation, Resident resident, String[] split, boolean admin) throws TownyException {
// Give the resident a title
if (split.length < 2)
TownyMessaging.sendErrorMsg(sender, "Eg: /nation set surname bilbo the dwarf ");
Expand Down Expand Up @@ -2062,7 +2061,7 @@ private static void nationSetSurname(CommandSender sender, Nation nation, Reside

}

private static void nationSetTitle(CommandSender sender, Nation nation, Resident resident, String[] split, boolean admin) throws NotRegisteredException {
private static void nationSetTitle(CommandSender sender, Nation nation, Resident resident, String[] split, boolean admin) throws TownyException {
// Give the resident a title
if (split.length < 2)
TownyMessaging.sendErrorMsg(sender, "Eg: /nation set title bilbo Jester ");
Expand Down Expand Up @@ -2296,7 +2295,7 @@ private static void nationSetKing(CommandSender sender, Nation nation, String[]
if (!newKing.isMayor())
throw new TownyException(Translatable.of("msg_err_new_king_notmayor"));

changeNationOwnership(sender, nation, getResidentOrThrow(split[1]).getTown(), admin);
changeNationOwnership(sender, nation, newKing.getTownOrNull(), admin);
} catch (TownyException e) {
TownyMessaging.sendErrorMsg(sender, e.getMessage(sender));
}
Expand Down Expand Up @@ -2357,7 +2356,7 @@ public static void nationToggle(CommandSender sender, String[] split, boolean ad
Resident resident;

if (!admin) {
resident = getResidentOrThrow(((Player) sender).getUniqueId());
resident = getResidentOrThrow((Player) sender);
nation = getNationFromResidentOrThrow(resident);
} else // Treat any resident tests as though the king were doing it.
resident = nation.getKing();
Expand Down Expand Up @@ -2524,7 +2523,7 @@ private static void nationTransaction(Player player, String[] args, boolean with
}

try {
Resident resident = getResidentOrThrow(player.getUniqueId());
Resident resident = getResidentOrThrow(player);
Nation nation = getNationFromResidentOrThrow(resident);

if (args.length < 2 || args.length > 3)
Expand Down
Loading