01_emoji_impact - kmcnaught/MinecraftJavaTasks GitHub Wiki

We will inspect and modify the behaviour of a projectile when it hits an entity.

We will practise:

  • using the debugger to follow what's happening
  • changing control flow to tweak behaviour
  • using eclipse "suggest" to explore the methods we can call on an entity

image of emoji projectile being thrown

Intro

We are using a custom item which is based on the built-in snowball item. The EmojiEntity can be thrown like a snowball, randomly producing either a happy face projectile or a grumpy face projectile. When the projectile hits its target, it applies a potion effect. We will customise the behaviour that is executed when the item hits other entities. The code has been modified from the original MBE code so make sure you have the new files (see setup instructions)

Debug the project in Eclipse for these tasks. Make sure Project -> Build automatically is selected. While working on this task, you can pause at breakpoints, edit the code and then hit Play to continue - you do not need to restart Minecraft when you make changes.

Setup

Copy EmojiEntity.java and EmojiItem.java into this directory to replace the existing files:

MinecraftByExample/src/main/java/minecraftbyexample/mbe81_entity_projectile/

You will be working in the EmojiEntity.java file only. You only need to look inside the protected void onImpact(RayTraceResult rayTraceResult) method. This method gets called by Minecraft when the entity impacts something, and the rayTraceResult object contains information about what was hit.

screenshot showing EmojiEntity.java file in project hierarchy

Tasks

A - Baseline - get started

  1. Get yourself into a creative world and find some Pigs, Sheep and other entities (you may need to spawn some yourself).
  2. Select the MBE81a Throwable Emoji item from the Miscellaneous tab in the inventory (either the happy or grumpy one)
  3. Right click or "Use item" to throw it.
  4. Notice that the emoji is sometimes a happy face and sometimes a grumpy face.
  5. See what happens when it hits an animal.

B - Debugging - explore the existing code

  1. Put a breakpoint in line 79 of EmojiEntity.java. This will pause the code when you manage to hit an entity with your emoji. Now go find an animal and throw emojis at it until you've successfully entered the debug view.
  2. Use "Step over" to execute each line one by one and inspect the variables. You do not need to understand all of them but you should try to figure out what they might be used for. Can you follow the flow of the code?
  3. Add another log using System.out.println to print out the effect that is applied
  4. Remove your breakpoint(s) and hit Play, check you can see the log in the console next time you hit an entity. Adding logs can be very useful when you are debugging.

C - Fixing a bug

  1. There's a mistake! We want the HAPPY emoji to give happy effects, but it seems to be adding a poison effect. Can you fix this? The poison effect should only occur with a grumpy face emoji.

D - Adding some more behaviour

  1. There is a commented out block around line 105. Add this back in by removing the /* and */.
  2. Try it out, what happens when you fire an emoji at a pig? How about a sheep?
  3. You decide you would like to fire all types of entities into the air, not just pigs. Change the code to allow this.
  4. You have a change of heart and decide that only adult entities deserve to be launched. Can you make this happen? HINT: A LivingEntity has a isChild() method you can call which returns a boolean
  5. What else might you do when an emoji hits an entity? Use the eclipse "suggest" function to try other methods you can call on the livingEntity. HINT: type livingEntity.set then press "suggest" to find all the methods starting with the word set.... Have fun!