Make the checking of instances of other mods' interfaces safer.

Fixes (part of) #2316
This commit is contained in:
Ben Spiers 2015-03-23 01:10:32 +00:00
parent dfefcaf69a
commit 4590a71759
9 changed files with 76 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import java.util.HashMap;
import mekanism.api.MekanismConfig.general; import mekanism.api.MekanismConfig.general;
import mekanism.api.gas.Gas; import mekanism.api.gas.Gas;
import mekanism.common.util.MekanismUtils;
import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidContainerRegistry;
import cpw.mods.fml.common.ModAPIManager; import cpw.mods.fml.common.ModAPIManager;
@ -27,7 +28,7 @@ public class FuelHandler
return fuels.get(gas.getName()); return fuels.get(gas.getName());
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|fuels") && gas.hasFluid()) if(BCPresent() && gas.hasFluid())
{ {
IFuel bcFuel = BuildcraftFuelRegistry.fuel.getFuel(gas.getFluid()); IFuel bcFuel = BuildcraftFuelRegistry.fuel.getFuel(gas.getFluid());
@ -60,4 +61,9 @@ public class FuelHandler
energyPerTick = bcFuel.getPowerPerCycle() * general.FROM_TE; energyPerTick = bcFuel.getPowerPerCycle() * general.FROM_TE;
} }
} }
public static boolean BCPresent()
{
return ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|fuels") && MekanismUtils.classExists("buildcraft.api.fuels.BuildcraftFuelRegistry") && MekanismUtils.classExists("buildcraft.api.fuels.IFuel");
}
} }

View file

@ -171,7 +171,7 @@ public class BlockEnergyCube extends BlockContainer implements IPeripheralProvid
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) if(MekanismUtils.isBCWrench(tool))
((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z);
int change = ForgeDirection.ROTATION_MATRIX[side][tileEntity.facing]; int change = ForgeDirection.ROTATION_MATRIX[side][tileEntity.facing];

View file

@ -110,7 +110,7 @@ public class BlockGasTank extends BlockContainer
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) if(MekanismUtils.isBCWrench(tool))
((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z);
int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing]; int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing];

View file

@ -617,7 +617,7 @@ public class BlockMachine extends BlockContainer implements ISpecialBounds, IPer
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) if(MekanismUtils.isBCWrench(tool))
{ {
((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z);
} }

View file

@ -11,6 +11,7 @@ import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -94,6 +95,8 @@ public final class MekanismUtils
{ {
public static final ForgeDirection[] SIDE_DIRS = new ForgeDirection[] {ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.EAST}; public static final ForgeDirection[] SIDE_DIRS = new ForgeDirection[] {ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.EAST};
public static final Map<String, Class<?>> classesFound = new HashMap<String, Class<?>>();
/** /**
* Checks for a new version of Mekanism. * Checks for a new version of Mekanism.
*/ */
@ -1389,6 +1392,63 @@ public final class MekanismUtils
return Item.getIdFromItem(itemStack.getItem()); return Item.getIdFromItem(itemStack.getItem());
} }
public static boolean classExists(String className)
{
if(classesFound.containsKey(className))
{
return classesFound.get(className) != null;
}
Class<?> found;
try
{
found = Class.forName(className);
}
catch(ClassNotFoundException e)
{
found = null;
}
classesFound.put(className, found);
return found != null;
}
public static boolean existsAndInstance(Object obj, String className)
{
Class<?> theClass;
if(classesFound.containsKey(className))
{
theClass = classesFound.get(className);
}
else
{
try
{
theClass = Class.forName(className);
classesFound.put(className, theClass);
} catch(ClassNotFoundException e)
{
classesFound.put(className, null);
return false;
}
}
return theClass != null && theClass.isInstance(obj);
}
public static boolean isBCWrench(Item tool)
{
return existsAndInstance(tool, "buildcraft.api.tools.IToolWrench");
}
public static boolean isCoFHHammer(Item tool)
{
return existsAndInstance(tool, "cofh.api.item.IToolHammer");
}
/** /**
* Whether or not the player has a usable wrench for a block at the coordinates given. * Whether or not the player has a usable wrench for a block at the coordinates given.
* @param player - the player using the wrench * @param player - the player using the wrench
@ -1406,12 +1466,12 @@ public final class MekanismUtils
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool.getItem() instanceof IToolWrench && ((IToolWrench)tool.getItem()).canWrench(player, x, y, z)) if(isBCWrench(tool.getItem()) && ((IToolWrench)tool.getItem()).canWrench(player, x, y, z))
{ {
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("CoFHAPI") && tool.getItem() instanceof IToolHammer && ((IToolHammer)tool.getItem()).isUsable(tool, player, x, y, z)) if(isCoFHHammer(tool.getItem()) && ((IToolHammer)tool.getItem()).isUsable(tool, player, x, y, z))
{ {
return true; return true;
} }

View file

@ -87,7 +87,7 @@ public class MekanismGenerators implements IModule
@EventHandler @EventHandler
public void postInit(FMLPostInitializationEvent event) public void postInit(FMLPostInitializationEvent event)
{ {
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|fuels") && BuildcraftFuelRegistry.fuel != null) if(FuelHandler.BCPresent() && BuildcraftFuelRegistry.fuel != null)
{ {
for(IFuel s : BuildcraftFuelRegistry.fuel.getFuels()) for(IFuel s : BuildcraftFuelRegistry.fuel.getFuels())
{ {

View file

@ -327,7 +327,7 @@ public class BlockGenerator extends BlockContainer implements ISpecialBounds, IP
return true; return true;
} }
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) if(MekanismUtils.isBCWrench(tool))
((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z);
int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing]; int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing];

View file

@ -175,7 +175,7 @@ public class BlockReactor extends BlockContainer implements IBlockCTM
if(entityplayer.getCurrentEquippedItem() != null) if(entityplayer.getCurrentEquippedItem() != null)
{ {
if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && entityplayer.getCurrentEquippedItem().getItem() instanceof IToolWrench && !entityplayer.getCurrentEquippedItem().getUnlocalizedName().contains("omniwrench")) if(MekanismUtils.isBCWrench(entityplayer.getCurrentEquippedItem().getItem()) && !entityplayer.getCurrentEquippedItem().getUnlocalizedName().contains("omniwrench"))
{ {
if(entityplayer.isSneaking()) if(entityplayer.isSneaking())
{ {

View file

@ -131,7 +131,7 @@ public class TileEntitySolarGenerator extends TileEntityGenerator
{ {
ret = GENERATION_RATE; ret = GENERATION_RATE;
if(ModAPIManager.INSTANCE.hasAPI("Galacticraft API") && worldObj.provider instanceof ISolarLevel) if(MekanismUtils.existsAndInstance(worldObj.provider, "micdoodle8.mods.galacticraft.api.world.ISolarLevel"))
{ {
ret *= ((ISolarLevel)worldObj.provider).getSolarEnergyMultiplier(); ret *= ((ISolarLevel)worldObj.provider).getSolarEnergyMultiplier();
} }