Fixed an issue with loot chest unregistration

This commit is contained in:
Indyuce 2022-02-26 22:44:11 +01:00
parent 52c2a07e47
commit cb460b7752
3 changed files with 24 additions and 8 deletions

View File

@ -58,7 +58,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level;
public class MMOCore extends LuminePlugin {
@ -200,8 +200,14 @@ public class MMOCore extends LuminePlugin {
*/
new BukkitRunnable() {
public void run() {
for (LootChest chest : new HashSet<>(lootChests.getActive()))
if (chest.shouldExpire()) chest.unregister(false);
Iterator<LootChest> iterator = lootChests.getActive().iterator();
while (iterator.hasNext()) {
LootChest next = iterator.next();
if (next.shouldExpire()) {
iterator.remove();
next.expire(false);
}
}
}
}.runTaskTimer(this, 5 * 60 * 20, 5 * 60 * 20);
@ -369,7 +375,7 @@ public class MMOCore extends LuminePlugin {
mineManager.resetRemainingBlocks();
// Clear spawned loot chests
lootChests.getActive().forEach(chest -> chest.unregister(false));
lootChests.getActive().forEach(chest -> chest.expire(false));
}
/**

View File

@ -17,6 +17,6 @@ public class LootableChestsListener implements Listener {
Chest chest = (Chest) event.getInventory().getHolder();
LootChest lootChest = MMOCore.plugin.lootChests.getChest(chest.getLocation());
if (lootChest != null)
lootChest.unregister(true);
lootChest.expire(true);
}
}

View File

@ -2,7 +2,7 @@ package net.Indyuce.mmocore.loot.chest;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.manager.SoundManager;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
@ -12,13 +12,18 @@ import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import javax.annotation.Nullable;
public class LootChest {
private final ChestTier tier;
private final LootChestRegion region;
private final ReplacedBlock block;
@Nullable
private final BukkitRunnable effectRunnable;
private final long date = System.currentTimeMillis();
private boolean active = true;
/**
* Called when a loot chest is placed as a Bukkit block, and used
* to save the data of the block which has been replaced.
@ -57,19 +62,24 @@ public class LootChest {
}
/**
* This does NOT remove the loot chest from the plugin registry.
*
* @param player If a player triggered the unregistration of that chest by
* opening and then closing it for the first time. It's set
* to false when a loot chest expires or when MMOCore disables.
* <p>
* When no player is closing the chest, its content should be lost
*/
public void unregister(boolean player) {
public void expire(boolean player) {
// Check for expire
Validate.isTrue(active, "Chest has already expired");
active = false;
// If a player is responsible of closing the chest, close it with sound
if (player) {
MMOCore.plugin.soundManager.getSound(SoundEvent.CLOSE_LOOT_CHEST).playAt(block.loc);
block.loc.getWorld().spawnParticle(Particle.CRIT, block.loc.clone().add(.5, .5, .5), 16, 0, 0, 0, .5);
MMOCore.plugin.lootChests.unregister(this);
}
/*