Modding 1.4.7: Portal Placer Item Part 2

In this tutorial we will finish up the item that will place the portal when you right click with it. All of the code in this tutorial will be done in the Item file. This is the file I will start with.


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;
    }
}

The first thing you need to do when you want to place more than one block using an item is getting the player’s direction. To do that you will have to add one line of code inside of the if statement.


int direction = MathHelper.floor_double((double)(par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;


This code will take the rotation of the player, multiply it by 4, devide it by 360, add 0.5 and round it of downwards. Then it will use a bitwise or operator of 3 to make sure that if the number is above 3 goes back to 0.
There are 4 directions after this formula. 0, 1, 2 and 3. These numbers will be used to place the portal in a certain place when the player has a certain direction.
Now you will have to make 2 types of portal placement codes. One for the x and one for the z. To do that you will first need an if and an else statement in there just like this.


if(!par3World.isRemote)

{
       if(direction == 1 || direction == 3)
       {
                           
       }
       else
       {
                         
       }
       returntrue;
}

The whole method 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.util.MathHelper;
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)
             {
                    int direction = MathHelper.floor_double((double)(par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
                   
                    if(direction == 1 || direction == 3)
                    {
                          
                    }
                    else
                    {
                          
                    }
                   
                    returntrue;
             }
              else
                    returnfalse;
    }
}

Next up is placing the blocks in the world. For this tutorial I will go with the basic portal structure just like the nether portal, but with sandstone and the custom portal. The code in the if statement should be like this.


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

par3World.setBlock(par4, par5 + 1, par6 + 1, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 1, par6 + 2, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 1, par6 – 1, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 2, par6 – 1, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 3, par6 – 1, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 4, par6 – 1, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 5, par6 – 1, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 2, par6 + 2, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 3, par6 + 2, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 4, par6 + 2, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 5, par6 + 2, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
par3World.setBlock(par4, par5 + 5, par6 + 1, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 2, par6 + 1, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 3, par6 + 1, Tutorial.portal.blockID);

par3World.setBlock(par4, par5 + 4, par6 + 1, Tutorial.portal.blockID);


The first 4 lines of this code place the bottom of the portal. The next 4 place one side and the next 4 the other. The 2 lines place the roof of the portal on there. The 6 lines below place the portal blocks inside of the sandstone frame.
Now for the else you will need mostly the same code.


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

par3World.setBlock(par4 + 1, par5 + 1, par6, Block.sandStone.blockID);
par3World.setBlock(par4 + 2, par5 + 1, par6, Block.sandStone.blockID);
par3World.setBlock(par4 – 1, par5 + 1, par6, Block.sandStone.blockID);
                          
par3World.setBlock(par4 – 1, par5 + 2, par6, Block.sandStone.blockID);
par3World.setBlock(par4 – 1, par5 + 3, par6, Block.sandStone.blockID);
par3World.setBlock(par4 – 1, par5 + 4, par6, Block.sandStone.blockID);
par3World.setBlock(par4 – 1, par5 + 5, par6, Block.sandStone.blockID);
                          
par3World.setBlock(par4 + 2, par5 + 2, par6, Block.sandStone.blockID);
par3World.setBlock(par4 + 2, par5 + 3, par6, Block.sandStone.blockID);
par3World.setBlock(par4 + 2, par5 + 4, par6, Block.sandStone.blockID);
par3World.setBlock(par4 + 2, par5 + 5, par6, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
par3World.setBlock(par4 + 1, par5 + 5, par6, Block.sandStone.blockID);
                          
par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
par3World.setBlock(par4 + 1, par5 + 2, par6, Tutorial.portal.blockID);
par3World.setBlock(par4 + 1, par5 + 3, par6, Tutorial.portal.blockID);
par3World.setBlock(par4 + 1, par5 + 4, par6, Tutorial.portal.blockID);

If you read through this code you should notice that the only difference between the 2 directions is that the + and – are behind par4 which is x instead of par6 which is z.
The onItemUse method should now 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)
             {
                    int direction = MathHelper.floor_double((double)(par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
                   
                    if(direction == 1 || direction == 3)
                    {
                           par3World.setBlock(par4, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 + 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 – 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 – 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 + 2, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 + 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 2, par6 + 1, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 + 1, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 + 1, Tutorial.portal.blockID);
                    }
                    else
                    {
                           par3World.setBlock(par4, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 1, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 1, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4 – 1, par5 + 2, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 3, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 4, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4 + 2, par5 + 2, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 3, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 4, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 1, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 4, par6, Tutorial.portal.blockID);
                    }
                   
                    returntrue;
             }
             else
                    returnfalse;
    }

Right now when you try out this item in your game it will place the portal for you, but there is one problem. It will remove any block it it’s way and just replace them with the portal. To stop that you will need to add this code in the if.


for(int y = 1; y < 5; y++)

                           {
                                  for (int z = -1; z < 2; z++)
                                  {
                                        if(par3World.getBlockId(par4, par5 + y, par6 + z) != 0)
                                        {
                                               returnfalse;
                                        }
                                  }
                           }

This code is made to check for every y and z position the portal will be placing the portal. If a single one of those blocks is not air it will return false and stop the block placing code. The way this works is that it runs through the first for once making y = 1. Then it enters the for with z and first makes z = -1. Then it checks if the block is empty. After that it will add one to the z and go again. Repeat that untill z < 2. Then it will return to the first for and make y = 2. Then it will once again run through all the z’s and check the blocks. This repeats itself a couple of times and if all the blocks are air it will place the portal.
However, if any of the blocks is not empty it will not do anything and then the player might be surprised and thinks that the item is broken. To stop them from thinking that you can simply add this line of code right above the return.


par2EntityPlayer.addChatMessage(“No room for a portal.”);


This will simply write the message No room for a portal. In the chat window inside of Minecraft.

You will need mostly the same code for the other direction. However, it is slightly different. It looks like this.


for(int y = 1; y < 5; y++)

                           {
                                  for (int x = -1; x < 2; x++)
                                  {
                                        if(par3World.getBlockId(par4 + x, par5 + y, par6) != 0)
                                        {
                                               par2EntityPlayer.addChatMessage(“No room for a portal.”);
                                               returnfalse;
                                        }
                                  }
                           }

The only thing changed here is the name z into x and the variable counted up with par4 and not par6.
The whole 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.util.MathHelper;
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)
             {
                    int direction = MathHelper.floor_double((double)(par2EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
                   
                    if(direction == 1 || direction == 3)
                    {
                           for(int y = 1; y < 5; y++)
                           {
                                  for (int z = -1; z < 2; z++)
                                  {
                                        if(par3World.getBlockId(par4, par5 + y, par6 + z) != 0)
                                        {
                                               par2EntityPlayer.addChatMessage(“No room for a portal.”);
                                               returnfalse;
                                        }
                                  }
                           }
                          
                           par3World.setBlock(par4, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 + 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 1, par6 – 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 – 1, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 – 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 + 2, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 + 2, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4, par5 + 5, par6 + 1, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 2, par6 + 1, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6 + 1, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6 + 1, Tutorial.portal.blockID);
                    }
                    else
                    {
                           for(int y = 1; y < 5; y++)
                           {
                                  for (int x = -1; x < 2; x++)
                                  {
                                        if(par3World.getBlockId(par4 + x, par5 + y, par6) != 0)
                                        {
                                               par2EntityPlayer.addChatMessage(“No room for a portal.”);
                                               returnfalse;
                                        }
                                  }
                           }
                          
                           par3World.setBlock(par4, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 1, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 1, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 1, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4 – 1, par5 + 2, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 3, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 4, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 – 1, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4 + 2, par5 + 2, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 3, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 4, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 2, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 5, par6, Block.sandStone.blockID);
                           par3World.setBlock(par4 + 1, par5 + 5, par6, Block.sandStone.blockID);
                          
                           par3World.setBlock(par4, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4, par5 + 4, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 2, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 3, par6, Tutorial.portal.blockID);
                           par3World.setBlock(par4 + 1, par5 + 4, par6, Tutorial.portal.blockID);
                    }
                   
                    returntrue;
             }
             else
                    returnfalse;
    }
}

So know when you use this item it will place a portal in the vanilla shape in the right direction, but only if there is space for it. If there ins’t any space it will not place it.

You can download the source code over here.

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