Java Developer Instructions - HoshiKurama/MCWandsFramework GitHub Wiki

NOTE: Before beginning this section, be sure you have already looked at the README.md!

While MCWandsFramework is written in Kotlin, Java users were never once forgotten about! After all, most minecraft developers use Java!

Gradle

Before beginning, this page assumes you use Gradle, specifically the Groovy syntax for gradle. If you use the Kotlin DSL syntax for Gradle, take a look at the Kotlin page! If you use Maven, I strongly recommended changing to Gradle. While it's most likely possible to do the following things in Maven, you are on your own.

To get the API ready for use:

  1. Download the API jar file. This can be found in the releases section with every x.0.0 release. The release title will also mention the API.
  2. Drag the API jar into the root project.

build.gradle

// NOTE: This file does not include code already expected to be here
plugins {
    id 'java'
}

dependencies {
    compileOnly files('Name_Of_API_JAR.jar')    // Dependency for API jar placed in the root project
}

You're now ready to create custom wands!

Service and Registration

Service is provided via the ServicesManager API.

RegisteredServiceProvider<MCWandsService> rsp = Bukkit.getServicesManager().getRegistration(MCWandsService.class);
MCWandsService wandsService;

if (rsp != null) wandsService = rsp.getProvider();

Wand behaviour is defined with Consumer<WandData>. WandData contains the following fields (getters are provided):

  • player - Player using cooldown
  • cooldown - Specialty cooldown in seconds
  • intensity - Multi-purpose value representing the strength of the specialty action
  • range - Multi-purpose value representing the range of the specialty action
// Example action that tells player hello
Consumer<WandData> helloKnownPlayer = params -> {
    Player player = params.getPlayer();
    player.sendMessage("Hello, " + player.getName() + "!");
};  

Wands are registered using the MCWandsService#registerWandJava() method, which takes a wand name and a Consumer<WandData object. The function returns a boolean describing if registration was successful or not (when attempted). You cannot register a wand with a name that has already been registered.

if (wandsService != null) {
    // You can define the behaviour at registration...
    wandsService.registerWandJava("HelloUnknownPlayer", params -> {
        val player = params.getPlayer();
        player.sendMessage("Hello, random player!");
    });

    // ... or you may use other Consumer<WandData> objects!
    wandsService.registerWandJava("HelloKnownPlayer", helloKnownPlayer);
}

Java Optionals are a more elegant solution to getting the provider and registering wands.

Optional.ofNullable(Bukkit.getServicesManager().getRegistration(MCWandsService.class))
    .map(RegisteredServiceProvider::getProvider)
    .ifPresent( wandsService -> {
        wandsService.registerWandJava("HelloKnownPlayer", helloKnownPlayer);    // Register wand from Consumer
        wandsService.registerWandJava("HelloUnknownPlayer", params -> {         // Register wand and create Consumer at same place
            val player = params.getPlayer();
            player.sendMessage("Hello, random player!");
       });
    });

NOTE: Please make sure to list MCWandsFramework as a dependency in your plugin.yml!

⚠️ **GitHub.com Fallback** ⚠️