Cleanup entity data on chunk and world unload

Fixes #4720
This commit is contained in:
nossr50
2022-01-14 17:08:58 -08:00
parent 2e0a371ed8
commit f8e3111ec0
6 changed files with 48 additions and 3 deletions

View File

@@ -38,5 +38,14 @@ public class PersistentDataConfig extends BukkitConfig {
return config.getBoolean("mcMMO_Region_System.Enabled", true);
}
public boolean cleanupMobDataOnChunkUnload() {
return config.getBoolean("Persistent_Data.Advanced.Transient_Memory_Cleanup.On_Chunk_Unload", true);
}
public boolean cleanupMobDataOnWorldUnload() {
return config.getBoolean("Persistent_Data.Advanced.Transient_Memory_Cleanup.On_World_Unload", true);
}
}

View File

@@ -664,7 +664,7 @@ public class EntityListener implements Listener {
* @param event
* The event to watch
*/
@EventHandler(priority = EventPriority.LOWEST)
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeathLowest(EntityDeathEvent event) {
mcMMO.getTransientMetadataTools().cleanLivingEntityMetadata(event.getEntity());
}

View File

@@ -1,9 +1,12 @@
package com.gmail.nossr50.listeners;
import com.gmail.nossr50.config.PersistentDataConfig;
import com.gmail.nossr50.config.WorldBlacklist;
import com.gmail.nossr50.mcMMO;
import org.bukkit.Chunk;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@@ -39,13 +42,23 @@ public class WorldListener implements Listener {
*
* @param event The event to watch
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onWorldUnload(WorldUnloadEvent event) {
/* WORLD BLACKLIST CHECK */
if(WorldBlacklist.isWorldBlacklisted(event.getWorld()))
return;
mcMMO.getPlaceStore().unloadWorld(event.getWorld());
if(PersistentDataConfig.getInstance().cleanupMobDataOnWorldUnload())
{
for(Entity entity : event.getWorld().getEntities()) {
if(entity instanceof LivingEntity livingEntity) {
mcMMO.getTransientMetadataTools().cleanLivingEntityMetadata(livingEntity);
}
}
}
}
/**
@@ -62,5 +75,14 @@ public class WorldListener implements Listener {
Chunk chunk = event.getChunk();
mcMMO.getPlaceStore().chunkUnloaded(chunk.getX(), chunk.getZ(), event.getWorld());
if(PersistentDataConfig.getInstance().cleanupMobDataOnChunkUnload()) {
for(Entity entity : event.getWorld().getEntities()) {
if(entity instanceof LivingEntity livingEntity) {
mcMMO.getTransientMetadataTools().cleanLivingEntityMetadata(livingEntity);
}
}
}
}
}