From 5834a266fdf425ad56558db71691f9b3b0df5e31 Mon Sep 17 00:00:00 2001 From: Draknyte1 Date: Tue, 17 Jan 2017 19:41:46 +1000 Subject: % Tried to improve the logic for the Tree Farmer, to support Forestry 4. % Moved COFH API related files. + Added a child mod, for misc handling. $$ Borrowed some code from MFR, to attempt forestry support. (Credit left, will re-do if I even get it working). --- .../minefactoryreloaded/api/ReplacementBlock.java | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java (limited to 'src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java') diff --git a/src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java b/src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java new file mode 100644 index 0000000000..c67e8a39a2 --- /dev/null +++ b/src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java @@ -0,0 +1,135 @@ +package powercrystals.minefactoryreloaded.api; + +import net.minecraft.block.Block; +import net.minecraft.item.*; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class ReplacementBlock +{ + protected byte _hasMeta; + protected int _meta; + protected final Block _block; + protected final NBTTagCompound _tileTag; + + /** + * Called to replace a block in the world. + * @param world The world object + * @param x The X coord + * @param y The Y coord + * @param z The Z coord + * @param stack The ItemStack being used to replace the block (may be null) + * @return True if the block was set successfully + */ + public boolean replaceBlock(World world, int x, int y, int z, ItemStack stack) + { + int meta = getMeta(world, x, y, z, stack); + if (world.setBlock(x, y, z, _block, meta, 3)) + { + if (hasTag(stack) && _block.hasTileEntity(meta)) + { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile != null) + tile.readFromNBT(getTag(world, x, y, z, stack)); + } + return true; + } + return false; + } + + /** + * Called to get the metadata of the replacement block in the world. + * @param world The world object + * @param x The X coord + * @param y The Y coord + * @param z The Z coord + * @param stack The ItemStack being used to replace the block (may be null) + * @return The metadata of the block + */ + protected int getMeta(World world, int x, int y, int z, ItemStack stack) + { + int m = 0; + if (_hasMeta > 0) + { + if (_hasMeta > 1) + return _meta; + m = stack.getItemDamage(); + Item item = stack.getItem(); + if (item instanceof ItemBlock) + m = ((ItemBlock)item).getMetadata(m); + } + return m; + } + + /** + * Called to set the metdata of this ReplacementBlock to a fixed value + * @param meta The metadata of the block + * @return This instance + */ + public ReplacementBlock setMeta(int meta) + { + if (meta >= 0) + { + _hasMeta = 2; + _meta = meta; + } + return this; + } + + /** + * Called to set the metdata of this ReplacementBlock to a value read from an ItemStack + * @param meta The metadata of the block + * @return This instance + */ + public ReplacementBlock setMeta(boolean hasMeta) + { + _hasMeta = (byte) (hasMeta ? 1 : 0); + return this; + } + + /** + * Called to get the NBTTagCompound a TileEntity will read its state from + * @param world The world object + * @param x The X coord + * @param y The Y coord + * @param z The Z coord + * @param stack The ItemStack being used to replace the block (may be null) + * @return The NBTTagCompound a TileEntity will read its state from + */ + protected NBTTagCompound getTag(World world, int x, int y, int z, ItemStack stack) + { + return _tileTag; + } + + /** + * Called to see if a TileEntity should have its state set + * @param stack The ItemStack being used to replace the block (may be null) + * @return True if the TileEntity should have its state set + */ + protected boolean hasTag(ItemStack stack) + { + return _tileTag != null; + } + + public ReplacementBlock(Item block) + { + this(Block.getBlockFromItem(block)); + } + + public ReplacementBlock(Item block, NBTTagCompound tag) + { + this(Block.getBlockFromItem(block), tag); + } + + public ReplacementBlock(Block block) + { + this(block, null); + } + + public ReplacementBlock(Block block, NBTTagCompound tag) + { + _block = block; + _tileTag = tag; + } +} -- cgit