diff --git a/src/main/java/crazypants/enderio/machine/farm/Fertilizer.java b/src/main/java/crazypants/enderio/machine/farm/Fertilizer.java new file mode 100644 index 00000000..36aab38e --- /dev/null +++ b/src/main/java/crazypants/enderio/machine/farm/Fertilizer.java @@ -0,0 +1,111 @@ +package crazypants.enderio.machine.farm; + +import java.util.ArrayList; +import java.util.List; + +import cpw.mods.fml.common.registry.GameRegistry; +import crazypants.util.BlockCoord; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +abstract public class Fertilizer { + + private static boolean initialized = false; + private static final Fertilizer NONE = new None(); + private static List instances = new ArrayList(); + + /** + * Returns the singleton instance for the fertilizer that was given as + * parameter. If the given item is no fertilizer, it will return an instance + * of Fertilizer.None. + * + */ + public static Fertilizer getInstance(ItemStack stack) { + if (instances.isEmpty()) { + instances.add(new Bonemeal()); + instances.add(new ForestryFertilizerCompound()); + } + for (Fertilizer fertilizer : instances) { + if (fertilizer.matches(stack)) { + return fertilizer; + } + } + return NONE; + } + + /** + * Returns true if the given item can be used as fertilizer. + */ + public static boolean isFertilizer(ItemStack stack) { + return getInstance(stack) != NONE; + } + + protected abstract boolean matches(ItemStack stack); + + /** + * Tries to apply the given item on the given block using the type-specific + * method. SFX is played on success. + * + * If the item was successfully applied, the stacksize will be decreased if + * appropriate. The caller will need to check for stacksize 0 and null the + * inventory slot if needed. + * + * @param stack + * @param player + * @param world + * @param bc + * @return true if the fertilizer was applied + */ + public abstract boolean apply(ItemStack stack, EntityPlayer player, World world, BlockCoord bc); + + /** + * Not a fertilizer. Using this handler class any item can be "used" as a + * fertilizer. Meaning, fertilizing will always fail. + * + */ + private static class None extends Fertilizer { + + @Override + protected boolean matches(ItemStack stack) { + return true; + } + + @Override + public boolean apply(ItemStack stack, EntityPlayer player, World world, BlockCoord bc) { + return false; + } + + } + + private static class Bonemeal extends Fertilizer { + + @Override + protected boolean matches(ItemStack stack) { + return stack.getItem() == Items.dye && stack.getItemDamage() == 15; + } + + public boolean apply(ItemStack stack, EntityPlayer player, World world, BlockCoord bc) { + return stack.getItem().onItemUse(stack, player, world, bc.x, bc.y, bc.z, 1, 0.5f, 0.5f, 0.5f); + } + + } + + private static class ForestryFertilizerCompound extends Bonemeal { + + private final Item forestryFertilizerCompound; + + protected ForestryFertilizerCompound() { + forestryFertilizerCompound = GameRegistry.findItem("Forestry", "fertilizerCompound"); + } + + @Override + protected boolean matches(ItemStack stack) { + return forestryFertilizerCompound != null && stack.getItem() == forestryFertilizerCompound; + } + + } + +} diff --git a/src/main/java/crazypants/enderio/machine/farm/TileFarmStation.java b/src/main/java/crazypants/enderio/machine/farm/TileFarmStation.java index 1fe70b53..cc4ebf82 100644 --- a/src/main/java/crazypants/enderio/machine/farm/TileFarmStation.java +++ b/src/main/java/crazypants/enderio/machine/farm/TileFarmStation.java @@ -356,7 +356,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity { } return false; } else if (i <= maxFirtSlot) { - return stack.getItem() == Items.dye && stack.getItemDamage() == 15; + return Fertilizer.isFertilizer(stack); } else if (i <= maxSupSlot) { return (inventory[i] != null || !isSlotLocked(i)) && FarmersCommune.instance.canPlant(stack); } else { @@ -463,7 +463,7 @@ public class TileFarmStation extends AbstractPoweredTaskEntity { } } else if (!isOpen(bc) && hasBonemeal() && bonemealCooldown-- <= 0) { // there's a block and it did not produce harvest. Try bonemealing it - if (inventory[minFirtSlot].getItem().onItemUse(inventory[minFirtSlot], farmerJoe, worldObj, bc.x, bc.y, bc.z, 1, 0.5f, 0.5f, 0.5f)) { + if (Fertilizer.getInstance(inventory[minFirtSlot]).apply(inventory[minFirtSlot], farmerJoe, worldObj, bc)) { PacketHandler.INSTANCE.sendToAllAround(new PacketFarmAction(bc), new TargetPoint(worldObj.provider.dimensionId, bc.x, bc.y, bc.z, 64)); if (inventory[minFirtSlot].stackSize == 0) { inventory[minFirtSlot] = null;