From ec53b6d882c793de36b9e0cfd75fb99ab554f0a9 Mon Sep 17 00:00:00 2001 From: mezz Date: Fri, 4 Mar 2016 01:42:17 -0800 Subject: [PATCH] Clean up exception catching --- .../apiculture/VillageHandlerApiculture.java | 6 +- .../genetics/alleles/AlleleEffect.java | 13 +--- .../alleles/AlleleEffectIgnition.java | 2 +- .../genetics/alleles/AlleleEffectPotion.java | 2 +- .../genetics/alleles/AlleleFruit.java | 1 - src/main/java/forestry/core/PluginFluids.java | 15 ---- .../core/blocks/MachineProperties.java | 4 +- .../core/circuits/GuiSolderingIron.java | 16 ++--- .../core/commands/CommandHelpers.java | 2 +- .../core/commands/CommandSaveStats.java | 3 +- .../java/forestry/core/config/Config.java | 14 ++-- .../java/forestry/core/fluids/Fluids.java | 3 +- .../core/gui/ContainerSocketedHelper.java | 8 +-- .../core/models/ModelBlockOverlay.java | 5 +- .../java/forestry/core/models/ModelCrate.java | 9 ++- .../core/network/DataInputStreamForestry.java | 2 +- .../forestry/core/network/ForestryPacket.java | 4 +- .../forestry/core/network/PacketHandler.java | 10 +-- .../forestry/core/recipes/RecipeUtil.java | 5 +- .../java/forestry/core/render/FontColour.java | 7 +- .../utils/ForestryModEnvWarningCallable.java | 2 +- .../java/forestry/farming/PluginFarming.java | 16 ++--- .../farming/circuits/CircuitFarmLogic.java | 11 ++- .../java/forestry/storage/PluginStorage.java | 5 +- test/ItemStackTest.java | 48 ------------- test/NetworkTest.java | 68 ------------------- 26 files changed, 71 insertions(+), 210 deletions(-) delete mode 100644 test/ItemStackTest.java delete mode 100644 test/NetworkTest.java diff --git a/src/main/java/forestry/apiculture/VillageHandlerApiculture.java b/src/main/java/forestry/apiculture/VillageHandlerApiculture.java index 038bc9f58..d77a12109 100755 --- a/src/main/java/forestry/apiculture/VillageHandlerApiculture.java +++ b/src/main/java/forestry/apiculture/VillageHandlerApiculture.java @@ -19,11 +19,7 @@ import net.minecraftforge.fml.common.registry.VillagerRegistry.VillagerProfessio public class VillageHandlerApiculture /*implements IVillageCreationHandler, IVillageTradeHandler*/ { /*public static void registerVillageComponents() { - try { - MapGenStructureIO.func_143031_a(ComponentVillageBeeHouse.class, "Forestry:BeeHouse"); - } catch (Throwable e) { - Log.severe("Failed to register village beehouse."); - } + MapGenStructureIO.registerStructureComponent(ComponentVillageBeeHouse.class, "Forestry:BeeHouse"); } @SuppressWarnings("unchecked") diff --git a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffect.java b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffect.java index e491221a4..fc1383a6b 100644 --- a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffect.java +++ b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffect.java @@ -16,7 +16,6 @@ import java.util.List; import net.minecraft.entity.Entity; import net.minecraft.potion.Potion; import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.BlockPos; import net.minecraft.util.Vec3; import forestry.api.apiculture.BeeManager; @@ -103,21 +102,11 @@ public abstract class AlleleEffect extends AlleleCategorized implements IAlleleB @Override public IEffectData doFX(IBeeGenome genome, IEffectData storedData, IBeeHousing housing) { - Vec3 beeFXCoordinates = getFXCoordinates(housing); + Vec3 beeFXCoordinates = housing.getBeeFXCoordinates(); Proxies.render.addBeeHiveFX("particles/swarm_bee", housing.getWorld(), beeFXCoordinates.xCoord, beeFXCoordinates.yCoord, beeFXCoordinates.zCoord, genome.getPrimary().getSpriteColour(0)); return storedData; } - protected Vec3 getFXCoordinates(IBeeHousing housing) { - try { - return housing.getBeeFXCoordinates(); - } catch (Throwable error) { - // getBeeFXCoordinates() is only newly added to the API, fall back on getCoordinates() - BlockPos coordinates = housing.getCoordinates(); - return new Vec3(coordinates.getX() + 0.5D, coordinates.getY() + 0.5D, coordinates.getZ() + 0.5D); - } - } - protected Vect getModifiedArea(IBeeGenome genome, IBeeHousing housing) { IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(housing); float territoryModifier = beeModifier.getTerritoryModifier(genome, 1f); diff --git a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectIgnition.java b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectIgnition.java index 517335ee8..b660e177c 100644 --- a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectIgnition.java +++ b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectIgnition.java @@ -68,7 +68,7 @@ public class AlleleEffectIgnition extends AlleleEffectThrottled { if (housing.getWorld().rand.nextInt(2) != 0) { super.doFX(genome, storedData, housing); } else { - Vec3 beeFXCoordinates = getFXCoordinates(housing); + Vec3 beeFXCoordinates = housing.getBeeFXCoordinates(); Proxies.render.addEntityIgnitionFX(housing.getWorld(), beeFXCoordinates.xCoord, beeFXCoordinates.yCoord + 0.5, beeFXCoordinates.zCoord); } return storedData; diff --git a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectPotion.java b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectPotion.java index 6a9b66089..4f08cbe59 100644 --- a/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectPotion.java +++ b/src/main/java/forestry/apiculture/genetics/alleles/AlleleEffectPotion.java @@ -89,7 +89,7 @@ public class AlleleEffectPotion extends AlleleEffectThrottled { if (housing.getWorld().rand.nextBoolean()) { super.doFX(genome, storedData, housing); } else { - Vec3 beeFXCoordinates = getFXCoordinates(housing); + Vec3 beeFXCoordinates = housing.getBeeFXCoordinates(); Proxies.render.addEntityPotionFX(housing.getWorld(), beeFXCoordinates.xCoord, beeFXCoordinates.yCoord + 0.5, beeFXCoordinates.zCoord, potionFXColor); } return storedData; diff --git a/src/main/java/forestry/arboriculture/genetics/alleles/AlleleFruit.java b/src/main/java/forestry/arboriculture/genetics/alleles/AlleleFruit.java index 8fb699257..498713b25 100644 --- a/src/main/java/forestry/arboriculture/genetics/alleles/AlleleFruit.java +++ b/src/main/java/forestry/arboriculture/genetics/alleles/AlleleFruit.java @@ -10,7 +10,6 @@ ******************************************************************************/ package forestry.arboriculture.genetics.alleles; -import javax.annotation.Nonnull; import java.util.Arrays; import java.util.List; diff --git a/src/main/java/forestry/core/PluginFluids.java b/src/main/java/forestry/core/PluginFluids.java index 5506a3850..8eeb46328 100644 --- a/src/main/java/forestry/core/PluginFluids.java +++ b/src/main/java/forestry/core/PluginFluids.java @@ -145,21 +145,6 @@ public class PluginFluids extends BlankForestryPlugin { FuelManager.generatorFuel.put(biomass.getFluid(), biomassFuel); } - public static class MissingFluidException extends RuntimeException { - public MissingFluidException(String tag) { - super("Fluid '" + tag + "' was not found. Please check your configs."); - } - } - - @Override - public void postInit() { - for (Fluids fluidType : Fluids.forestryFluids) { - if (fluidType.getFluid() == null && Config.isFluidEnabled(fluidType)) { - throw new MissingFluidException(fluidType.getTag()); - } - } - } - public static class FillBucketHook { @SubscribeEvent public void fillBucket(FillBucketEvent event) { diff --git a/src/main/java/forestry/core/blocks/MachineProperties.java b/src/main/java/forestry/core/blocks/MachineProperties.java index e64ce6557..79ba1c996 100644 --- a/src/main/java/forestry/core/blocks/MachineProperties.java +++ b/src/main/java/forestry/core/blocks/MachineProperties.java @@ -114,8 +114,8 @@ public class MachineProperties implements IMachineProper public TileEntity createTileEntity() { try { return teClass.getConstructor().newInstance(); - } catch (Exception ex) { - throw new RuntimeException("Failed to instantiate tile entity of class " + teClass.getName()); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to instantiate tile entity of class " + teClass.getName(), e); } } diff --git a/src/main/java/forestry/core/circuits/GuiSolderingIron.java b/src/main/java/forestry/core/circuits/GuiSolderingIron.java index 1b34ec469..9d68bb607 100755 --- a/src/main/java/forestry/core/circuits/GuiSolderingIron.java +++ b/src/main/java/forestry/core/circuits/GuiSolderingIron.java @@ -57,16 +57,12 @@ public class GuiSolderingIron extends GuiForestry 0) { outstream.write(buffer, 0, readBytes); } - } catch (Exception ex) { - ex.printStackTrace(); + } catch (FileNotFoundException e) { + Log.error("File not found.", e); + } catch (IOException e) { + Log.error("Failed to copy file.", e); } } @@ -299,8 +303,8 @@ public class Config { try { InputStream hintStream = Config.class.getResourceAsStream("/config/forestry/hints.properties"); prop.load(hintStream); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (IOException e) { + Log.error("Failed to load hints file.", e); } for (String key : prop.stringPropertyNames()) { diff --git a/src/main/java/forestry/core/fluids/Fluids.java b/src/main/java/forestry/core/fluids/Fluids.java index 732a64994..dfd51e3b2 100644 --- a/src/main/java/forestry/core/fluids/Fluids.java +++ b/src/main/java/forestry/core/fluids/Fluids.java @@ -12,6 +12,7 @@ package forestry.core.fluids; import javax.annotation.Nonnull; import java.awt.Color; +import java.io.IOException; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; @@ -418,7 +419,7 @@ public enum Fluids { ResourceLocation resourceLocation = new ResourceLocation(Constants.ID, "blocks/liquid/" + getTag() + "_flow"); IResourceManager resourceManager = Proxies.common.getClientInstance().getResourceManager(); return resourceManager.getResource(resourceLocation) != null; - } catch (java.lang.Exception e) { + } catch (IOException e) { return false; } } diff --git a/src/main/java/forestry/core/gui/ContainerSocketedHelper.java b/src/main/java/forestry/core/gui/ContainerSocketedHelper.java index 7187edbb0..8f628c967 100644 --- a/src/main/java/forestry/core/gui/ContainerSocketedHelper.java +++ b/src/main/java/forestry/core/gui/ContainerSocketedHelper.java @@ -51,12 +51,8 @@ public class ContainerSocketedHelper impleme return; } - try { - if (!tile.getSocketType().equals(circuitBoard.getSocketType())) { - return; - } - } catch (Throwable ignored) { - // older circuitBoards don't have getSocketType() + if (!tile.getSocketType().equals(circuitBoard.getSocketType())) { + return; } ItemStack toSocket = itemstack.copy(); diff --git a/src/main/java/forestry/core/models/ModelBlockOverlay.java b/src/main/java/forestry/core/models/ModelBlockOverlay.java index 581becfe0..d8a92ed01 100644 --- a/src/main/java/forestry/core/models/ModelBlockOverlay.java +++ b/src/main/java/forestry/core/models/ModelBlockOverlay.java @@ -26,6 +26,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.IBlockAccess; + import net.minecraftforge.client.model.IFlexibleBakedModel; import net.minecraftforge.client.model.ISmartBlockModel; import net.minecraftforge.client.model.ISmartItemModel; @@ -189,7 +190,7 @@ public abstract class ModelBlockOverlay implements IFlexibleBak baker.setRenderBoundsFromBlock(block); try { backeWorldBlock(bBlock, world, pos, baker); - } catch (Exception e) { + } catch (RuntimeException e) { return null; } @@ -209,7 +210,7 @@ public abstract class ModelBlockOverlay implements IFlexibleBak baker.setRenderBoundsFromBlock(block); try { backeInventoryBlock(bBlock, stack, baker); - } catch (Exception e) { + } catch (RuntimeException e) { return null; } diff --git a/src/main/java/forestry/core/models/ModelCrate.java b/src/main/java/forestry/core/models/ModelCrate.java index 51b5e8ed7..9c798425c 100644 --- a/src/main/java/forestry/core/models/ModelCrate.java +++ b/src/main/java/forestry/core/models/ModelCrate.java @@ -49,6 +49,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import forestry.core.items.ItemCrated; +import forestry.core.utils.Log; import forestry.core.utils.StringUtil; import forestry.storage.PluginStorage; @@ -112,7 +113,7 @@ public class ModelCrate implements ISmartItemModel { ModelBlock modelblock = p_177581_1_.getRootModel(); return modelblock == MODEL_GENERATED || modelblock == MODEL_COMPASS || modelblock == MODEL_CLOCK; } - } catch (Exception e) { + } catch (IOException e) { return false; } } @@ -148,7 +149,11 @@ public class ModelCrate implements ISmartItemModel { modelblock.name = p_177594_1_.toString(); model = modelblock; } finally { - reader.close(); + try { + reader.close(); + } catch (IOException e) { + Log.error("Failed to close crate model reader.", e); + } } if (model != null && model.getParentLocation() != null) { diff --git a/src/main/java/forestry/core/network/DataInputStreamForestry.java b/src/main/java/forestry/core/network/DataInputStreamForestry.java index b3a08f7a6..d9ff43866 100644 --- a/src/main/java/forestry/core/network/DataInputStreamForestry.java +++ b/src/main/java/forestry/core/network/DataInputStreamForestry.java @@ -79,7 +79,7 @@ public class DataInputStreamForestry extends DataInputStream { T streamable = streamableClass.newInstance(); streamable.readData(this); return streamable; - } catch (ReflectiveOperationException e) { + } catch (IllegalAccessException | InstantiationException | IOException e) { throw new InvalidObjectException("Failed to read Streamable for class " + streamableClass + " with error " + e); } } diff --git a/src/main/java/forestry/core/network/ForestryPacket.java b/src/main/java/forestry/core/network/ForestryPacket.java index e7c9a8e1d..765b81d63 100755 --- a/src/main/java/forestry/core/network/ForestryPacket.java +++ b/src/main/java/forestry/core/network/ForestryPacket.java @@ -16,6 +16,8 @@ import net.minecraft.network.PacketBuffer; import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; +import forestry.core.utils.Log; + import io.netty.buffer.ByteBufOutputStream; import io.netty.buffer.Unpooled; @@ -31,7 +33,7 @@ public abstract class ForestryPacket implements IForestryPacket { data.writeByte(id.ordinal()); writeData(data); } catch (IOException e) { - e.printStackTrace(); + Log.error("Failed to write packet.", e); } return new FMLProxyPacket(new PacketBuffer(buf.buffer()), PacketHandler.channelId); diff --git a/src/main/java/forestry/core/network/PacketHandler.java b/src/main/java/forestry/core/network/PacketHandler.java index 7cd602102..5ea3f56c5 100644 --- a/src/main/java/forestry/core/network/PacketHandler.java +++ b/src/main/java/forestry/core/network/PacketHandler.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.network; +import java.io.IOException; import java.io.InputStream; import net.minecraft.entity.player.EntityPlayer; @@ -24,6 +25,7 @@ import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.internal.FMLProxyPacket; import forestry.core.proxy.Proxies; +import forestry.core.utils.Log; import io.netty.buffer.ByteBufInputStream; @@ -47,8 +49,8 @@ public class PacketHandler { IForestryPacketServer packetHandler = packetId.getPacketHandler(); packetHandler.readData(data); packetHandler.onPacketData(data, player); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (IOException e) { + Log.error("Failed to read packet.", e); } } @@ -63,8 +65,8 @@ public class PacketHandler { IForestryPacketClient packetHandler = packetId.getPacketHandler(); packetHandler.readData(data); packetHandler.onPacketData(data, player); - } catch (Exception ex) { - ex.printStackTrace(); + } catch (IOException e) { + Log.error("Failed to read packet.", e); } } diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index 74f6ab4a2..a32ca5ef3 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -74,9 +74,8 @@ public abstract class RecipeUtil { try { return getShapedRecipeAsArray(recipe.getWidth(), recipe.getHeight(), recipe.getIngredients(), recipe.getRecipeOutput()); - } catch (Exception ex) { - Log.warning("Exception while trying to parse an ItemStack[10] from an IRecipe:"); - Log.warning(ex.getMessage()); + } catch (RuntimeException ex) { + Log.warning("Exception while trying to parse an ItemStack[10] from an IRecipe:", ex); } return null; diff --git a/src/main/java/forestry/core/render/FontColour.java b/src/main/java/forestry/core/render/FontColour.java index b9e75f0b4..5a679f0e9 100644 --- a/src/main/java/forestry/core/render/FontColour.java +++ b/src/main/java/forestry/core/render/FontColour.java @@ -10,11 +10,14 @@ ******************************************************************************/ package forestry.core.render; +import java.io.IOException; import java.io.InputStream; import java.util.Properties; import net.minecraft.client.resources.IResourceManager; +import forestry.core.utils.Log; + public class FontColour { //private static final ResourceLocation colourDefinitions = new ForestryResource("/config/forestry/colour.properties"); @@ -41,8 +44,8 @@ public class FontColour { //if (fontStream != null) // fontStream.close(); defaultFontStream.close(); - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + Log.error("Failed to load font colors.", e); } } diff --git a/src/main/java/forestry/core/utils/ForestryModEnvWarningCallable.java b/src/main/java/forestry/core/utils/ForestryModEnvWarningCallable.java index 545f750ea..ca575bb83 100755 --- a/src/main/java/forestry/core/utils/ForestryModEnvWarningCallable.java +++ b/src/main/java/forestry/core/utils/ForestryModEnvWarningCallable.java @@ -53,7 +53,7 @@ public class ForestryModEnvWarningCallable implements ICrashCallable { @Nonnull @Override - public String call() throws Exception { + public String call() { return disabledModulesMessage; } diff --git a/src/main/java/forestry/farming/PluginFarming.java b/src/main/java/forestry/farming/PluginFarming.java index 662f676f2..e5c1a32f0 100644 --- a/src/main/java/forestry/farming/PluginFarming.java +++ b/src/main/java/forestry/farming/PluginFarming.java @@ -215,7 +215,7 @@ public class PluginFarming extends BlankForestryPlugin { try { Block sapling = ItemStackUtil.getBlockFromRegistry(saplingString); if (sapling == null || sapling == Blocks.air) { - throw new RuntimeException("can't find block for " + saplingString); + throw new IllegalArgumentException("can't find block for " + saplingString); } int saplingMeta = Integer.parseInt(saplingMetaString); @@ -224,15 +224,15 @@ public class PluginFarming extends BlankForestryPlugin { } else { Item windfall = ItemStackUtil.getItemFromRegistry(windfallString); if (windfall == null) { - throw new RuntimeException("can't find item for " + windfallString); + throw new IllegalArgumentException("can't find item for " + windfallString); } ItemStack windfallStack = new ItemStack(windfall, 1, Integer.parseInt(windfallMetaString)); farmables.add(new FarmableGenericSapling(sapling, saplingMeta, windfallStack)); } - } catch (Exception ex) { - Log.warning("%s for farm '%s': %s", errormsg, tokens[0], ex.getMessage()); + } catch (IllegalArgumentException e) { + Log.warning("%s for farm '%s'", errormsg, tokens[0], e.getMessage()); } return true; @@ -259,19 +259,19 @@ public class PluginFarming extends BlankForestryPlugin { try { Item seed = ItemStackUtil.getItemFromRegistry(items[0]); if (seed == null) { - throw new RuntimeException("can't find item for " + items[0]); + throw new IllegalArgumentException("can't find item for " + items[0]); } Block crop = ItemStackUtil.getBlockFromRegistry(items[2]); if (crop == null || crop == Blocks.air) { - throw new RuntimeException("can't find block for " + items[2]); + throw new IllegalArgumentException("can't find block for " + items[2]); } Farmables.farmables.get(tokens[0]).add( new FarmableGenericCrop(new ItemStack(seed, 1, Integer.parseInt(items[1])), crop, Integer.parseInt(items[3]))); - } catch (Exception ex) { - Log.warning("%s for farm '%s': %s", errormsg, tokens[0], ex.getMessage()); + } catch (IllegalArgumentException e) { + Log.warning("%s for farm '%s': %s", errormsg, tokens[0], e.getMessage()); } return true; diff --git a/src/main/java/forestry/farming/circuits/CircuitFarmLogic.java b/src/main/java/forestry/farming/circuits/CircuitFarmLogic.java index 029b8e2a1..9a80b066c 100755 --- a/src/main/java/forestry/farming/circuits/CircuitFarmLogic.java +++ b/src/main/java/forestry/farming/circuits/CircuitFarmLogic.java @@ -53,15 +53,12 @@ public class CircuitFarmLogic extends Circuit { IFarmLogic logic; try { logic = logicClass.getConstructor(IFarmHousing.class).newInstance(housing); - } catch (Exception ex) { - throw new RuntimeException("Failed to instantiate logic of class " + logicClass.getName() + ": " + ex.getMessage()); + } catch (ReflectiveOperationException ex) { + throw new RuntimeException("Failed to instantiate logic of class " + logicClass.getName() + ": " + ex.getMessage(), ex); } - try { - logic.setManual(isManual); - } catch (Throwable e) { - // uses older version of the API that doesn't implement setManual - } + logic.setManual(isManual); + housing.setFarmLogic(FarmDirection.values()[slot], logic); } diff --git a/src/main/java/forestry/storage/PluginStorage.java b/src/main/java/forestry/storage/PluginStorage.java index 884671cf8..e3eef82a2 100644 --- a/src/main/java/forestry/storage/PluginStorage.java +++ b/src/main/java/forestry/storage/PluginStorage.java @@ -12,6 +12,7 @@ package forestry.storage; import java.awt.Color; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -583,8 +584,8 @@ public class PluginStorage extends BlankForestryPlugin { ModelCrate.MODEL_COMPASS = ObfuscationReflectionHelper.getPrivateValue(ModelBakery.class, event.modelLoader, 15); ModelCrate.MODEL_CLOCK = ObfuscationReflectionHelper.getPrivateValue(ModelBakery.class, event.modelLoader, 16); ModelCrate.loader = event.modelLoader; - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + Log.error("Failed to bake model.", e); } } } diff --git a/test/ItemStackTest.java b/test/ItemStackTest.java deleted file mode 100644 index 142d5d6fb..000000000 --- a/test/ItemStackTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011-2014 SirSengir. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Lesser Public License v3 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/lgpl-3.0.txt - * - * Various Contributors including, but not limited to: - * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges - ******************************************************************************/ - -import net.minecraft.item.ItemFirework; -import net.minecraft.item.ItemStack; - -import forestry.apiculture.items.ItemHoneycomb; -import forestry.core.utils.ItemStackUtil; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class ItemStackTest { - - private ItemStack comb1; - private ItemStack comb2; - private ItemStack firework; - - @Before - public void setup() { - comb1 = new ItemStack(new ItemHoneycomb(), 32, 1); - comb2 = new ItemStack(new ItemHoneycomb(), 32, 2); - firework = new ItemStack(new ItemFirework(), 1, 0); - } - - @Test - public void testEquality() { - Assert.assertTrue("Equal ItemStacks are identical.", ItemStackUtil.isIdenticalItem(comb1, comb1)); - Assert.assertTrue("Equal ItemStacks are identical.", ItemStackUtil.isIdenticalItem(comb2, comb2)); - Assert.assertFalse("Unequal ItemStacks are not identical.", ItemStackUtil.isIdenticalItem(comb1, comb2)); - - // OreDict crashes when minecraft hasn't loaded -// Assert.assertTrue("Different combs are crafting equivalent with Ore Dictionary.", ItemStackUtil.isCraftingEquivalent(comb1, comb2, true, false)); -// Assert.assertFalse("Different combs are not crafting equivalent without Ore Dictionary.", ItemStackUtil.isCraftingEquivalent(comb1, comb2, false, false)); -// -// Assert.assertFalse("Different items are not crafting equivalent with Ore Dictionary.", ItemStackUtil.isCraftingEquivalent(firework, comb1, true, false)); -// Assert.assertFalse("Different items are not crafting equivalent without Ore Dictionary.", ItemStackUtil.isCraftingEquivalent(firework, comb1, false, false)); - } -} diff --git a/test/NetworkTest.java b/test/NetworkTest.java deleted file mode 100644 index 92ec7c4f1..000000000 --- a/test/NetworkTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011-2014 SirSengir. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Lesser Public License v3 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/lgpl-3.0.txt - * - * Various Contributors including, but not limited to: - * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges - ******************************************************************************/ - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import forestry.core.network.DataInputStreamForestry; -import forestry.core.network.DataOutputStreamForestry; - -import org.junit.Assert; -import org.junit.Test; - -public class NetworkTest { - @Test - public void testVarInt() { - ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - DataOutputStreamForestry dataOutput = new DataOutputStreamForestry(bytesOut); - - final int oneByteThreshold = (int) Math.pow(2, 7); - final int twoByteThreshold = (int) Math.pow(2, 14); - final int threeByteThreshold = (int) Math.pow(2, 21); - final int fourByteThreshold = (int) Math.pow(2, 28); - - try { - int prevSize = 0; - for (int i = 0; i < Integer.MAX_VALUE; i = (i * 2) + 1) { - dataOutput.writeVarInt(i); - - int sizeGained = dataOutput.size() - prevSize; - prevSize = dataOutput.size(); - if (i < oneByteThreshold) { - Assert.assertEquals(1, sizeGained); - } else if (i < twoByteThreshold) { - Assert.assertEquals(2, sizeGained); - } else if (i < threeByteThreshold) { - Assert.assertEquals(3, sizeGained); - } else if (i < fourByteThreshold) { - Assert.assertEquals(4, sizeGained); - } else { - Assert.assertEquals(5, sizeGained); - } - } - } catch (IOException e) { - Assert.fail("Writing var int threw an exception."); - } - - ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesOut.toByteArray()); - DataInputStreamForestry dataInput = new DataInputStreamForestry(bytesIn); - - try { - for (int i = 0; i < Integer.MAX_VALUE; i = (i * 2) + 1) { - int varInt = dataInput.readVarInt(); - Assert.assertEquals("Var int didn't read back correctly", i, varInt); - } - } catch (IOException e) { - Assert.fail("Reading var int threw an exception."); - } - } -}