Minecraft server MOD Spigot is useful, but there are a few inconvenient methods.
Let's change them using Kotlin Extensions.
- 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)
Let's create a Location-based method with the following extensions.
LocationExtensions.kt
fun Location.dropItem(itemStack: ItemStack) {
world.dropItem(this, itemStack)
}
- Give ItemStack
You can give items to the player below.
val itemStack = ItemStack(Material.IRON_SWORD)
player.inventory.addItem(itemStack)
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)
}
}
- Play Sound
Do it the same way.
location.world.playSound(location, sound, volume, pitch)
LocationExtensions.kt
fun Location.playSound(sound: Sound, volume: Float, pitch: Float) {
this.world.playSound(this, sound, volume, pitch)
}
spawnParticle can be simplified in the same way.
Top comments (0)