Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unknown command message #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.thenextlvl.commander.paper;

import core.file.FileIO;
import core.file.format.GsonFile;
import core.i18n.file.ComponentBundle;
import core.io.IO;
import lombok.Getter;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.thenextlvl.commander.Commander;
import net.thenextlvl.commander.paper.command.CommanderCommand;
import net.thenextlvl.commander.paper.config.PluginConfig;
import net.thenextlvl.commander.paper.implementation.PaperCommandFinder;
import net.thenextlvl.commander.paper.implementation.PaperCommandRegistry;
import net.thenextlvl.commander.paper.implementation.PaperPermissionOverride;
Expand Down Expand Up @@ -38,6 +42,10 @@ public class CommanderPlugin extends JavaPlugin implements Commander {
TagResolver.standard(),
Placeholder.component("prefix", bundle.component(Locale.US, "prefix"))
)).build());
private final FileIO<PluginConfig> config = new GsonFile<>(
IO.of(getDataFolder(), "config.json"),
new PluginConfig(true)
).validate().save();

private final PaperCommandFinder commandFinder = new PaperCommandFinder(this);
private final PaperCommandRegistry commandRegistry = new PaperCommandRegistry(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class ReloadCommand {
private int reload(CommandContext<CommandSourceStack> context) {
var sender = context.getSource().getSender();
try {
var config = reloadConfig();
var commands = plugin.commandRegistry().reload(sender);
var permissions = plugin.permissionOverride().reload(sender);
var success = commands || permissions;
var success = config || commands || permissions;
if (success && sender instanceof Player player) player.updateCommands();
var message = success ? "command.reload.success" : "nothing.changed";
plugin.bundle().sendMessage(sender, message);
Expand All @@ -37,4 +38,9 @@ private int reload(CommandContext<CommandSourceStack> context) {
return 0;
}
}

private boolean reloadConfig() {
var previous = plugin.config().getRoot();
return !plugin.config().reload().getRoot().equals(previous);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.thenextlvl.commander.paper.config;

import com.google.gson.annotations.SerializedName;
import org.jspecify.annotations.NullMarked;

@NullMarked
public record PluginConfig(
@SerializedName("unknown-command-message") boolean unknownCommandMessage
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.command.UnknownCommandEvent;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.jspecify.annotations.NullMarked;

Expand All @@ -19,4 +20,16 @@ public void onCommandSend(PlayerCommandSendEvent event) {
if (event.getPlayer().permissionValue("commander.bypass").equals(TriState.TRUE)) return;
event.getCommands().removeAll(plugin.commandRegistry().hiddenCommands());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onUnknownCommand(UnknownCommandEvent event) {
if (!plugin.config().getRoot().unknownCommandMessage()) return;
var split = event.getCommandLine().split(" ", 2);
var command = plugin.getServer().getCommandMap().getCommand(split[0]);
if (command != null
&& !event.getCommandLine().equals(split[0])
&& command.testPermissionSilent(event.getSender()))
return;
event.message(plugin.bundle().component(event.getSender(), "command.unknown"));
}
}
Loading