From 6f3bacdf7f4e7acc649c08f69ddf9869d14e3358 Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:18:11 +0200 Subject: [PATCH] Handle empty towns during duplicate resident removal --- .../bukkit/towny/db/TownyFlatFileSource.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java index e90cfc4976..2af736d86f 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java @@ -14,6 +14,7 @@ import com.palmergames.bukkit.towny.event.DeleteNationEvent; import com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException; import com.palmergames.bukkit.towny.exceptions.EmptyNationException; +import com.palmergames.bukkit.towny.exceptions.EmptyTownException; import com.palmergames.bukkit.towny.exceptions.InvalidNameException; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.exceptions.TownyException; @@ -465,11 +466,17 @@ public boolean loadResident(Resident resident) { universe.unregisterResident(olderRes); } catch (NotRegisteredException ignored) {} // Check if the older resident is a part of a town - if (olderRes.hasTown()) { + Town olderResTown = olderRes.getTownOrNull(); + if (olderResTown != null) { try { // Resident#removeTown saves the resident, so we can't use it. - olderRes.getTown().removeResident(olderRes); - } catch (NotRegisteredException ignored) {} + olderResTown.removeResident(olderRes); + } catch (EmptyTownException e) { + try { + universe.unregisterTown(olderResTown); + } catch (NotRegisteredException ignored) {} + deleteTown(olderResTown); + } } deleteResident(olderRes); } else { @@ -481,7 +488,7 @@ public boolean loadResident(Resident resident) { save = false; return true; } - } + } resident.setUUID(uuid); universe.registerResidentUUID(resident); }