Category Archives: Follow Up

Minecraft Python Modding Files

As promised, here are the files so that you can do your own Minecraft Python Modding at home! You can download the files here:

http://fredxcoders.com/files/MC.zip

And here are the instructions in case you need a refresher:

Everything is setup to run out of the MC folder just like it was in the Dojo.

1 **********  Server  ************

Uses CanaryMod Minecraft server – http://www.canarymod.net/
The server is in MC\server\Canarymod
Currently setup for 1.7.10

Start server first using MC\minecraft_server.bat
press enter when prompted.
It takes a few seconds to startup so be patient.  When it is started there will be a > prompt.  At this point just minimize it.  There is no need to interact with it any more until you shut it down.

To shut it down Hold down CTRL and press the C key in the window with the server running.

NOTE: The server must be running to connect MineCraft.

2 **********  MineCraft ************

Start Minecraft using MC\minecraft_client.bat
Login with your minecraft account
you should see the MCPI profile selected.  If not make sure you use VERSION 1.7.10 of minecraft.
Click play to start the game and select a multi-player game.
Do a direct connect to server name localhost (this means your computer)  The server we just started will connect to your minecraft game.

3 ***********  Python  *************

MOST IMPORTANT: Use the portable python version in the MC\Python2.7.6.1 folder or these programs will not run properly

Start IDLE-Portable.exe in MC\Python2.7.6.1\
You must OPEN the files in IDLE FIRST, then run them from within IDLE
Click the FILE menu and select the OPEN opion.  Open hello.py from MC\MyCode\
Run the module from IDLE by clicking the RUN menu and then selecting the RUN MODULE option.

You should see the message Hello MineCraft World! in the chat within Minecraft

Once this is working you are free to hack away.

Setting up Minecraft Mod Development (Updated)

Below are the instructions for setting up minecraft using the 1.7.10 version of Forge on a Windows computer, now with a one click forge/eclipse setup process!

JDK

  1. Go here to get the Java Development Kit.
  2. Click the button to ” Accept License Agreement”.
  3. Find this link and download: jdk-7u65-windows-i586.exe (the version may be higher than u65 which is ok as long as it is jdk7 and not jdk8)
  4. Open the download and install it.

Alternative JDK here: https://github.com/alexkasko/openjdk-unofficial-builds#openjdk-unofficial-installers-for-windows-linux-and-mac-os-x

Eclipse/Forge

  1. Get the eclipse/forge installation file here.
  2. Extract this on to your desktop.
  3. Go into the “dev” folder. (Depending on how you extracted it there may be another “dev” folder. Go into that one if there is).
  4. Double click on the file “oneTimeSetup.bat” file. This will run your forge installation. You should see a window pop up which has a variety of messages. This process can take up to half an hour depending on your internet connection.
  5. When the first part is done you should see a message saying “BUILD SUCCESSFUL” and “Press any key to continue”. Hit enter and Eclipse will start up.
  6. When eclipse loads click the green “play” icon in the upper left which says “Run Client” when you hover over it. This will start up minecraft with our mod loaded on it.
  7. The next time you want to run Eclipse use the “startEclipse.bat” file in this folder. You won’t need to run “oneTimeSetup.bat” again.

That’s it! Hopefully the one step forge/eclipse installation will help remove issues people encountered in the past. Please leave a comment below if you encounter any issues. Once you have Minecraft loaded be sure to check out some of the previous Minecraft posts to get up to speed with our mod:

Minecraft Recipes | Player/World Manipulation | Ore Generation

Be sure to also check out the GitHub tutorial here to learn more about the social coding aspect of development.

Social Coding Lesson and Challenge

Here is a recap of how to get started with social coding by copying one of our FredXCoders repos:

  1. Go to github.com and create an account. Choose the free option.
  2. Go to the FredXCoders/WebProject here repository and click “Fork” in the upper right hand corner.
  3. Download the Git GUI (windows and mac). Login with your GitHub account.
  4. In the Git GUI you should see the {Your_GitHub_Username}/WebProject in the main panel. Select it and click “Clone” next to the title.
  5. Double click the {Your_GitHub_Username}/WebProject repo to step into it. In the top right corner you’ll see a gear. Click on it and click the link to “Open in Explorer”. This will take you to your local copy of the repo.
  6. Go into the “firstwebsite” folder. Open the file “firstwebsite.html” to checkout the website. For best results use Firefox (or IE if you have to).
  7. Now open the file “info.json”. Replace “{INSERT_FIRST_NAME_HERE}” with your first name and save your changes. Re-open/refresh “firstwebsite.html” to test your changes.
  8. If your name shows up go ahead and commit your changes. Go back to the Git GUI and you should see a message in the upper left which mentions “Uncommitted Changes”. Add a nice message like “Personalized my page” in the “Summary” field and click “Commit to master”.
  9. Next to {Your_GitHub_Username}/WebProject you should now see an option to “Sync”. Click this to push your local changes back up to your GitHub repo.
  10. That’s it! You can verify your changes made it back up to your repo by going to your account on GitHub and seeing that “info.json” was updated and has your name on it.

Challenge
Our website is a bit bland so I invite you all to help spruce it up! Make some changes to firstwebsite.html, firstwebsite.js, and/or firstwebsite.css, test them and submit a pull request. I’ll leave a comment if we get some cool changes and you can re-sync and see what other people have added!

Anatomy of a Bug

Those who were at our last event in the Minecraft Mod room might remember this curious site we came across:

bug

Clearly something has gone wrong here…

Apparently we created a recipe which takes no ingredients. Looks like we have a bug in our software. This actually illustrates an important lesson to learn as a developer:

All software has bugs.

Whether your software is complex, simple, hardly used, or the most popular app in the app store, it has bugs. Even the most intensive testing will not find all bugs that exist in a piece of software.

So, now that we have a bug we want to figure out why it’s happening. I tried making small changes to the code, removing code to simplify what was going on, and change working code to be more like the broken code all to try and diagnose what went wrong. Finally I came across this piece of code:

String row1 = ((input[0][0] != null) ? "a" : " ") + ((input[0][1] != null) ? "a" : " ") + ((input[0][2] != null) ? "c" : " ");
String row2 = ((input[1][0] != null) ? "d" : " ") + ((input[1][1] != null) ? "e" : " ") + ((input[1][2] != null) ? "f" : " ");
String row3 = ((input[2][0] != null) ? "g" : " ") + ((input[2][1] != null) ? "h" : " ") + ((input[2][2] != null) ? "i" : " ");

Do you see something which looks out of place? If you can say your ABC’s you might… Even though you may not know what’s going on here, (using the ternary operator as a quick way to evaluate if/then/else statements) you still may be able to spot the error

That’s right. I copied and pasted this code a number of times instead of typing it all out (refer to the lazy/efficient programmer discussion we had). I managed to not update the second “a” reference which should be a “b”. This was causing recipes which had an ingredient in the position [0][1] to not be created properly. With that simple fix in place now we can see:

fixed

Much Better!

I was curious how long this bug existed so I looked at the code’s history in GitHub and found out it has been there ever since the code was first committed, about a year ago. How could this bug have existed for so long? Well, this specific use was simply never tested.

Bugs happen! They can be frustrating, but they can also be fixed. I fixed this one and committed it to our source code with a nice message (“Fixed recipe bug (dupe ‘a’ keys)”) so that hopefully nobody will encounter this bug again. Or if they do, then can look through the history and found out how to fix it!

In the comments to this post, tell of a time you found a software bug. What happened? Could you reproduce it? Have you found any bugs in Minecraft?

And as promised, here is an excellent tutorial for getting your Minecraft Modding environment setup for Minecraft 1.7:

Follow Up: Greenfoot

Greenfoot was a huge success at our March event. Be sure to download Greenfoot to a computer at home so you can try it out on your own! You can download Greenfoot here.

Like Scratch, Greenfoot has an awesome community around it who have shared some excellent creations. Check out this Animated Logo, A Simple Game, A More Complicated Game, and many more from the Greenfoot site.

This month’s challenge is to download a Greenfoot scenario take a look at what it does and modify it yourself. If it’s a logo, change the wording. If it’s a game, make sure you have the highest score ;).

Leave a comment to this post with what you came up with!

Getting Back in the Coding Groove

Howdy! For some of you, it’s been awhile since you were coding. For others you may be getting ready for your first Dojo event. In either case I’d like to offer a great way to get into the coder groove:

Hour of Code

Hour of code is an initiative to get kids a taste of learning to write code and parallels the goal of our Dojo. There are a bunch of self paced lessons which you can do in about an hour. There are some pretty heavy hitters behind the initiative (Bill Gates, Mark Zuckerberg, etc) and some really great modules (anyone like Angry Birds?).

Still not sold? Check this video out:

Follow Up: Player and World Manipulation

There are two areas which our Dojoers explored at this past event which I wanted to highlight. The first has to do with the code which determines what is dropped when we break our custom block. Normally it looks like this:

/**
* This method determines what is dropped when the block is broken
*/
@Override
public int idDropped(int par1, Random random, int zero) {
	if(random.nextBoolean()){        //get a random boolean (flip a coin)
		return Item.appleRed.itemID;
	} else {
		return this.blockID;
	}
}

This “flips a coin” and determines whether our custom block will drop itself or a delicious red apple. But what if we want to drop one of three items? Our boolean is only either “true” or “false”. We’ll need to use another operator: modulus.

The modulus operator in Java looks like ‘%’ and is a fancy way to deal with remainders when dividing. Let’s update the code and then I’ll walk you through it.

/**
* This method determines what is dropped when the block is broken
*/
@Override
public int idDropped(int par1, Random random, int zero) {
	int randomInt = random.nextInt();
	if((randomInt % 3) == 0){        //if our number divided by 3 goes evenly
		return Item.appleRed.itemID;
	} else if((randomInt % 3) == 1){        //if our number divided by 3 leaves a remainder of 1
		return Block.dirt.blockID;	//Drop a block of dirt!	
	} else {					//here for simplicity we just catch all other scenarios
		return this.blockID;			//but we could also say (random.nextInt() % 3) == 2 
	}						//for when our number divided by 3 leaves a remainder of 2
}

You’ll notice the code: if((random.nextInt() % 3) == 0) which in English reads: if we divide a random integer by 3 and the remainder is 0, do this. Similarly, the next check says: if we divide our random integer by 3 and the remainder is 1, do this instead. Our last else block will catch every other scenario, which in this case is only then we divide our random integer by 3 and we get a remainder of 2. We take a shortcut here and simply write else. Now we will drop one of three items instead of our original two.

The second cool piece of code a dojoer discovered is how to make explosions when we click on our block. The normal click code is below:

/**
  * This method determines what will happen if you right click the block while it's placed.
  */
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int par6, float par7, float par8, float par9) {
    
    //First we check to make sure the player doesn't have any items equipped so we know they're not trying to place a block
    if(entityplayer.getCurrentEquippedItem() == null){	
	    if(entityplayer.isSneaking()){	//Do something different if a player is sneaking
		    if(world.isRaining()){
			    world.getWorldInfo().setRaining(false);
		    } else {
			    world.getWorldInfo().setRaining(true);
		    }
	    } else {	//We do this if the player isn't sneaking
		    if(world.isDaytime()){
			    world.getWorldInfo().setWorldTime(18000);
		    } else {
			    world.getWorldInfo().setWorldTime(6000);
		    }
	    }
	    return true;
    } else {
	    return false;
    }
}

This normally toggles if it’s raining if you click while sneaking or toggles if it’s day/night if you’re not sneaking. By poking around the World object our dojoer found the method he wanted to use: createExplosion. You too can poke around the world object by creating a new line under “if(entityplayer.getCurrentEquippedItem() == null){“, typing “world.” (without quotes) and then hitting ctrl+spacebar. This will show you all the methods available to the World object. You can also hold crtl while clicking on the World object to take you directly to the class which will show you it’s variables, methods, and the code inside each method. Hit alt+left arrow to go back.

Now, when you select createExplosion, you’ll see some default parameters are created like this:

world.createExplosion(par1Entity, par2, par4, par6, par8, par9);

Because Forge decompiles Minecraft in order to help mod it, sometimes the names of method parameters can be tricky to understand. And even when we DO understand them it can also be tricky to decide if we should create new variables or use existing ones for the method. One great way to try and understand how this method works is to Google it. Remember, there are many other modders in your same shoes trying to understand the code and multiple communities with experienced members who are happy to help. When I searched for “minecraft forge world.createExplosion”, I found the following bit of code:

par3World.createExplosion((Entity) null, x, y, z, 3F, true);

This gives me a bit of understanding about what each of these parameters does. I know the first is an Entity object(perhaps who “causes” the explosion for death messages), the next three are probably coordinates, the next is a floating point number (which is can include fractions of integers like 12.2 or -144.0927) which I estimate to be the strength of the explosion, and finally a boolean which (according to the page I read) controls whether the explosion happens client side or server side (I am currently unaware of the implications of this).

So getting back to our method, if we switch it up to this:

/**
  * This method determines what will happen if you right click the block while it's placed.
  */
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int par6, float par7, float par8, float par9) {
    
    //First we check to make sure the player doesn't have any items equipped so we know they're not trying to place a block
    if(entityplayer.getCurrentEquippedItem() == null){	
	    world.createExplosion(entityplayer, x, y, z, 10.0F, true); //boom
	    return true;
    } else {
	    return false;
    }
}

We’ll make the block explode with a force of 10 (not sure how big this is). This will explode right where it is since we are using the same quordinates we get of where the block is when it’s clicked from “onBlockActivated”. A quick note about the float we passed in as 10.0F. Sometimes Java is able to realize when we type a number in what kind of number we’re saying it should be (whether an integer or float), as it does in turning our x, y, and z integers into doubles (a double is very similar to a float). But sometimes Java needs a bit of help. Here in this method we help Java out by putting an ‘F’ after the number to say, “Yes, this is a float variable”.

Time for some homework:
– Make our block drop one of FIVE items instead of 3. Bonus: break 25 blocks, record what is dropped each time, and post your results in a comment to this post!
– Play around with creating an explosion. Make it really big or really small. Turn the block into a cannon by creating the explosion 10 units in front of you. Play around with the boolean parameter. Do you notice any difference when it’s true or false? Bonus: Try to crash the game. See what it would take for the explosion to literally crash the world and take note of what happens. Be sure to post anything interesting you come across as a comment below!

Follow up: Minecraft Mods

Hey Dojers: I have a couple of tasks to help keep what we learned from yesterday fresh in your minds while we wait for the next Dojo event:

  1. Install a mod into MC. Take a look at Direwolf20’s video below and use this forge instller link. The video mentions minecraft 1.6.1 but the process is the same for Minecraft 1.6.2. You can search for mods to install at minecraft-mods.org. Be sure to pick the 1.6.2 version of the mod, and choose the “forge” version if one is available. Share any cool mods you find in a comment to this post!
  2. Start your own LAN party! Play with a buddy and take turns being the server and client. Try playing with mods installed. What happens if the server has a mod that the client doesn’t? What about if the client has a mod the server doesn’t? What about different versions of the same MOD? If you have our mod installed in eclipse, can you use it in a LAN party? Post your experiences in a comment on this post!
  3. Take a look at the BuildCraft source code here and find something you’d like to add to a mod. It can be an image, the name of a single file or variable, or an entire block of code! Share what you’d like to add in the comments to this post!

Follow Up: Minecraft Recipes

At our last Dojo event we went over the basics of creating a custom recipe for our mod. Several Dojers turned dirt into gold, Nether Brick, and even a block of Diamond. This was our basic recipe:

@Init
public void load(FMLInitializationEvent event){
ItemStack[][] recipe = RecipeHelper.getBlankRecipe();
recipe[0][0] = new ItemStack(Block.dirt);
RecipeHelper.addRecipe(new ItemStack(Block.dirt), recipe);    //dirt makes dirt!
}

But wait, there’s more! We can add multiple ingredients to our recipe like so:


@Init
public void load(FMLInitializationEvent event){
ItemStack[][] recipe = RecipeHelper.getBlankRecipe();
recipe[0][0] = new ItemStack(Block.dirt);
recipe[2][2] = new ItemStack(Block.dirt);
RecipeHelper.addRecipe(new ItemStack(Block.skull), recipe);    //dirt + dirt makes a skull!
}

And we can use multiple ingredients as well:

@Init
public void load(FMLInitializationEvent event){
ItemStack[][] recipe = RecipeHelper.getBlankRecipe();
recipe[0][0] = new ItemStack(Block.dirt);
recipe[2][2] = new ItemStack(Block.cobblestone);
RecipeHelper.addRecipe(new ItemStack(Block.snow), recipe);    //dirt + cobblestone makes snow!
}

We can even output more than one item:

@Init
public void load(FMLInitializationEvent event){
ItemStack[][] recipe = RecipeHelper.getBlankRecipe();
recipe[0][0] = new ItemStack(Block.dirt);
recipe[2][2] = new ItemStack(Block.cobblestone);
RecipeHelper.addRecipe(new ItemStack(Block.cake, 32), recipe);    //dirt + cobblestone makes 32 cake! (Yummy!)
}

What other recipes have you come up with? Share in the comments below!