From edc2c455c431618240313d1a400a3783e583ddfa Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 23 Nov 2018 10:42:20 -0800 Subject: [PATCH] Fixes inventory item removal bug (for sure this time). Also saves player data after player completes a challenge or the challenge is reset. Now reloading challenges doesn't cause challenge completion loss. https://github.com/BentoBoxWorld/addon-challenges/issues/27 https://github.com/BentoBoxWorld/addon-challenges/issues/30 --- .../addon/challenges/ChallengesAddon.java | 13 +++++++++++-- .../addon/challenges/ChallengesManager.java | 15 ++++++++++++++- .../addon/challenges/panel/TryToComplete.java | 16 ++++++++++------ .../challenges/panel/TryToCompleteTest.java | 10 ++++------ 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java index 50aaca7..ee5e59f 100644 --- a/src/main/java/bentobox/addon/challenges/ChallengesAddon.java +++ b/src/main/java/bentobox/addon/challenges/ChallengesAddon.java @@ -1,18 +1,21 @@ package bentobox.addon.challenges; import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import bentobox.addon.challenges.commands.ChallengesCommand; import bentobox.addon.challenges.commands.admin.Challenges; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.level.event.IslandLevelCalculatedEvent; /** * Add-on to BSkyBlock that enables challenges * @author tastybento * */ -public class ChallengesAddon extends Addon { +public class ChallengesAddon extends Addon implements Listener { private ChallengesManager challengesManager; private String permissionPrefix = "addon"; @@ -24,9 +27,15 @@ public class ChallengesAddon extends Addon { // Save default config.yml saveDefaultConfig(); } - + @EventHandler + public void onIslandLevelChange(IslandLevelCalculatedEvent event) + { + event.getResults(); + Bukkit.getLogger().info("DEBUG: event called"); + } @Override public void onEnable() { + this.registerListener(this); // Check if it is enabled - it might be loaded, but not enabled. if (getPlugin() == null || !getPlugin().isEnabled()) { Bukkit.getLogger().severe("BentoBox is not available or disabled!"); diff --git a/src/main/java/bentobox/addon/challenges/ChallengesManager.java b/src/main/java/bentobox/addon/challenges/ChallengesManager.java index 72884f6..0e50e7c 100644 --- a/src/main/java/bentobox/addon/challenges/ChallengesManager.java +++ b/src/main/java/bentobox/addon/challenges/ChallengesManager.java @@ -372,9 +372,19 @@ public class ChallengesManager { lvConfig.saveConfigObject(en.getKey()); en.getValue().forEach(chConfig::saveConfigObject); }); + savePlayers(); + } + + private void savePlayers() { playerData.values().forEach(players :: saveObject); } + private void savePlayer(UUID playerUUID) { + if (playerData.containsKey(playerUUID)) { + players.saveObject(playerData.get(playerUUID)); + } + } + /** * Save to the database * @param async - if true, saving will be done async @@ -396,6 +406,8 @@ public class ChallengesManager { public void setChallengeComplete(User user, String challengeUniqueId, World world) { addPlayer(user); playerData.get(user.getUniqueId()).setChallengeDone(world, challengeUniqueId); + // Save + savePlayer(user.getUniqueId()); } /** @@ -407,7 +419,8 @@ public class ChallengesManager { public void setResetChallenge(User user, String challengeUniqueId, World world) { addPlayer(user); playerData.get(user.getUniqueId()).setChallengeTimes(world, challengeUniqueId, 0); - + // Save + savePlayer(user.getUniqueId()); } /** diff --git a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java index bea600d..c4dd019 100644 --- a/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java +++ b/src/main/java/bentobox/addon/challenges/panel/TryToComplete.java @@ -261,17 +261,21 @@ public class TryToComplete { for (ItemStack i : itemsInInv) { if (amountToBeRemoved > 0) { // Remove either the full amount or the remaining amount - i.setAmount(Math.min(i.getAmount(), amountToBeRemoved)); - // Remove all of this item - HashMap remaining = user.getInventory().removeItem(i); - if (!remaining.isEmpty()) { - remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v.getType() + " x " + v.getAmount())); + if (i.getAmount() >= amountToBeRemoved) { + i.setAmount(i.getAmount() - amountToBeRemoved); + removed.merge(i.getType(), amountToBeRemoved, Integer::sum); + amountToBeRemoved = 0; } else { - amountToBeRemoved -= i.getAmount(); removed.merge(i.getType(), i.getAmount(), Integer::sum); + amountToBeRemoved -= i.getAmount(); + i.setAmount(0); + } } } + if (amountToBeRemoved > 0) { + addon.logError("Could not remove " + amountToBeRemoved + " of " + req.getType() + " from player's inventory!"); + } } return removed; } diff --git a/src/test/java/bentobox/addon/challenges/panel/TryToCompleteTest.java b/src/test/java/bentobox/addon/challenges/panel/TryToCompleteTest.java index 7fc6853..8e5209f 100644 --- a/src/test/java/bentobox/addon/challenges/panel/TryToCompleteTest.java +++ b/src/test/java/bentobox/addon/challenges/panel/TryToCompleteTest.java @@ -8,7 +8,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -153,14 +152,13 @@ public class TryToCompleteTest { */ @Test public void testRemoveItemsFail() { - HashMap unremovable = new HashMap<>(); - unremovable.put(0, new ItemStack(Material.GOLD_BLOCK, 2)); - when(inv.removeItem(Mockito.any(ItemStack.class))).thenReturn(unremovable); - required.add(new ItemStack(Material.GOLD_BLOCK, 5)); + required.add(new ItemStack(Material.GOLD_BLOCK, 55)); TryToComplete x = new TryToComplete(addon); x.user(user); Map removed = x.removeItems(required); - assertTrue(removed.isEmpty()); + // It will remove 32, but not any more + assertTrue(removed.get(Material.GOLD_BLOCK) == 32); + // An error will be thrown Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString()); } }