Updating a Minecraft Mod, Part Two: Exploding Anvils

Continuing my attempt to update what I know about modding minecraft from 1.8 to 1.15. Based on my experience from this book;

To keep things simple, I am merely modifying the example mod. The Gupta’s had you create your own, and you should do that eventually. But, for testing out the sample effects, you can just modify the code in place.

When things happen in Minecraft, the computer program has an class of objects called “Events” that represent them. Your mod code needs a function to handle these events, or provide actions that you want to take. One of the events we are going to look at is picking something up. Again, this is just going to print a log message. Don’t worry, more fun stuff comes afterwards.

Add the following code to the sample mod.

    public void onItemPickup(PlayerEvent.ItemPickupEvent event) {
        LOGGER.info("You picked up an item:" + event.getStack().toString());

Make sure you place it before the closing } or the code will not compile: You will see a bunch of Red lines on the right hand side of the editor showing you all the lines that are bad, but really, it just needs to be inside the curly braces.

What does this do? The @SubscribeEvent annotation tells the code, when run, that it has code to handle some kind of event. The program figures out what kind by looking at the Parameter to the function, in this case PlayerEvent.ItemPickupEvent which is, I hope, self explanatory. The string I had you add will tell you what kind of item you picked up and the player put into their inventory. Run the server, and break some things, then pick them up.

Look in the logging section of Intellij. You should see messages like this:

[12:31:25] [Server thread/INFO] [co.ex.ex.ExampleMod/]: You picked up an item:1 dirt
[12:31:43] [Render thread/INFO] [minecraft/AdvancementList]: Loaded 82 advancements
[12:31:43] [Server thread/INFO] [co.ex.ex.ExampleMod/]: You picked up an item:1 white_wool
[12:31:43] [Server thread/INFO] [co.ex.ex.ExampleMod/]: You picked up an item:1 mutton

You might need to scroll up.

Let’s do something more insteresting, but harder to test. We’re going to react to a different Event: when an entity gets hurt. Specifically, we are going to drop an Anvil on a Creeper, and make the anvil explode. here is the code:

    public void boom(LivingDamageEvent event){
        if (event.getSource() != DamageSource.ANVIL){
        Entity entity = event.getEntity();
                entity, entity.getPosX(), entity.getPosY(), entity.getPosZ(),
                2, Explosion.Mode.BREAK);

This function handles a new event: LivingDamageEvent. Inside that event handler, it checks to see that the source of the Damage was an Anvil. If not, we leave the function with no action.

If the source is an Anvil, we create an Explosion.

To test this code, have your coder build a structure like this (in creative mode):

Flip the lever to drop the anvil, and blow up the creeper. I use the unbreakable obsidian block to make mine, so I don’t have to rebuild it if I mess up.

You can and should experiment with this. If you add the same explosion code onto the block break event, you can make it impossible to play the game; every time you break a a block, you get a huge explosion. This may well delight your coder.

We’ll start looking at mobs next article.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.