Skip to content

Commit

Permalink
add: AlpinePlugin#setInvalidCommandUseHandler
Browse files Browse the repository at this point in the history
grants developers the ability to use their own invalid usage handler
  • Loading branch information
BestBearr committed Dec 11, 2024
1 parent 372b078 commit 8566809
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/java/co/crystaldev/alpinecore/AlpineCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import co.crystaldev.alpinecore.event.ServerTickEvent;
import co.crystaldev.alpinecore.framework.command.AlpineArgumentResolver;
import co.crystaldev.alpinecore.framework.teleport.AlpineTeleportHandler;
import dev.rollczi.litecommands.invalidusage.InvalidUsageHandler;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.server.PluginDisableEvent;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -34,6 +37,9 @@ public final class AlpineCore extends AlpinePlugin {

private final Map<AlpinePlugin, Set<AlpineArgumentResolver<?>>> argumentResolvers = new HashMap<>();

@Getter @Setter
private InvalidUsageHandler<CommandSender> invalidCommandUsageHandler;

@Override
public void onStart() {
ServerTickEvent event = new ServerTickEvent();
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/co/crystaldev/alpinecore/AlpinePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import co.crystaldev.alpinecore.framework.storage.SerializerRegistry;
import co.crystaldev.alpinecore.framework.teleport.TeleportManager;
import co.crystaldev.alpinecore.framework.ui.UIManager;
import co.crystaldev.alpinecore.handler.CommandInvalidUsageHandler;
import co.crystaldev.alpinecore.handler.InvalidCommandUsageHandler;
import co.crystaldev.alpinecore.integration.PlaceholderIntegration;
import co.crystaldev.alpinecore.integration.VaultIntegration;
import co.crystaldev.alpinecore.util.ChatColor;
Expand All @@ -29,6 +29,7 @@
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
import dev.rollczi.litecommands.bukkit.LiteBukkitMessages;
import dev.rollczi.litecommands.bukkit.LiteBukkitSettings;
import dev.rollczi.litecommands.invalidusage.InvalidUsageHandler;
import dev.rollczi.litecommands.message.LiteMessages;
import dev.rollczi.litecommands.schematic.SchematicFormat;
import lombok.Getter;
Expand Down Expand Up @@ -301,6 +302,18 @@ public long getCurrentTick() {
return AlpineCore.TICK_COUNTER.get();
}

/**
* Sets a global handler for managing invalid command usage.
* <p>
* This handler is shared globally across all {@link AlpinePlugin}
* implementations and will override any previously set handler.
*
* @param handler The {@link InvalidUsageHandler} to handle invalid command usage.
*/
public void setInvalidCommandUseHandler(@Nullable InvalidUsageHandler<CommandSender> handler) {
AlpineCore.getInstance().setInvalidCommandUseHandler(handler);
}

/**
* Logs an information message with color formatting.
*
Expand Down Expand Up @@ -436,7 +449,7 @@ private void setupCommandManager() {
.commands((Object[]) commands)

// Input our configurable messages
.invalidUsage(new CommandInvalidUsageHandler(this))
.invalidUsage(new InvalidCommandUsageHandler(this))
.message(LiteMessages.MISSING_PERMISSIONS, permission -> messages.missingPermissions.buildString(this, "permission", permission))
.message(LiteMessages.INVALID_NUMBER, input -> messages.invalidNumber.buildString(this, "input", input))
.message(LiteMessages.INSTANT_INVALID_FORMAT, input -> messages.invalidInstant.buildString(this, "input", input))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.crystaldev.alpinecore.handler;

import co.crystaldev.alpinecore.AlpineCore;
import co.crystaldev.alpinecore.AlpinePlugin;
import co.crystaldev.alpinecore.framework.config.AlpinePluginConfig;
import co.crystaldev.alpinecore.util.Messaging;
Expand All @@ -16,12 +17,19 @@
* @since 0.2.0
*/
@RequiredArgsConstructor
public final class CommandInvalidUsageHandler implements InvalidUsageHandler<CommandSender> {
public final class InvalidCommandUsageHandler implements InvalidUsageHandler<CommandSender> {

private final AlpinePlugin plugin;

@Override
public void handle(Invocation<CommandSender> invocation, InvalidUsage<CommandSender> result, ResultHandlerChain<CommandSender> chain) {

InvalidUsageHandler<CommandSender> handler = AlpineCore.getInstance().getInvalidCommandUsageHandler();
if (handler != null) {
handler.handle(invocation, result, chain);
return;
}

AlpinePluginConfig config = this.plugin.getAlpineConfig();
CommandSender sender = invocation.sender();
Schematic command = result.getSchematic();
Expand Down

0 comments on commit 8566809

Please sign in to comment.