Alright so I was looking for a good GUI tutorial a while ago, but I could not find one, nor could I figure it out.
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:
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
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 BaseMod
The 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.
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,
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
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
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:
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 GuiScreen
this 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:
Alright so here is a short tutorial on how to add buttons to the Example GUI we created above.
In the GUIExampleGUI.java:
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:
+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:
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:
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:
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:
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)
Just noticed this tutorial. Funny how you made a tutorial on guis and on dimensions: those were exactly the tutorials I was looking for.
Still having troubles with getting a complex gui to work: it's pretty much like a crafting table except you need more than one item per slot (kinda like RedPower's Alloy Furnaces) and it requires fuel. The time it takes to get the output should also vary depending on the inputs and the type of fuel. It also needs to be able to output more than one item for a specific input.
Challenge accepted.
im actually going to cover that in a later tutorial if you want to check it out if you haven't figured it out yet
There is no method in Block called activated. Only blockActivated.
Rollback Post to RevisionRollBack
“Computers are incredibly fast, accurate and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination."
There is no method in Block called activated. Only blockActivated.
yeah i see that thanks again, i was trying to write all these in a hurry and having no sleep so they are going to be messy but now i got some sleep so i can finish them without errors
sadly, not much i released a day changing tool for this guy that asked me, and a few other tweaks. but i am part of the team for creating the Otherworld mod, plus i am working on other things for people and trying to make a change in the up coming economy system to make it more like equivalent exchange but more balanced and fun. making things like credit cards and ATMs plus Kiosks to buy things and stuff like that its a work in progress but its coming along good
FontRenderer fontrenderer = mc.fontRenderer;
fontrenderer.drawStringWithShadow("Extrastyle: Extra small corner extension", 2, 2, 0xffffff);
Also these tutorials are pretty good (you made some mistakes like [minecarft] but that doesn't matter)
You should add some more to these, like how to use them with keyboard keys and such.
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:
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
+ let's take a look at the first part:
The 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:
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:
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
+
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,
+
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.
+
BlockGUIBlock.java
+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
+ 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:
in this part we have the standard package declaration, and an import
which is needed for the constructor.
+How about the next part:
this part is important for making a GUI, if you don't extend this it wont work
+
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
+
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
+
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.
+
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:
Alright so here is a short tutorial on how to add buttons to the Example GUI we created above.
In the GUIExampleGUI.java:
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 clear the screen of anything that is there. Now add the following to make a 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:
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:
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:
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:
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:
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:
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)
and you are a girl that knows how to make mods and tutorial please marry me
im actually going to cover that in a later tutorial if you want to check it out if you haven't figured it out yet
together they are powerful beyond imagination."
yeah i see that thanks again, i was trying to write all these in a hurry and having no sleep so they are going to be messy but now i got some sleep so i can finish them without errors
lol yay (What about eloraam?), cool glad i could help some people, and just wait there is way more to be done
lol we are awesome i know
sadly, not much i released a day changing tool for this guy that asked me, and a few other tweaks. but i am part of the team for creating the Otherworld mod, plus i am working on other things for people and trying to make a change in the up coming economy system to make it more like equivalent exchange but more balanced and fun. making things like credit cards and ATMs plus Kiosks to buy things and stuff like that its a work in progress but its coming along good
no i am 15 going to be 16 soon lol i realized that
like a chat message??
alright ill look at doing one, thats another of my favorite thins to code
Or do you mean a text box like where you put in text??
yes ill post something on that soon
i will look into doing that
Example:
Also these tutorials are pretty good (you made some mistakes like [minecarft] but that doesn't matter)
You should add some more to these, like how to use them with keyboard keys and such.