Skip to main content

Creating an Effect Booster (Java)

Below is an example of how you can create a booster that applies effects to the player

Main Class

package com.insurgencedev.potioneffectsaddon;

import com.insurgencedev.potioneffectsaddon.listeners.BoosterListener;
import org.insurgencedev.insurgenceboosters.api.addon.IBoostersAddon;
import org.insurgencedev.insurgenceboosters.api.addon.InsurgenceBoostersAddon;

@IBoostersAddon(name = "PotionEffects", version = "1.0.0", author = "Insurgence Dev Team", description = "Apply Potion Effects")
public class PotionEffectsAddon extends InsurgenceBoostersAddon {

    @Override
    public void onAddonStart() {
        new MyConfig();
    }

    @Override
    public void onAddonReloadablesStart() {
        registerEvent(new BoosterListener());
    }

}

Listener Class

package com.insurgencedev.exampleaddon.listeners;

import com.insurgencedev.exampleaddon.ExampleAddon;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.potion.PotionEffectType;
import org.insurgencedev.insurgenceboosters.events.IBoosterEndEvent;
import org.insurgencedev.insurgenceboosters.events.IBoosterStartEvent;
import org.insurgencedev.insurgenceboosters.settings.IBoostersPlayerCache;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public final class BoosterListener implements Listener {

    private final List<UUID> activeList = new ArrayList<>();
    private final String NAMESPACE = "CUSTOM_EFFECT";

    @EventHandler
    private void onStart(IBoosterStartEvent event) {
        Player player = event.getPlayer();

        IBoostersPlayerCache.Data.Booster data = event.getBoosterData();
        if (!data.getType().equals(ExampleAddon.instance().getConfig().getType()) && !data.getNamespace().equals(NAMESPACE)) {
            return;
        }

        applyEffect(player);
    }

    @EventHandler
    private void onEnd(IBoosterEndEvent event) {
        IBoostersPlayerCache.Data.Booster data = event.getBoosterData();
        if (!data.getType().equals(ExampleAddon.instance().getConfig().getType()) && !data.getNamespace().equals(NAMESPACE)) {
            return;
        }

        Player player = event.getPlayer();
        removeEffect(player);

    }

    @EventHandler
    private void onRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        if (activeList.contains(player.getUniqueId())) {
            applyEffect(player);
        }
    }

    private void applyEffect(Player player) {
        activeList.add(player.getUniqueId());
        PotionEffectType type = PotionEffectType.getByName(ExampleAddon.instance().getConfig().getEffect());
        if (type != null) {
            player.addPotionEffect(type.createEffect(9999999, ExampleAddon.instance().getConfig().getAmplifier() - 1));
        }
    }

    private void removeEffect(Player player) {
        activeList.remove(player.getUniqueId());
        PotionEffectType type = PotionEffectType.getByName(ExampleAddon.instance().getConfig().getEffect());
        if (type != null) {
            player.removePotionEffect(type);
        }
    }
}

Custom Config Class

package com.insurgencedev.exampleaddon;

import lombok.Getter;
import org.insurgencedev.insurgenceboosters.api.addon.AddonConfig;


@Getter
public class MyConfig extends AddonConfig {

    private String type;
    private String effect;
    private int amplifier;

    public MyConfig() {
        loadAddonConfig("config.yml", "config.yml");
    }

    @Override
    protected void onLoad() {
        type = getString("Type");
        effect = getString("Effect");
        amplifier = getInteger("Amplifier");
    }
}

Internal Config file

Type: Strength
Effect: INCREASE_DAMAGE
Amplifier: 2