Sueta

[OLD] KUtils : High level Minecraft plugins

3.3.2

for Spigot & BungeeCord

·Plugin

Simplified event listening
You can use listen() to easily listen to events

With KUtils

listen<PlayerJoinEvent>{
  it.player.msg("Hello world!")
}

Without KUtils

server.pluginManager.registerEvent(object: Listener{
  @EventHandler
  fun onPlayerJoin(e: PlayerJoinEvent){
      e.player.sendMessage("Hello world!")
  }
}, this)

In Java

getServer().getPluginManager().registerEvents(new Listener() {
  @EventHandler
  public void onPlayerJoin(PlayerJoinEvent e){
      e.getPlayer().sendMessage("Hello world!");
  }
}, this);

Simplified scheduling

schedule(delay = 10){
    // this will be executed after 10 seconds
}

schedule(delay = 10, period = 20){
    // this will be executed after 10 seconds then each 20 seconds
}

schedule(async = true){
    // this will be executed asynchronously now
}

schedule(true, delay = 20){
    // this will be executed asynchronously after 20 seconds
}

schedule(true, period = 3, unit = TimeUnit.MINUTES){
    // this will be executed asynchronously each 3 minutes
}

// Tasks can cancel themselves
schedule(period = ...) {
    if(...) cancel()
}

Simplified commands

With KUtils

command("hello"){ args ->
    msg("&bHello!")
}

Without KUtils

// Bungee
proxy.pluginManager.registerCommand(this,
    object: Command("hello"){
        override fun execute(sender: CommandSender, args: Array<String>){
            sender.sendMessage("§bHello!")
        }
    }
)

// Bukkit
getCommand("hello").executor = CommandExecutor {
    sender, command, label, args ->
    sender.sendMessage("§bHello!")
    true;
}

Delegated configurations

You can use delegated configurations to manage big configurations

class MyPlugin: BukkitPlugin(){

    object MyConfig: ConfigFile("config"){
        var debug by boolean("debug")
        var alertMessage by string("alert-message")
        var enabledWorlds by stringList("enabled-worlds")
    }

    override fun onEnable(){
        // Initialize the object with the current file
        init(MyConfig) // (it will copy the resource config.yml to the data folder)
        // We can access properties dynamically
        info("debug: " + MyConfig.debug)
        // Change them (if "var" is used)
        MyConfig.debug = false
        // Save them (if auto-saving is disabled)
        MyConfig.save()
        // Reload the config from the file
        MyConfig.reload()
    }
}

More infos here

Simplified messages sending
Let's send a message to a player (any Player, ProxiedPlayer, CommandSender is supported)

With KUtils

player.msg("&6Hello world!")

Without KUtils

// BungeeCord
player.sendMessage(TextComponent("&6Hello world!".replace("&", "§")))

// Bukkit
player.sendMessage("&6Hello world!".replace("&", "§"))

Fast logging
You can use info(), warning() and severe() with String or Exception to log them in the console
You can also use logToFile() to write to a file named "log.txt" in your plugin's data folder

With KUtils

info("Hello world!")

Without KUtils

logger.info("Hello world!")

In Java

getLogger().info("Hello world!");

Type aliases for multi-platform plugins
If you want to specify the platform of any type, just add "Bungee" or "Bukkit" before its name


With KUtils

class MyPluginOnBungee: BungeePlugin(){
    override fun onEnable() = info("Hello Bungee!")
}

class MyPluginOnBukkit: BukkitPlugin(){
    override fun onEnable() = info("Hello Bukkit!")
}

Without KUtils: two separated files for each platform

Plugin updates checker
You can check for updates of your plugin using Spiget

Just use update() with your Spigot resource ID

update(15938)

You can specify the color

update(15938, LIGHT_PURPLE)

and the permission (the default permission is "rhaz.update")

update(15938, LIGHT_PURPLE, "myplugin.updates")


Advanced exception catching

Exceptions can be catched with a beautiful syntax

With KUtils

// This will catch any exception and log it as a warning
catch<Exception>(::warning){
    // ex() is a short replacement of Exception()
    throw ex("An error occured")
}

Without KUtils

try{
    throw Exception("An error occured")
} catch(ex: Exception){
    warning(ex)
}


Catch only specific exceptions

// The callback can be ommited, the default one is ::printStackTrace
catch<CommandException>{
    throw Exception("This won't be catched")
}

Catch and redirect to the sender

val sender: CommandSender = ...
catch<Exception>(sender::msg){
    if(sender !is Player)
        throw ex("&cYou're not a player!")
    sender.gamemode = GameMode.CREATIVE
    sender.msg("&bYou're now in creative mode :)")
}

Custom callbacks

// Tell the admins about the exception
val admins = server.onlinePlayers.filter{it.hasPermission("test.admin")}
fun tellToAdmins(ex: Exception) = admins.forEach{it.msg(ex)}

catch<Exception>(::tellToAdmins){
    throw ex("Alert!")
}

// Anonymous callback that prepend the warning with "An error occured"
catch<Exception>({ warning("An error occured: ${it.message}")}){
    throw ex(...)
}

Custom exceptions

class RedException(message: String): Exception(){
    override val message = "&c$message"
}

fun test(){
    catch<RedException>(::warning){
        throw RedException("This message will be red")
    }
}

Catching with result, callback and default value

fun default(ex: Exception) =
    {warning(ex); "This is the default message"}()

val msg = catch(::default){
    val line1 = read() ?: throw ex("Could not read first line")
    val line2 = read() ?: throw ex("Could not read second line")
    val line3 = read() ?: throw ex("Could not read third line")
    info("Sucessfully read three lines")
    "$line1, $line2, $line3"
    // Return the three lines separated by ","
}

// Will print "This is the default message"
// if one of the three lines could not be read
info("The message is: $msg")

And much more here

Similar projects

Plugin →