Using configured settings within your plugin
Our current EventHandler
of the MobEnhancer
plugin sets the health of zombies to 40
. The number 40
is hardcoded. This means that the value of 40
is a part of the code itself, and cannot be changed after the code is compiled. We wish to make this value softcoded
which, as you can guess, is retrieving the value from an external source, in our case, config.yml
.
Currently our 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 only zombies. As we discussed earlier, we also want to remove the hardcoded 40
with an integer that will be read from the config
file. Therefore, 40
should be replaced with getConfig(...