Skip to content

Java Usage

Jaimss edited this page Jul 24, 2020 · 2 revisions

Java Usage

mcutils is written in Kotlin, and it makes use of Kotlin's extension functions.

If you are using kotlin, this allows for quick extensions of things like Players so you can do stuff like CommandSender#send(message) and message will be colorized correctly.

When using java however, it won't accept the syntax for extension functions. Extension functions compile to static receiver methods. This means that you will use the class that the method extends as the first parameter.

Examples:

For these examples, I will be using the CommandSender#send extension. The documentation can be found here.

In most cases with this library, I use CommandSender instead of player, because you can call the CommandSender methods on a player, but you can't call the Player methods on a CommandSender.

First way to include a method.

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static dev.jaims.mcutils.BukkitUtilKt.send;

public class SomeClass {
    // some method that will at some point send a message to a player
    void someMethod (Player player) {
        send(player, "This is the message", null);
    }   
    // this will send a message to a commandsender using the same send extension function from mcutils
    void someOtherMethod(CommandSender sender) {
        send(sender, "Message here", null);
    }   
    // null is a default for this method in kotlin, but java doesn't accept defaults, so you need to either provide 
    // null, or provide a player if you would like a player to be used for placeholders
}

Second Way:

// everything is the same, you just don't import statically, you use the name of the class followed by Kt then call the method
// for this we will imagine that both `player` and `sender` are valid variables for a `Player` and `CommandSender` respectively.
BukkitUtilKt.send(player, "&aMessage", null)
BukkitUtilKt.send(sender, "&bOther Message", null)

That is how you can make use of the extension functions in this library if you choose to use java instead of Kotlin for your plugins.

I would recommend using Kotlin (or at least giving it a try), because it makes things like this far easier and quicker to type.

Full examples in Kotlin can be found here. Just remember if it is an extension function like Player#doSomething you will have to call it in java as ClassKt.doSomething(Player /*other params*/).

Clone this wiki locally