so now that I know how to do it, I figure I would make a tutorial on how to do it.
Please note that these tutorials solely rely on Minecraft 1.2.5, ModLoader 1.2.5, and Minecraft Forge, or you could use FML (Which is way better).
Alright so let's begin:
Example GUI:
Spoiler:
The example GUI is really easy, it just displays text though, the buttons and containers and all the other things that make GUI good will come later.
mod_BasicGUI.java
BlockGUIBlock.java
GUIExampleGUI.java
The example GUI is really easy, it just displays text though, the buttons and containers and all the other things that make GUI good will come later.
mod_BasicGUI.java
Spoiler:
+ let's take a look at the first part:
The second-third lines are the imports, all of these are important to be imported, except with the last one that is optional, because it is not needed but it can be handy.
The final part is the class declaration nothing is different than any other mod.
+ Now the next part:
NOTE: I chose loader as the ModLoader reference, because that is what i am used to, but you can make it anything.
+ How about the next:
+
+
The first method is allowing ModLoader to allow in game methods such as manipulating ticks in the game and using ticks as triggers for something.
The next part is registering the block with ModLoader.
And the last is adding a name to the block with ModLoader.
+
package net.minecraft.src;
import net.minecraft.src.forge;
import net.minecraft.client.Minecraft;
import java.util.*;
public class mod_BasicGUI extends BaseMod{
private Minecarft minecraft;
private mod_BasicGUI instance;
private MinecraftForge forge;
private ModLoader loader;
public static Block GUIExampleBlock = new BlockGUIBlock(255, 32).setBlockName("GUI Block");
public String getVersion(){
return "1.2.5";
}
public void load(){
loader.setInGameHook(this, true, true);
loader.setInGUIHook(this, true, true);
loader.registerBlock(GUIExampleBlock);
loader.addName(GUIExampleBlock, "GUI Block");
}
public void openExampleGUI(EntityPlayer player){
loader.openGUI(player, new GUIExampleGUI(player, minecraft, world));
}
}
+ let's take a look at the first part:
package net.minecraft.src; import net.minecraft.src.forge; import net.minecraft.client.Minecraft; import java.util.*; public class mod_BasicGUI extends BaseModThe first line is just the package declaration, do not change this unless you know what you are doing, and by doing I mean manipulating java packages.
The second-third lines are the imports, all of these are important to be imported, except with the last one that is optional, because it is not needed but it can be handy.
The final part is the class declaration nothing is different than any other mod.
+ Now the next part:
private Minecraft minecraft; private mod_BasicGUI instance; private MinecraftForge forge; private ModLoader loader;These are the pointers, if you are used to C++ they basicaly give the file a reference name.
NOTE: I chose loader as the ModLoader reference, because that is what i am used to, but you can make it anything.
+ How about the next:
public static Block GUIExampleBlock = new BlockGUIBlock(255, 32).setBlockName("GUI Block");
This is just defining the block that we are going to add a GUI to, but you can make this with a item to just change it to an item and continue the tutorial+
public String getVersion(){
return "1.2.5";
}
This part is just getting the version of your mod that you are making, it is important that you have this because it is required by ModLoader and FML,+
public void load(){
loader.setInGameHook(this, true, true);
loader.setInGUIHook(this, true, true);
loader.registerBlock(GUIExampleBlock):
loader.addName(GUIExampleBlock, "GUI Block");
}
This part is also required by ModLoader and FML (just the method, not the contents).The first method is allowing ModLoader to allow in game methods such as manipulating ticks in the game and using ticks as triggers for something.
The next part is registering the block with ModLoader.
And the last is adding a name to the block with ModLoader.
+
public void openExampleGUI(EntityPlayer player){
loader.openGUI(player, new GUIExampleGUI(player, minecraft, world));
}
This is the only method actually required to make a GUI in this file, it takes 1 parameter which is EntityPlayer and it is referenced as player
The actual code loader.openGUI is calling a method from ModLoader and this method opens the specified GUI, it has 2 parameters,
The first one is EntityPlayer which we already referenced it to player so you put player,
The next is the GUI you are opening which the file we are going to create is GUIExampleGUI, this has 3 parameters, EntityPlayer, Minecraft, World,
we already have references for them so they are player, minecraft, world.
BlockGUIBlock.java
Spoiler:
+Basically here there is not much explaining, really because all you have to do, is make a new block in this case we created the block BlockGUIBlock and then make a new method called public boolean activated, which has five parameters World world, int x, int y, int z, EntityPlayer player the world is just the minecraft world, int x, y, z are the x, y, z coordinates, and EntityPlayer player is the player. then you just call from the mod_BasicGUI the open GUI method and put in player as its parameter. and simply return false. now when you start up the game you will click on the block and it will give you an error, which is good because we haven't coded the GUI which is coming up next
package net.minecraft.src;
public class BlockGUIBlock extends Block{
public BlockGUIBlock(int i, int j){
super(i, j, Material.rock);
}
public int idDropped(){
return mod_BasicGUI.GUIExampleBlock.blockID;
}
public int quantityDropped(){
return 1;
}
public boolean blockActivated(World world, int x, int y, int z, EntityPlayer player){
mod_BasicGUI.openGUI(player);
return false;
}
+Basically here there is not much explaining, really because all you have to do, is make a new block in this case we created the block BlockGUIBlock and then make a new method called public boolean activated, which has five parameters World world, int x, int y, int z, EntityPlayer player the world is just the minecraft world, int x, y, z are the x, y, z coordinates, and EntityPlayer player is the player. then you just call from the mod_BasicGUI the open GUI method and put in player as its parameter. and simply return false. now when you start up the game you will click on the block and it will give you an error, which is good because we haven't coded the GUI which is coming up next
GUIExampleGUI.java
Spoiler:
+ Alright so this code will draw a string of text around the middle of the screen, saying "We Made A GUI".
+alright so let's take a look at the first part of it:
+How about the next part:
+
+
+
+
package net.minecraft.src;
import net.minecraft.client.Minecraft;
public class GUIExampleGUI extends GuiScreen{
public GUIExampleGUI(World world, Minecraft minecraft, EntityPlayer player){
}
public void initGui(){}
public void actionPerformed(GuiButton button){}
public void drawScreen(int i, int j, float f){
drawDefaultBackground();
drawCenteredString(fontRenderer, "We Made A GUI", width / 2, height / 2 - 20, 0xffffff);
super.drawScreen(i, j, f);
}
}
+ Alright so this code will draw a string of text around the middle of the screen, saying "We Made A GUI".
+alright so let's take a look at the first part of it:
package net.minecraft.src; import net.minecraft.client.Minecraft;in this part we have the standard package declaration, and an import
import net.minecraft.client.Minecraft;which is needed for the constructor.
+How about the next part:
public class GUIExampleGUI extends GuiScreenthis part is important for making a GUI, if you don't extend this it wont work
+
public GUIExampleGUI(World world, Minecraft minecraft, EntityPlayer player){
}
this is the constructor of the GUI, it has nothing in it right now, but it takes 3 parameters the World instance, the Mincraft instance, and the EntityPlayer instance+
public void initGui(){}
this part is important for drawing buttons on the screen but since we don't have any buttons to be drawn yet it has nothing in it+
public void actionPerformed(GuiButton button){}
this is where all the buttons you draw on the screen get their functions from, its similar to the AWT style of buttons where you make a button then add the functions in later on, if you don't add any functions for your buttons to do they of course won't do anything.+
public void drawScreen(int i, int j, float f){
drawDefaultBackground();
drawCenteredString(fontRenderer, "We Made A GUI", width / 2, height / 2 - 20, 0xffffff);
super.drawScreen(i, j, f);
}
this is the most important part, besides the extension of GuiScreen, because without this, your GUI wont do anything. The function first takes 3 params 2 ints they can be named anything, i like to go with i + j, and a float i chose f. Next is the drawDefaultBackground(); function it basically takes everything off of the screen like your health bar, and food bar, and the hotbar, all that stuff, not the world though. Then you have the drawCenteredString(); method it takes 4 parameters the fontRenderer which helps in drawing string in the minecraft font, a string (which is what you want to draw, in our case we are going to draw "We Made A GUI") and the width which to make the string in the center of the screen you put width / 2, The height which again to get it in the middle of the screen you do the same thing height / 2 but we off centered this by 20 pixels so its not right in the middle of the screen but its around there. and finally the color which we went with white the color is written in hex so white is 0xffffff 6 f's if anyone cant count them,(i couldn't at first) and the last function is the super.drawScreen(i, j, f) this is needed to complete the GUI it just lets the game know there is a GUI to draw.Adding Buttons To Example GUI:
Spoiler:
Alright so here is a short tutorial on how to add buttons to the Example GUI we created above.
In the GUIExampleGUI.java:
Alright so here is a short tutorial on how to add buttons to the Example GUI we created above.
In the GUIExampleGUI.java:
Spoiler:
This is the only area where we need to add anything to have a button its really simple. first go to the initGui(){} method and add a short line of code:
+This will make a new button, on the screen when the GUI is called to be displayed.
alright so to make this button we have to call a method from controlList called add and inside of the function add we put new GuiButton() which will make a new button like the ones you see in the game, it has 6 parameters.
-The first parameter is the button id which will be used later.
-The next parameter is the location on the screen in a x-coordinate fashion
-The next is the location on the screen in a y-coordinate fashion.
-Next is the width, 100 is the standard width and should not have to be adjusted.
-Next is the height, 20 is also the standard and should not have to be adjusted.
-And finally the last is what it says. This needs to be in the form of a string, which in our case we chose "New Button"
This is the only area where we need to add anything to have a button its really simple. first go to the initGui(){} method and add a short line of code:
controlList.clear();this will clear the screen of anything that is there. Now add the following to make a button:
controlList.add(new GuiButton(1, width / 2 - 50, height / 2 - 40, 100, 20, "New Button"));
+This will make a new button, on the screen when the GUI is called to be displayed.
alright so to make this button we have to call a method from controlList called add and inside of the function add we put new GuiButton() which will make a new button like the ones you see in the game, it has 6 parameters.
-The first parameter is the button id which will be used later.
-The next parameter is the location on the screen in a x-coordinate fashion
-The next is the location on the screen in a y-coordinate fashion.
-Next is the width, 100 is the standard width and should not have to be adjusted.
-Next is the height, 20 is also the standard and should not have to be adjusted.
-And finally the last is what it says. This needs to be in the form of a string, which in our case we chose "New Button"
Adding A Function To The New Button:
Spoiler:
okay so in this tutorial we are going to add a small function to the button we created above.
+first go to the GUIExampleGUI.java file and make a new String:
+then go to the actionPerformed(GuiButton button) {} function and
add a new if else statement:
+Now for the last step. go to the drawScreen(){} method and replace the string in the drawCenteredString method to tutorialString so it should look like:
okay so in this tutorial we are going to add a small function to the button we created above.
+first go to the GUIExampleGUI.java file and make a new String:
public static String tutorialString = "Hello World";
+then go to the actionPerformed(GuiButton button) {} function and
add a new if else statement:
if(button.id == 1){
tutorialString = "GoodBye World";
}
This really isn't worth making for real but its good for showing you how to make buttons.+Now for the last step. go to the drawScreen(){} method and replace the string in the drawCenteredString method to tutorialString so it should look like:
drawCenteredString(fontRenderer, tutorialString, width / 2, height / 2 - 20, 0xffffff);this will whenever you press the button in the GUI it will make the text being displayed, change from Hello World to GoodBye World
Adding A Background:
Spoiler:
Alright here I'm going to show you how to add a background to you GUI.
first go to the GuiExampleGUI.java file:
and the following to the drawScreen(){} method:
"yourmod/textures/gui/examplegui.png"
in the same method add the following line:
Alright here I'm going to show you how to add a background to you GUI.
first go to the GuiExampleGUI.java file:
and the following to the drawScreen(){} method:
int picture = mc.renderEngine.getTexture(pathToFile);the param pathToFile is the path to the file you want to use, so for example:
"yourmod/textures/gui/examplegui.png"
in the same method add the following line:
mc.renderEngine.bindTexture(picture);adding this will make the picture you selected appear on the screen. The bindTexture() method has 1 param which is the integer that you created earlier (int picture)
Edited by Asyncronous, 31 July 2012 - 12:46 AM.











