From 4590a7175950f572fad618abb672bcb571d06056 Mon Sep 17 00:00:00 2001 From: Ben Spiers Date: Mon, 23 Mar 2015 01:10:32 +0000 Subject: [PATCH] Make the checking of instances of other mods' interfaces safer. Fixes (part of) #2316 --- .../java/mekanism/common/FuelHandler.java | 8 ++- .../common/block/BlockEnergyCube.java | 2 +- .../mekanism/common/block/BlockGasTank.java | 2 +- .../mekanism/common/block/BlockMachine.java | 2 +- .../mekanism/common/util/MekanismUtils.java | 64 ++++++++++++++++++- .../generators/common/MekanismGenerators.java | 2 +- .../common/block/BlockGenerator.java | 2 +- .../generators/common/block/BlockReactor.java | 2 +- .../common/tile/TileEntitySolarGenerator.java | 2 +- 9 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/main/java/mekanism/common/FuelHandler.java b/src/main/java/mekanism/common/FuelHandler.java index 2dc9221b6..8d90c4217 100644 --- a/src/main/java/mekanism/common/FuelHandler.java +++ b/src/main/java/mekanism/common/FuelHandler.java @@ -4,6 +4,7 @@ import java.util.HashMap; import mekanism.api.MekanismConfig.general; import mekanism.api.gas.Gas; +import mekanism.common.util.MekanismUtils; import net.minecraftforge.fluids.FluidContainerRegistry; import cpw.mods.fml.common.ModAPIManager; @@ -27,7 +28,7 @@ public class FuelHandler return fuels.get(gas.getName()); } - if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|fuels") && gas.hasFluid()) + if(BCPresent() && gas.hasFluid()) { IFuel bcFuel = BuildcraftFuelRegistry.fuel.getFuel(gas.getFluid()); @@ -60,4 +61,9 @@ public class FuelHandler 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"); + } } diff --git a/src/main/java/mekanism/common/block/BlockEnergyCube.java b/src/main/java/mekanism/common/block/BlockEnergyCube.java index 684859838..972ce2ab3 100644 --- a/src/main/java/mekanism/common/block/BlockEnergyCube.java +++ b/src/main/java/mekanism/common/block/BlockEnergyCube.java @@ -171,7 +171,7 @@ public class BlockEnergyCube extends BlockContainer implements IPeripheralProvid return true; } - if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) + if(MekanismUtils.isBCWrench(tool)) ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); int change = ForgeDirection.ROTATION_MATRIX[side][tileEntity.facing]; diff --git a/src/main/java/mekanism/common/block/BlockGasTank.java b/src/main/java/mekanism/common/block/BlockGasTank.java index 75ef59353..e2ca814a1 100644 --- a/src/main/java/mekanism/common/block/BlockGasTank.java +++ b/src/main/java/mekanism/common/block/BlockGasTank.java @@ -110,7 +110,7 @@ public class BlockGasTank extends BlockContainer return true; } - if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) + if(MekanismUtils.isBCWrench(tool)) ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing]; diff --git a/src/main/java/mekanism/common/block/BlockMachine.java b/src/main/java/mekanism/common/block/BlockMachine.java index 3f75f5df6..15152ee8c 100644 --- a/src/main/java/mekanism/common/block/BlockMachine.java +++ b/src/main/java/mekanism/common/block/BlockMachine.java @@ -617,7 +617,7 @@ public class BlockMachine extends BlockContainer implements ISpecialBounds, IPer return true; } - if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) + if(MekanismUtils.isBCWrench(tool)) { ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); } diff --git a/src/main/java/mekanism/common/util/MekanismUtils.java b/src/main/java/mekanism/common/util/MekanismUtils.java index 6fe6e9d85..fe4875429 100644 --- a/src/main/java/mekanism/common/util/MekanismUtils.java +++ b/src/main/java/mekanism/common/util/MekanismUtils.java @@ -11,6 +11,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.Iterator; import java.util.List; 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 Map> classesFound = new HashMap>(); + /** * Checks for a new version of Mekanism. */ @@ -1389,6 +1392,63 @@ public final class MekanismUtils 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. * @param player - the player using the wrench @@ -1406,12 +1466,12 @@ public final class MekanismUtils 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; } - 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; } diff --git a/src/main/java/mekanism/generators/common/MekanismGenerators.java b/src/main/java/mekanism/generators/common/MekanismGenerators.java index d3589914f..60f601286 100644 --- a/src/main/java/mekanism/generators/common/MekanismGenerators.java +++ b/src/main/java/mekanism/generators/common/MekanismGenerators.java @@ -87,7 +87,7 @@ public class MekanismGenerators implements IModule @EventHandler 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()) { diff --git a/src/main/java/mekanism/generators/common/block/BlockGenerator.java b/src/main/java/mekanism/generators/common/block/BlockGenerator.java index c335aa626..5abd29118 100644 --- a/src/main/java/mekanism/generators/common/block/BlockGenerator.java +++ b/src/main/java/mekanism/generators/common/block/BlockGenerator.java @@ -327,7 +327,7 @@ public class BlockGenerator extends BlockContainer implements ISpecialBounds, IP return true; } - if(ModAPIManager.INSTANCE.hasAPI("BuildCraftAPI|tools") && tool instanceof IToolWrench) + if(MekanismUtils.isBCWrench(tool)) ((IToolWrench)tool).wrenchUsed(entityplayer, x, y, z); int change = ForgeDirection.ROTATION_MATRIX[ForgeDirection.UP.ordinal()][tileEntity.facing]; diff --git a/src/main/java/mekanism/generators/common/block/BlockReactor.java b/src/main/java/mekanism/generators/common/block/BlockReactor.java index a5dcfca1a..6976fb9e6 100644 --- a/src/main/java/mekanism/generators/common/block/BlockReactor.java +++ b/src/main/java/mekanism/generators/common/block/BlockReactor.java @@ -175,7 +175,7 @@ public class BlockReactor extends BlockContainer implements IBlockCTM 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()) { diff --git a/src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java b/src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java index 9524c4641..4f09037e1 100644 --- a/src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java +++ b/src/main/java/mekanism/generators/common/tile/TileEntitySolarGenerator.java @@ -131,7 +131,7 @@ public class TileEntitySolarGenerator extends TileEntityGenerator { 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(); }