Procházet zdrojové kódy

Fix NPE in ChunkUnloadEvent
fixes #5133 fixes #5118

nossr50 před 2 měsíci
rodič
revize
82af006ea4

+ 1 - 0
Changelog.txt

@@ -1,4 +1,5 @@
 Version 2.2.034
+    Fixed a rare edge case where null entities during chunk unload would cause a NullPointerException and potentially lead to server instability
     Fixed bug where arrow would award archery xp after a crossbow trickshot bounce
 
 Version 2.2.033

+ 13 - 4
src/main/java/com/gmail/nossr50/listeners/ChunkListener.java

@@ -15,9 +15,18 @@ public class ChunkListener implements Listener {
     public void onChunkUnload(ChunkUnloadEvent event) {
         final Chunk unloadingChunk = event.getChunk();
 
-        Arrays.stream(unloadingChunk.getEntities())
-                .filter(entity -> entity instanceof LivingEntity)
-                .map(entity -> (LivingEntity) entity)
-                .forEach(livingEntity -> mcMMO.getTransientEntityTracker().removeTrackedEntity(livingEntity));
+        // Avoid processing if chunk is null or unloaded
+        if (unloadingChunk == null || !unloadingChunk.isLoaded() || unloadingChunk.getEntities() == null) {
+            return;
+        }
+
+        try {
+            Arrays.stream(unloadingChunk.getEntities())
+                    .filter(entity -> entity instanceof LivingEntity)
+                    .map(entity -> (LivingEntity) entity)
+                    .forEach(livingEntity -> mcMMO.getTransientEntityTracker().removeTrackedEntity(livingEntity));
+        } catch (Exception ex) {
+            mcMMO.p.getLogger().warning("Caught exception during chunk unload event processing: " + ex.getMessage());
+        }
     }
 }