homes: Fix calculation of new home count when overwriting, close #293

This commit is contained in:
William committed 2023-05-11 16:41:13 +01:00
1 parent f57f90cb67
commit 679400c616
1 file changed
+5 -5
@@ -162,24 +162,24 @@ public class HomesManager {
throw new ValidationException(ValidationException.Type.NAME_INVALID);
}
// Validate against user max homes
final int homeCount = plugin.getDatabase().getHomes(owner).size();
if (homeCount >= getMaxHomes(owner)) {
// Determine what the new home count would be & validate against user max homes
int homes = plugin.getDatabase().getHomes(owner).size() + (existingHome.isPresent() ? 0 : 1);
if (homes > getMaxHomes(owner)) {
throw new ValidationException(ValidationException.Type.REACHED_MAX_HOMES);
}
// Validate against user home slots
final SavedUser savedOwner = plugin.getSavedUser(owner)
.orElseThrow(() -> new IllegalStateException("User data not found for " + owner.getUuid()));
if (plugin.getSettings().doEconomy() && homeCount >= getFreeHomes(owner) && homeCount >= savedOwner.getHomeSlots()) {
if (plugin.getSettings().doEconomy() && homes > getFreeHomes(owner) && homes > savedOwner.getHomeSlots()) {
if (!buyAdditionalSlots || plugin.getEconomyHook().isEmpty() || !(owner instanceof OnlineUser online)) {
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_HOME_SLOTS);
}
// Perform transaction and increase user slot size
if (!plugin.canPerformTransaction(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT)) {
throw new ValidationException(ValidationException.Type.NOT_ENOUGH_MONEY);
}
plugin.performTransaction(online, EconomyHook.Action.ADDITIONAL_HOME_SLOT);
plugin.editUserData(online, (SavedUser saved) -> saved.setHomeSlots(saved.getHomeSlots() + 1));
}