Browse Source

Remove COTW entities on chunk unload
Fixes #4289

nossr50 4 years ago
parent
commit
8c52884ac6

+ 1 - 0
Changelog.txt

@@ -2,6 +2,7 @@ Version 2.1.163
     Fixed the translate URL pointing to the wrong place (thanks chew)
     Fixed a bug where FlatFile databases would always attempt a UUID conversion task every save operation (every 10 minutes) causing console spam
     mcMMO will no longer throw errors when incoming XP is below 0 (it will just silently cancel the operation)
+    COTW Summoned entities are now removed when the chunk they are in is unloaded (prevents some exploits)
 
     NOTES:
     I often test in SQL environments so I missed this bug, reminder to come bother me on discord if you find any annoying bugs!

+ 30 - 0
src/main/java/com/gmail/nossr50/listeners/ChunkListener.java

@@ -0,0 +1,30 @@
+package com.gmail.nossr50.listeners;
+
+import com.gmail.nossr50.mcMMO;
+import com.gmail.nossr50.util.compat.layers.persistentdata.MobMetaFlagType;
+import org.bukkit.entity.Entity;
+import org.bukkit.entity.LivingEntity;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.world.ChunkUnloadEvent;
+
+public class ChunkListener implements Listener {
+
+    @EventHandler(ignoreCancelled = true)
+    public void onChunkUnload(ChunkUnloadEvent event) {
+        for(Entity entity : event.getChunk().getEntities()) {
+            if(entity instanceof LivingEntity) {
+                LivingEntity livingEntity = (LivingEntity) entity;
+                if(mcMMO.getCompatibilityManager().getPersistentDataLayer().hasMobFlag(MobMetaFlagType.COTW_SUMMONED_MOB, livingEntity)) {
+
+                    //Remove from existence
+                    if(livingEntity.isValid()) {
+                        mcMMO.getCompatibilityManager().getPersistentDataLayer().removeMobFlags(livingEntity);
+                        livingEntity.setHealth(0);
+                        livingEntity.remove();
+                    }
+                }
+            }
+        }
+    }
+}

+ 1 - 0
src/main/java/com/gmail/nossr50/skills/taming/TamingManager.java

@@ -570,6 +570,7 @@ public class TamingManager extends SkillManager {
 
                 //Remove from existence
                 if(livingEntity != null && livingEntity.isValid()) {
+                    mcMMO.getCompatibilityManager().getPersistentDataLayer().removeMobFlags(livingEntity);
                     livingEntity.setHealth(0);
                     livingEntity.remove();
                 }