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

Handle empty towns during duplicate resident removal #7474

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Comment on lines +475 to +478
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try {
universe.unregisterTown(olderResTown);
} catch (NotRegisteredException ignored) {}
deleteTown(olderResTown);
TownyUniverse.getInstance().getDataSource().removeTown(town, DeleteTownEvent.Cause.NO_RESIDENTS, null, false);

Copy link
Member

@LlmDl LlmDl Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could just call resident.removeTown() and this would be taken care of I think.

I see the comment saying we cannot use resident.removeTown()...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The town & townblocks are not loaded yet at this point so I figured we could get away with unregistering it and deleting the file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

touché

}
}
deleteResident(olderRes);
} else {
Expand All @@ -481,7 +488,7 @@ public boolean loadResident(Resident resident) {
save = false;
return true;
}
}
}
resident.setUUID(uuid);
universe.registerResidentUUID(resident);
}
Expand Down
Loading