Modding 1.4.7: Block Placer Item

In this tutorial I will show you how to make a basic Block or Structure placer item. This is the mod file I will start with.


package tutorial;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
@Mod(modid = “Tutorial_Tutorialmod”, name = “Tutorial”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       @SidedProxy(clientSide = “tutorial.ClientProxyTutorial”, serverSide = “tutorial.CommonProxyTutorial”)
       publicstatic CommonProxyTutorial proxy;
      
       @Init
       publicvoid load(FMLInitializationEvent event)
       {
             proxy.registerRenderThings();
       }
}

This tutorial will be a lot like the Portal placer item tutorial, because it is the same technique.
First you will have to make a basic Item.


publicstatic Item blockPlacer;

blockPlacer = new ItemBlockPlacer(5001).setIconIndex(1).setItemName(“blockPlacer”);
LanguageRegistry.addName(blockPlacer, “Block Placer”);

If you don’t understand this code you will have to take a look at the basic Item tutorial over here.
Now simply create the basic Item file by hovering your mouse over it and making it look like this.


package tutorial;
importnet.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
publicclass ItemBlockPlacer extends Item
{
       public ItemBlockPlacer(int id)
       {
             super(id);
             this.setCreativeTab(CreativeTabs.tabMaterials);
       }
       public String getTextureFile()
       {
             return“/tutorialitems.png”;
    }
}

If you don’t understand this code you will have to read through this tutorial.
Now the method where you will let the Item place blocks in the world looks like this.


publicboolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)

    {
             returntrue;
    }

Now in here you first have to make sure it only places blocks on the server side. To do that you will have to make it look like this.


publicboolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)

    {
             if(!par3World.isRemote)
             {
                    returntrue;
             }
             else
                    returnfalse;
    }

The location of the block placement code has to be inside of the if statement or it will not work.
Code to place a block could be like this.


par3World.setBlock(par4, par5, par6, Block.bedrock.blockID);


par3World will be used to acces methods inside of the World class.
.setBlock is the method you have to use to place a block inside of the world.
As you can see there are 4 parameters in this method.
The first one, par4, is the x location of the block you will place. When you use par4 it simply uses the block you right clicked the item on.
The second one, par5, is the y location of the block you will place. Also par5 is the location of the block you right click on with your item.
Then you have the third one which is par6. Par6 is the z location of the block and just like par4 and par5 it is the also the location of the block you right clicked the item on.
The last parameter in here is the Block you want to place. There are 3 parts to this code. The first one is the file you are going to use. In this case it is Block, but if you want to use a block from your mod you will have to place the modfile in here. The second part is the name of the block. You can find the names by looking in the files. The last part is the .blockID. This will make sure that it gets the Id of the block you want to place. If you don’t have this at the end it will give you errors.
If you want to change the position of the block you place you can simply add + 1 or – 5 or something like that behind the x, y and z parameters. The whole Item file should now look like this.


package tutorial;

import net.minecraft.block.Block;
importnet.minecraft.creativetab.CreativeTabs;
importnet.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
publicclass ItemBlockPlacer extends Item
{
       public ItemBlockPlacer(int id)
       {
             super(id);
             this.setCreativeTab(CreativeTabs.tabMaterials);
       }
       public String getTextureFile()
       {
             return“/tutorialitems.png”;
    }
      
       publicboolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
    {
             if(!par3World.isRemote)
             {
                    par3World.setBlock(par4, par5, par6, Block.bedrock.blockID);
                    returntrue;
             }
             else
                    returnfalse;
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">