Skip to main content
Version: Next

Creating a Custom Effect

This is an example of how a custom effect is structured and registered.

Effect Class

package org.yourname.yourplugin

import me.m64diamondstar.effectmaster.locations.LocationUtils
import me.m64diamondstar.effectmaster.shows.EffectShow
import me.m64diamondstar.effectmaster.shows.utils.Effect
import me.m64diamondstar.effectmaster.utils.Pair
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.Particle
import org.bukkit.entity.Player

import me.m64diamondstar.effectmaster.locations.LocationUtils
import me.m64diamondstar.effectmaster.shows.EffectShow
import me.m64diamondstar.effectmaster.shows.utils.Effect
import me.m64diamondstar.effectmaster.shows.utils.Parameter
import me.m64diamondstar.effectmaster.shows.utils.ShowSetting
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.Particle
import org.bukkit.entity.Player

class CoolKotlinEffect: Effect() {

/**
* The execute method of the effect. This method is called when the effect should be executed.
* @param players the list of players when it's played privately. This list will be null if the effect should be played for everyone.
* @param effectShow the show the effect is placed in
* @param id the index of the effect
* @param settings the settings which should be applied for the effect. This can for example be the Play At setting.
*/
override fun execute(players: List<Player>?, effectShow: EffectShow, id: Int, settings: Set<ShowSetting>) {
val location = LocationUtils.getLocationFromString(this.getSection(effectShow, id).getString("Location")) // Use the location utils to retrieve the location from the string.
val amount = this.getSection(effectShow, id).getInt("Amount") // Get the amount of particles to spawn.

(players ?: Bukkit.getOnlinePlayers()).forEach { // Loop through the list of players.
location?.let { p1 -> it.spawnParticle(Particle.FLAME, p1, amount, 0.5, 0.5, 0.5, 0.3) } // Create whatever cool visual effect you want.
}
}

/**
* The default parameter setup of the effect. Check the ParameterType class for available types.
* @return the default parameters
*/
override fun getDefaults(): List<Parameter> {
val list = ArrayList<Parameter>()

/*
* Add parameters here.
* The first value is the name of the parameter.
* The second value is the default value.
* The third value is the description.
* The fourth value is the type of the parameter. This is needed so the plugin can correctly save the value in the config.
* The fifth value is the validator. This checks whether the parameter is valid, or the player should re-enter the value.
*/
list.add(Parameter(
"Location",
"world, 0, 0, 0",
"The location at which the particles should spawn.",
{ it },
{ LocationUtils.getLocationFromString(it) != null}
))

list.add(Parameter(
"Amount",
10,
"The amount of cool particles to spawn.",
{ it.toInt() },
{ it.toIntOrNull() != null && it.toInt() > 0 }
))

list.add(Parameter(
"Delay",
0,
"The delay between the start of the show and this effect.",
{ it.toInt() },
{ it.toIntOrNull() != null && it.toInt() > 0 }
))

return list
}

/**
* The description of the effect. This will be shown when the user hovers over the effect in the effect creation gui.
* @return the description
*/
override fun getDescription(): String {
return "A super cool effect."
}

/**
* The display material of the effect. This material is used in the guis.
* @return the display material
*/
override fun getDisplayMaterial(): Material {
return Material.DIAMOND
}

/**
* This is the identifier (with other words, the name) of the effect.
* @return the identifier
*/
override fun getIdentifier(): String {
return "COOL_KOTLIN_EFFECT" // The effect won't load if the identifier already exists from another effect.
}

/**
* Tells whether the effect should be run synchronously or asynchronously.
* @return true if the effect should be run synchronously and false if it should be run asynchronously
*/
override fun isSync(): Boolean {
return false
}

}

Main Class

class EffectMasterTestIntegration : JavaPlugin() {

override fun onEnable() {
// Register the effect when your plugin enables
Effect.Type.registerExternalEffect(CoolKotlinEffect(), this)
}

}