package zmaster587.advancedRocketry.util;

import java.util.WeakHashMap;

import zmaster587.advancedRocketry.api.AdvancedRocketryAPI;
import zmaster587.advancedRocketry.api.IGravityManager;
import zmaster587.advancedRocketry.api.IPlanetaryProvider;
import zmaster587.advancedRocketry.dimension.DimensionManager;
import zmaster587.advancedRocketry.world.provider.WorldProviderSpace;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;

public class GravityHandler implements IGravityManager {
	
	static {
		AdvancedRocketryAPI.gravityManager = new GravityHandler();
	}

	private static WeakHashMap<Entity, Double> entityMap = new WeakHashMap<Entity, Double>();
	public static void applyGravity(Entity entity) {

		if(!entity.isInWater() || entity instanceof EntityItem) {
			if(!(entity instanceof EntityPlayer) || !((EntityPlayer)entity).capabilities.isFlying) {
				if(DimensionManager.getInstance().isDimensionCreated(entity.worldObj.provider.dimensionId) || entity.worldObj.provider instanceof WorldProviderSpace) {
					double gravMult;
					if(entity.worldObj.provider instanceof IPlanetaryProvider)
						gravMult = ((IPlanetaryProvider)entity.worldObj.provider).getGravitationalMultiplier((int)entity.posX, (int)entity.posZ);
					else
						gravMult = DimensionManager.getInstance().getDimensionProperties(entity.worldObj.provider.dimensionId).gravitationalMultiplier;

					if(entity instanceof EntityItem)
						entity.motionY -= gravMult*0.04f;
					else
						entity.motionY -= gravMult*0.075f;
					return;
				}
				else {
					if(entity instanceof EntityItem)
						entity.motionY -= 0.04f;
					else
						entity.motionY -= 0.08D;
				}
			}
		}

	}

	@Override
	public void setGravityMultiplier(Entity entity, double multiplier) {
		entityMap.put(entity, multiplier);
	}

	@Override
	public void clearGravityEffect(Entity entity) {
		entityMap.remove(entity);
	}
}

@@ -52,26 +52,164 @@
 ava.
-util.WeakHashMap;%0A
+lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0Aimport java.util.WeakHashMap;%0A%0Aimport zmaster587.advancedRocketry.AdvancedRocketry;
 %0Aimp
@@ -700,16 +700,77 @@
 anager %7B
+%0A%0A%09static Class gcWorldProvider;%0A%09static Method gcGetGravity;
 %0A%09%0A%09stat
@@ -835,16 +835,552 @@
 dler();%0A
+%09%09%0A%09%09%0A%09%09try %7B%0A%09%09%09gcWorldProvider = Class.forName(%22micdoodle8.mods.galacticraft.api.world.IGalacticraftWorldProvider%22);%0A%09%09%09AdvancedRocketry.logger.info(%22GC IGalacticraftWorldProvider  found%22);%0A%09%09%09gcGetGravity = gcWorldProvider.getMethod(%22getGravity%22);%0A%09%09%7D catch(ClassNotFoundException e)%7B%0A%09%09%09gcWorldProvider = null;%0A%09%09%09AdvancedRocketry.logger.info(%22GC IGalacticraftWorldProvider not found%22);%0A%09%09%7D%0A%09%09catch(NoSuchMethodException e)%7B%0A%09%09%09gcWorldProvider = null;%0A%09%09%09AdvancedRocketry.logger.info(%22GC IGalacticraftWorldProvider not found%22);%0A%09%09%7D%0A
 %09%7D%0A%0A%09pri
@@ -2343,16 +2343,405 @@
 %09else %7B%0A
+%09%09%09%09%09//GC handling%0A%09%09%09%09%09if(gcWorldProvider != null && gcWorldProvider.isAssignableFrom(entity.worldObj.provider.getClass())) %7B%0A%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09entity.motionY -= 0.075f - (float)gcGetGravity.invoke(entity.worldObj.provider);%0A%09%09%09%09%09%09%7D catch (IllegalAccessException %7C IllegalArgumentException%0A%09%09%09%09%09%09%09%09%7C InvocationTargetException e) %7B%0A%09%09%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else %7B%0A%09
 %09%09%09%09%09if(
@@ -2762,32 +2762,33 @@
 eof EntityItem)%0A
+%09
 %09%09%09%09%09%09entity.mot
@@ -2799,37 +2799,39 @@
  -= 0.04f;%0A%09%09%09%09%09
+%09
 else%0A
+%09
 %09%09%09%09%09%09entity.mot
@@ -2845,16 +2845,23 @@
  0.08D;%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09

fix GC handling in MC 1.7.10