Modding 1.4.7: Portal Placer Item Part 1

In this tutorial I will show you how to create an Item which will place down a portal frame when you right click with it on a block. A bit of this code is done in the mod file. This is the file I will start with.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
importnet.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
importnet.minecraft.entity.EnumCreatureType;
importnet.minecraft.world.biome.BiomeGenBase;
importnet.minecraftforge.common.DimensionManager;
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;
importcpw.mods.fml.common.registry.EntityRegistry;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@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;
      
       staticintstartEntityId = 300;
      
       publicstaticintdimension = 20;
      
       publicstatic Block portal;
      
       @Init
       publicvoid load(FMLInitializationEvent event)
       {
             proxy.registerRenderThings();
            
             portal = newBlockPortalTutorial(251, 1).setBlockName(“portal”);
             GameRegistry.registerBlock(portal, “Wuppy29_Portal”);
             LanguageRegistry.addName(portal, “Custom Portal”);
            
             EntityRegistry.registerModEntity(EntityTutorial.class, “Tutorial”, 1, this, 80, 3, true);
            
             EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4, EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);
            
              LanguageRegistry.instance().addStringLocalization(“entity.Tutorial_Tutorialmod.Tutorial.name”, “Tutorial”);
            
             registerEntityEgg(EntityTutorial.class, 0x7A65CF, 0x4DF200);
            
             DimensionManager.registerProviderType(dimension, WorldProviderTutorial.class, false);
             DimensionManager.registerDimension(dimension, dimension);
       }
      
       publicstaticint getUniqueEntityId()
       {
             do
             {
                    startEntityId++;
             }
             while(EntityList.getStringFromID(startEntityId) != null);
                   
             returnstartEntityId;
       }
      
       publicstaticvoid registerEntityEgg(Class <? extends Entity> entity, int primaryColor, int secondaryColor)
       {
             int id = getUniqueEntityId();
             EntityList.IDtoClassMapping.put(id, entity);
             EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
       }
}

You should start by adding the basic Item code to the mod file. These lines.


publicstaticItem portalPlacer;

portalPlacer = newItemPortalPlacer(5000).setIconIndex(0).setItemName(“tutorial”);
            
LanguageRegistry.addName(portalPlacer, “Portal Placer”);

If you don’t understand this code you should take a look at the basic Item tutorials.
The whole file should now look like this.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
importnet.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
importnet.minecraft.entity.EnumCreatureType;
import net.minecraft.item.Item;
importnet.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.DimensionManager;
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;
importcpw.mods.fml.common.registry.EntityRegistry;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@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;
      
       staticintstartEntityId = 300;
      
       publicstaticintdimension = 20;
      
       publicstatic Block portal;
      
       publicstatic Item portalPlacer;
      
       @Init
       publicvoid load(FMLInitializationEvent event)
       {
             proxy.registerRenderThings();
             portalPlacer = new ItemPortalPlacer(5000).setIconIndex(0).setItemName(“tutorial”);
            
             LanguageRegistry.addName(portalPlacer, “Portal Placer”);
            
             portal = newBlockPortalTutorial(251, 1).setBlockName(“portal”);
             GameRegistry.registerBlock(portal, “Wuppy29_Portal”);
             LanguageRegistry.addName(portal, “Custom Portal”);
            
             EntityRegistry.registerModEntity(EntityTutorial.class, “Tutorial”, 1, this, 80, 3, true);
            
             EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4, EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);
            
              LanguageRegistry.instance().addStringLocalization(“entity.Tutorial_Tutorialmod.Tutorial.name”, “Tutorial”);
            
             registerEntityEgg(EntityTutorial.class, 0x7A65CF, 0x4DF200);
            
             DimensionManager.registerProviderType(dimension, WorldProviderTutorial.class, false);
             DimensionManager.registerDimension(dimension, dimension);
       }
      
       publicstaticint getUniqueEntityId()
       {
             do
             {
                    startEntityId++;
             }
             while(EntityList.getStringFromID(startEntityId) != null);
                   
             returnstartEntityId;
       }
      
       publicstaticvoid registerEntityEgg(Class <? extends Entity> entity, int primaryColor, int secondaryColor)
       {
             int id = getUniqueEntityId();
             EntityList.IDtoClassMapping.put(id, entity);
             EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
       }
}

Next you should simply hover your mouse over ItemPortalPlacer and click Create Class.
Now make sure the file looks like this one.


package tutorial;

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

This is simply the code for a basic item. If you don’t understand it I suggest reading through the basic Item tutorials.
To make an item place something when you right click is really easy. You simply need one method. It 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;
    }

This method will simply run when you right click with the item on a block.
You might be thinking why it has the line return true in there. This is because it is a boolean method. This means that it has to return true or false no matter what.
par1ItemStack is the item you are using. par2EntityPlayer is the player that is using it. par3World is the world you are using it in. par4, par5 and par6 are x, y and z.
The others are unknown to me. If you do know them I would apreciate it if you mailed it to me at wuppy29@gmail.com.
Now you will have to add a simple check in this method to make sure it only places the blocks on the server side, because it doesn’t work on the client side. That code 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)

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

If you want to place a block in the world you will have to use a line of code just like this one.


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


This code will place a block of sandstone at the location where you clicked it.
You have to add that code inside of the if. So the method should 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)
             {
                    par3World.setBlock(par4, par5, par6, Block.sandStone.blockID);
                    returntrue;
             }
             else
                    returnfalse;
    }

And the whole Item file should now look something 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 ItemPortalPlacer extends Item
{
       public ItemPortalPlacer(int id)
       {
             super(id);
             this.setCreativeTab(CreativeTabs.tabMisc);
       }
      
       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.sandStone.blockID);
                    returntrue;
             }
             else
                    returnfalse;
    }
}

In the next tutorial I will show you how to place the full portal properly.

The source code will be available at the end of the last tutorial.

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="">