Lab 6: Leaf Converter Robot

Objective

For lab 6, you will create an Entity that wanders around the map, and converts blocks around it into leaves. You will need a basic understanding of how the EntityAIBase class and task registration works.

Notes

To add a new task with a specific priority

tasks.addTask (priority, task).

Getting a reference to the world object

Use the worldObj field from your Robot.

Getting & setting the block at a given location in the world

Block World.getBlock (int x, int y, int z); void World.setBlock (int x, int y, int z, Block block);

To spawn a robot in game

Tasks

Part 1 — Construct a Simple Wandering LeafBot

Our first entity will be a simple robot that randomly wanders around the world. We will add more complex behavior later in part 3.

  1. In src/main/java/tealsmc/mods/entities, find the file LeafBot.java.
  2. Open the LeafBot constructor. In the template, you'll see that we've already added the constructor call for the superclass (Robot), where we pass the World parameter.
  3. In the constructor, after the call to the superclass, create a new EntityAIWander object, passing a this reference to this new Entity, and a speed value of SPEED_NORMAL. This AI task will cause our robot to randomly wander around the world at a normal pace.
  4. Add the EntityAIWander task that you just created with a priority of 1. See the notes section above for the way to do this.

Part 2 — Register and Try Out the New LeafBot

In prior labs you used the ItemsModule class to register new items we've created (like RockSifter and CrystalGrowingItem), and you used the BlocksModule class to register blocks that we've created (like CheckeredBlock and TreeConeBlock).

Now that you're going to start creating entities, you will register new entities in the EntitiesModule.

  1. You need to register the new LeafBot you created. Find the EntitiesModule.java source in src/main/java/tealsmc/mods/entities. Add the following line to the onLoad() method:

    parentMod.entityRegistry.newInstance ("leafbot", LeafBot.class, "blue_robot");

    Note that registration for entities is a bit different than for items and blocks. In particular, we register an entity class (WanderingRobot.class) rather than an entity instance.

    "blue_robot" is the entity texture. You can choose any of the following colors:

    • "red_robot"
    • "green_robot"
    • "blue_robot"
    • "gold_robot"
    • "gray_robot"
    • "rainbow_robot"
  2. Let's run the Minecraft client and summon a few leafbots. From the registration line above, you'll see that we named these entities "leafbot". See the notes section above for a reminder of how to summon entities in game. Note that robots only move once in a while, so create six or so leafbots so you don't have to wait a long time to see one move. After you've done that, you can experiment with different colors or movement speeds for your leafbot if you wish.

Part 3 — Create a Leaf Conversion Task

Now that your leafbot can wander around the map, let's update it to do something more interesting.

  1. In the entities directory, create a new class called EntityAIConvertLeaves that extends EntityAIBase (from net.minecraft.entity.ai).
  2. Implement the constructor. The constructor will take a Robot reference and store it in a private field in the class.
  3. Start by stubbing out the required methods for EntityAIBase. These are
    • public boolean shouldExecute(),
    • public void startExecuting(), and
    • public boolean continueExecuting()

    Return false from shouldExecute() and continueExecuting for now. At this point, you shouldn't see any more warnings about EntityAIConvertLeaves methods that need to be implemented.

  4. Time to implement the public boolean shouldExecute() method. This task should have a 5% probability of running each time it's queried. In order to do this, you need to have a random number generator in your class that you can use to generate random values. Use the Random class from java.util.Random in the following steps:

    1. Add a field of type Random to your EntityAIConvertLeaves named random, and initialize it to a new instance.
    2. Use random.nextDouble() to determine if shouldExecute() should return true (run the task) for a given call. Recall the nextDouble() function returns a double value in the range of 0–1, not including 1. Figure out how to return true with 5% probability.
  5. Implement the public boolean continueExecuting() method. This method indicates whether the task should continue executing after the current update. For the leaf conversion robot, we will only convert one leaf at a time, so return false to indicate that we don't require another task execution.
  6. Implement the public void startExecuting() method. In this method, you'll need to create the following code:
    1. Find a random X, Y, Z location within 2 block's length of the WanderingEntities location. Again, use your random field to generate random values.
    2. Examine the block at that location and make sure it's convertible (not air or leaves).
    3. If the block at the chosen location is convertible, then change its material to leaves (Blocks.leaves).
  7. Now that we've finished with the leaf conversion task, go back to the LeafBot class and add a new task of priority 2 of EntityAIConvertLeaves.
  8. Run the game again and test out your new LeafBot.