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
worldObj field from your Robot.
Getting & setting the block at a given location in the world
To spawn a robot in game
- While in the world, type "/robot entity", where
entityis the name of the registered robot you wish to summon. - For this lab, the command would be "
/robot leafbot".
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.
- In src/main/java/tealsmc/mods/entities, find the file LeafBot.java.
- Open the
LeafBotconstructor. In the template, you'll see that we've already added the constructor call for the superclass (Robot), where we pass theWorldparameter. - In the constructor, after the call to the superclass, create a new
EntityAIWanderobject, passing athisreference to this new Entity, and a speed value ofSPEED_NORMAL. This AI task will cause our robot to randomly wander around the world at a normal pace. - Add the
EntityAIWandertask 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.
-
You need to register the new
LeafBotyou created. Find the EntitiesModule.java source insrc/main/java/tealsmc/mods/entities. Add the following line to theonLoad()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"
- 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.
- In the entities directory, create a new class called
EntityAIConvertLeavesthat extendsEntityAIBase(fromnet.minecraft.entity.ai). - Implement the constructor. The constructor will take a
Robotreference and store it in a private field in the class. - Start by stubbing out the required methods for
EntityAIBase. These arepublic boolean shouldExecute(),public void startExecuting(), andpublic boolean continueExecuting()
Return
falsefromshouldExecute()andcontinueExecutingfor now. At this point, you shouldn't see any more warnings aboutEntityAIConvertLeavesmethods that need to be implemented. -
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 theRandomclass fromjava.util.Randomin the following steps:- Add a field of type
Randomto yourEntityAIConvertLeavesnamedrandom, and initialize it to a new instance. - Use
random.nextDouble()to determine ifshouldExecute()should returntrue(run the task) for a given call. Recall thenextDouble()function returns adoublevalue in the range of 0–1, not including 1. Figure out how to returntruewith 5% probability.
- Add a field of type
- 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 returnfalseto indicate that we don't require another task execution. - Implement the
public void startExecuting()method. In this method, you'll need to create the following code:- Find a random X, Y, Z location within 2 block's length of the
WanderingEntitieslocation. Again, use yourrandomfield to generate random values. - Examine the block at that location and make sure it's convertible (not air or leaves).
- If the block at the chosen location is convertible, then change its
material to leaves (
Blocks.leaves).
- Find a random X, Y, Z location within 2 block's length of the
- 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. - Run the game again and test out your new LeafBot.