Wuppy’s Minecraft Forge Modding Tutorials for 1.6.2: Dimensions Part 3: Portal Block

In the last two tutorials I showed you how to register a dimension and how to create the code to generate it. However, there is no way to get in there just yet. That is what we are going to do in this tutorial. Create a portal block which will make you get into the dimension.
Some of this code will be done in the Tutorial file which looks like this.


package tutorial;
importnet.minecraft.block.Block;
importnet.minecraft.block.material.Material;
importnet.minecraft.item.Item;
importnet.minecraft.item.ItemStack;
importnet.minecraftforge.common.DimensionManager;
importcpw.mods.fml.common.Mod;
importcpw.mods.fml.common.Mod.EventHandler;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Tutorial Mod”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       publicstaticfinal String modid = “YourName_ModName”;
      
       publicstatic Block tutorialBlock;
      
       publicstatic Item tutorialItem;
      
       publicstaticintdimensionId = 8;
      
       EventManager eventmanager = new EventManager();
      
       @EventHandler
       publicvoidload(FMLInitializationEvent event)
       {
              tutorialBlock = newBlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.class, modid + (tutorialBlock.getUnlocalizedName().substring(5)));
        LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
        LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
             
              tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorialItem”);
             
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 0), “Tutorial Item”);
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 1), “Second Tutorial Item”);
             
              GameRegistry.registerWorldGenerator(eventmanager);
             
              TutorialCrafting.loadRecipes();
             
              DimensionManager.registerProviderType(Tutorial.dimensionId, WorldProviderTutorial.class, false);
              DimensionManager.registerDimension(Tutorial.dimensionId, Tutorial.dimensionId);
       }
}

For a portal block you will have to start with the basic block registry. The code for that should look something like this.


public static Block portalTutorialBlock;
portalTutorialBlock = new BlockPortalTutorial(501).setUnlocalizedName(“portalTutorialBlock”);
GameRegistry.registerBlock(portalTutorialBlockmodid + (portalTutorialBlock.getUnlocalizedName().substring(5)));
LanguageRegistry.addName(portalTutorialBlock“Portal Tutorial Block”);


You should recognize all of this code from the basic block tutorial.
The whole file should now look like this.


package tutorial;
importnet.minecraft.block.Block;
importnet.minecraft.block.material.Material;
importnet.minecraft.item.Item;
importnet.minecraft.item.ItemStack;
importnet.minecraftforge.common.DimensionManager;
importcpw.mods.fml.common.Mod;
importcpw.mods.fml.common.Mod.EventHandler;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Tutorial Mod”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       publicstaticfinal String modid = “YourName_ModName”;
      
       publicstatic Block tutorialBlock;
       publicstatic Block portalTutorialBlock;
      
       publicstatic Item tutorialItem;
      
       publicstaticintdimensionId = 8;
      
       EventManager eventmanager = new EventManager();
      
       @EventHandler
       publicvoidload(FMLInitializationEvent event)
       {
              tutorialBlock = newBlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
              portalTutorialBlock = new BlockPortalTutorial(501).setUnlocalizedName(“portalTutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.class, modid + (tutorialBlock.getUnlocalizedName().substring(5)));
              GameRegistry.registerBlock(portalTutorialBlock, modid + (portalTutorialBlock.getUnlocalizedName().substring(5)));
             
        LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
        LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
       
        LanguageRegistry.addName(portalTutorialBlock, “Portal Tutorial Block”);
             
              tutorialItem = newItemTutorial(5000).setUnlocalizedName(“tutorialItem”);
             
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 0), “Tutorial Item”);
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 1), “Second Tutorial Item”);
             
              GameRegistry.registerWorldGenerator(eventmanager);
             
              TutorialCrafting.loadRecipes();
             
              DimensionManager.registerProviderType(Tutorial.dimensionId, WorldProviderTutorial.class, false);
              DimensionManager.registerDimension(Tutorial.dimensionId, Tutorial.dimensionId);
       }
}

The next thing we have to do is create the BlockPortalTutorial file. Hover your mouse over the BlockPortalTutorial and click create class which will create a class for you that should look like this.


package tutorial;
publicclass BlockPortalTutorial {
}

The next thing you will have to do is make it extend BlockPortal. When you do this you will first get an error under that which you can fix by importing. After that you will get an error under the class name. To fix that you will have to add the unimplemented methods.
After cleaning up the code a bit and adding it to the blocks creative tab it should look like this.


package tutorial;
importnet.minecraft.block.BlockPortal;
importnet.minecraft.creativetab.CreativeTabs;
publicclass BlockPortalTutorial extends BlockPortal
{
       public BlockPortalTutorial(int id)
       {
              super(id);

              this.setCreativeTab(CreativeTabs.tabBlock);
       }
}

Because of the way Java works, the BlockPortalTutorial file now has all the methods exactly the same as BlockPortal. This means that it currently also works exactly the same which is good. However, there are some changes we will have to make.
In this tutorial I will change only the things necessary for a working portal, but there will be customization of this block as well including other ways to get this portal (then getting it from the creative menu).
For the most simple version of a custom portal block there is only 1 method we will have to change. That method is the onEntityCollidedWithBlock method which handles the teleportation of the player.


publicvoidonEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
       {
              if ((par5Entity.ridingEntity == null) && (par5Entity.riddenByEntity == null) && ((par5Entity instanceof EntityPlayerMP)))
              {
                     EntityPlayerMP player = (EntityPlayerMP) par5Entity;
                     ModLoader.getMinecraftServerInstance();
                     MinecraftServer mServer = MinecraftServer.getServer();
                     if (player.timeUntilPortal > 0)
                     {
                           player.timeUntilPortal = 10;
                     }
                     elseif (player.dimension != Tutorial.dimensionId)
                     {
                           player.timeUntilPortal = 10;
                           player.mcServer.getConfigurationManager().transferPlayerToDimension(player, Tutorial.dimensionId, new TeleporterTutorial(mServer.worldServerForDimension(Tutorial.dimensionId)));
                     }
                     else
                     {
                           player.timeUntilPortal = 10;
                           player.mcServer.getConfigurationManager().transferPlayerToDimension(player, 0, new TeleporterTutorial(mServer.worldServerForDimension(1)));
                     }
              }
       }

This code looks really complicated, but it is actually quite simple. First it checks if the entity colliding with the portal is a player. Anything that isn’t a player won’t be allowed to move through the portal.
It will then check for the dimension and teleport it accordingly. If the entity is in the custom dimension it will put him in the overworld and if it’s in anything but the custom dimension it will be moved there. You will also notice a timeUntilPortal variable used quite frequently in the code. This is a variable in EntityPlayer which makes sure that the player doesn’t constantly teleport back and forth.
You will now have an error under the TeleporterTutorial file. This file will handle the actual teleportation for the player. I will show how to make this file in the next tutorial.
The whole block file should now look like this.


package tutorial;
importnet.minecraft.block.BlockPortal;
importnet.minecraft.creativetab.CreativeTabs;
importnet.minecraft.entity.Entity;
importnet.minecraft.entity.player.EntityPlayerMP;
importnet.minecraft.server.MinecraftServer;
importnet.minecraft.src.ModLoader;
importnet.minecraft.world.World;
publicclass BlockPortalTutorial extends BlockPortal
{
       public BlockPortalTutorial(int id)
       {
              super(id);
              this.setCreativeTab(CreativeTabs.tabBlock);
       }
      
       publicvoidonEntityCollidedWithBlock(World par1World, int par2, int par3, int par4, Entity par5Entity)
       {
              if ((par5Entity.ridingEntity == null) && (par5Entity.riddenByEntity == null) && ((par5Entity instanceof EntityPlayerMP)))
              {
                     EntityPlayerMP player = (EntityPlayerMP) par5Entity;
                     ModLoader.getMinecraftServerInstance();
                     MinecraftServer mServer = MinecraftServer.getServer();
                     if (player.timeUntilPortal > 0)
                     {
                           player.timeUntilPortal = 10;
                     }
                     elseif (player.dimension != Tutorial.dimensionId)
                     {
                           player.timeUntilPortal = 10;
                           player.mcServer.getConfigurationManager().transferPlayerToDimension(player, Tutorial.dimensionId, new TeleporterTutorial(mServer.worldServerForDimension(Tutorial.dimensionId)));
                     }
                     else
                     {
                           player.timeUntilPortal = 10;
                           player.mcServer.getConfigurationManager().transferPlayerToDimension(player, 0, new TeleporterTutorial(mServer.worldServerForDimension(1)));
                     }
              }
       }
}

In the next tutorial I will show you how to create the Teleporter file and you will have your own very basic dimension working.

You can download the source code and the assets folder 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="">