Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Decorators

Iltotore edited this page Jul 23, 2020 · 2 revisions

SpigotScala provides implicit decorators for Spigot. It means you don't need to instantiate the decorator explicitly.

Example

Let's use the StringDecorator with the color method:

import io.github.iltotore.scala.spigot.Implicits._

val translatedText = "&b&lAqua bold".color() //Compilated code: val translatedText = new StringDecorator("&b&lAqua bold").color('&')

List of implicit decorators

Decorator Original Class
StringDecorator String
InventoryDecorator Inventory
WorldDecorator World
CommandAssignement Take a String and an implicit JavaPlugin. Used to sugar command executor assignement
EquipmentDecorator EntityEquipment
VectorArithmetic Vector
LocationDecorator Location
BlockDecorator Block
ItemDecorator ItemStack
ObjectiveDecorator Objective

Decorators methods can be consulted in the Scaladoc

Special decorating methods

Operators

Some decorators contains new operators for the wrapped object.

Here is an example using the org.bukkit.util.Vector class:

val vector = new Vector(1, 2, 3) //Keep in mind that the 'new' keyword will be optional in Scala 3
vector+=Vector(0, 1, 0) //Our Vector is now (1, 3, 3)

Better generics

Scala's generic is more powerful than Java and allows us to completely remove the "generic class parameter". Here is an example using org.bukkit.World:

val world = World("world")

//Old method
world.spawn(location, Slime.class) //Spawn a Slime

//New method
world.spawnGeneric[Slime](location) //Spawn a Slime