1

Modding Tutorial~! {For beginners}

CaptainWalfrus's Avatar CaptainWalfrus11/28/12 7:06 pm
1 emeralds 382 3
11/29/2012 3:30 pm
ehdfawq3er's Avatar ehdfawq3er
Welcome.

With this thread, I am attempting to spread the art of modding further out than has previously been possible. I'm aiming at walking newcomers through the act of learning the Java needed for modding, to have them think for themselves, come up with their own solutions and develop more, and far better, mods than seen before. I will be explaining how the code works, how Minecraft reads it, and how it should be understood. I hope you will find these tutorials helpful,

Disclaimer

But, before you read these tutorials, please remember this:


You will find several grammar mistakes throughout the tutorial. This is due to the fact that I do not speak english as my first language (actually, it is caused by my hyper-intelligent dancing monkey messing with my brain).
You should not contact I, nor any other writer of tutorials, by PM with anything concerning or relating to these tutorials, unless they (the writer of the tutorial) allow you to.
I, nor any other writer of tutorials, are responsible for any damage done to your computer/system.
Have fun!

Furthermore, before posting error logs or questions, please read and follow these guidelines:
Use [/code] tags to show any code.
If any technical text (error log, code, etc.) is more than 5 lines long, please use [/spoiler] tags.


PART 1, SETTING UP MCP
Click to reveal
Setting up MCP

Welcome to this, our first tutorial in the series. I am going to walk you through the act of setting up the current MCP (Minecraft Coder Pack), the 2.9a version.

Downloading MCP
Navigate to the Minecraft Coder Pack's forum post. It is a good practice to use the search function to find what you're searching for.
Locate the "Downloads" link, and click it. This should direct you to the offical wiki of MCP
Locate the "Releases" section, and click the "mcp29a.zip" link. This will redirect you to a MediaFire link. From here, click "Click here to start download".
Create an empty folder on your desktop, and name it "MCP". Extract (Using either WinRAR or 7zip) the files from the downloaded .zip into this folder.


The PATH variable
Locate the "README-MCP" text file, and open it with your preferred text editor.
Install the Java SDK from the link given. Make sure you're downloading the SE JDK.
Add the path as described
Follow the official Java instructions for how to access your PATH variable.
Once you've accessed your PATH variable, it's time to edit it.
From the "README-MCP", copy the PATH variable example ("C:\Program Files\Java\jdk1.6.0_23\bin;C:\Program Files\Java\jre6\bin").
Make sure you can actually access both path's, by opening Windows Explorer (Or your systems file browser), and entering them into the main field.
For Windows, you would have to enter "C:\Program Files\Java\jdk1.6.0_23\bin" into the path field (The text-box at the top of the window), and, if successful, "C:\Program Files\Java\jre6\bin".
If one of them isn't successful (I.E, your system reports an error, stating that no file at that location exists), go one level down.
For example, instead of "C:\Program Files\Java\jdk1.6.0_23\bin", enter "C:\Program Files\Java\jdk1.6.0_23".
If not successful, go one level below. Once successful, locate a path similar to the one in the PATH variable (I.E "C:\Program Files\Java\jdk1.6.0_21\bin"), and copy that path into the PATH variable.
Congratulations! The hardest part of the setup is now over!


Decompiling
Locate your ".minecraft". If you're wishing to make mods, you should already know where this is located.
Make sure your minecraft.jar file is unmodified. Then copy the "resources" and "bin" folders, and place them in the "jars" folder in your "MCP" folder.
Go to minecraft.net and login.
Click the "Download" link. This will direct you to the download page.
Scroll to the very bottom of the page, and locate the "minecraft_server.jar" link. Notice; it is important that it is the minecraft_server.jar, and not minecraft_server.exe!
Place this newly downloaded .jar file inside your "jars" folder in your "MCP" folder.
Start the "decompile.bat" file.



Explanation (The boring stuff)

The PATH variable

The PATH variable is required for the Java SDK to function properly. The PATH variable defines the location of various Java files used by Windows. By setting this, you will be able to run and use the Java SDK, which is what MCP uses. Once this is set, it won't have to be set again, unless you update your Java SDK.


Decompiling

The bin and resources folders are what MCP uses to create the client files. These files are opened by the Java SDK, and then run through a custom built function, that renames functions (More on these later) to a more user-friendly variant. The minecraft_server.jar is required to create the server files, which a modder can use to mod the server.


PART 2, GET CODING!
Click to reveal
Block - The Lightdirt

So, the time has come for us to start with the actual modding!
During this tutorial, I am going to show you how you set up the basic variables of blocks (More on this later), as well as learn you a bit of Java. But lets get started already!

The Idea
So, we want to make our own block. Let's quickly write down a few of the main key-points of this block:
  • Emits light equal to torches.
  • New texture, representing dust-covered dirt.
  • A bit slippy ('cause it's frigging powder!)
  • Crafted by enclosing a dirt block in light-stone dust.
  • As hard to break as dirt
    As we've finished our list of key-points, we're ready to start coding.


    Locating references
    An important part of learning to mod Minecraft is looking for references. In this case, we can look through the existing code, and figure out how to define each of the defined properties we want our block to have.

    Let's quickly revisit our key-points list.
  • Emits light equal to torches.
  • New texture, representing dust-covered dirt.
  • A bit slippy ('cause it's frigging powder!)
  • Crafted by enclosing a dirt block in light-stone dust.
  • As hard to break as dirt
    Hm... Let's see.
    For our light level, we would need to look up how torches defines their light level. We might want to check lightstone too, as it is not given that place-able items (Such as torches) define their light level equally to how blocks do it.
    We want to check out the ice code too, to see how it sets it slipperiness.
    And lastly, we want to see through the dirt coding to see its hardness.


    The coding
    Well well, it looks like we have something of a job ahead of us! So let's get started. Open the MCP folder we created in the previous tutorial, and enter the "sources" folder. In programming, source is the raw code. When a compiler is run, this compiler runs through the code, and translates it into binary language. This binary language consists of 1's and 0's (Such as 01101111 01101000 01100001 01101001), which can be read directly by the computer. But enough on that. Once inside the "sources" folder, you should see two folders. One of them is called "minecraft_server", while the other one is called "minecraft". As the names suggest, one is the code for the server, while the other one is for the actual game. For now, we're going to work with the game, as some special rules applies to the server, which we wont bug our head with right now. So, open the "minecraft" folder, and go through the next two folders until you reach two additional folders. One of them should be called "client", while the other one should be "src". For now, we'll simply use the files in the "src" folder, as that's where all the source code is (src means source code, by the way).

    So, open up "src", and you'll be greeted by an overwhelming number of .java files. All of these .java files represents one block, entity or other function, which obviously runs up into a great number for such a game as Minecraft. Luckily, we have our list, so we can focus on a few files at a time.

    Let's start out with finding the most basic; the dirt part. Locate the "BlockDirt.java" file, and open it up with a text editor of your choice. I use Notepad++ myself, which features highlighting (Color-coding) for many languages, but you can choose yourself. Such software as Eclipse, which is a fully-featured editor made solely for use with Java, are great if you want to go more advanced, while the regular text editors, Notepad, Word etc. are harder to use for quick read-throughs. Anyway. Once you've opened BlockDirt.java, you should see something similar to the following:
    package net.minecraft.src;
    // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
    // Jad home page: http://www.kpdus.com/jad.html
    // Decompiler options: packimports(3) braces deadcode


    public class BlockDirt extends Block
    {

    protected BlockDirt(int i, int j)
    {
    super(i, j, Material.ground);
    }
    }
    Well, it's short and easy, but there's a few syntax's I'll have to explain. Let's take one line at a time:
    package net.minecraft.src;
    Arh yes, the package. This package can be used for a number of different things, such as permissions, but for now, just think of it as where the file is located. Since this is located in the "net/minecraft/src" folder, that is what is entered.// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
    // Jad home page: http://www.kpdus.com/jad.html
    // Decompiler options: packimports(3) braces deadcode
    What is interesting about this part is the slashes before each line. If you've opened the file with a text-editor which enables high-lighting, you'll be able to see that these lines are colored green. In Java, two slashes tells the computer "Oh, could you ignore everything from this line and until the next line?". These "ignore-lines" are known as comments. They are often used if many coders work together, as they will be able to tell eachother what the specific line does. It's a good practice to comment the code when you mod, even if noone else has to see it. If you return after a few months of coding something else, you'll be able to quickly see what each function (More on these later) does, instead of having to use hours tracking it down.public class BlockDirt extends BlockNow this is interesting. Actually, it's so interesting that I will take it one word at a time:
    public
    This is a widely used word in Java. What this does, is tell the computer who can access the specified class (More on these in just a moment), variables (More on these later) etc. By saying that it is "public", we tell the computer that anyone can run, or make a new one of this class (As said, more on these in a few lines!). Basically, I could ask the creeper to place a new dirt block, I could ask the time to place a new dirt block, I could ask anything to place a new dirt block. I'll be sure to explain more of this later, but this tutorial is getting quite long, so we won't have time for that for now.
    class
    Another important and widely used word in Java. Actually, you have to use this for every new file you make. A class is, explained shortly, a collection of code. For example, in this example, anything we add inside the two brackets of the class belongs to the BlockDirt. Whenever anything, the player, the WorldGenerator or something else wants to make a new block of dirt, they tell the computer to find this class and make a copy of it. This copy is known as an object. You can easily see, hundreds of thousands of different objects could quickly stack up in a Minecraft world. If I edit the BlockDirt class to emit light, every single dirt block will emit light, as they are all copies (In programming language; instances) of the BlockDirt class.
    BlockDirt
    This is the name of the class. As you may have noticed, the file we opened was "BlockDirt.java". We have to call the class BlockDirt too. When something asks for the computer to make a new BlockDirt instance (copy), it will not know where to find it if the file and the class isn't named equally. First, the computer looks for the file, in this case "BlockDirt.java", and then it looks for the class, in this case "BlockDirt" inside that file. Usually, only one class is made in a file, but more can be made. It is not adviced though.
    extends
    Well well, this small line is filled with interesting things, huh? This time, it's the extend word. As the name implyes, extending something means copying everything that class (Remember before? A class is a collection of code) can do. This means, if I make a new monster that extends the creeper, my new mob will be able to explode, it will have the same AI, it will have the same texture, it will be able to do everything the creeper can do.
    Block
    And finally, the name of the class being extended. In Minecraft, the block class is a class that is used for all blocks in one way or the other.

    That was all we managed to do for now! It's been a pretty long tutorial, and I hope you learned something! Keep an eye out for part 3!

  • Get ready for part 3! COMING SOON! -Collin
    Posted by CaptainWalfrus's Avatar
    CaptainWalfrus
    Level 20 : Expert Miner
    27

    Create an account or sign in to comment.

    3

    1
    11/29/2012 3:30 pm
    Level 61 : High Grandmaster Modder
    ehdfawq3er
    ehdfawq3er's Avatar
    Nice ones. You might wanna make your subtitles bold.
    1
    11/29/2012 3:20 pm
    Level 15 : Journeyman Engineer
    exilepilot
    exilepilot's Avatar
    I'm aiming at walking newcomers through the act of learning the Java


    Does this mean, 'newcomers' are required to know what variables are, or you're going to teach them what variables, classes, and other datatypes (including OOP) are?
    1
    11/29/2012 1:45 am
    Level 20 : Expert Miner
    CaptainWalfrus
    CaptainWalfrus's Avatar
    Bump?
    Planet Minecraft

    Website

    © 2010 - 2024
    www.planetminecraft.com

    Welcome