<commit_before>package StevenDimDoors.mod_pocketDim.helpers;
/**
 * This class regulates all the operations involving the storage and manipulation of dimensions. It handles saving dim data, teleporting the player, and 
 * creating/registering new dimensions as well as loading old dimensions on startup
 * @Return
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet41EntityEffect;
import net.minecraft.network.packet.Packet43Experience;
import net.minecraft.network.packet.Packet9Respawn;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraftforge.common.DimensionManager;
import StevenDimDoors.mod_pocketDim.DDProperties;
import StevenDimDoors.mod_pocketDim.DimData;
import StevenDimDoors.mod_pocketDim.LinkData;
import StevenDimDoors.mod_pocketDim.ObjectSaveInputStream;
import StevenDimDoors.mod_pocketDim.PacketHandler;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.SchematicLoader;
import StevenDimDoors.mod_pocketDim.TileEntityRift;
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.schematic.BlockRotator;
import StevenDimDoors.mod_pocketDim.world.LimboProvider;
import StevenDimDoors.mod_pocketDim.world.PocketProvider;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;

public class dimHelper extends DimensionManager
{
	/**
	 * hashMap for the private pocket dimensions
	 */
	
	public static HashMap<String, DimData> privatePockets = new HashMap<String, DimData>();
	/**
	 * HashMap containing all the dims registered with DimDoors, sorted by dim ID. loaded on startup
	 * @Return
	 */
	public static HashMap<Integer, DimData> dimList=new HashMap<Integer, DimData>();
	public static boolean isSaving=false;
	
	/**
	 * ArrayList containing any blocks in limbo that have been placed by the player. Cycled through in the common tick manager
	 * @Return
	 */
	public static ArrayList<Point3D> blocksToDecay = new ArrayList<Point3D>();
	
	/**
	 * instance of the dimHelper
	 * @Return
	 */
	public static dimHelper instance = new dimHelper();
	
	/**
	 * HashMap for temporary storage of Link Signature damage hash values. See itemLinkSignature for more details
	 * @Return
	 */
	public HashMap<Integer, LinkData> interDimLinkList= new HashMap<Integer,LinkData>();
	
	/**
	 * ArrayList containing all link data not sorted for easy random access, used for random doors and for recreating rifts if they have a block placed over them. 
	 * See the common tick manager and the Chaos door for details on usage
	 * @Return
	 */
	//public ArrayList<LinkData> linksForRendering =new ArrayList<LinkData>();
	Random rand= new Random();
	
	public static final int DEFAULT_POCKET_SIZE = 39;
	public static final int DEFAULT_POCKET_WALL_THICKNESS = 5;
	public static final int MAX_WORLD_HEIGHT = 254;
	
	public int getDimDepth(int DimID)
	{
		if (dimList.containsKey(DimID))
		{
			return dimList.get(DimID).depth;
		}
		else return 1;
	}
	
	private Entity teleportEntity(World oldWorld, Entity entity, LinkData link) //this beautiful teleport method is based off of xCompWiz's teleport function. 
	{
		Entity cart=entity.ridingEntity;
		if(entity.riddenByEntity!=null)
		{
			return this.teleportEntity(oldWorld,entity.riddenByEntity, link);
		}
		
		if (entity.ridingEntity != null)
		{
			entity.mountEntity(null);
			cart = teleportEntity(oldWorld, cart, link);
		}
		
		WorldServer newWorld;
		
		if(DimensionManager.getWorld(link.destDimID)==null)
		{
			DimensionManager.initDimension(link.destDimID);
		}
		boolean difDest = link.destDimID != link.locDimID;
		
		if(difDest)
		{
			newWorld = DimensionManager.getWorld(link.destDimID);
		}
		else
		{
			newWorld=(WorldServer)oldWorld;
		}
		
		mod_pocketDim.teleporter.placeInPortal(entity, newWorld, link);

	    if ((entity instanceof EntityPlayerMP)) 
	    {
	    	EntityPlayerMP player = (EntityPlayerMP)entity;
	    	if (difDest) 
	    	{
				GameRegistry.onPlayerChangedDimension((EntityPlayer)entity);

	    		player.dimension = link.destDimID;
	    		player.playerNetServerHandler.sendPacketToPlayer(new Packet9Respawn(player.dimension, (byte)player.worldObj.difficultySetting, newWorld.getWorldInfo().getTerrainType(), newWorld.getHeight(), player.theItemInWorldManager.getGameType()));
	    	
	    		WorldServer.class.cast(oldWorld).removeEntity(player);
	    		player.isDead=false;
	    		
	    		oldWorld.playerEntities.remove(player);
	    		WorldServer.class.cast(oldWorld).getPlayerManager().removePlayer(player);
	    		newWorld.getPlayerManager().addPlayer(player);
	    		
	    		player.theItemInWorldManager.setWorld((WorldServer)newWorld);
		    	
		    	player.mcServer.getConfigurationManager().updateTimeAndWeatherForPlayer(player, (WorldServer)newWorld);
		    	player.mcServer.getConfigurationManager().syncPlayerInventory(player);
		      	
		    	for(Object potionEffect : player.getActivePotionEffects())
		    	{
		    		PotionEffect effect = (PotionEffect)potionEffect;
		    		player.playerNetServerHandler.sendPacketToPlayer(new Packet41EntityEffect(player.entityId, effect));
		    	}
		    	player.playerNetServerHandler.sendPacketToPlayer(new Packet43Experience(player.experience, player.experienceTotal, player.experienceLevel));
	    	}
	    }	    	
	    if(difDest)
	    {
	        int entX = entity.chunkCoordX;
		    int entZ = entity.chunkCoordZ;
		    if ((entity.addedToChunk) && (oldWorld.getChunkProvider().chunkExists(entX, entZ)))
		    {
		    	oldWorld.getChunkFromChunkCoords(entX, entZ).removeEntity(entity);
		    	oldWorld.getChunkFromChunkCoords(entX, entZ).isModified = true;
		    }
		    oldWorld.releaseEntitySkin(entity);
	    	if (!(entity instanceof EntityPlayer)) 
	    	{
	    		NBTTagCompound entityNBT = new NBTTagCompound();
	    		entity.isDead = false;
	    		entity.addEntityID(entityNBT);
	    		entity.isDead = true;
	    		entity = EntityList.createEntityFromNBT(entityNBT, newWorld);
	    		
	    		if (entity == null) 
	    		{
	    			 
	    		}
	    	}
	    	newWorld.spawnEntityInWorld(entity);
	    	entity.setWorld(newWorld);
	    }
	    entity.worldObj.updateEntityWithOptionalForce(entity, false);
		if ((entity != null) && (cart != null))
		{
			if ((entity instanceof EntityPlayerMP))
			{
				entity.worldObj.updateEntityWithOptionalForce(entity, true);	
			}
			entity.mountEntity(cart);
		}
		if(entity instanceof EntityPlayerMP)
		{
			WorldServer.class.cast(newWorld).getChunkProvider().loadChunk(MathHelper.floor_double(entity.posX) >> 4, MathHelper.floor_double(entity.posZ) >> 4);
		}
		mod_pocketDim.teleporter.placeInPortal(entity, newWorld, link);
		return entity;    
	}<commit_msg>Fixed the NEI breaking bug (and an undiscovered bug involving sleeping and entering pocket dimensions)<commit_after>package StevenDimDoors.mod_pocketDim.helpers;
/**
 * This class regulates all the operations involving the storage and manipulation of dimensions. It handles saving dim data, teleporting the player, and 
 * creating/registering new dimensions as well as loading old dimensions on startup
 * @Return
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet41EntityEffect;
import net.minecraft.network.packet.Packet43Experience;
import net.minecraft.network.packet.Packet9Respawn;
import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraftforge.common.DimensionManager;
import StevenDimDoors.mod_pocketDim.DDProperties;
import StevenDimDoors.mod_pocketDim.DimData;
import StevenDimDoors.mod_pocketDim.LinkData;
import StevenDimDoors.mod_pocketDim.ObjectSaveInputStream;
import StevenDimDoors.mod_pocketDim.PacketHandler;
import StevenDimDoors.mod_pocketDim.Point3D;
import StevenDimDoors.mod_pocketDim.SchematicLoader;
import StevenDimDoors.mod_pocketDim.TileEntityRift;
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.schematic.BlockRotator;
import StevenDimDoors.mod_pocketDim.world.LimboProvider;
import StevenDimDoors.mod_pocketDim.world.PocketProvider;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;

public class dimHelper extends DimensionManager
{
	/**
	 * hashMap for the private pocket dimensions
	 */
	
	public static HashMap<String, DimData> privatePockets = new HashMap<String, DimData>();
	/**
	 * HashMap containing all the dims registered with DimDoors, sorted by dim ID. loaded on startup
	 * @Return
	 */
	public static HashMap<Integer, DimData> dimList=new HashMap<Integer, DimData>();
	public static boolean isSaving=false;
	
	/**
	 * ArrayList containing any blocks in limbo that have been placed by the player. Cycled through in the common tick manager
	 * @Return
	 */
	public static ArrayList<Point3D> blocksToDecay = new ArrayList<Point3D>();
	
	/**
	 * instance of the dimHelper
	 * @Return
	 */
	public static dimHelper instance = new dimHelper();
	
	/**
	 * HashMap for temporary storage of Link Signature damage hash values. See itemLinkSignature for more details
	 * @Return
	 */
	public HashMap<Integer, LinkData> interDimLinkList= new HashMap<Integer,LinkData>();
	
	/**
	 * ArrayList containing all link data not sorted for easy random access, used for random doors and for recreating rifts if they have a block placed over them. 
	 * See the common tick manager and the Chaos door for details on usage
	 * @Return
	 */
	//public ArrayList<LinkData> linksForRendering =new ArrayList<LinkData>();
	Random rand= new Random();
	
	public static final int DEFAULT_POCKET_SIZE = 39;
	public static final int DEFAULT_POCKET_WALL_THICKNESS = 5;
	public static final int MAX_WORLD_HEIGHT = 254;
	
	public int getDimDepth(int DimID)
	{
		if (dimList.containsKey(DimID))
		{
			return dimList.get(DimID).depth;
		}
		else return 1;
	}
	
	// GreyMaria: My god, what a mess. Here, let me clean it up a bit.
	private Entity teleportEntity(World world, Entity entity, LinkData link) //this beautiful teleport method is based off of xCompWiz's teleport function. 
	{		
		// GreyMaria: Added to simplify some code further in.
		WorldServer oldWorld = (WorldServer)world;
		
		// Is something riding? Handle it first.
		if(entity.riddenByEntity != null)
		{
			return this.teleportEntity(oldWorld,entity.riddenByEntity, link);
		}
		
		// Are we riding something? Dismount and tell the mount to go first.
		Entity cart = entity.ridingEntity;
		if (cart != null)
		{
			entity.mountEntity(null);
			cart = teleportEntity(oldWorld, cart, link);
			// We keep track of both so we can remount them on the other side.
		}
		
		WorldServer newWorld;
		
		// Destination doesn't exist? We need to make it.
		if(DimensionManager.getWorld(link.destDimID)==null)
		{
			DimensionManager.initDimension(link.destDimID);
		}
		
		// Determine if our destination's in another realm.
		boolean difDest = link.destDimID != link.locDimID;
		if(difDest)
		{
			newWorld = DimensionManager.getWorld(link.destDimID);
		}
		else
		{
			newWorld=(WorldServer)oldWorld;
		}
		
		// GreyMaria: What is this even accomplishing? We're doing the exact same thing at the end of this all.
		// TODO Check to see if this is actually vital.
		mod_pocketDim.teleporter.placeInPortal(entity, newWorld, link);

		// We'll need to know if we're teleporting a player entity.
		// If this value is NULL, then we're not teleporting a player entity.
		EntityPlayerMP player = null;
	    if (entity instanceof EntityPlayerMP) player = (EntityPlayerMP)entity;
	    
    	if (difDest) // Are we moving our target to a new dimension?
    	{
    		if(player != null) // Are we working with a player?
    		{
    			// We need to do all this special stuff to move a player between dimensions.
    			
				// Set the new dimension and inform the client that it's moving to a new world.
	    		player.dimension = link.destDimID;
	    		player.playerNetServerHandler.sendPacketToPlayer(new Packet9Respawn(player.dimension, (byte)player.worldObj.difficultySetting, newWorld.getWorldInfo().getTerrainType(), newWorld.getHeight(), player.theItemInWorldManager.getGameType()));
	    	
	    		// GreyMaria: Used the safe player entity remover.
	    		// This should fix an apparently unreported bug where
	    		// the last non-sleeping player leaves the Overworld
	    		// for a pocket dimension, causing all sleeping players
	    		// to remain asleep instead of progressing to day.
	    		oldWorld.removePlayerEntityDangerously(player);
	    		player.isDead=false;
	    		
	    		// Creates sanity by ensuring that we're only known to exist where we're supposed to be known to exist.
	    		oldWorld.getPlayerManager().removePlayer(player);
	    		newWorld.getPlayerManager().addPlayer(player);
	    		
	    		player.theItemInWorldManager.setWorld((WorldServer)newWorld);
		    	
	    		// Synchronize with the server so the client knows what time it is and what it's holding.
		    	player.mcServer.getConfigurationManager().updateTimeAndWeatherForPlayer(player, (WorldServer)newWorld);
		    	player.mcServer.getConfigurationManager().syncPlayerInventory(player);
		    	for(Object potionEffect : player.getActivePotionEffects())
		    	{
		    		PotionEffect effect = (PotionEffect)potionEffect;
		    		player.playerNetServerHandler.sendPacketToPlayer(new Packet41EntityEffect(player.entityId, effect));
		    	}
		    	
		    	// GreyMaria: Can someone tell me why we need this? I'm commenting it out for now. We'll see if it causes problems.
		    	//player.playerNetServerHandler.sendPacketToPlayer(new Packet43Experience(player.experience, player.experienceTotal, player.experienceLevel));
    		}  	
    		
    		// Creates sanity by removing the entity from its old location's chunk entity list, if applicable.
	        int entX = entity.chunkCoordX;
		    int entZ = entity.chunkCoordZ;
		    if ((entity.addedToChunk) && (oldWorld.getChunkProvider().chunkExists(entX, entZ)))
		    {
		    	oldWorld.getChunkFromChunkCoords(entX, entZ).removeEntity(entity);
		    	oldWorld.getChunkFromChunkCoords(entX, entZ).isModified = true;
		    }
		    // Memory concerns.
		    oldWorld.releaseEntitySkin(entity);
		    
	    	if (player == null) // Are we NOT working with a player?
	    	{
	    		NBTTagCompound entityNBT = new NBTTagCompound();
	    		entity.isDead = false;
	    		entity.addEntityID(entityNBT);
	    		entity.isDead = true;
	    		entity = EntityList.createEntityFromNBT(entityNBT, newWorld);
	    		
	    		if (entity == null)
	    		{   // TODO FIXME IMPLEMENT NULL CHECKS THAT ACTUALLY DO SOMETHING.
	    			/* 
	    			 * shit ourselves in an organized fashion, preferably
	    			 * in a neat pile instead of all over our users' games
	    			 */
	    		}
	    		
	    	}
	    	
	    	// Finally, respawn the entity in its new home.
	    	newWorld.spawnEntityInWorld(entity);
	    	entity.setWorld(newWorld);
	    }
	    entity.worldObj.updateEntityWithOptionalForce(entity, false);
	    
	    // Hey, remember me? It's time to remount.
		if (cart != null)
		{
			// Was there a player teleported? If there was, it's important that we update shit.
			if (player != null)
			{
				entity.worldObj.updateEntityWithOptionalForce(entity, true);	
			}
			entity.mountEntity(cart);
		}
		
		// Did we teleport a player? Load the chunk for them.
		if(player != null)
		{
			WorldServer.class.cast(newWorld).getChunkProvider().loadChunk(MathHelper.floor_double(entity.posX) >> 4, MathHelper.floor_double(entity.posZ) >> 4);
			
			// Tell Forge we're moving its players so everyone else knows.
			// Let's try doing this down here in case this is what's killing NEI.
			GameRegistry.onPlayerChangedDimension((EntityPlayer)entity);
			
		}
		mod_pocketDim.teleporter.placeInPortal(entity, newWorld, link);
		return entity;    
	}
{'fraction_non_alphanumeric': 0.08973915050784857, 'fraction_numerical': 0.0025392428439519853, 'mean_word_length': 3.9867625899280577, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308632', 'n_tokens_mistral': 5631, 'n_tokens_neox': 5116, 'n_words': 1300}