Kosp - helpful plugin library for Kotlin

##Kosp

I am developing all of my plugins in Kotlin. It brings some nice features like cool constructors and extensions. In the end, I feel like I can work more efficient using Kotlin.

###Source

###Usage

Please read the README.md

####Name

Kotlin + Sponge = Kosp; maybe not that creative but I just needed a name :wink:

###Extensions

A good example of using an extension is formatting a Text:

fun Text.aqua(): Text = toBuilder().color(TextColors.AQUA).build()

Generalizing this:

fun Text.format(format : TextFormat): Text = toBuilder().format(format).build()
fun Text.color(color : TextColor): Text = format(format.color(color))
fun Text.aqua(): Text = color(TextColors.AQUA)

Note that format.color is format.getColor() and this is the receiver of the function, in this case Text.

###Config

Features: automatic hyphen separated key names, simple Text and TextTemplate serializer.

Example config:

@ConfigSerializable
class Config(
    @Setting val ticketCosts: Int = 100
)

Plugin main class

@Plugin(...)
class PluginMainClass @Inject constructor(
    @DefaultConfig(sharedRoot = true) configLoader: ConfigurationLoader<CommentedConfigurationNode>
) {
    val configManager = ConfigManager(
            configLoader = configLoader,
            clazz = Config::class)
}

And then simply: configManager.get() to get the Config object. .save() to save a modified one(modifying is really easy in Kotlin with data classes)

The .conf would be:
ticket-costs = 100

###BStats

A client for BStats.

###Other extensions

For example:

  • text concatenation: "red text".red() + " green text".green() or simply "text".toText() + "a string"
  • text actions: "click text".action(TextActions.runCommand("/time set day"))
  • text template formatting: "arg_name".toArg().green().bold()
  • UUID to Player, User, World: uuid.toPlayer(), uuid.toUser(), uuid.toWorld()

####Explore it


I will integrate Kosp in my older plugins in th future, so there will be many examples of how to use this lib.

This lib was meant just for my own use but as always I do it opensource, so feel free to use it :slight_smile:
Using it in Java is possible but not recommended, the extensions become static methods which are not so nice to use.

If you use it, please send me a message, I would like to see how you use it :slight_smile:

RandomByte

2 Likes

Sponge + Kotlin = Splin :wink:

Here you have all you need about kotlin https://antonioleiva.com/custom-views-android-kotlin/

That is not an introduction into Kotlin. It is about Android with Kotlin, more specifically about custom views. I recommend the official docs by JetBrains to learn Kotlin.: Reference - Kotlin Programming Language