So, I'm trying to get my Weee! Flowers mod using tile entities for the sixteen flower crops.
I've managed to do everything except the crops don't show their textures and when broken, they drop nothing.
Any help you could give me with this would be fantastic. So many people are waiting for me to update! >.<
BlockPamFlowerCrop
package assets.pamweeeflowers;
import java.util.ArrayList;
import java.util.Random;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
public class BlockPamFlowerCrop extends BlockFlower
{
@SideOnly(Side.CLIENT)
public Icon[][] iconArray;
public String[] cropName = { "white", "orange", "magenta", "lightblue", "lime", "pink", "darkgrey", "lightgrey", "cyan", "purple", "blue", "brown", "black", "yellow", "red" };
protected BlockPamFlowerCrop(int par1)
{
super(par1);
this.setTickRandomly(true);
float f = 0.5F;
this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
this.setCreativeTab((CreativeTabs)null);
this.setHardness(0.0F);
this.setStepSound(soundGrassFootstep);
this.disableStats();
}
public int idDropped(int i, Random random, int j)
{
//We will use our own drop function
return 0;
}
protected boolean canThisPlantGrowOnThisBlockID(int i)
{
return i == Block.tilledField.blockID;
}
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int i, int j, int k)
{
return null;
}
public int getRenderType()
{
//set our RenderID
//return PamWeeeFlowers.FlowerCropRenderID;
return 6;
}
public boolean renderAsNormalBlock()
{
//use custom renderID
return false;
}
public boolean isOpaqueCube()
{
//Xray sides appear on the sides of the block while this is true;
return false;
}
public TileEntity getBlockEntity()
{
//get a TileEntity.
return new TileEntityPamFlowerCrop();
}
public static TileEntityPamFlowerCrop getFlowerCrop(World world, int i, int j, int k)
{
TileEntityPamFlowerCrop crop = (TileEntityPamFlowerCrop)world.getBlockTileEntity(i, j, k);
return crop;
//This will do all the casting and checking we need
}
/**
* Apply bonemeal to the crops.
*/
public void fertilize(World world, int i, int j, int k, EntityPlayer entityplayer)
{
//Primative bonemeal function
ItemStack helditem = entityplayer.inventory.getStackInSlot(entityplayer.inventory.currentItem); //get selected slot
TileEntityPamFlowerCrop crop = getFlowerCrop(world, i, j, k);
if (crop != null && helditem != null)
{
if (helditem.itemID == Item.dyePowder.itemID && helditem.getItemDamage() == 15) /* If it is white dye */
{
crop.applyBonemeal();
entityplayer.inventory.decrStackSize(entityplayer.inventory.currentItem, 1); //decrease current item id slot by 1. Remove 1 bonemeal.
}
}
return;
}
public void updateTick(World world, int i, int j, int k, Random random)
{
super.updateTick(world, i, j, k, random);
if (world.getBlockLightValue(i, j + 1, k) >= 9)
{
//Removed a bunch of stuff involving metadata. addGrowthStage() will do it all.
float f = getGrowthRate(world, i, j, k);
if (random.nextInt((int)(260F / f)) == 0)
{
TileEntityPamFlowerCrop crop = getFlowerCrop(world, i, j, k);
if (crop != null) /* still need to make sure we have a tileentity */
{
crop.addGrowthStage(); //right click grows it one stage
}
}
}
}
/**
* Gets the growth rate for the crop. Setup to encourage rows by halving growth rate if there is diagonals, crops on
* different sides that aren't opposing, and by adding growth for every crop next to this one (and for crop below
* this one). Args: x, y, z
*/
private float getGrowthRate(World par1World, int par2, int par3, int par4)
{
float f = 1.0F;
int l = par1World.getBlockId(par2, par3, par4 - 1);
int i1 = par1World.getBlockId(par2, par3, par4 + 1);
int j1 = par1World.getBlockId(par2 - 1, par3, par4);
int k1 = par1World.getBlockId(par2 + 1, par3, par4);
int l1 = par1World.getBlockId(par2 - 1, par3, par4 - 1);
int i2 = par1World.getBlockId(par2 + 1, par3, par4 - 1);
int j2 = par1World.getBlockId(par2 + 1, par3, par4 + 1);
int k2 = par1World.getBlockId(par2 - 1, par3, par4 + 1);
boolean flag = j1 == this.blockID || k1 == this.blockID;
boolean flag1 = l == this.blockID || i1 == this.blockID;
boolean flag2 = l1 == this.blockID || i2 == this.blockID || j2 == this.blockID || k2 == this.blockID;
for (int l2 = par2 - 1; l2 <= par2 + 1; ++l2)
{
for (int i3 = par4 - 1; i3 <= par4 + 1; ++i3)
{
int j3 = par1World.getBlockId(l2, par3 - 1, i3);
float f1 = 0.0F;
if (blocksList[j3] != null && blocksList[j3].canSustainPlant(par1World, l2, par3 - 1, i3, ForgeDirection.UP, this))
{
f1 = 1.0F;
if (blocksList[j3].isFertile(par1World, l2, par3 - 1, i3))
{
f1 = 3.0F;
}
}
if (l2 != par2 || i3 != par4)
{
f1 /= 4.0F;
}
f += f1;
}
}
if (flag2 || flag && flag1)
{
f /= 2.0F;
}
return f;
}
@SideOnly(Side.CLIENT)
public Icon getIcon(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
{
int cropID = 0;
int stage = 0;
TileEntityPamFlowerCrop tileentitypamflowercrop = (TileEntityPamFlowerCrop)par1IBlockAccess.getBlockTileEntity(par2, par3, par4);
if (tileentitypamflowercrop != null)
{
cropID = tileentitypamflowercrop.getFlowerCropID();
stage = tileentitypamflowercrop.getGrowthStage();
}
return this.iconArray[cropID][stage];
}
@SideOnly(Side.CLIENT)
public void registerIcon(IconRegister ir)
{
this.iconArray = new Icon[15][2];
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 2; j++)
{
this.iconArray[i][j] = ir.registerIcon("pamweeeflowers:" + this.cropName[i] + (j));
}
}
}
}
ClientProxyPamWeeeFlowers
package assets.pamweeeflowers;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry;
import net.minecraftforge.client.MinecraftForgeClient;
public class ClientProxyPamWeeeFlowers extends CommonProxyPamWeeeFlowers
{
@Override
public void registerRenderThings()
{
RenderingRegistry.registerBlockHandler(new PamFlowerCropRenderHandler());
RenderingRegistry.registerBlockHandler(PamWeeeFlowers.FlowerCropRenderID, new PamFlowerCropRenderHandler());
}
}
PamFlowerCropRenderHandler
package assets.pamweeeflowers;
import javax.swing.Renderer;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.client.registry.RenderingRegistry;
public class PamFlowerCropRenderHandler
implements ISimpleBlockRenderingHandler
{
public PamFlowerCropRenderHandler()
{
PamWeeeFlowers.FlowerCropRenderID = RenderingRegistry.getNextAvailableRenderId();
}
public int getRenderId()
{
return PamWeeeFlowers.FlowerCropRenderID;
}
public boolean shouldRender3DInInventory()
{
return false;
}
@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer)
{
if (modelId == PamWeeeFlowers.FlowerCropRenderID)
{
TileEntityPamFlowerCrop tileentitypamflowercrop = (TileEntityPamFlowerCrop)world.getBlockTileEntity(x, y, z);
if (tileentitypamflowercrop == null)
{
return false;
}
int cropID = tileentitypamflowercrop.getFlowerCropID();
int stage = tileentitypamflowercrop.getGrowthStage();
if ((block != null) && ((block instanceof BlockPamFlowerCrop)))
{
BlockPamFlowerCrop pamflowerCrop = (BlockPamFlowerCrop)block;
renderer.setOverrideBlockTexture(pamflowerCrop.iconArray[cropID][stage]);
renderer.renderBlockCrops(block, x, y, z);
renderer.hasOverrideBlockTexture();
return true;
}
return false;
}
return false;
}
@Override
public void renderInventoryBlock(Block block, int metadata, int modelID,
RenderBlocks renderer) {
// TODO Auto-generated method stub
}
}
TileEntityPamFlowerCrop
package assets.pamweeeflowers;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
public class TileEntityPamFlowerCrop extends TileEntity
{
public TileEntityPamFlowerCrop()
{
}
public void readFromNBT(NBTTagCompound nbttagcompound)
{
//Load the data
super.readFromNBT(nbttagcompound);
FlowerCropID = nbttagcompound.getShort("FlowerCropID");
GrowthStage = nbttagcompound.getShort("GrowthStage");
}
public void writeToNBT(NBTTagCompound nbttagcompound)
{
//Save the data
super.writeToNBT(nbttagcompound);
nbttagcompound.setShort("FlowerCropID", (short) FlowerCropID);
nbttagcompound.setShort("GrowthStage", (short) GrowthStage);
}
public void invalidate()
{
super.invalidate();
int i = GrowthStageCount[FlowerCropID] - 1;
int j = 0;
int k = 0;
if (worldObj.rand.nextInt(1) == 0)
{
j++;
}
if (GrowthStage > 0 && worldObj.rand.nextInt(2) == 0)
{
j++;
}
if (GrowthStage == i)
{
if (j < 1)
{
j = 1;
}
j += worldObj.rand.nextInt(2);
k = 1;
}
/// Added Here
if (GrowthStage == -1)
{
j = 0;
k = 0;
}
///
if (FlowerCropID < SeedDrops.length && j > 0)
{
DropItem(SeedDrops[FlowerCropID], j);
}
if (FlowerCropID < HarvestDrops.length && k > 0)
{
DropItem(HarvestDrops[FlowerCropID], k);
}
}
public void DropItem(int Item, int Count)
{
float f = 0.7F;
double d = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
double d1 = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
double d2 = (double)(worldObj.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
if (FlowerCropID > 13)
{
EntityItem entityitem = new EntityItem(worldObj, (double)xCoord + d, (double)yCoord + d1, (double)zCoord + d2, new ItemStack(Item, Count, 0));
entityitem.delayBeforeCanPickup = 10;
worldObj.spawnEntityInWorld(entityitem);
}
else
{
EntityItem entityitem = new EntityItem(worldObj, (double)xCoord + d, (double)yCoord + d1, (double)zCoord + d2, new ItemStack(Item, Count, FlowerCropID));
entityitem.delayBeforeCanPickup = 10;
worldObj.spawnEntityInWorld(entityitem);
}
return;
}
public void setFlowerCropID(int ID)
{
FlowerCropID = ID;//used by the seed item
}
public int getFlowerCropID()
{
//Base value for fetching other values
return FlowerCropID;
}
public void addGrowthStage()
{
//Run this instead of metadata functions
if (FlowerCropID < GrowthStageCount.length) /* Check if the CropID is in range to avoid a crash */
{
if (GrowthStage < GrowthStageCount[FlowerCropID] - 1) /* If it hasn't hit the max for that CropID, add a stage. Subtract one because the start stage was already used. */
{
GrowthStage++;
}
}
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, worldObj.getBlockId(xCoord, yCoord, zCoord)); //Updates the block so it displays properly
}
public void setGrowthStage(int Stage)
{
//In case you need an override
if (FlowerCropID < GrowthStageCount.length)
{
int MaxStage = GrowthStageCount[FlowerCropID] - 1; //Make sure we don't get above max
if (Stage > MaxStage)
{
GrowthStage = MaxStage;
}
else
{
GrowthStage = Stage;
}
}
worldObj.markBlockForRenderUpdate(xCoord, yCoord, zCoord);
worldObj.setBlock(xCoord, yCoord, zCoord, worldObj.getBlockId(xCoord, yCoord, zCoord));
}
public int getGrowthStage()
{
//In case you need to check the stage
return GrowthStage;
}
public void applyBonemeal()
{
//Set it to the Max Stage
if (FlowerCropID < GrowthStageCount.length)
{
int MaxStage = GrowthStageCount[FlowerCropID] - 1; //Make sure we don't get above max
System.out.println("I'm Growing");
GrowthStage = MaxStage;
}
worldObj.setBlock(xCoord, yCoord, zCoord, worldObj.getBlockId(xCoord, yCoord, zCoord));
}
private int FlowerCropID = 0; //This will be set in a custom seed item during placement
private int GrowthStage = 0; //should always start at 0
private int[] GrowthStageCount = {3,//White
3,//Orange
3,//Magenta
3,//Lightblue
3,//Lime
3,//Pink
3,//Grey
3,//Lightgrey
3,//Cyan
3,//Purple
3,//Blue
3,//Brown
3,//Green
3,//Black
3,//Yellow
3};//Red
//There are 8 default wheat stages. We will put 8 for CropID 3 so we can go through all stages.
private int[] SeedDrops = {PamWeeeFlowers.pamFlower.blockID,//White
PamWeeeFlowers.pamFlower.blockID,//Orange
PamWeeeFlowers.pamFlower.blockID,//Magenta
PamWeeeFlowers.pamFlower.blockID,//Lightblue
PamWeeeFlowers.pamFlower.blockID,//Lime
PamWeeeFlowers.pamFlower.blockID,//Pink
PamWeeeFlowers.pamFlower.blockID,//Grey
PamWeeeFlowers.pamFlower.blockID,//Lightgrey
PamWeeeFlowers.pamFlower.blockID,//Cyan
PamWeeeFlowers.pamFlower.blockID,//Purple
PamWeeeFlowers.pamFlower.blockID,//Blue
PamWeeeFlowers.pamFlower.blockID,//Brown
PamWeeeFlowers.pamFlower.blockID,//Green
PamWeeeFlowers.pamFlower.blockID,//Black
Block.plantYellow.blockID,//Yellow
Block.plantRed.blockID};//Red
//These are seed drops in the same array format. Just fill it with Item/Block IDs.
private int[] HarvestDrops = {PamWeeeFlowers.pamFlower.blockID,//White
PamWeeeFlowers.pamFlower.blockID,//Orange
PamWeeeFlowers.pamFlower.blockID,//Magenta
PamWeeeFlowers.pamFlower.blockID,//Lightblue
PamWeeeFlowers.pamFlower.blockID,//Lime
PamWeeeFlowers.pamFlower.blockID,//Pink
PamWeeeFlowers.pamFlower.blockID,//Grey
PamWeeeFlowers.pamFlower.blockID,//Lightgrey
PamWeeeFlowers.pamFlower.blockID,//Cyan
PamWeeeFlowers.pamFlower.blockID,//Purple
PamWeeeFlowers.pamFlower.blockID,//Blue
PamWeeeFlowers.pamFlower.blockID,//Brown
PamWeeeFlowers.pamFlower.blockID,//Green
PamWeeeFlowers.pamFlower.blockID,//Black
Block.plantYellow.blockID,//Yellow
Block.plantRed.blockID};//Red
}
I've managed to do everything except the crops don't show their textures and when broken, they drop nothing.
Any help you could give me with this would be fantastic. So many people are waiting for me to update! >.<
BlockPamFlowerCrop
ClientProxyPamWeeeFlowers
PamFlowerCropRenderHandler
TileEntityPamFlowerCrop