Modding 1.4.7: Custom Dimension Part 2: BlockPortal

In this tutorial there will be more about the portal block you will need. All the code in this tutorial will be done in the Block file. This is the file I will start with.


package tutorial;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class BlockPortalTutorial extends Block
{
public BlockPortalTutorial(int id, int texture)
{
super(id, texture, Material.portal);
this.setCreativeTab(CreativeTabs.tabBlock);
}

public String getTextureFile()
    {
            return “/tutorialblocks.png”;
    }
}


The first thing you will have to do is change Block into BlockPortal. Once you do that you get an error under the BlockPortal. Import it to fix that.
After that you will get an error under the super. Remove the Material.portal and it will be fixed.

Now the next method you will need in here is this.


public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
{

}


You have to add this method or it will randomly be spawning zombie pigmen, becuase that is what the extended file which is BlockPortal does.
The good part about making this file extend BlockPortal is that it does a lot of things the same as the normal portal without having to write any code at all. For example right now it is already in the size of the portal etc.
There are some methods that you will have to change though. The first one has to do with the creation of a portal.


public boolean tryToCreatePortal(World par1World, int par2, int par3, int par4)
    {
        byte var5 = 0;
        byte var6 = 0;

        if (par1World.getBlockId(par2 – 1, par3, par4) == Block.sandStone.blockID || par1World.getBlockId(par2 + 1, par3, par4) == Block.sandStone.blockID)
        {
            var5 = 1;
        }

        if (par1World.getBlockId(par2, par3, par4 – 1) == Block.sandStone.blockID || par1World.getBlockId(par2, par3, par4 + 1) == Block.sandStone.blockID)
        {
            var6 = 1;
        }

        if (var5 == var6)
        {
            return false;
        }
        else
        {
            if (par1World.getBlockId(par2 – var5, par3, par4 – var6) == 0)
            {
                par2 -= var5;
                par4 -= var6;
            }

            int var7;
            int var8;

            for (var7 = -1; var7 <= 2; ++var7)
            {
                for (var8 = -1; var8 <= 3; ++var8)
                {
                    boolean var9 = var7 == -1 || var7 == 2 || var8 == -1 || var8 == 3;

                    if (var7 != -1 && var7 != 2 || var8 != -1 && var8 != 3)
                    {
                        int var10 = par1World.getBlockId(par2 + var5 * var7, par3 + var8, par4 + var6 * var7);

                        if (var9)
                        {
                            if (var10 != Block.sandStone.blockID)
                            {
                                return false;
                            }
                        }
                        else if (var10 != 0 && var10 != Block.fire.blockID)
                        {
                            return false;
                        }
                    }
                }
            }

            par1World.editingBlocks = true;

            for (var7 = 0; var7 < 2; ++var7)
            {
                for (var8 = 0; var8 < 3; ++var8)
                {
                    par1World.setBlockWithNotify(par2 + var5 * var7, par3 + var8, par4 + var6 * var7, this.blockID);
                }
            }

            par1World.editingBlocks = false;
            return true;
        }
    }


This method simply checks for a valid location to place a portal.
If it does find a valid location it will create a portal in exactly the same shape as with vanilla. It will create the 3 by 2 area in the middle with your portal block. Around it will be sandstone in this case. If you want the portal to be made out of another block, for example stone, you have to change every sandStone into stone.

One thing you will see when you place the block right now is that it has exactly the same particles as the vanilla portal. To fix that you have to add this method.


public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
        
    }


If you do want those particles you don’t need this method.
There is one last method you will need right now. It looks like this.


public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
{
byte var6 = 0;
byte var7 = 1;

if (par1World.getBlockId(par2 – 1, par3, par4) == this.blockID || par1World.getBlockId(par2 + 1, par3, par4) == this.blockID)
{
var6 = 1;
var7 = 0;
}

int var8;

for (var8 = par3; par1World.getBlockId(par2, var8 – 1, par4) == this.blockID; –var8)
{
;
}

if (par1World.getBlockId(par2, var8 – 1, par4) != Block.sandStone.blockID)
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
else
{
int var9;

for (var9 = 1; var9 < 4 && par1World.getBlockId(par2, var8 + var9, par4) == this.blockID; ++var9)
{
;
}

if (var9 == 3 && par1World.getBlockId(par2, var8 + var9, par4) == Block.sandStone.blockID)
{
boolean var10 = par1World.getBlockId(par2 – 1, par3, par4) == this.blockID || par1World.getBlockId(par2 + 1, par3, par4) == this.blockID;
boolean var11 = par1World.getBlockId(par2, par3, par4 – 1) == this.blockID || par1World.getBlockId(par2, par3, par4 + 1) == this.blockID;

if (var10 && var11)
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
else
{
if ((par1World.getBlockId(par2 + var6, par3, par4 + var7) != Block.sandStone.blockID || par1World.getBlockId(par2 – var6, par3, par4 – var7) != this.blockID) && (par1World.getBlockId(par2 – var6, par3, par4 – var7) != Block.sandStone.blockID || par1World.getBlockId(par2 + var6, par3, par4 + var7) != this.blockID))
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
}
}
else
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
}
}


This method will make sure that the block gets destroyed if the portal around it is broken.
Right now it checks if the blocks around it are made of sandstone. If they are not the block will get destroyed. If you want to have your portal made out of another block make sure you change the block in here as well.

The whole file should now look something like this.


package tutorial;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockPortal;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.world.World;

public class BlockPortalTutorial extends BlockPortal
{
public BlockPortalTutorial(int id, int texture)
{
super(id, texture);
this.setCreativeTab(CreativeTabs.tabBlock);
}

public String getTextureFile()
    {
            return “/tutorialblocks.png”;
    }

public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
{

}

public boolean tryToCreatePortal(World par1World, int par2, int par3, int par4)
    {
        byte var5 = 0;
        byte var6 = 0;

        if (par1World.getBlockId(par2 – 1, par3, par4) == Block.sandStone.blockID || par1World.getBlockId(par2 + 1, par3, par4) == Block.sandStone.blockID)
        {
            var5 = 1;
        }

        if (par1World.getBlockId(par2, par3, par4 – 1) == Block.sandStone.blockID || par1World.getBlockId(par2, par3, par4 + 1) == Block.sandStone.blockID)
        {
            var6 = 1;
        }

        if (var5 == var6)
        {
            return false;
        }
        else
        {
            if (par1World.getBlockId(par2 – var5, par3, par4 – var6) == 0)
            {
                par2 -= var5;
                par4 -= var6;
            }

            int var7;
            int var8;

            for (var7 = -1; var7 <= 2; ++var7)
            {
                for (var8 = -1; var8 <= 3; ++var8)
                {
                    boolean var9 = var7 == -1 || var7 == 2 || var8 == -1 || var8 == 3;

                    if (var7 != -1 && var7 != 2 || var8 != -1 && var8 != 3)
                    {
                        int var10 = par1World.getBlockId(par2 + var5 * var7, par3 + var8, par4 + var6 * var7);

                        if (var9)
                        {
                            if (var10 != Block.sandStone.blockID)
                            {
                                return false;
                            }
                        }
                        else if (var10 != 0 && var10 != Block.fire.blockID)
                        {
                            return false;
                        }
                    }
                }
            }

            par1World.editingBlocks = true;

            for (var7 = 0; var7 < 2; ++var7)
            {
                for (var8 = 0; var8 < 3; ++var8)
                {
                    par1World.setBlockWithNotify(par2 + var5 * var7, par3 + var8, par4 + var6 * var7, this.blockID);
                }
            }

            par1World.editingBlocks = false;
            return true;
        }
    }

public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
        
    }

public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
{
byte var6 = 0;
byte var7 = 1;

if (par1World.getBlockId(par2 – 1, par3, par4) == this.blockID || par1World.getBlockId(par2 + 1, par3, par4) == this.blockID)
{
var6 = 1;
var7 = 0;
}

int var8;

for (var8 = par3; par1World.getBlockId(par2, var8 – 1, par4) == this.blockID; –var8)
{
;
}

if (par1World.getBlockId(par2, var8 – 1, par4) != Block.sandStone.blockID)
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
else
{
int var9;

for (var9 = 1; var9 < 4 && par1World.getBlockId(par2, var8 + var9, par4) == this.blockID; ++var9)
{
;
}

if (var9 == 3 && par1World.getBlockId(par2, var8 + var9, par4) == Block.sandStone.blockID)
{
boolean var10 = par1World.getBlockId(par2 – 1, par3, par4) == this.blockID || par1World.getBlockId(par2 + 1, par3, par4) == this.blockID;
boolean var11 = par1World.getBlockId(par2, par3, par4 – 1) == this.blockID || par1World.getBlockId(par2, par3, par4 + 1) == this.blockID;

if (var10 && var11)
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
else
{
if ((par1World.getBlockId(par2 + var6, par3, par4 + var7) != Block.sandStone.blockID || par1World.getBlockId(par2 – var6, par3, par4 – var7) != this.blockID) && (par1World.getBlockId(par2 – var6, par3, par4 – var7) != Block.sandStone.blockID || par1World.getBlockId(par2 + var6, par3, par4 + var7) != this.blockID))
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
}
}
else
{
par1World.setBlockWithNotify(par2, par3, par4, 0);
}
}
}
}


In the next tutorial I will show you how to register your dimension. After that I will get back to this file to add a really important method.
The source code will be available at the end of the last dimension 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="">