From 679400c616c233b612752cb8c574c101ee1321db Mon Sep 17 00:00:00 2001 From: William Date: Thu, 11 May 2023 16:41:13 +0100 Subject: [PATCH] homes: Fix calculation of new home count when overwriting, close #293 --- .../net/william278/huskhomes/manager/HomesManager.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java b/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java index 97a6500c..ef46024b 100644 --- a/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java +++ b/common/src/main/java/net/william278/huskhomes/manager/HomesManager.java @@ -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)); }