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
This commit is contained in:
tastybento 2018-11-23 10:42:20 -08:00
parent 326b8ca0cf
commit edc2c455c4
4 changed files with 39 additions and 15 deletions

View File

@ -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!");

View File

@ -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());
}
/**

View File

@ -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<Integer, ItemStack> 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;
}

View File

@ -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<Integer, ItemStack> 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<Material, Integer> 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());
}
}