Jump to content

Recommended Posts

Posted

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....

Posted

I might be wrong, but I don't think you can register recipes in the PreInit, I think they have to be done in the PostInit, as for the item thing, I don't know I am not to good with aabbs and things like that. Hope this helps!

Don't be afraid to ask question when modding, there are no stupid question! Unless you don't know java then all your questions are stupid!

Posted

You are spawning the Items on the client side, but you need to spawn them server side. You need to use

if(!world.isRemote) at the beginning of your shearSheep method.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Mecblader:

 

I had this same issue before I created the crafting recipe, so i don't think that is the problem

 

Edit:  I meant to say: I had the "items can't be picked up issue"  before I had the crafting recipe.  You might be on to something about the crash on crafting.  I will have to dig deeper on this.

 

larsgerrits:

 

I changed the shearSheep method to include if (!world.isRemote), but now it crashes every time I try to craft the item???

 

code here:

 


public void shearSheep(World world, int x, int y, int z, Entity target) {
	if (!world.isRemote) {
		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;

				}
			}
		} else {
		}
	}
}

 

Here is the message from the server:

 

 

java.lang.NullPointerException
        at net.minecraft.item.ItemStack.func_77980_a(ItemStack.java:385) ~[add.class:?]
        at net.minecraft.inventory.SlotCrafting.func_75208_c(SlotCrafting.java:53) ~[aax.class:?]
        at net.minecraft.inventory.SlotCrafting.func_82870_a(SlotCrafting.java:110) ~[aax.class:?]
        at net.minecraft.inventory.Container.func_75144_a(SourceFile:238) ~[zs.class:?]
        at net.minecraft.network.NetHandlerPlayServer.func_147351_a(NetHandlerPlayServer.java:878) ~[nh.class:?]
        at net.minecraft.network.play.client.C0EPacketClickWindow.func_148833_a(SourceFile:32) ~[ix.class:?]
        at net.minecraft.network.play.client.C0EPacketClickWindow.func_148833_a(SourceFile:10) ~[ix.class:?]
        at net.minecraft.network.NetworkManager.func_74428_b(NetworkManager.java:212) ~[ej.class:?]
        at net.minecraft.network.NetworkSystem.func_151269_c(NetworkSystem.java:165) [nc.class:?]
        at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:659) [MinecraftServer.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:335) [lt.class:?]
        at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:547) [MinecraftServer.class:?]
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:427) [MinecraftServer.class:?]
        at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:685) [li.class:?]

 

Posted

Why do you have 2 FMLPreInitializationEvents? Copy the recipe to the FMLInitializationEvent and remove the first FMLPreInitializationEvent. That should solve the issue, as the issue was caused by registering a recipe with a ItemStack containing a null item (as the items weren't initialized before the recipe).

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Thank you!  :)

 

I didn't intend to have 2 preInitializationEvents, I copied the recipe portion from another mod I was working on and accidentally grabbed that part.  That makes sense that the item should be registered before the recipe. 

 

It looks like the if (!world.isRemote) fixed the item issue also.

 

Thank you very much!

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.