mirror of
https://github.com/BentoBoxWorld/Challenges.git
synced 2024-11-24 19:45:14 +01:00
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:
parent
326b8ca0cf
commit
edc2c455c4
@ -1,18 +1,21 @@
|
|||||||
package bentobox.addon.challenges;
|
package bentobox.addon.challenges;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
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.ChallengesCommand;
|
||||||
import bentobox.addon.challenges.commands.admin.Challenges;
|
import bentobox.addon.challenges.commands.admin.Challenges;
|
||||||
import world.bentobox.bentobox.api.addons.Addon;
|
import world.bentobox.bentobox.api.addons.Addon;
|
||||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||||
|
import world.bentobox.level.event.IslandLevelCalculatedEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add-on to BSkyBlock that enables challenges
|
* Add-on to BSkyBlock that enables challenges
|
||||||
* @author tastybento
|
* @author tastybento
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ChallengesAddon extends Addon {
|
public class ChallengesAddon extends Addon implements Listener {
|
||||||
|
|
||||||
private ChallengesManager challengesManager;
|
private ChallengesManager challengesManager;
|
||||||
private String permissionPrefix = "addon";
|
private String permissionPrefix = "addon";
|
||||||
@ -24,9 +27,15 @@ public class ChallengesAddon extends Addon {
|
|||||||
// Save default config.yml
|
// Save default config.yml
|
||||||
saveDefaultConfig();
|
saveDefaultConfig();
|
||||||
}
|
}
|
||||||
|
@EventHandler
|
||||||
|
public void onIslandLevelChange(IslandLevelCalculatedEvent event)
|
||||||
|
{
|
||||||
|
event.getResults();
|
||||||
|
Bukkit.getLogger().info("DEBUG: event called");
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
this.registerListener(this);
|
||||||
// Check if it is enabled - it might be loaded, but not enabled.
|
// Check if it is enabled - it might be loaded, but not enabled.
|
||||||
if (getPlugin() == null || !getPlugin().isEnabled()) {
|
if (getPlugin() == null || !getPlugin().isEnabled()) {
|
||||||
Bukkit.getLogger().severe("BentoBox is not available or disabled!");
|
Bukkit.getLogger().severe("BentoBox is not available or disabled!");
|
||||||
|
@ -372,9 +372,19 @@ public class ChallengesManager {
|
|||||||
lvConfig.saveConfigObject(en.getKey());
|
lvConfig.saveConfigObject(en.getKey());
|
||||||
en.getValue().forEach(chConfig::saveConfigObject);
|
en.getValue().forEach(chConfig::saveConfigObject);
|
||||||
});
|
});
|
||||||
|
savePlayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void savePlayers() {
|
||||||
playerData.values().forEach(players :: saveObject);
|
playerData.values().forEach(players :: saveObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void savePlayer(UUID playerUUID) {
|
||||||
|
if (playerData.containsKey(playerUUID)) {
|
||||||
|
players.saveObject(playerData.get(playerUUID));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save to the database
|
* Save to the database
|
||||||
* @param async - if true, saving will be done async
|
* @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) {
|
public void setChallengeComplete(User user, String challengeUniqueId, World world) {
|
||||||
addPlayer(user);
|
addPlayer(user);
|
||||||
playerData.get(user.getUniqueId()).setChallengeDone(world, challengeUniqueId);
|
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) {
|
public void setResetChallenge(User user, String challengeUniqueId, World world) {
|
||||||
addPlayer(user);
|
addPlayer(user);
|
||||||
playerData.get(user.getUniqueId()).setChallengeTimes(world, challengeUniqueId, 0);
|
playerData.get(user.getUniqueId()).setChallengeTimes(world, challengeUniqueId, 0);
|
||||||
|
// Save
|
||||||
|
savePlayer(user.getUniqueId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -261,17 +261,21 @@ public class TryToComplete {
|
|||||||
for (ItemStack i : itemsInInv) {
|
for (ItemStack i : itemsInInv) {
|
||||||
if (amountToBeRemoved > 0) {
|
if (amountToBeRemoved > 0) {
|
||||||
// Remove either the full amount or the remaining amount
|
// Remove either the full amount or the remaining amount
|
||||||
i.setAmount(Math.min(i.getAmount(), amountToBeRemoved));
|
if (i.getAmount() >= amountToBeRemoved) {
|
||||||
// Remove all of this item
|
i.setAmount(i.getAmount() - amountToBeRemoved);
|
||||||
HashMap<Integer, ItemStack> remaining = user.getInventory().removeItem(i);
|
removed.merge(i.getType(), amountToBeRemoved, Integer::sum);
|
||||||
if (!remaining.isEmpty()) {
|
amountToBeRemoved = 0;
|
||||||
remaining.forEach((k,v) -> addon.logError("Could not remove items: " + v.getType() + " x " + v.getAmount()));
|
|
||||||
} else {
|
} else {
|
||||||
amountToBeRemoved -= i.getAmount();
|
|
||||||
removed.merge(i.getType(), i.getAmount(), Integer::sum);
|
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;
|
return removed;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import static org.mockito.Mockito.mock;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -153,14 +152,13 @@ public class TryToCompleteTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testRemoveItemsFail() {
|
public void testRemoveItemsFail() {
|
||||||
HashMap<Integer, ItemStack> unremovable = new HashMap<>();
|
required.add(new ItemStack(Material.GOLD_BLOCK, 55));
|
||||||
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));
|
|
||||||
TryToComplete x = new TryToComplete(addon);
|
TryToComplete x = new TryToComplete(addon);
|
||||||
x.user(user);
|
x.user(user);
|
||||||
Map<Material, Integer> removed = x.removeItems(required);
|
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());
|
Mockito.verify(addon, Mockito.times(1)).logError(Mockito.anyString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user