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;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.DimensionManager;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = Tutorial.modid, name = "Tutorial Mod", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = "YourName_ModName";
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       public static int dimensionId = 8;
      
       EventManager eventmanager = new EventManager();
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
              tutorialBlock = new BlockTutorialBlock(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;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.DimensionManager;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = Tutorial.modid, name = "Tutorial Mod", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = "YourName_ModName";
      
       public static Block tutorialBlock;
       public static Block portalTutorialBlock;
      
       public static Item tutorialItem;
      
       public static int dimensionId = 8;
      
       EventManager eventmanager = new EventManager();
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
              tutorialBlock = new BlockTutorialBlock(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 = 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);
       }
}

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;

public class 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;

import net.minecraft.block.BlockPortal;
import net.minecraft.creativetab.CreativeTabs;

public class 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.

public void onEntityCollidedWithBlock(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;
                     }
                     else if (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;

import net.minecraft.block.BlockPortal;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.ModLoader;
import net.minecraft.world.World;

public class BlockPortalTutorial extends BlockPortal
{
       public BlockPortalTutorial(int id)
       {
              super(id);
              this.setCreativeTab(CreativeTabs.tabBlock);
       }
      
       public void onEntityCollidedWithBlock(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;
                     }
                     else if (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.


When you are done you can go back to the tutorial list over here.

1 opmerking:

  1. ROBLOX is powered by a growing player base of more than 300,000 creator players who generate an infinite variety of highly immersive experiences.

    These experiences range from 3D multiplayer games and competitions, to interactive adventures where players can take on new identities discovering what it's like to be a dinosaur, a miner working a mine or an astronaut on a space exploration.

    BeantwoordenVerwijderen