Skip to content

Commit

Permalink
improvement: migrated to different command framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-EmPee committed Jul 26, 2024
1 parent 01943bf commit 226b396
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 186 deletions.
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ tasks.runServer {
tasks.shadowJar {
isEnableRelocation = isRelease()
relocationPrefix = project.group.toString()

relocate("net.kyori.adventure", "$relocationPrefix.net.kyori.adventure")
}

fun isRelease(): Boolean {
Expand Down
3 changes: 1 addition & 2 deletions plugin/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ plugins {

dependencies {
compileOnly("com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT")
compileOnly("com.mojang:brigadier:1.0.18")
compileOnly("com.comphenix.protocol:ProtocolLib:4.8.0")

implementation(project(":plugin:utils"))

implementation("io.github.mr-empee:lightwire:0.0.3")
implementation("io.github.mr-empee:colonel:0.0.4")
implementation("io.github.mr-empee.command-forge:bukkit:0.0.1-SNAPSHOT")
implementation("io.github.mr-empee:easy-gui:0.0.3")
implementation("io.github.mr-empee:item-builder:0.0.2")

Expand Down
20 changes: 12 additions & 8 deletions plugin/core/src/main/java/core/MysticalBarriers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@

public class MysticalBarriers extends JavaPlugin {

public static final String COMMAND = "mb";
private final LightWire iocContainer = LightWire.of(getClass().getPackage());
private static LightWire IOC;

@Override
public void onEnable() {
EasyGUI.init(this);

iocContainer.addComponent(this);
iocContainer.addComponent(new BukkitSyncedExecutor(this));
iocContainer.addComponent(new BukkitAsyncExecutor(this));
IOC = LightWire.of(getClass().getPackage());
IOC.addComponent(this);
IOC.addComponent(new BukkitSyncedExecutor(this));
IOC.addComponent(new BukkitAsyncExecutor(this));

iocContainer.load();
IOC.load();
}

public void reload() {
for (IReloadable reloadable : iocContainer.getInstances(IReloadable.class)) {
for (IReloadable reloadable : IOC.getInstances(IReloadable.class)) {
reloadable.reload();
}
}

@Override
public void onDisable() {
for (Closeable closeable : iocContainer.getInstances(Closeable.class)) {
for (Closeable closeable : IOC.getInstances(Closeable.class)) {
try {
closeable.close();
} catch (Exception e) {
Expand All @@ -55,4 +55,8 @@ public boolean isDevelop() {
return getDescription().getVersion().endsWith("-SNAPSHOT");
}

public static <T> T getInstance(Class<T> clazz) {
return IOC.getInstances(clazz).get(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,28 @@
public class PluginConfig extends ResourceConfig {

public PluginConfig(MysticalBarriers plugin) {
super(plugin, "configs/config.yml", plugin.isDevelop(), List.of());
super(plugin, "configs/config.yml", plugin.isDevelop(), List.of(
fromV2ToV3()
));

Messenger.setPrefix(getPrefix());
}

private static Migrator fromV2ToV3() {
return (currentVersion, config) -> {
if (currentVersion > 2) {
return currentVersion;
}

String prefix = config.getString("messages.prefix");
Component prefixComponent = LegacyComponentSerializer.legacy('&').deserialize(prefix);

config.set("messages.prefix", MiniMessage.miniMessage().serialize(prefixComponent));

return 3;
};
}

public boolean blockChorusTp() {
return getConfig().getBoolean("block-chorus-teleportation");
}
Expand Down
99 changes: 65 additions & 34 deletions plugin/core/src/main/java/core/configs/server/CommandsConfig.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,82 @@
package core.configs.server;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.empee.colonel.BrigadierCommand;
import io.github.empee.colonel.BrigadierManager;
import com.github.empee.commands.CommandContext;
import com.github.empee.commands.CommandManager;
import com.github.empee.commands.CommandNode;
import com.github.empee.commands.exceptions.ArgumentException;
import com.github.empee.commands.exceptions.CommandException;
import com.github.empee.commands.spigot.BukkitInjector;
import core.configs.client.resources.MessagesConfig;
import core.controllers.commands.ICommand;
import io.github.empee.lightwire.annotations.LightWired;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import core.configs.client.resources.MessagesConfig;
import core.exceptions.PluginException;
import utils.Messenger;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@LightWired
public class CommandsConfig extends BrigadierManager<CommandSender> {
private final MessagesConfig messages;

public CommandsConfig(
JavaPlugin plugin, MessagesConfig messages,
List<BrigadierCommand<CommandSender>> commands
) {
super(plugin, new CommandDispatcher<>());

this.messages = messages;
commands.forEach(c -> {
var registeredLabel = register(c.get());
Messenger.log("The command '{}' has been registered", registeredLabel);
});
public class CommandsConfig {

CommandManager<CommandSender> commandManager = new CommandManager<>(CommandSender.class);

private final JavaPlugin plugin;
private final MessagesConfig messagesConfig;
private final List<ICommand> commands;

public CommandsConfig(JavaPlugin plugin, MessagesConfig messagesConfig, List<ICommand> commands) {
this.messagesConfig = messagesConfig;
this.plugin = plugin;
this.commands = commands;

registerExceptionHandler();
registerCommands();
}

@Override
@SneakyThrows
protected void handleException(CommandSender source, Exception e) {
if (e instanceof PluginException) {
Messenger.log(source, messages.get(((PluginException) e).getId(), ((PluginException) e).getArguments()));
} else if (e instanceof CommandSyntaxException) {
Messenger.log(source, e.getMessage());
} else {
Messenger.log(source, messages.get("errors.internal_error"));
Messenger.log("Error while executing command", e);
private void registerCommands() {
BukkitInjector injector = new BukkitInjector(plugin, commandManager);

for (ICommand command : commands) {
CommandNode<? extends CommandSender> cmd = command.get();

commandManager.register(cmd);
injector.inject(command.get());
}
}

private void registerExceptionHandler() {
commandManager.setExceptionHandler(this::handleCommandException);
}

public void handleCommandException(CommandContext<CommandSender> context, CommandException exception) {
if (exception.getType() == CommandException.Type.ARG_PARSING_ERROR) {
handleCommandArgumentException(context, (ArgumentException) exception);
return;
}

if (exception.getType() == CommandException.Type.EXECUTION_ERROR) {
Messenger.error("Error while executing command '/{}'", exception.getCause(), context.getReader().getText());
}

Messenger.log(context.getSource(), messagesConfig.get("commands.errors." + exception.getType()));
}

@Override
protected CommandSender getSource(CommandSender source) {
return source;
private void handleCommandArgumentException(CommandContext<CommandSender> context, ArgumentException exception) {
String message;

if (exception.getCauseID() == null) {
message = messagesConfig.get("commands.errors.arg_parsing_error.unk");
Messenger.error("Error while parsing argument '{}' of command '/{}'", exception.getCause(), exception.getInput(), context.getReader().getText());
} else {
var placeholders = Map.of("input", (Object) exception.getInput());
message = messagesConfig.get("commands.errors.arg_parsing_error." + exception.getCauseID(), placeholders);
}

Messenger.log(context.getSource(), message);
}

}
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@
package core.controllers.commands;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import core.controllers.commands.arguments.BarrierArgumentType;
import core.controllers.guis.BarrierEditGUI;
import io.github.empee.colonel.BrigadierCommand;
import io.github.empee.lightwire.annotations.LightWired;
import lombok.RequiredArgsConstructor;
import org.bukkit.command.CommandSender;
import com.github.empee.commands.CommandNode;
import com.github.empee.commands.arguments.StringArgument;
import core.MysticalBarriers;
import core.controllers.commands.arguments.BarrierArgument;
import core.controllers.guis.BarrierEditGUI;
import core.controllers.guis.BarrierListGUI;
import core.controllers.guis.PluginGUI;
import core.model.Barrier;
import core.registries.Permissions;
import core.items.RegionSelectorWand;
import core.registries.Permissions;
import core.services.BarriersService;
import io.github.empee.lightwire.annotations.LightWired;
import lombok.RequiredArgsConstructor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import utils.Messenger;

@LightWired
@RequiredArgsConstructor
public class BarriersCommand extends BrigadierCommand<CommandSender> {
public class BarriersCommand implements ICommand {

private final MysticalBarriers plugin;
private final RegionSelectorWand selectionWand;
private final BarriersService barriersService;

@Override
public LiteralArgumentBuilder<CommandSender> get() {
return literal(MysticalBarriers.COMMAND)
.requires(s -> s.hasPermission(Permissions.ADMIN))
.then(wand())
.then(create())
.then(modify())
.then(list())
.then(reload());
public CommandNode<CommandSender> get() {
return CommandNode.of("mysticalbarriers", CommandSender.class)
.withPermission(s -> s.hasPermission(Permissions.ADMIN))
.withAliases("mb")
.withChild(wand())
.withChild(create())
.withChild(modify())
.withChild(list())
.withChild(reload());
}

public ArgumentBuilder<CommandSender, ?> wand() {
return node(literal("wand"))
.executes(c -> {
var player = player(c);
public CommandNode<Player> wand() {
return CommandNode.of("wand", Player.class)
.withExecutor(c -> {
Player player = c.getSource();

player.getInventory().addItem(selectionWand.get());
Messenger.log(player, "&aSelection wand given");
}).build();
});
}

public ArgumentBuilder<CommandSender, ?> modify() {
return node(literal("edit"), arg("barrier", BarrierArgumentType.barrier(barriersService)))
.executes(c -> PluginGUI.get(BarrierEditGUI.class).open(player(c), c.getArgument("barrier", Barrier.class)))
.build();
public CommandNode<Player> modify() {
return CommandNode.of("edit", Player.class)
.withArgs(BarrierArgument.of("barrier"))
.withExecutor(c -> {
PluginGUI.get(BarrierEditGUI.class).open(c.getSource(), c.get("barrier"));
});
}

public ArgumentBuilder<CommandSender, ?> list() {
return node(literal("list"))
.executes(c -> PluginGUI.get(BarrierListGUI.class).open(player(c)))
.build();
public CommandNode<Player> list() {
return CommandNode.of("list", Player.class)
.withExecutor(c -> {
PluginGUI.get(BarrierListGUI.class).open(c.getSource());
});
}

public ArgumentBuilder<CommandSender, ?> create() {
return node(literal("create"), arg("id", StringArgumentType.string()))
.executes(c -> {
var player = player(c);
var id = c.getArgument("id", String.class);
public CommandNode<Player> create() {
return CommandNode.of("create", Player.class)
.withArgs(StringArgument.of("id"))
.withExecutor(c -> {
Player player = c.getSource();
String id = c.get("id");

if (barriersService.findById(id).isPresent()) {
Messenger.log(player, "&cA barrier with that id already exists");
return;
Expand All @@ -78,13 +82,16 @@ public LiteralArgumentBuilder<CommandSender> get() {
selectionWand.invalidate(player.getUniqueId());

Messenger.log(player, "&aBarrier created");
}).build();
});
}

public ArgumentBuilder<CommandSender, ?> reload() {
return node(literal("reload"))
.executes(c -> plugin.reload())
.build();
public CommandNode<CommandSender> reload() {
return CommandNode.of("reload", CommandSender.class)
.withExecutor(c -> {
plugin.reload();

Messenger.log(c.getSource(), "&aThe plugin has been reloaded");
});
}

}
10 changes: 10 additions & 0 deletions plugin/core/src/main/java/core/controllers/commands/ICommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.controllers.commands;

import com.github.empee.commands.CommandNode;
import org.bukkit.command.CommandSender;

public interface ICommand {

CommandNode<? extends CommandSender> get();

}
Loading

0 comments on commit 226b396

Please sign in to comment.