The current EventHandler method
of the MobEnhancer
plugin sets the health of zombies to 40
, where the number 40 is
hardcoded. This means that the value of 40
is a part of the code itself, and this cannot be changed after the code is compiled. We wish to make this value
softcoded, that is, we want to retrieve the value from an external source, which is config.yml
in our case:
Currently, the onMobSpawn
method is as follows:
@EventHandler public void onMobSpawn(CreatureSpawnEvent event) { if (event.getEntityType() == EntityType.ZOMBIE) { int health = 40; event.getEntity().setMaxHealth(health); event.getEntity().setHealth(health); } }
We will work from this existing code. The if
statement is no longer needed, because we don't want to limit the plugin to zombies only. As discussed earlier, we also want to replace the hardcoded 40 value
with a double
value, which will be read from the config
file. Therefore, 40
should be replaced with getConfig().getDouble(type)
. You will also have to change the variable type from int
to double
. The Type
in this statement will be a string of the Entity
type. Some examples of this are ZOMBIE
, SKELETON
, or any of the other entity types that are listed in config.yml
. We already know that we can get the type of the entity that was spawned by using event.getEntityType()
. However, this gives us EntityType
in the enum
form, and we require it in the string
form. The EntityType
page of the Bukkit API documentation informs us that we can call the getname
method to return the string that we are looking for. The new onMobSpawn
method is as follows:
@EventHandler public void onMobSpawn(CreatureSpawnEvent event) { //Find the type of the Entity that spawned String type = event.getEntityType().name(); //Retrieve the custom health amount for the EntityType //This will be 0 if the EntityType is not included in the config double health = getConfig().getDouble(type); event.getEntity().setMaxHealth(health); event.getEntity().setHealth(health); }
This EventHandler method
is nearly complete. We are allowing other people to set the health
value. We want to ensure that they are entering a valid number. We don't want the plugin to crash because it is being misused. We know that we are receiving a double
value because even if the user sets a non-numeric value, we will be given the default value of 0
instead. However, not every valid double value will be useable in our situation. For example, we cannot set the health of an entity to a negative value. We also do not want to set the health to 0
, because this will instantly kill the entity. Therefore, we should only modify the health if the new health is set to a positive number. This can be done with a simple if
statement, as follows:
if (health > 0)
The MobEnhancer
plugin is now configurable and supports any type of creature. It is no longer limited to just zombies. The finished code will be similar to the following:
package com.codisimus.mobenhancer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; 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, CommandExecutor { @Override public void onEnable() { //Save the default config file if it does not already exist saveDefaultConfig(); //Register all of the EventHandlers within this class getServer().getPluginManager().registerEvents(this, this); //Register this class as the Executor of the /merl command getCommand("mobenhancerreload").setExecutor(this); } @EventHandler public void onMobSpawn(CreatureSpawnEvent event) { //Find the type of the Entity that spawned String type = event.getEntityType().name(); //Retrieve the custom health amount for the EntityType //This will be 0 if the EntityType is not in the config double health = getConfig().getDouble(type); //Mobs cannot have negative health if (health > 0) { event.getEntity().setMaxHealth(health); event.getEntity().setHealth(health); } } @Override public boolean onCommand(CommandSender sender, Command command,String alias, String[] args) { reloadConfig(); sender.sendMessage("MobEnhancer config has been reloaded"); return true; //The command was executed successfully } }
13.58.82.79