Cull FakePlayers when worlds are unloaded.

This commit is contained in:
Lex Manos 2014-02-11 18:25:33 -08:00
parent c8e0b4a193
commit b43d63a7ad
2 changed files with 24 additions and 2 deletions

View file

@ -3,11 +3,12 @@ package net.minecraftforge.common;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.event.entity.*;
import net.minecraftforge.event.world.WorldEvent;
@ -73,5 +74,7 @@ public class ForgeInternalHandler
public void onDimensionUnload(WorldEvent.Unload event)
{
ForgeChunkManager.unloadWorld(event.world);
if (event.world instanceof WorldServer)
FakePlayerFactory.unloadWorld((WorldServer)event.world);
}
}

View file

@ -1,10 +1,14 @@
package net.minecraftforge.common.util;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
//To be expanded for generic Mod fake players?
public class FakePlayerFactory
@ -24,7 +28,9 @@ public class FakePlayerFactory
}
/**
* Get a fake player with a given username
* Get a fake player with a given username,
* Mods should either hold weak references to the return value, or listen for a
* WorldEvent.Unload and kill all references to prevent worlds staying in memory.
*/
public static FakePlayer get(WorldServer world, GameProfile username)
{
@ -36,4 +42,17 @@ public class FakePlayerFactory
return fakePlayers.get(username);
}
public static void unloadWorld(WorldServer world)
{
Iterator<Entry<GameProfile, FakePlayer>> itr = fakePlayers.entrySet().iterator();
while (itr.hasNext())
{
Entry<GameProfile, FakePlayer> entry = itr.next();
if (entry.getValue().worldObj == world)
{
itr.remove();
}
}
}
}