package me.nallar.patched;

import java.util.Hashtable;
import java.util.logging.Level;

import cpw.mods.fml.common.FMLLog;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.unsafe.UnsafeUtil;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;

public abstract class PatchDimensionManager extends DimensionManager {
	public static void unloadWorlds(Hashtable<Integer, long[]> worldTickTimes) {
		for (int id : unloadQueue) {
			WorldServer w = worlds.get(id);
			try {
				if (w != null) {
					w.saveAllChunks(true, null);
				} else {
					FMLLog.warning("Unexpected world unload - world %d is already unloaded", id);
				}
			} catch (Exception e) {
				FMLLog.log(Level.SEVERE, e, "Exception saving chunks when unloading world " + w);
			} finally {
				if (w != null) {
					MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(w));
					w.flush();
					setWorld(id, null);
					if (TickThreading.instance.cleanWorlds) {
						UnsafeUtil.clean(w);
					}
				}
			}
		}
		unloadQueue.clear();
	}
}

@@ -119,24 +119,61 @@
 on.FMLLog;%0D%0A
+import me.nallar.tickthreading.Log;%0D%0A
 import me.na
@@ -990,24 +990,37 @@
 != null) %7B%0D%0A
+%09%09%09%09%09try %7B%0D%0A%09
 %09%09%09%09%09Minecra
@@ -1066,24 +1066,125 @@
 nload(w));%0D%0A
+%09%09%09%09%09%7D catch (Throwable t) %7B%0D%0A%09%09%09%09%09%09Log.severe(%22A mod failed to handle a world unload%22, t);%0D%0A%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09w.flush

Fix world unload spam if a mod fails to unload, repeatedly.