Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2015 – Curt Hill Your First Mod Creating an Item and Block in Eclipse using Forge Annotations! Inheritance! Proxies! Oh my!

Similar presentations


Presentation on theme: "Copyright © 2015 – Curt Hill Your First Mod Creating an Item and Block in Eclipse using Forge Annotations! Inheritance! Proxies! Oh my!"— Presentation transcript:

1 Copyright © 2015 – Curt Hill Your First Mod Creating an Item and Block in Eclipse using Forge Annotations! Inheritance! Proxies! Oh my!

2 Introduction By this time you should have the Forge environment created The Example mod may be deleted –We will create a new one This presentation will take us through the process You may do this as we go on but we do have to keep moving and not deal with mistakes Copyright © 2015 – Curt Hill

3 Creating the Mod The Mod will actually be a class within a new package Lets go through the steps: Copyright © 2015 – Curt Hill

4 Creating a new package Start with the src/…/java directory Right click and choose new package The package needs a unique name Java documentation generally suggests your domain name in reverse I will use: edu.vcsu.curt but you can choose anything You should see a new package in the package explorer –White indicates it is empty Copyright © 2015 – Curt Hill

5 Create a new class Right click in your new package and choose New Class The new class dialog will give you the new package The name of the class should capitalize words I will use CurtMod When I do this I should end up with the package and a class in a file Copyright © 2015 – Curt Hill

6 Constants The mod must be preceded by an annotation indicating it is a mod This annotation may have three parameters We will use all three For convenience we will use named constants for each –These will be defined in the class Copyright © 2015 – Curt Hill

7 Named constants The constants: MODID = "curtmod"; MODNAME = "Curt's Mod"; VERSION = "0.1"; Each of these is preceded by: public static final String These follow the CurtMod class Notice the ModID is lower case –Similar to a package Copyright © 2015 – Curt Hill

8 Mod Annotation The class construction is now prefixed by the mod annotation: @Mod(modid = CurtMod.MODID, version = CurtMod.VERSION, name = CurtMod.MODNAME) Usually just on one line –This has been warped by PowerPoint Copyright © 2015 – Curt Hill

9 The class so far Copyright © 2015 – Curt Hill package edu.vcsu.curt; @Mod(modid = CurtMod.MODID, version = CurtMod.VERSION, name = CurtMod.MODNAME) public class CurtMod { public static final String MODID = "curtmod"; public static final String MODNAME = "Curt's Mod"; public static final String VERSION = "0.1";

10 Instance A class must be instantiated in order to be usable –This is our next task We create the instance: @Instance public static CurtMod instance = new CurtMod(); The instance annotation is so the forge mod loader may find it Why must it be public and static? Copyright © 2015 – Curt Hill

11 Initializations We now create three initializations –preInit, init, postInit Each will be methods with one parameter –A FMLInitializationEvent item Each will be public void methods Each will be prefixed by the annotation: @EventHandler We will come back and put something in these later Copyright © 2015 – Curt Hill

12 Imports We have not added any imports as of yet so it is now time Each item that we added that had errors was likely from a missing import Eclipse has this cool feature: If you hit Ctrl-Shift O Eclipse will find the imports that you currently need and insert them into your code We will do this frequently but I will not mention it again Copyright © 2015 – Curt Hill

13 Proxy Now we need to create a proxy This will be a base class called CommonProxy and two descendents, ServerProxy and ClientProxy Each of these will also have the three event handlers: preInit, init, postInit CommonProxy will declare them The other two will override them We will thus create three new classes in the current package Copyright © 2015 – Curt Hill

14 CommonProxy Copyright © 2015 – Curt Hill package edu.vcsu.curt; import cpw.mods.fml.common.Mod.EventHandler; … public class CommonProxy { public void preInit(FMLInitializationEvent event) { } public void init(FMLInitializationEvent event) { } public void postInit( FMLInitializationEvent event) { }

15 Client and Server The two derived classes will now have to use the extends keyword to indicate they are derivations They will also use the override annotation for the three init routines Copyright © 2015 – Curt Hill

16 ClientProxy Copyright © 2015 – Curt Hill package edu.vcsu.curt; import cpw.mods.fml.common.event.FMLInitializationEvent; public class ClientProxy extends CommonProxy { @Override public void preInit(FMLInitializationEvent event) { super.preInit(event); } @Override public void init(FMLInitializationEvent event) { super.init(event); } …

17 Commentary The extends shows that this is a derivation @Override indicates that this should have the same signature as a method in the ancestral class The super is how we call the ancestral method rather than the local method –Leaving super out gives infinite recursion There were three methods but the last one was left out for space Copyright © 2015 – Curt Hill

18 Proxy Again As of yet we have declared three classes but there is no instantiation The instance will be in the original mod class: CurtMod We now return to it Here is where we need the SidedProxy annotation Copyright © 2015 – Curt Hill

19 SidedProxy The annotation will look like this: @SidedProxy( clientSide="edu.vcsu.curt.ClientProxy", serverSide="edu.vcsu.curt.ServerProxy") public static CommonProxy proxy; This is a message to the forge mod loader: –If you are client do the ClientProxy otherwise the ServerProxy This is a declaration, not done the new yet –However, we do not need it until we have a block Copyright © 2015 – Curt Hill

20 Blocks and Items This presentation will create a block and an item There are two basic approaches that could be used Inheritance – harder but with more capabilities –Do a derivation of Block or Item –Apply new methods and set properties A new instance - easier –Create an instance of existing Block or Item and then set properties so to make it sufficiently different Copyright © 2015 – Curt Hill

21 Blocks and Items Item has a single public constructor –The default constructor –We can derive or do an instance The Block constructor is protected –Takes a parameter of Material –Many derivations have a protected constructor and some have a public constructor –A few with public: BlockOre, BlockGlass, BlockQuartz, BlockTNT amoung others –This makes derivation preferred Copyright © 2015 – Curt Hill

22 Block To organize things create a new package that is one level deeper edu.vcsu.curt.block Place in that a new class CurtModBlocks We will make this new class final and it will only contain static items –We will never need to instantiate nor will it occupy run-time space Copyright © 2015 – Curt Hill

23 Derivation In this package insert another class CurtBlock which will be a derivation of Block Create a constructor that takes a Material –Will be protected and call Block’s constructor with the parameter Then set properties that are desired See next screen Copyright © 2015 – Curt Hill

24 CurtBlock Copyright © 2015 – Curt Hill package edu.vcsu.curt.block; import net.minecraft.block.Block; … public class CurtBlock extends Block { public CurtBlock(Material material){ super(material); setBlockName("Curt Block"); setBlockTextureName( CurtMod.MODID + ":curtblock"); setCreativeTab(CreativeTabs.tabBlock); }

25 Commentary We do not create a block here Rather it will be created in the CurtModBlocks object This will be seen later Copyright © 2015 – Curt Hill

26 Block Method Calls setBlockName(String) –The block name when showing as a hint setBlockTextureName(String) –The covering for this kind of block setCreativeTab(tab) –Which tab? –These include tabBlock, tabCombat, tabMaterials, tabMisc, tabTools among others Many others are possible Copyright © 2015 – Curt Hill

27 Textures Any block or item may be textured The texture is a 32 by 32 (or 16 by 16) pixel picture or type.PNG May be created by any suitable program It must be in a particular location in the Eclipse project Copyright © 2015 – Curt Hill

28 Textures Location The Eclipse project should contain a directory named: src/main/java –This should contain the packages We now need to make a new directory: src/main/resources if it does not already exist Insert into that two packages: assets.curtmod.textures.blocks assets.curtmod.textures.items –The curtmod should be whatever your modid is These contain the textures Copyright © 2015 – Curt Hill

29 Finally on textures Copy a texture for the block into the block package Later when we create an item the texture for that will be placed there A garish pink and black check is the pattern if it cannot find the.PNG file Copyright © 2015 – Curt Hill

30 Item with Instance To organize things create a new package that is one level deeper edu.vcsu.curt.item Place in that a new class CurtModItems We will make this new class final and it will only contain static items –We will never need to instantiate nor will it occupy run-time space It will contain a static method Copyright © 2015 – Curt Hill

31 Now What? We have created the block but there is no instantiation nor a registration We now return to CurtModBlocks In the init method we insert a couple of things –A new CurtBlock declaration –A call to GameRegistry Here it is: Copyright © 2015 – Curt Hill

32 CurtModBlocks Copyright © 2015 – Curt Hill package edu.vcsu.curt.block; import net.minecraft.block.Block; … public final class CurtModBlocks { Block curtBlock; public static final void init(){ Block curtBlock = new CurtBlock(Material.clay); GameRegistry.registerBlock( curtBlock, "curtBlock"); }

33 CommonProxy CurtModBlocks is not handled at this point Call the init from CommonProxy Copyright © 2015 – Curt Hill

34 CommonProxy Copyright © 2015 – Curt Hill package edu.vcsu.curt; import cpw.mods.fml.common.Mod.EventHandler; … public class CommonProxy { public void preInit(FMLInitializationEvent event) { CurtModBlocks.init(); } public void init(FMLInitializationEvent event) { } public void postInit( FMLInitializationEvent event) { }

35 Items The block is now ready We turn our attention to an item The process is similar in some ways However, we will not do a derivation of Item, but instead just create an item and modify the properties We could do this with blocks if the new block were only slightly different than an existing block Copyright © 2015 – Curt Hill

36 CurtModItems Copyright © 2015 – Curt Hill package edu.vcsu.curt0.item; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; … import edu.vcsu.curt.CurtMod; public final class CurtModItems { public static final void init(){ Item curtItem = new Item(); curtItem.setUnlocalizedName("curtItem"). setCreativeTab(CreativeTabs.tabMisc); curtItem. setTextureName(CurtMod.MODID+":curtItem"); GameRegistry.registerItem( curtItem, "curtItem"); }

37 Commentary We create the Item with new We set various properties with method calls –We look at those in the next screen There is no problem that it is a local member –The GameRegistry call will give it to the game – the garbage collector will not get it as long as there is a valid reference Copyright © 2015 – Curt Hill

38 Item Method calls setUnlocalizedName(String) –A name seen on the creative tab setCreativeTab(tab) –Which tab? –These include tabBlock, tabCombat, tabMisc, tabTools among others setTextureName(String) –Gives the name of the texture to cover this with Copyright © 2015 – Curt Hill

39 Package Explorer Copyright © 2015 – Curt Hill

40 Classes Copyright © 2015 – Curt Hill CurtModCommonProxie ServerProxie ClientProxie edu.vcsu.curt edu.vcsu.curt.item CurtModItems edu.vcsu.curt.block CurtModBlocks CurtBlock Block Packages

41 Conclusion If all went well we now have one modded block and one modded item What we need next is some behavior –What should these do that normal blocks or items do not do? This will wait for another presentation Copyright © 2015 – Curt Hill

42 Assignment Now you are ready to try this yourself Create a new block and item Copyright © 2015 – Curt Hill


Download ppt "Copyright © 2015 – Curt Hill Your First Mod Creating an Item and Block in Eclipse using Forge Annotations! Inheritance! Proxies! Oh my!"

Similar presentations


Ads by Google