Skip to content

Commit

Permalink
Refactor command registration
Browse files Browse the repository at this point in the history
  • Loading branch information
md5sha256 committed Mar 1, 2024
1 parent 37328ac commit 9f65f55
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
7 changes: 4 additions & 3 deletions AreaShop/src/main/java/me/wiefferink/areashop/AreaShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.papermc.lib.PaperLib;
import me.wiefferink.areashop.adapters.platform.MinecraftPlatform;
import me.wiefferink.areashop.adapters.platform.paper.PaperPlatform;
import me.wiefferink.areashop.commands.cloud.AreashopCloudCommands;
import me.wiefferink.areashop.extensions.AreashopExtension;
import me.wiefferink.areashop.features.signs.SignManager;
import me.wiefferink.areashop.interfaces.AreaShopInterface;
Expand Down Expand Up @@ -307,9 +308,9 @@ public void onEnable() {

setupTasks();

// Startup the CommandManager (registers itself for the command)
commandManager = injector.getInstance(CommandManager.class);
managers.add(commandManager);
// Register commands
AreashopCloudCommands commands = injector.getInstance(AreashopCloudCommands.class);
commands.registerCommands();

// Create a signLinkerManager
signLinkerManager = injector.getInstance(SignLinkerManager.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.wiefferink.areashop.commands.cloud;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import me.wiefferink.areashop.commands.CloudCommandBean;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.PaperCommandManager;

import java.util.List;
import java.util.function.Function;

@Singleton
public class AreashopCloudCommands {

private static final List<Class<? extends CloudCommandBean>> COMMAND_CLASSES = List.of(
AddCommandCloud.class,
AddFriendCloudCommand.class,
AddSignCloudCommand.class
);

private final Injector injector;
private final PaperCommandManager<CommandSender> commandManager;

@Inject
AreashopCloudCommands(@NonNull Injector injector, @NonNull Plugin plugin) {
this.injector = injector;
this.commandManager = new PaperCommandManager<>(
plugin,
ExecutionCoordinator.simpleCoordinator(),
SenderMapper.identity()
);
}

public void registerCommands() {
initCommandManager(this.commandManager);
for (Class<? extends CloudCommandBean> commandClass : COMMAND_CLASSES) {
CloudCommandBean commandBean = injector.getInstance(commandClass);
commandManager.command(commandBean);
}
}

private static void initCommandManager(PaperCommandManager<?> commandManager) {
if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
commandManager.registerBrigadier();
}
}



}

0 comments on commit 9f65f55

Please sign in to comment.