Hi all,
I have been working on making a simple mod that shears sheep when they collide with the block. Everything works fine, except sometimes on loading, the first time the block shears the sheep I can't pick up the wool. I think it has something to do with the server side not registering the items on the first collision but i am not sure.
Another odd thing that happens, usually right after loading, the game sometimes crashes when crafting the "Shear Block".
Here is the code:
ShearBlockMod.java
package net.bb.shearblockmod;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
@Mod(modid = ShearBlockMod.modid, version = ShearBlockMod.version)
public class ShearBlockMod {
public static final String modid = "ShearBlockMod";
public static final String version = "0.1 MC1.7.2";
@EventHandler
public void preinit(FMLPreInitializationEvent event)
{
GameRegistry.addRecipe(new ItemStack(ShearBlockMod.shearBlock), new Object[]{
" A",
" A ",
"A ",
'A', Items.iron_ingot});
}
public static CreativeTabs tabExample = new CreativeTabs("tabExample") {
public Item getTabIconItem() {
return Items.bed;
}
};
public static CreativeTabs tabExample2 = new CreativeTabs("tabExample2") {
public Item getTabIconItem() {
return Item.getItemFromBlock(Blocks.carpet);
}
};
public static Item tutorialItem;
public static Block shearBlock;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
shearBlock = new BlockShearBlock().setBlockName("shearBlock").setCreativeTab(tabExample2).setBlockTextureName(modid + ":" + "shearBlock");
GameRegistry.registerBlock(shearBlock, "shearBlock");
tutorialItem = new Item().setCreativeTab(tabExample).setUnlocalizedName("tutorialItem").setTextureName(modid + ":" + "tutorialItem");
GameRegistry.registerItem(tutorialItem, "tutorialItem");
}
@EventHandler
public void init(FMLInitializationEvent event) {
}
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
}
BlockShearBlock.java
package net.bb.shearblockmod;
import java.util.ArrayList;
import java.util.Random;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockShearBlock extends Block{
boolean powered;
public BlockShearBlock(){
super(Material.rock);
this.setTickRandomly(true);
}
public void onBlockAdded(World world, int x, int y, int z, Block id)
{
if (!world.isRemote)
{
//AxisAlignedBB myAABB = AxisAlignedBB.getBoundingBox(x+1, y+1, z+1, x-1, y+2, z-1);
if (world.getBlockPowerInput(x, y, z) > 0)
{
System.out.println("new power on!");
powered = true;
}
else
{
System.out.println("new power off!");
powered = false;
}
}
//world.notifyBlocksOfNeighborChange(x, y, z, this);
}
@Override
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
{
int shearBlockID = Block.getIdFromBlock(null);
if (!par1World.isRemote)
System.out.println("Block Activated Test" + shearBlockID );
return true;
}
public void shearSheep(World world, int x, int y, int z, Entity target)
{
Entity theSheep = (Entity)target;
if (target instanceof IShearable)
{
IShearable Entity = (IShearable)theSheep;
if (Entity.isShearable(null, world , x, y, z))
{
ArrayList<ItemStack> drops = ((IShearable) theSheep).onSheared(null, world, (int)target.posX, (int)target.posY, (int)target.posZ, 3);
Random rand = new Random();
for(ItemStack stack : drops)
{
EntityItem ent = ((Entity) Entity).entityDropItem(stack, 1.0F);
ent.motionY += rand.nextFloat() * 0.05F;
ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
}
}
}
}
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int i, int j, int k)
{
float f = 0.0625F;
return AxisAlignedBB.getBoundingBox((float)i + f, j, (float)k + f, (float)(i + 1) - f, (float)(j + 1) - f, (float)(k + 1) - f);
}
@Override
public AxisAlignedBB getSelectedBoundingBoxFromPool(World world, int i, int j, int k)
{
float f = 0.0625F;
return AxisAlignedBB.getBoundingBox((float)i + f, j, (float)k + f, (float)(i + 1) - f, j + 1, (float)(k + 1) - f);
}
@Override
public void onEntityCollidedWithBlock(World world, int i, int j, int k, Entity entity)
{
//if(powered == true)
{
System.out.println("Engage Shearing!");
shearSheep(world, (int)entity.posX, (int)entity.posY, (int)entity.posZ, entity);
}
}
}
Any help would be much appreciated....