From 1e1f05897337a2256c80f94895de6be553e9e6b7 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Tue, 8 Oct 2019 06:04:45 -0500 Subject: [PATCH] remove entities in loaded chunks (need to store others for future load) --- .../epicbosses/holder/ActiveBossHolder.java | 33 ++++++++++++++++--- .../epicbosses/holder/ActiveMinionHolder.java | 33 +++++++++++++++---- .../managers/BossEntityManager.java | 14 ++++---- .../epicbosses/panel/AutoSpawnsPanel.java | 2 +- .../AutoSpawnEntitiesEditorPanel.java | 2 +- 5 files changed, 65 insertions(+), 19 deletions(-) diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveBossHolder.java b/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveBossHolder.java index 7808885..aa34499 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveBossHolder.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveBossHolder.java @@ -15,6 +15,7 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import java.util.*; +import java.util.stream.Collectors; /** * @author Charles Cullen @@ -87,17 +88,39 @@ public class ActiveBossHolder implements IActiveHolder { this.activeMinionHolderMap.values().forEach(ActiveMinionHolder::killAll); } + public int count() { + return livingEntityMap.size() + activeMinionHolderMap.values().stream() + .map(e -> e.count()) + .reduce((e1, e2) -> e1 + e2) + .orElse(0); + } + public boolean killAllSubBosses(World world) { if (world != null && !this.location.getWorld().equals(world)) return false; - this.livingEntityMap.values().forEach(e -> { - Entity entity = ServerUtils.get().getEntity(e); - if (entity != null) - entity.remove(); +// this.livingEntityMap.values().forEach(e -> { +// Entity entity = ServerUtils.get().getEntity(e); +// if (entity != null) +// entity.remove(); +// }); +// this.livingEntityMap.clear(); + + // grab list of all valid entities by UUID that can be removed + Map toRemove = this.livingEntityMap.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> ServerUtils.get().getEntity(e.getValue()))) + .entrySet().stream() + .filter(e -> e.getValue() != null && e.getValue().getWorld().isChunkLoaded( + e.getValue().getLocation().getBlockX() >> 4, + e.getValue().getLocation().getBlockZ() >> 4)) + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); + + // remove everything we can + toRemove.entrySet().stream().forEach(e -> { + e.getValue().remove(); + livingEntityMap.remove(e.getKey()); }); - this.livingEntityMap.clear(); return true; } } diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveMinionHolder.java b/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveMinionHolder.java index a0f1f74..6f78139 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveMinionHolder.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/holder/ActiveMinionHolder.java @@ -12,6 +12,8 @@ import org.bukkit.entity.LivingEntity; import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; +import org.bukkit.entity.Entity; /** * @author Charles Cullen @@ -60,14 +62,33 @@ public class ActiveMinionHolder implements IActiveHolder { return (LivingEntity) ServerUtils.get().getEntity(target); } + public int count() { + return livingEntityMap.size(); + } + @Override public void killAll() { - for (UUID livingEntity : this.livingEntityMap.values()) { - LivingEntity target = (LivingEntity) ServerUtils.get().getEntity(livingEntity); - if (target != null) - target.remove(); - } - this.livingEntityMap.clear(); +// for (UUID livingEntity : this.livingEntityMap.values()) { +// LivingEntity target = (LivingEntity) ServerUtils.get().getEntity(livingEntity); +// if (target != null) +// target.remove(); +// } +// this.livingEntityMap.clear(); + + // grab list of all valid entities by UUID that can be removed + Map toRemove = this.livingEntityMap.entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> ServerUtils.get().getEntity(e.getValue()))) + .entrySet().stream() + .filter(e -> e.getValue() != null && e.getValue().getWorld().isChunkLoaded( + e.getValue().getLocation().getBlockX() >> 4, + e.getValue().getLocation().getBlockZ() >> 4)) + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); + + // remove everything we can + toRemove.entrySet().stream().forEach(e -> { + e.getValue().remove(); + livingEntityMap.remove(e.getKey()); + }); } @Override diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java b/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java index e4adeed..2905f2c 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/managers/BossEntityManager.java @@ -17,14 +17,12 @@ import com.songoda.epicbosses.managers.files.DropTableFileManager; import com.songoda.epicbosses.managers.files.ItemsFileManager; import com.songoda.epicbosses.managers.files.MinionsFileManager; import com.songoda.epicbosses.skills.Skill; -import com.songoda.epicbosses.skills.custom.Minions; import com.songoda.epicbosses.skills.elements.CustomMinionSkillElement; import com.songoda.epicbosses.utils.BossesGson; import com.songoda.epicbosses.utils.Debug; import com.songoda.epicbosses.utils.RandomUtils; import com.songoda.epicbosses.utils.ServerUtils; import com.songoda.epicbosses.utils.itemstack.holder.ItemStackHolder; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Item; @@ -110,10 +108,12 @@ public class BossEntityManager { for(ActiveBossHolder activeBossHolder : getActiveBossHolders()) { if(activeBossHolder.killAllSubBosses(world)) { activeBossHolder.killAllMinions(world); - activeBossHolder.setDead(true); amountOfBosses++; - ACTIVE_BOSS_HOLDERS.remove(activeBossHolder); + if(activeBossHolder.count() == 0) { + activeBossHolder.setDead(true); + ACTIVE_BOSS_HOLDERS.remove(activeBossHolder); + } } } @@ -127,9 +127,11 @@ public class BossEntityManager { if(activeBossHolder.getBossEntity().equals(bossEntity)) { activeBossHolder.killAll(); activeBossHolder.killAllMinions(); - activeBossHolder.setDead(true); - ACTIVE_BOSS_HOLDERS.remove(activeBossHolder); + if(activeBossHolder.count() == 0) { + activeBossHolder.setDead(true); + ACTIVE_BOSS_HOLDERS.remove(activeBossHolder); + } } } } diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/panel/AutoSpawnsPanel.java b/plugin-modules/Core/src/com/songoda/epicbosses/panel/AutoSpawnsPanel.java index ecc4c6d..acb889b 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/panel/AutoSpawnsPanel.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/panel/AutoSpawnsPanel.java @@ -67,7 +67,7 @@ public class AutoSpawnsPanel extends MainListPanelHandler { } Map replaceMap = new HashMap<>(); - List entities = ObjectUtils.getValue(autoSpawn.getEntities(), new ArrayList<>()); + List entities = (List) ObjectUtils.getValue(autoSpawn.getEntities(), new ArrayList<>()); AutoSpawnSettings settings = autoSpawn.getAutoSpawnSettings(); String entitiesSize = entities.size()+""; String maxAlive = ""+ObjectUtils.getValue(settings.getMaxAliveAtOnce(), 1); diff --git a/plugin-modules/Core/src/com/songoda/epicbosses/panel/autospawns/AutoSpawnEntitiesEditorPanel.java b/plugin-modules/Core/src/com/songoda/epicbosses/panel/autospawns/AutoSpawnEntitiesEditorPanel.java index 61a18f8..78869f8 100644 --- a/plugin-modules/Core/src/com/songoda/epicbosses/panel/autospawns/AutoSpawnEntitiesEditorPanel.java +++ b/plugin-modules/Core/src/com/songoda/epicbosses/panel/autospawns/AutoSpawnEntitiesEditorPanel.java @@ -83,7 +83,7 @@ public class AutoSpawnEntitiesEditorPanel extends VariablePanelHandler currentEntities, List entryList, AutoSpawn autoSpawn) { - List current = ObjectUtils.getValue(autoSpawn.getEntities(), new ArrayList<>()); + List current = (List) ObjectUtils.getValue(autoSpawn.getEntities(), new ArrayList<>()); panel.loadPage(page, (slot, realisticSlot) -> { if(slot >= entryList.size()) {