Creating a Custom Booster (Java)
Below is an example of an Experience booster. When a player receives exp, the amount received will be multiplied based on the boosters that are active. We locate active personal and global boosters of the required Type
and Namespace
. If located, we apply the associated multipliers.
package com.insurgencedev.exampleaddon.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.insurgencedev.insurgenceboosters.api.IBoosterAPI;
import org.insurgencedev.insurgenceboosters.models.booster.GlobalBoosterManager;
import org.insurgencedev.insurgenceboosters.settings.IBoostersPlayerCache;
public final class ExperienceChangeListener implements Listener {
@EventHandler
private void onChange(PlayerExpChangeEvent event) {
final String TYPE = "Experience";
final String NAMESPACE = "CUSTOM";
double totalMulti = 1;
IBoostersPlayerCache.BoosterFindResult pResult = IBoosterAPI.getCache(event.getPlayer()).findActiveBooster(TYPE, NAMESPACE);
if (pResult instanceof IBoostersPlayerCache.BoosterFindResult.Success boosterResult) {
totalMulti += boosterResult.getBooster().getMultiplier();
}
GlobalBoosterManager.BoosterFindResult gResult = IBoosterAPI.getGlobalBoosterManager().findBooster(TYPE, NAMESPACE);
if (gResult instanceof GlobalBoosterManager.BoosterFindResult.Success boosterResult) {
totalMulti += boosterResult.getBooster().getMultiplier();
}
event.setAmount((int) (event.getAmount() * totalMulti));
}
}
Register your listener
For your listener class to take effect, it must be registered.
package com.insurgencedev.exampleaddon;
import com.insurgencedev.exampleaddon.listeners.ExperienceChangeListener;
import org.insurgencedev.insurgenceboosters.api.addon.IBoostersAddon;
import org.insurgencedev.insurgenceboosters.api.addon.InsurgenceBoostersAddon;
@IBoostersAddon(name = "ExampleAddon", version = "1.0.0", author = "Insurgence Dev Team", description = "Example Addon")
public class ExampleAddon extends InsurgenceBoostersAddon {
@Override
public void onAddonReloadablesStart() {
registerEvent(new ExperienceChangeListener());
}
}
No Comments