Modding 1.4.7: Custom Dimension Part 4: Teleportation

In this tutorial I will show you how to make the player actually teleport to your dimension when he is inside of the portal block. To do this you will have to add code to your Block file. This is the file I will start with.


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


All of this code is done in one single method, but it is very important that you get this code just right. If you make even the smallest mistake in this part of the code there can be some very strange errors. The method you will need looks like this.


public void onEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
{

}


This method will run once any Entity walks into this portal.
The first thing you have to add in here are two check to keep the portal block from crashing. The first one is to make sure that there isn’t a riding or ridden Entity trying to get through the portal. Two examples for that are the spider jockey and players on a pig. The next check is to make sure that only players can move through the portal.
This is the code you have to add in the onEntityCollideWithBlock method.


if (par5Entity.ridingEntity == null && par5Entity.riddenByEntity == null)
{
if (par5Entity instanceof EntityPlayerMP)
{

}
}


Again. The first line checks for any riders and the second line makes sure that the Entity walking through is a player. Specifically the player on the server side.
Right now when you walk through the portal nothing will happen. To make something happen you will have to add this code inside of the two if statements you have just made.


EntityPlayerMP thePlayer = (EntityPlayerMP) par5Entity;
if (par5Entity.dimension != Tutorial.dimension)
{
thePlayer.mcServer.getConfigurationManager().transferPlayerToDimension(thePlayer, Tutorial.dimension, new TeleporterTutorial(thePlayer.mcServer.worldServerForDimension(Tutorial.dimension)));
}
else
{
thePlayer.mcServer.getConfigurationManager().transferPlayerToDimension(thePlayer, 0, new TeleporterTutorial(thePlayer.mcServer.worldServerForDimension(0)));
}


This looks pretty complicated, but it’s pretty simple.
The first line of code makes an EntityPlayerMP variable with the name thePlayer. It uses the par5Entity from the method and the if right above to make sure that the thePlayer variable is always the player on the server side.
The if statement checks if the player is inside of the dimension. If it isn’t it will run the code inside of the brackets.
This code will transfer the player to the dimension by using the thePlayer variable, your dimension id and lastly a custom Teleporter file which you will make in a later tutorial.
If the player is inside of the dimension it will do mostly the same. It will transfor the player to the overworld. Once again by using the thePlayer variable, the overworld dimension id and the Teleporter which we will make in a later tutorial.

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.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
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 void onEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
{
if (par5Entity.ridingEntity == null && par5Entity.riddenByEntity == null)
{
if (par5Entity instanceof EntityPlayerMP)
{
EntityPlayerMP thePlayer = (EntityPlayerMP) par5Entity;
if (par5Entity.dimension != Tutorial.dimension)
{
thePlayer.mcServer.getConfigurationManager().transferPlayerToDimension(thePlayer, Tutorial.dimension, new TeleporterTutorial(thePlayer.mcServer.worldServerForDimension(Tutorial.dimension)));
}
else
{
thePlayer.mcServer.getConfigurationManager().transferPlayerToDimension(thePlayer, 0, new TeleporterTutorial(thePlayer.mcServer.worldServerForDimension(0)));
}
}
}
}

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 tutorials we will be creating the WorldProvider and Teleporter files.

The source code will be avaliable 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="">