Skip to content

KotlinPlugin

Wittawat Manha edited this page Sep 8, 2020 · 2 revisions

KotlinPlugin

public abstract class KotlinPlugin extends JavaPlugin

this plugin is based on kotlin so tutorial will be kotlin

Features

to make plugin using KotlinPlugin

// kotlin
// old
class PluginName : JavaPlugin(){}

// new
class PluginName : com.wireless4024.mc.bukcore.api.KotlinPlugin(){}

Logging

// old
val plugin:JavaPlugin = ... // or somehow

plugin.logger.fine(String.valueOf(plugin))
plugin.logger.info(String.valueOf(1234))
plugin.logger.warning(String.valueOf(5678))
plugin.logger.warning(arrayOf(1,2,3,4).joinToString()) // yea need to join into a string

// new
val plugin:KotlinPlugin = ... // or somehow

plugin.log(plugin)
plugin.info(1234)
plugin.warning(5678)
plugin.warning(arrayOf(1,2,3,4)) // it's will print [1, 2, 3, 4]

Managing commands

if you didn't use CommandBase you will need to register command to KotlinPlugin

plugin:KotlinPlugin = ...

plugin.registerCommand("command-name", plugin.getCommand("command-name"))

via CommandBase is easy as

CommandName(plugin).register()
// just single line 👍 

if command is registered into kotlin plugin here are what you can do

plugin:KotlinPlugin = ...
// plugin.enableCommand(name:String)
// plugin.disableCommand(name:String)

// plugin.registerCommand(name:String, command:Command)
// plugin.unregisterCommand(name:String, purge:Boolean)


plugin.disableCommand("command-name")
// if you use /command-name in game it's will unknown command

plugin.enableCommand("command-name")
// if you use /command-name in game it's will be back 💯 

plugin.unregisterCommand("command-name")
// if you use /command-name in game it's will unknown command

plugin.enableCommand("command-name")
// if you use /command-name in game it's will be back lol?

plugin.enableAllCommand()
// to enable all disabled command

get file in config folder

// old
File("plugin/${plugin.name}/file-name.whatever")

// new
plugin:KotlinPlugin = ...
plugin.getFile("file-name.whatever")

to run a task

arguments are moved to front of a task because you'll able to use closure with it

val plugin:KotlinPlugin = ...
val runnable = object :BukkitRunnable(){ // you can't use closure with abstract class
	override fun run() {
		// do whatever
	}
}
val closure = {
	// do whatever
}

/* to run a task as soon as possible */
// old
runnable.runTask(plugin)
// new
plugin.runTask(closure)
// or
plugin.runTask { /* do whatever */ }
// or
plugin { /* do whatever */ }
/* --------------------------------- */

/* to run a task as soon as possible asynchronously */
// old
runnable.runTaskAsynchronously(plugin)
// new
plugin.runAsync(closure)
// or
plugin.runAsync { /* do whatever */ }
/* ------------------------------------------------ */

/* to run a task later */
val delay = 10 // tick
// old
runnable.runTaskLater(plugin, delay)
// new
plugin.runTask(delay, closure)
// or
plugin.runTask(delay){ /* do whatever */ }
// or
plugin(delay) { /* do whatever */ }
/* ------------------- */

/* to run a task later asynchronously */
// old
runnable.runTaskLaterAsynchronously(plugin, delay)
// new
plugin.runAsync(delay, closure)
// or
plugin.runAsync(delay){ /* do whatever */ }
/* ---------------------------------- */

/* to run a task timer */
val period = 10 // 10 tick
// old
runnable.runTaskTimer(plugin, delay, period)
// new
plugin.runTimer(delay, period, closure)
// or
plugin.runTimer(delay, period){ /* do whatever */ }
/* ------------------- */

/* to run a task timer asynchronously */
runnable.runTaskTimerAsynchronously(plugin, delay, period) // very long
// new
plugin.runTimerAsync(delay, period, closure)
// or
plugin.runTimerAsync(delay, period){ /* do whatever */ }
/* ---------------------------------- */

// only with kotlin plugin
// if this is running in async thread
val location:Location = ...
val higestBlock:Block = plugin.call {
	// a task that needed to call in main thread
	// eg get block
	location.world.getHighestBlockAt(location)
}

Clone this wiki locally