Modifying an event as it occurs

The Bukkit API allows a programmer to do more than just cancel an event. Depending on the event, you can modify many of its aspects. In this next project, we will modify zombies as they spawn. Every time a zombie spawns, we will give it 40 health rather than the default 20. This will make zombies more difficult to kill.

Create a new project as you would for any plugin. We will call this plugin MobEnhancer. Similar to what we did with the NoRain plugin, have the main class implement Listener and add the following line of code to the onEnable method to register the EventHandlers method:

getServer().getPluginManager().registerEvents(this, this);

For this project, we will have an EventHandler method that listens for mobs spawning. This will be the CreatureSpawnEvent class. This event has many methods that we can call to either modify the event or gain more information about it. We only wish to modify zombies that are spawned. Therefore, the first thing that we will add is an if statement, which will check whether the EntityType method is ZOMBIE. This is done by using the following block of code:

if (event.getEntityType() == EntityType.ZOMBIE) {
}

Inside the curly braces, we will change the health of the Entity class to 40. We can retrieve the Entity class by calling event.getEntity(). Once we have the Entity class, we have access to many additional methods. You can view all of these methods in the API documentation, which is available at https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Entity.html.

One of the methods is setHealth. Before we can set the health to 40, we must set the maximum health, which can have a value of 40. An Entity class cannot have a health of 40 when its maximum health is still 20. These two lines of code will complete this plugin. The code now looks like this:

package com.codisimus.mobenhancer;

import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class MobEnhancer extends JavaPlugin implements Listener {
  @Override
  public void onEnable() {
    //Register all of the EventHandlers within this class
    getServer().getPluginManager().registerEvents(this, this);
  }

  @EventHandler
  public void onMobSpawn(CreatureSpawnEvent event) {
    if (event.getEntityType() == EntityType.ZOMBIE) {
      int health = 40;
      event.getEntity().setMaxHealth(health);
      event.getEntity().setHealth(health);
    }
  }
}

The first version of the MobEnhancer plugin is complete with this small class. You can test the plugin by installing it on your server. You will notice that zombies will be much more difficult to kill.

Tip

Note that we declared a local variable named health of the int type and set its value to 40. Alternatively, we could simply write 40 in the two lines that follow. However, programming the amount of health this way allows us to easily change it in the future. We only have to change the number in one line of code rather than two or more. Also, you may have noticed that the setMaxHealth and setHealth methods accept a variable of the double type. However, an int value may still be passed to the method, as it will be automatically converted to a double value with a value of 40.0.

You can add more code to the plugin in order to modify the health of more types of entities. A list of all the EntityType methods can be found in the Bukkit API documentation under the EntityType class reference page at https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/EntityType.html. However, in the next chapter, we will make this plugin configurable in order to change the health of every type of Entity that spawns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.67.251