Since everyone seems to be making tutorials encouraging bad coding habits, I decided to make a tutorial on how to make a client that is made according to common java standards and hopefully promote those habits in new coders. Hopefully this will help make it so we won't have anymore of the type of people that post code asking for help that has no indents whatsoever and has variable names like j and k.
The Main Classes
The first thing you want to do when making any client is make the main client class; the class that manages and configures all of the hacks. This class should NOT re-declare the static variable theMinecraft, and instead, it should either directly reference it (Minecraft.getMinecraft()) or make a method that returns a direct reference to it. Duplicating static variables just causes more memory to be used, with no real advantage to make it worthwhile. The class is normally mostly static, because it basically is just a wrapper for all of your hacks and the other features of your client (Keybinds, and GUI mainly.) My preferred way of making the hacks work is using an ArrayList of my abstract hacks class. My Hack class normally looks something like this:
Code:
import java.util.*;
public abstract class Hack {
public boolean isOn;
public String description;
public String name;
public ArrayList<Integer> keyBinds=new ArrayList<Integer>();
public void toggle(){
isOn=!isOn;
if(isOn){
onEnable();
}else{
onDisable();
}
}
public void addKeybind(int key){
keyBinds.add(key);
}
public abstract void onEnable();
public abstract void onDisable();
public abstract void onUpdate(Minecraft mc);
}
The toggle method allows code to not be repeated in every class, while at the same time allowing hack-specific enable/disable code. The name and description variables are namely for either an ArrayList GUI, or a help message. The onUpdate method has the Minecraft variable passed into it as to avoid redeclaring the static variable Minecraft.theMinecraft;
Now that we got our abstract Hack class done, and we have made our hacks, we should now finish that main class I was talking about in the beginning. Here is a pretty good template for a main class:
Code:
import java.util.*;
public class ClientNameHere {
private static ArrayList<Hack> hacks=new ArrayList<Hack>();
/* Settings */
private static int flySpeed;
private static int whateverOtherSettingsYouWant;
static{
// Load all of your hacks here.
hacks.add(new HackYourHack());
}
public static void toggle(String hackName){
for(Hack hack:hacks){
if(hack.name.equalsIgnoreCase(hackName)){
hack.toggle();
break;
}
}
}
public static void processCommand(String command){
//Do your method processing.
}
public static void addKeybind(String hackName, int keyCode){
for(Hack hack:hacks){
if(hack.name.equalsIgnoreCase(hackName)){
hack.addKeybind(keyCode);
break;
}
}
}
private static Minecraft getMinecraft(){return Minecraft.getMinecraft();}
public static void onUpdate(){
for(Hack hack:hacks){
if(hack.isOn){
hack.onUpdate(getMinecraft());
}
}
GuiClass.render();
}
}
This class, as you can see, operates on an ArrayList of the hacks, like I said before. I find this to work well, because in order to toggle something, you don't have to know what your toggling at compile-time, which can simplify a lot of methods, for example your process-command method. I initialize the hacks into the arraylist in a static block, so it is guaranteed to be loaded at runtime. I'm starting to get tired, so I think I am going to split this into 3-parts and get the second part done later.
Post your opinion on this thread please (Constructive criticism allowed.)