diff --git a/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopSpawnerUI.java b/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopSpawnerUI.java index 525620b..ebee31c 100644 --- a/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopSpawnerUI.java +++ b/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopSpawnerUI.java @@ -31,6 +31,7 @@ import pink.zak.minestom.towerdefence.queue.QueueFailureReason; import pink.zak.minestom.towerdefence.storage.MobStorage; import pink.zak.minestom.towerdefence.ui.ConfirmationUI; +import pink.zak.minestom.towerdefence.upgrade.MobUpgradeFailureReason; import pink.zak.minestom.towerdefence.upgrade.UpgradeHandler; import pink.zak.minestom.towerdefence.utils.Result; @@ -182,7 +183,14 @@ private void attemptToUnlockMob(@NotNull Player player, @NotNull UpgradeHandler } player.openInventory(new ConfirmationUI(player, Component.text("This action will cost you $" + cost + ".", NamedTextColor.RED), result -> { - if (result) upgradeHandler.unlock(mob); + if (result) { + if (upgradeHandler.unlock(mob) instanceof Result.Failure failure) { + player.sendMessage(switch (failure.reason()) { // todo: better feedback + case ALREADY_AT_LEVEL -> "You have already unlocked this mob."; + case CANNOT_AFFORD -> "You can not afford to unlock this mob."; + }); + } + } player.openInventory(this); }, false)); } diff --git a/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopUpgradeUI.java b/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopUpgradeUI.java index f931541..c4bb353 100644 --- a/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopUpgradeUI.java +++ b/src/main/java/pink/zak/minestom/towerdefence/ui/spawner/TroopUpgradeUI.java @@ -12,7 +12,9 @@ import pink.zak.minestom.towerdefence.model.mob.config.EnemyMob; import pink.zak.minestom.towerdefence.model.mob.config.EnemyMobLevel; import pink.zak.minestom.towerdefence.model.user.GameUser; +import pink.zak.minestom.towerdefence.upgrade.MobUpgradeFailureReason; import pink.zak.minestom.towerdefence.upgrade.UpgradeHandler; +import pink.zak.minestom.towerdefence.utils.Result; public final class TroopUpgradeUI extends Inventory { @@ -85,7 +87,12 @@ public void onClick(int slot) { EnemyMobLevel level = this.mob.getLevel(clickedLevel); if (level == null) return; - this.gameUser.getUpgradeHandler().upgrade(this.mob, level); + if (this.gameUser.getUpgradeHandler().upgrade(this.mob, level) instanceof Result.Failure failure) { + this.gameUser.getPlayer().sendMessage(switch (failure.reason()) { + case ALREADY_AT_LEVEL -> "You already have this level!"; + case CANNOT_AFFORD -> "You cannot afford this upgrade!"; + }); + } this.refresh(); } diff --git a/src/main/java/pink/zak/minestom/towerdefence/upgrade/MobUpgradeFailureReason.java b/src/main/java/pink/zak/minestom/towerdefence/upgrade/MobUpgradeFailureReason.java new file mode 100644 index 0000000..cc97903 --- /dev/null +++ b/src/main/java/pink/zak/minestom/towerdefence/upgrade/MobUpgradeFailureReason.java @@ -0,0 +1,8 @@ +package pink.zak.minestom.towerdefence.upgrade; + +public enum MobUpgradeFailureReason { + + ALREADY_AT_LEVEL, + CANNOT_AFFORD + +} diff --git a/src/main/java/pink/zak/minestom/towerdefence/upgrade/UpgradeHandler.java b/src/main/java/pink/zak/minestom/towerdefence/upgrade/UpgradeHandler.java index 9d87020..9590ed8 100644 --- a/src/main/java/pink/zak/minestom/towerdefence/upgrade/UpgradeHandler.java +++ b/src/main/java/pink/zak/minestom/towerdefence/upgrade/UpgradeHandler.java @@ -10,6 +10,7 @@ import pink.zak.minestom.towerdefence.model.mob.config.EnemyMob; import pink.zak.minestom.towerdefence.model.mob.config.EnemyMobLevel; import pink.zak.minestom.towerdefence.model.user.GameUser; +import pink.zak.minestom.towerdefence.utils.Result; public final class UpgradeHandler { @@ -36,27 +37,28 @@ public boolean has(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) { return Optional.ofNullable(this.mobs.get(mob)); } - public boolean unlock(@NotNull EnemyMob mob) { + public @NotNull Result unlock(@NotNull EnemyMob mob) { + //noinspection DataFlowIssue there will always be a level 1 return this.upgrade(mob, mob.getLevel(1)); } - public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) { + public @NotNull Result upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) { return this.upgrade(mob, level, false); } - public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, boolean free) { + public @NotNull Result upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, boolean free) { // get current level EnemyMobLevel currentLevel = this.getLevel(mob).orElse(null); // check if the mob is already at the level - if (currentLevel != null && currentLevel.compareTo(level) >= 0) return false; + if (currentLevel != null && currentLevel.compareTo(level) >= 0) return Result.failure(MobUpgradeFailureReason.ALREADY_AT_LEVEL); if (!free) { // calculate cost of upgrade int cost = this.getCost(mob, level); // check if user can afford upgrade - if (this.user.getCoins() < cost) return false; + if (this.user.getCoins() < cost) return Result.failure(MobUpgradeFailureReason.CANNOT_AFFORD); // charge user for upgrade this.user.updateCoins(balance -> balance - cost); @@ -72,7 +74,7 @@ public boolean upgrade(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level, bool level )); - return true; + return Result.success(); } public int getCost(@NotNull EnemyMob mob, @NotNull EnemyMobLevel level) {