Library that increases productivity in the production of spigot plugins.
Add the JitPack repository to your build file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add the dependency:
<dependency>
<groupId>com.github.unldenis</groupId>
<artifactId>UnldenisHelper</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
Classic use of the Spigotmc-api:
//imports
public class PlayerListeners implements Listener {
@EventHandler
public void onMove(PlayerMoveEvent event) {
if(!e.getPlayer().hasPermission("plugin.admin")) return;
if(e.getFrom().getX() != e.getTo().getX() || e.getFrom().getZ() != e.getTo().getZ()) return;
event.getPlayer().sendMessage("You moved an entire block");
}
}
/*
And in the main class:
*/
getServer().getPluginManager().registerEvents(new PlayerListeners(), this);
Using my library, these few lines of code will suffice for you to insert into any method!
Events.subscribe(PlayerMoveEvent.class)
.filter(e -> !e.getPlayer().hasPermission("plugin.admin"))
.filter(e -> e.getFrom().getX() != e.getTo().getX() || e.getFrom().getZ() != e.getTo().getZ())
.handler(event -> {
event.getPlayer().sendMessage("You moved an entire block");
}).bindWith(plugin);
To handle async and sync events the Bukkit API provides nothing less than a runnable. BukkitFuture allows you to return a new CompletableFuture that is completed by Bukkit schedule, which will return a value at the end of a task. Moreover, thanks to CompletableFuture you will be able to execute more tasks sequentially.
The method sync it is used to execute the biconsumer of the whenComplete synchronously so to be able to use the bukkit API.
//The bindWith method must be called once to register your plugin instance.
BukkitFuture.bindWith(plugin);
findPlayer()
.thenCompose(playerName -> getPlayTime(playerName))
.whenComplete(sync((playtime, t) -> {
if(t!=null) {
// handle possible errors
Bukkit.broadcastMessage(t.getMessage());
return;
}
Bukkit.broadcastMessage("Playtime sync is " + playtime);
}));
public CompletableFuture<String> findPlayer() {
return BukkitFuture.supplyAsync(()-> {
// load name from database
return "unldenis";
});
}
public CompletableFuture<Integer> getPlayTime(String player) {
return BukkitFuture.supplyAsync(()-> {
if(player==null || player.isEmpty()) throw new IllegalArgumentException("Player is invalid");
// load stat from database
return 0;
});
}
Creating commands also allows you to avoid boilerplate code.
Commands.create("hi").handler(((sender, args) -> {
if(sender instanceof Player player && player.hasPermission("plugin.admin")) {
player.sendMessage("Hi " + player.getName());
}
})).bindWith(plugin);