Kotlin Extensions for Minecraft Spigot

reyadayer

ReyAD

Posted on September 3, 2018

Kotlin Extensions for Minecraft Spigot

Minecraft server MOD Spigot is useful, but there are a few inconvenient methods.
Let's change them using Kotlin Extensions.

  1. Drop ItemStack

You can drop items in the world using the code below, but that is redundant.

val location = player.location
location.world.dropItem(location, itemStack)
Enter fullscreen mode Exit fullscreen mode

Let's create a Location-based method with the following extensions.

LocationExtensions.kt

fun Location.dropItem(itemStack: ItemStack) {
    world.dropItem(this, itemStack)
}
Enter fullscreen mode Exit fullscreen mode
  1. Give ItemStack

You can give items to the player below.

val itemStack = ItemStack(Material.IRON_SWORD)
player.inventory.addItem(itemStack)
Enter fullscreen mode Exit fullscreen mode

However, when the player 's inventory is full, you can not give items.

At that time it is kindness to define the following extensions and drop them to the ground.

PlayerExtensions.kt

fun Player.giveItem(itemStack: ItemStack) {
    if (inventory.firstEmpty() == -1) {
        player.location.dropItem(itemStack)
    } else {
        player.inventory.addItem(itemStack)
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Play Sound

Do it the same way.

location.world.playSound(location, sound, volume, pitch)
Enter fullscreen mode Exit fullscreen mode

LocationExtensions.kt

fun Location.playSound(sound: Sound, volume: Float, pitch: Float) {
    this.world.playSound(this, sound, volume, pitch)
}
Enter fullscreen mode Exit fullscreen mode

spawnParticle can be simplified in the same way.

💖 💪 🙅 🚩
reyadayer
ReyAD

Posted on September 3, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Kotlin Extensions for Minecraft Spigot
minecraft Kotlin Extensions for Minecraft Spigot

September 3, 2018