Skip to content

Commit

Permalink
GH-31 Update litecommands (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
imDMK committed Mar 8, 2024
1 parent 2ab3cb5 commit 96fcff9
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 133 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ This is a plugin that allows you to double-jump on the server.
![gif](assets/delay-streak.gif)

### Command permissions
| Command | Permission |
|:-------------------------|:---------------------------------|
| `doublejump` | command.doublejump |
| `doublejump -for` | command.doublejump.for |
| `doublejump item give` | command.doublejump.item.give |
| `doublejump item remove` | command.doublejump.item.remove |
| Command | Permission |
|:-------------------|:------------------------|
| `doublejump` | command.doublejump |
| `doublejump for` | command.doublejump.for |
| `doublejump item` | command.doublejump.item |

### Placeholder API formats
* `jump-player-delay` - Displays the remaining delay time until the next double jump can be used.
Expand Down
26 changes: 26 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
`java-library`
`maven-publish`
}

allprojects {
apply(plugin="java-library")
apply(plugin="maven-publish")

java {
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17
}

publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()

from(components["java"])
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.entity.Player;

import java.util.stream.Stream;

public class WorldGuardRegionProvider implements RegionProvider {

private final JumpRestriction regionRestriction;
Expand All @@ -20,9 +22,11 @@ public WorldGuardRegionProvider(JumpRestriction regionRestriction) {

@Override
public boolean isInAllowedRegion(Player player) {
Stream<String> regions = this.regionRestriction.list().stream();

return switch (this.regionRestriction.type()) {
case BLACKLIST -> this.regionRestriction.list().stream().anyMatch(region -> !this.isInRegion(player, region));
case WHITELIST -> this.regionRestriction.list().stream().anyMatch(region -> this.isInRegion(player, region));
case BLACKLIST -> regions.anyMatch(region -> !this.isInRegion(player, region));
case WHITELIST -> regions.anyMatch(region -> this.isInRegion(player, region));
};
}

Expand Down
3 changes: 1 addition & 2 deletions doublejump-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ dependencies {
implementation("net.kyori:adventure-platform-bukkit:4.3.1")
implementation("net.kyori:adventure-text-minimessage:4.14.0")

implementation("dev.rollczi.litecommands:core:2.8.9")
implementation("dev.rollczi.litecommands:bukkit-adventure:2.8.9")
implementation("dev.rollczi:litecommands-bukkit:3.4.0")

implementation("com.eternalcode:gitcheck:1.0.0")
implementation("org.bstats:bstats-bukkit:3.0.2")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.imdmk.doublejump;

import com.github.imdmk.doublejump.command.argument.PlayerArgument;
import com.github.imdmk.doublejump.command.handler.MissingPermissionHandler;
import com.github.imdmk.doublejump.command.context.PlayerContext;
import com.github.imdmk.doublejump.command.handler.NotificationHandler;
import com.github.imdmk.doublejump.command.handler.PermissionHandler;
import com.github.imdmk.doublejump.command.handler.UsageHandler;
import com.github.imdmk.doublejump.configuration.ConfigurationFactory;
import com.github.imdmk.doublejump.configuration.implementation.PluginConfiguration;
Expand Down Expand Up @@ -45,8 +46,7 @@
import com.github.imdmk.doublejump.update.UpdateService;
import com.google.common.base.Stopwatch;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.bukkit.adventure.platform.LiteBukkitAdventurePlatformFactory;
import dev.rollczi.litecommands.bukkit.tools.BukkitOnlyPlayerContextual;
import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Server;
Expand All @@ -65,6 +65,7 @@

public class DoubleJump implements DoubleJumpApi {

private final Plugin plugin;
private final Server server;

private final PluginConfiguration pluginConfiguration;
Expand Down Expand Up @@ -92,6 +93,7 @@ public DoubleJump(Plugin plugin) {
Logger logger = plugin.getLogger();
PluginDescriptionFile pluginDescriptionFile = plugin.getDescription();

this.plugin = plugin;
this.server = plugin.getServer();

/* Configuration */
Expand Down Expand Up @@ -168,7 +170,7 @@ public void disable() {
DoubleJumpApiProvider.unregister();

if (this.liteCommands != null) {
this.liteCommands.getPlatform().unregisterAll();
this.liteCommands.unregister();
}

if (this.placeholderRegistry != null) {
Expand All @@ -182,22 +184,23 @@ public void disable() {
}

private LiteCommands<CommandSender> registerLiteCommands() {
return LiteBukkitAdventurePlatformFactory.builder(this.server, "DoubleJump", false, this.bukkitAudiences, true)
.contextualBind(Player.class, new BukkitOnlyPlayerContextual<>("Only player can use this command."))
return LiteCommandsBukkit.builder("DoubleJump", this.plugin, this.server)
.settings(settings -> settings.nativePermissions(true))

.context(Player.class, new PlayerContext())
.argument(Player.class, new PlayerArgument(this.server, this.pluginConfiguration.notificationSettings))

.permissionHandler(new MissingPermissionHandler(this.pluginConfiguration.notificationSettings, this.notificationSender))
.resultHandler(Notification.class, new NotificationHandler(this.notificationSender))
.invalidUsageHandler(new UsageHandler(this.pluginConfiguration.notificationSettings, this.notificationSender))
.missingPermission(new PermissionHandler(this.pluginConfiguration.notificationSettings, this.notificationSender))
.result(Notification.class, new NotificationHandler(this.notificationSender))
.invalidUsage(new UsageHandler(this.pluginConfiguration.notificationSettings, this.notificationSender))

.commandInstance(
.commands(
new DoubleJumpCommand(this.pluginConfiguration.jumpSettings, this.notificationSender, this.jumpPlayerManager, this.jumpPlayerService, this.jumpRestrictionService),
new DoubleJumpForCommand(this.pluginConfiguration.jumpSettings, this.notificationSender, this.jumpPlayerService, this.jumpRestrictionService),
new DoubleJumpForCommand(this.pluginConfiguration.jumpSettings, this.notificationSender, this.jumpPlayerService),
new DoubleJumpItemCommand(this.pluginConfiguration.jumpSettings.itemSettings, this.notificationSender)
)

.register();
.build();
}

private void disableAllowFlightForOnlinePlayers() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.github.imdmk.doublejump.command.argument;

import com.github.imdmk.doublejump.notification.configuration.NotificationSettings;
import dev.rollczi.litecommands.argument.simple.OneArgument;
import dev.rollczi.litecommands.command.LiteInvocation;
import dev.rollczi.litecommands.suggestion.Suggestion;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import panda.std.Result;

import java.util.List;
import java.util.Optional;

public class PlayerArgument implements OneArgument<Player> {
public class PlayerArgument extends ArgumentResolver<CommandSender, Player> {

private final Server server;
private final NotificationSettings notificationSettings;
Expand All @@ -22,18 +25,16 @@ public PlayerArgument(Server server, NotificationSettings notificationSettings)
}

@Override
public Result<Player, ?> parse(LiteInvocation invocation, String argument) {
Optional<Player> playerOptional = Optional.ofNullable(this.server.getPlayerExact(argument));

return playerOptional.map(Result::ok)
.orElseGet(() -> Result.error(this.notificationSettings.playerNotFound));
protected ParseResult<Player> parse(Invocation<CommandSender> invocation, Argument<Player> context, String argument) {
return Optional.ofNullable(this.server.getPlayerExact(argument))
.map(ParseResult::success)
.orElseGet(() -> ParseResult.failure(this.notificationSettings.playerNotFound));
}

@Override
public List<Suggestion> suggest(LiteInvocation invocation) {
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<Player> argument, SuggestionContext context) {
return this.server.getOnlinePlayers().stream()
.map(Player::getName)
.map(Suggestion::of)
.toList();
.map(HumanEntity::getName)
.collect(SuggestionResult.collector());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.imdmk.doublejump.command.context;

import dev.rollczi.litecommands.context.ContextProvider;
import dev.rollczi.litecommands.context.ContextResult;
import dev.rollczi.litecommands.invocation.Invocation;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class PlayerContext implements ContextProvider<CommandSender, Player> {

@Override
public ContextResult<Player> provide(Invocation<CommandSender> invocation) {
if (invocation.sender() instanceof Player player) {
return ContextResult.ok(() -> player);
}

return ContextResult.error("Only player can use this command.");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.github.imdmk.doublejump.notification.Notification;
import com.github.imdmk.doublejump.notification.NotificationSender;
import dev.rollczi.litecommands.command.LiteInvocation;
import dev.rollczi.litecommands.handle.Handler;
import dev.rollczi.litecommands.handler.result.ResultHandler;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invocation.Invocation;
import org.bukkit.command.CommandSender;

public class NotificationHandler implements Handler<CommandSender, Notification> {
public class NotificationHandler implements ResultHandler<CommandSender, Notification> {

private final NotificationSender notificationSender;

Expand All @@ -15,7 +16,9 @@ public NotificationHandler(NotificationSender notificationSender) {
}

@Override
public void handle(CommandSender sender, LiteInvocation invocation, Notification notification) {
public void handle(Invocation<CommandSender> invocation, Notification notification, ResultHandlerChain<CommandSender> resultHandlerChain) {
CommandSender sender = invocation.sender();

this.notificationSender.send(sender, notification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.imdmk.doublejump.command.handler;

import com.github.imdmk.doublejump.notification.NotificationSender;
import com.github.imdmk.doublejump.notification.configuration.NotificationSettings;
import com.github.imdmk.doublejump.text.Formatter;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.permission.MissingPermissions;
import dev.rollczi.litecommands.permission.MissingPermissionsHandler;
import org.bukkit.command.CommandSender;

public class PermissionHandler implements MissingPermissionsHandler<CommandSender> {

private final NotificationSettings notificationSettings;
private final NotificationSender notificationSender;

public PermissionHandler(NotificationSettings notificationSettings, NotificationSender notificationSender) {
this.notificationSettings = notificationSettings;
this.notificationSender = notificationSender;
}

@Override
public void handle(Invocation<CommandSender> invocation, MissingPermissions missingPermissions, ResultHandlerChain<CommandSender> resultHandlerChain) {
CommandSender sender = invocation.sender();
Formatter formatter = new Formatter()
.placeholder("{PERMISSIONS}", missingPermissions.getPermissions());

this.notificationSender.send(sender, this.notificationSettings.missingPermissions, formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.github.imdmk.doublejump.notification.NotificationSender;
import com.github.imdmk.doublejump.notification.configuration.NotificationSettings;
import com.github.imdmk.doublejump.text.Formatter;
import dev.rollczi.litecommands.command.LiteInvocation;
import dev.rollczi.litecommands.handle.InvalidUsageHandler;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invalidusage.InvalidUsage;
import dev.rollczi.litecommands.invalidusage.InvalidUsageHandler;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.schematic.Schematic;
import org.bukkit.command.CommandSender;

Expand All @@ -19,7 +21,10 @@ public UsageHandler(NotificationSettings notificationSettings, NotificationSende
}

@Override
public void handle(CommandSender sender, LiteInvocation invocation, Schematic schematic) {
public void handle(Invocation<CommandSender> invocation, InvalidUsage<CommandSender> commandSenderInvalidUsage, ResultHandlerChain<CommandSender> resultHandlerChain) {
CommandSender sender = invocation.sender();
Schematic schematic = commandSenderInvalidUsage.getSchematic();

if (schematic.isOnlyFirst()) {
Formatter formatter = new Formatter()
.placeholder("{USAGE}", schematic.first());
Expand All @@ -30,7 +35,7 @@ public void handle(CommandSender sender, LiteInvocation invocation, Schematic sc

this.notificationSender.send(sender, this.notificationSettings.invalidUsageListFirst);

for (String schema : schematic.getSchematics()) {
for (String schema : schematic.all()) {
Formatter formatter = new Formatter()
.placeholder("{USAGE}", schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.github.imdmk.doublejump.jump.JumpSettings;
import com.github.imdmk.doublejump.jump.restriction.JumpRestrictionService;
import com.github.imdmk.doublejump.notification.NotificationSender;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.permission.Permission;
import dev.rollczi.litecommands.command.route.Route;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.entity.Player;

@Route(name = "doublejump")
@Command(name = "doublejump")
@Permission("command.doublejump")
public class DoubleJumpCommand {

Expand All @@ -28,8 +29,8 @@ public DoubleJumpCommand(JumpSettings jumpSettings, NotificationSender notificat
this.jumpRestrictionService = jumpRestrictionService;
}

@Execute(required = 0)
void execute(Player player) {
@Execute
void execute(@Context Player player) {
if (this.jumpRestrictionService.isPassedRestrictions(player, true)) {
return;
}
Expand Down

0 comments on commit 96fcff9

Please sign in to comment.