hglabor-kits
Dependency
The library is available on Maven Central, add the following dependencies:
implementation("net.axay:hglabor-kits:$version")
hglabor-kits also depends on spigot-language-kotlin and KSpigot.
Make sure to add spigot-language-kotlin
as a "compileOnly" dependency and provide its plugin at runtime. Also, do not forget to read the setup guide of KSpigot
.
Usage
Kit properties
A kit will likely need some variable properties, which can be changed in the config or via commands. Let us create a class representing these properties:
class NinjaProperties : KitProperties() {
val maxDistance by int(30)
}
A lot of kits do also need a cooldown of some kind, therefore there is a special CooldownProperties
class, let us use that one for our Ninja kit instead.
class NinjaProperties : CooldownProperties(30 * 1000) {
val maxDistance by int(30)
}
Now we do have a cooldown
property automatically, which opens up the possibility to use some nice extensions functions.
Access the properties
Inside the kit body (see below), you can access the properties via kit.properties.yourProperty
. In our example that would be kit.properties.maxDistance
.
Kit logic
Create the kit
A kit can be created by calling the invoke()
operator function of the Kit
companion object. You have to pass a reference to the properties:
val Ninja = Kit("Ninja", ::NinjaProperties) {
// inside the kit body
}
Manage state
It is best practice handling the state inside the kit body, therefore let us create a list to keep track of whom the kit player damaged last.
val Ninja = Kit("Ninja", ::NinjaProperties) {
val lastDamaged = OnlinePlayerMap<Player>()
}
The following code snippets will all be inside the kit body.
Kit events
hglabor-kits can automatically check if a player (of a PlayerEvent
has the kit).
kitPlayerEvent<PlayerToggleSneakEvent> {
it.player.sendMessage("You definitely have the ${kit.key} kit!")
}
If the event is not a PlayerEvent
, you have to provide some logic for getting the player for whom the check should be performed.
With this knowledge, let us create an event to find out who the kit player damages:
kitPlayerEvent<EntityDamageByEntityEvent>(
// the playerGetter:
{ it.damager as? Player }
) { it, player ->
lastDamaged[player] = it.entity as? Player ?: return@kitPlayerEvent
}
Note: You can return null in that playerGetter
, this will mean that there is no kit player and therefore the kit event won't be executed.
Cooldown
You can apply the cooldown your CooldownProperties
very easily if you are inside the kit body, by using the applyCooldown
function and passing the PlayerEvent
or the Player
to it.
Let us actually implement the teleport functionality of the Ninja kit, with our cooldown applied:
kitPlayerEvent<PlayerToggleSneakEvent> {
applyCooldown(it) {
val toPlayer = lastDamaged[it.player]
if (toPlayer == null || !toPlayer.isOnline) {
cancelCooldown()
} else {
if (it.player.location.distance(toPlayer.location) <= kit.properties.maxDistance)
it.player.teleport(toPlayer)
}
}
}
Final result of Ninja kit example
You can view the final implementation of the Ninja kit here .
Going further, there are features which we did not use when creating the Ninja kit:
Kit items
Simple
Just give an item to the player:
simpleItem(ItemStack(Material.NETHERITE_SWORD))
Clickable
Execute code when the player clicks his kit item:
clickableItem(
itemStack(Material.SPONGE) {
meta { setDisplayName("Jump") }
}
) {
// it is the PlayerInteractEvent here
it.player.velocity = it.player.velocity.add(vecY(4))
}