package tutorial;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import
net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import
net.minecraft.entity.EnumCreatureType;
import
net.minecraft.world.biome.BiomeGenBase;
import
net.minecraftforge.common.DimensionManager;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import
cpw.mods.fml.common.event.FMLInitializationEvent;
import
cpw.mods.fml.common.network.NetworkMod;
import
cpw.mods.fml.common.registry.EntityRegistry;
import
cpw.mods.fml.common.registry.GameRegistry;
import
cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = "Tutorial_Tutorialmod", name = "Tutorial", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired
= false)
public class Tutorial
{
@SidedProxy(clientSide = "tutorial.ClientProxyTutorial", serverSide = "tutorial.CommonProxyTutorial")
public static CommonProxyTutorial proxy;
static int startEntityId = 300;
public static int dimension = 20;
public static Block portal;
@Init
public void load(FMLInitializationEvent
event)
{
proxy.registerRenderThings();
portal = new
BlockPortalTutorial(251, 1).setBlockName("portal");
GameRegistry.registerBlock(portal, "Wuppy29_Portal");
LanguageRegistry.addName(portal, "Custom Portal");
EntityRegistry.registerModEntity(EntityTutorial.class, "Tutorial", 1, this, 80, 3, true);
EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4,
EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);
LanguageRegistry.instance().addStringLocalization("entity.Tutorial_Tutorialmod.Tutorial.name", "Tutorial");
registerEntityEgg(EntityTutorial.class, 0x7A65CF,
0x4DF200);
DimensionManager.registerProviderType(dimension,
WorldProviderTutorial.class, false);
DimensionManager.registerDimension(dimension, dimension);
}
public static int getUniqueEntityId()
{
do
{
startEntityId++;
}
while(EntityList.getStringFromID(startEntityId) != null);
return startEntityId;
}
public static void registerEntityEgg(Class <? extends Entity>
entity, int primaryColor, int secondaryColor)
{
int id = getUniqueEntityId();
EntityList.IDtoClassMapping.put(id, entity);
EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
}
}
You should start by adding the basic Item code to the mod file. These lines.
public static Item portalPlacer;
portalPlacer = new
ItemPortalPlacer(5000).setIconIndex(0).setItemName("tutorial");
LanguageRegistry.addName(portalPlacer, "Portal Placer");
If you don't understand this code you should take a look at the basic Item tutorials.
The whole file should now look like this.
package tutorial;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import
net.minecraft.entity.EntityEggInfo;
import net.minecraft.entity.EntityList;
import
net.minecraft.entity.EnumCreatureType;
import net.minecraft.item.Item;
import
net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.DimensionManager;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import
cpw.mods.fml.common.event.FMLInitializationEvent;
import
cpw.mods.fml.common.network.NetworkMod;
import
cpw.mods.fml.common.registry.EntityRegistry;
import
cpw.mods.fml.common.registry.GameRegistry;
import
cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = "Tutorial_Tutorialmod", name = "Tutorial", version = "1.0")
@NetworkMod(clientSideRequired = true, serverSideRequired
= false)
public class Tutorial
{
@SidedProxy(clientSide = "tutorial.ClientProxyTutorial", serverSide = "tutorial.CommonProxyTutorial")
public static CommonProxyTutorial proxy;
static int startEntityId = 300;
public static int dimension = 20;
public static Block portal;
public static Item portalPlacer;
@Init
public void load(FMLInitializationEvent
event)
{
proxy.registerRenderThings();
portalPlacer = new ItemPortalPlacer(5000).setIconIndex(0).setItemName("tutorial");
LanguageRegistry.addName(portalPlacer, "Portal Placer");
portal = new
BlockPortalTutorial(251, 1).setBlockName("portal");
GameRegistry.registerBlock(portal, "Wuppy29_Portal");
LanguageRegistry.addName(portal, "Custom Portal");
EntityRegistry.registerModEntity(EntityTutorial.class, "Tutorial", 1, this, 80, 3, true);
EntityRegistry.addSpawn(EntityTutorial.class, 10, 2, 4,
EnumCreatureType.monster, BiomeGenBase.desert, BiomeGenBase.desertHills, BiomeGenBase.forest);
LanguageRegistry.instance().addStringLocalization("entity.Tutorial_Tutorialmod.Tutorial.name", "Tutorial");
registerEntityEgg(EntityTutorial.class, 0x7A65CF,
0x4DF200);
DimensionManager.registerProviderType(dimension,
WorldProviderTutorial.class, false);
DimensionManager.registerDimension(dimension, dimension);
}
public static int getUniqueEntityId()
{
do
{
startEntityId++;
}
while(EntityList.getStringFromID(startEntityId) != null);
return startEntityId;
}
public static void registerEntityEgg(Class <? extends Entity>
entity, int primaryColor, int secondaryColor)
{
int id = getUniqueEntityId();
EntityList.IDtoClassMapping.put(id, entity);
EntityList.entityEggs.put(id, new EntityEggInfo(id, primaryColor, secondaryColor));
}
}
Next you should simply hover your mouse over ItemPortalPlacer and click Create Class.
Now make sure the file looks like this one.
package tutorial;
import
net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class ItemPortalPlacer extends Item
{
public ItemPortalPlacer(int id)
{
super(id);
this.setCreativeTab(CreativeTabs.tabMaterials);
}
public String getTextureFile()
{
return "/tutorialitems.png";
}
}
This is simply the code for a basic item. If you don't understand it I suggest reading through the basic Item tutorials.
To make an item place something when you right click is really easy. You simply need one method. It looks like this.
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
{
return true;
}
This method will simply run when you right click with the item on a block.
You might be thinking why it has the line return true in there. This is because it is a boolean method. This means that it has to return true or false no matter what.
par1ItemStack is the item you are using. par2EntityPlayer is the player that is using it. par3World is the world you are using it in. par4, par5 and par6 are x, y and z.
The others are unknown to me. If you do know them I would apreciate it if you mailed it to me at wuppy29@gmail.com.
Now you will have to add a simple check in this method to make sure it only places the blocks on the server side, because it doesn't work on the client side. That code looks like this.
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
{
if(!par3World.isRemote)
{
return true;
}
else
return false;
}
If you want to place a block in the world you will have to use a line of code just like this one.
par3World.setBlock(par4, par5, par6, Block.sandStone.blockID);
This code will place a block of sandstone at the location where you clicked it.
You have to add that code inside of the if. So the method should look like this.
public boolean 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);
return true;
}
else
return false;
}
And the whole Item file should now look something like this.
package tutorial;
import net.minecraft.block.Block;
import
net.minecraft.creativetab.CreativeTabs;
import
net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class ItemPortalPlacer extends Item
{
public ItemPortalPlacer(int id)
{
super(id);
this.setCreativeTab(CreativeTabs.tabMisc);
}
public String getTextureFile()
{
return "/tutorialitems.png";
}
public boolean 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);
return true;
}
else
return false;
}
}
In the next tutorial I will show you how to place the full portal properly.
The source code will be available at the end of the last tutorial.
When you are done you should go back to the tutorials list here.
Found a bug for ya:
BeantwoordenVerwijderenWhen you right click, it'll replace the block you click on instead of adding one on to it.