Category Archives: Lesson

Java Notes: Variables

Welcome to our lesson on variables in Java. First, get the project here:

http://www.greenfoot.org/scenarios/570/get_gfar

This is a simple little program but it could use a little code oraganization. Use this code instead:

http://pastebin.com/Wh9xy6AX

That cleaned up our code a bit! Once our lesson is done, you can check out these games:

http://www.greenfoot.org/scenarios/189

http://www.greenfoot.org/scenarios/494

http://www.greenfoot.org/scenarios/1083

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!

Getting your Minecraft Modding Environment Setup

This post is a bit out of date. Check out the new easier method here.

Here are some basic instructions for getting a Minecraft modding environment setup:

  • Download PC.zip or Mac.zip
  • Extract the contents
  • Extract Eclipse
  • Run Java JDK installation
  • Extract Forge
  • Run forge/install.cmd (or install.sh for Mac). This will take a few minutes to complete.
  • Extract BaseMod. Copy the “com” and “assets” folders to our mod to forge/mcp/src/minecraft. (It’s okay to overwrite any existing files)
  • Open your Eclipse folder and double click eclipse.exe (windows) or eclipse (mac).
  • Click File -> Import…
  • Search for Existing and double click “Existing Projects into Workspace”. Click Next.
  • Click “Browse” and go to your forge/mcp/eclipse/ folder. Click Finish.
  • Eclipse will import the project. Once finished you can click the green play icon which says “Run”. This will run Minecraft from the source that forge decompiled with our mod in there.
  • If you want to look at our BaseMod source code expand the “Minecraft” -> “src” -> “com.fredxcoders.dojo.mod” folders. BaseMod.java has our custom recipe to turn dirt into an instance of our custom block.

Updating to our latest mod code:

  • Go to our GitHub page here and click the “Download Zip” button in the bottom right.
  • Extract the contents of the zip and copy the “com” and “assets” folders to our mod to forge/mcp/src/minecraft. (It’s okay to overwrite any existing files).
  • If eclipse is open click the top “Minecraft” folder and hit F5. If it’s not, open eclipse.
  • Eclipse will now have our latest source code in it.

Lesson: Minecraft Ore Generation

Let’s recap what we’ve done so far. We’ve made custom recipes, custom blocks, and we made those blocks manipulate the Minecraft world around us. Now it’s time to make those blocks spawn naturally in the world.

In Minecraft all blocks have rules which control how and where they spawn. The block spawning process is determined the first time a chunk is loaded. In Minecraft a chunk is 16x 16 x 256 blocks. When you generate a new world some chunks around the spawn area are created and as you explore you will create and load more chunks.

You’ll want to download the latest zip of our mod here. Once downloaded open the zip file. Copy the “com” and “assests” folders to your folder “forge/mcp/src/minecraft/” (overwriting any previous files in there). Now fire up Eclipse and let’s take a look at the new code.

If you open up our com.fredxcoders.dojo.mod.BaseMod.java file you may miss the change we did here but it’s an important one.

EventManager eventManager = new EventManager();
GameRegistry.registerWorldGenerator(eventManager);

What we’ve done is create an EventManager object and register it as a WorldGenerator. What is an EventManager you ask? Well let’s take a look at that then. Open up com.fredxcoders.dojo.mod.managers.EventManager.java. You should see something like this:

package com.fredxcoders.dojo.mod.managers;</code>

import java.util.Random;

import com.fredxcoders.dojo.mod.BaseMod;

import net.minecraft.block.Block;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenMinable;
import cpw.mods.fml.common.IWorldGenerator;

public class EventManager implements IWorldGenerator{

@Override
public void generate(Random random, int chunkX, int chunkZ, World world,
IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {

//world.provider.dimensionId -1 is nether, 0 is overworld, 1 is end
if(world.provider.dimensionId == 0){
generateOverworld(world, random,chunkX*16, chunkZ*16);
}
}

private void generateOverworld(World world, Random random, int x, int z) {
int maxX = 16; //minX is one, so setting this to 16 says all of the x ranges can be used
int maxZ = 16; //minY is one, so setting this to 16 says all of the y ranges can be used
int maxVeinSize = 4 + random.nextInt(5); //this means each patch of our blocks will be between 4 and 8 large
int chancesToSpawn = 50; //How many times our ore can spawn in one chunk
int minY = 15; //how low the ore can spawn
int maxY = 80; //how high the ore can spawn
this.addOreSpawn(BaseMod.MY_BLOCK, world, random, x, z, maxX, maxZ, maxVeinSize, chancesToSpawn, minY, maxY);
}

/**
* Adds an Ore Spawn to Minecraft. Simply register all Ores to spawn with this method in your Generation method in your IWorldGeneration extending Class
*
* @param The Block to spawn
* @param The World to spawn in
* @param A Random object for retrieving random positions within the world to spawn the Block
* @param An int for passing the X-Coordinate for the Generation method
* @param An int for passing the Z-Coordinate for the Generation method
* @param An int for setting the maximum X-Coordinate values for spawning on the X-Axis on a Per-Chunk basis
* @param An int for setting the maximum Z-Coordinate values for spawning on the Z-Axis on a Per-Chunk basis
* @param An int for setting the maximum size of a vein
* @param An int for the Number of chances available for the Block to spawn per-chunk
* @param An int for the minimum Y-Coordinate height at which this block may spawn
* @param An int for the maximum Y-Coordinate height at which this block may spawn
**/
public void addOreSpawn(Block block, World world, Random random, int blockXPos, int blockZPos, int maxX, int maxZ, int maxVeinSize, int chancesToSpawn, int minY, int maxY)
{
int maxPossY = minY + (maxY - 1);
assert maxY &gt; minY: "The maximum Y must be greater than the Minimum Y";
assert maxX &gt; 0 &amp;&amp; maxX &lt;= 16: "addOreSpawn: The Maximum X must be greater than 0 and less than 16"; assert minY &gt; 0: "addOreSpawn: The Minimum Y must be greater than 0";
assert maxY &lt; 256 &amp;&amp; maxY &gt; 0: "addOreSpawn: The Maximum Y must be less than 256 but greater than 0";
assert maxZ &gt; 0 &amp;&amp; maxZ &lt;= 16: "addOreSpawn: The Maximum Z must be greater than 0 and less than 16";

int diffBtwnMinMaxY = maxY - minY;
for(int x = 0; x &lt; chancesToSpawn; x++)
{
int posX = blockXPos + random.nextInt(maxX);
int posY = minY + random.nextInt(diffBtwnMinMaxY);
int posZ = blockZPos + random.nextInt(maxZ);
(new WorldGenMinable(block.blockID, maxVeinSize)).generate(world, random, posX, posY, posZ);
// System.out.println("We just spawned a MY_BLOCK! [" + x + "]");
}
}
}

Let’s go over what this code is doing. First you can see that our EventManager object Implements IWorldGenerator. The IWorldGenerator class was created by Minecraft to help control how blocks are generated. When we implement that interface we have to include one method:

public void generate(Random random, int chunkX, int chunkZ, World world,
IChunkProvider chunkGenerator, IChunkProvider chunkProvider) {

This is the method Minecraft is going to execute when it goes to create a new chunk. What does this method do? Well, it checks  if the current world.provider.dimensionId is 0 (according to the comment that means that the player generating the new chunk is in the overworld) and then calls the generateOverworld method.

private void generateOverworld(World world, Random random, int x, int z) {
int maxX = 16; //minX is one, so setting this to 16 says all of the x ranges can be used
int maxZ = 16; //minY is one, so setting this to 16 says all of the y ranges can be used
int maxVeinSize = 4 + random.nextInt(5); //this means each patch of our blocks will be between 4 and 8 large
int chancesToSpawn = 50; //How many times our ore can spawn in one chunk
int minY = 15; //how low the ore can spawn
int maxY = 80; //how high the ore can spawn
this.addOreSpawn(BaseMod.MY_BLOCK, world, random, x, z, maxX, maxZ, maxVeinSize, chancesToSpawn, minY, maxY);
}

This method sets a bunch of parameters for how our ore will be spawned (check the comments for what each one does). Finally after the parameters are set it calls addOreSpawn which is a nice little method I borrowed from wuppy29 who has some great tutorials here. We pass BaseMod.MY_BLOCK (our custom block) to addOreSpawn so that it can be added to the chunk.

Now that we’ve looked at the code let’s fire up Minecraft and see if we can spot our ores in the wild! You’ll probably want to create a new world and hit  F3 to show your coordinated. Make sure your “y” is in the minY and maxY range specified in generateOverworld when looking for the ore. It also helps to be on creative mode.

Once you’ve spotted some ore and verify these changes worked, start playing around with the parameters in generateOverworld. Here are some tips:

    1. Make the ore spawn only in high places or only low.
    2. Make it rare but concentrated in large clusters or bountiful but in small amounts.
    3. Manage to crash Minecraft with your changes. Awesome! Write down your steps. Knowing how to crash a program will help you figure out how not to.
    4. What happens if you don’t start with a fresh world after each change? When do your changes start happening?
    5. Take a screenshot of something that looks cool.
    6. Uncomment this line. When do you notice these statements print to your console in Eclipse?:
// System.out.println("We just spawned a MY_BLOCK! [" + x + "]");

Extra credit:

  1. Our code currently spawns our custom block. What if we want to spawn some existing blocks in new places or next to our custom block. How could we do that?
  2. In our generate method there is a comment which references the nether. See if you can get our custom block to spawn there as well.