Skip to content

Commit

Permalink
Update cloud to get 1.21 support working
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Aug 10, 2024
1 parent a9775fd commit 182e957
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.clubobsidian.dynamicgui.api.command;

import cloud.commandframework.arguments.CommandArgument;
import org.incendo.cloud.component.CommandComponent;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
Expand All @@ -38,7 +38,7 @@ public interface CommandRegistrar {
*/
void registerGuiAliasCommand(@NotNull String guiName,
@NotNull String alias,
@NotNull Collection<CommandArgument> arguments);
@NotNull Collection<CommandComponent> arguments);

/**
* Unregisters a command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.clubobsidian.dynamicgui.api.command.cloud;

import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.standard.*;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.component.TypedCommandComponent;
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.standard.*;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
Expand All @@ -28,16 +30,16 @@
public final class CloudArgument {

public static final String PLAYER_ARG_NAME = "player";
public static final CloudArgument BOOLEAN = create(name -> BooleanArgument.builder(name));
public static final CloudArgument BYTE = create(name -> ByteArgument.builder(name));
public static final CloudArgument CHAR = create(name -> CharArgument.builder(name));
public static final CloudArgument DOUBLE = create(name -> DoubleArgument.builder(name));
public static final CloudArgument FLOAT = create(name -> FloatArgument.builder(name));
public static final CloudArgument INTEGER = create(name -> IntegerArgument.builder(name));
public static final CloudArgument LONG = create(name -> LongArgument.builder(name));
public static final CloudArgument SHORT = create(name -> ShortArgument.builder(name));
public static final CloudArgument STRING = create(name -> StringArgument.builder(name));
public static final CloudArgument UUID = create(name -> UUIDArgument.builder(name));
public static final CloudArgument BOOLEAN = create(name -> BooleanParser.booleanComponent().name(name));
public static final CloudArgument BYTE = create(name -> ByteParser.byteComponent().name(name));
public static final CloudArgument CHAR = create(name -> CharacterParser.characterComponent().name(name));
public static final CloudArgument DOUBLE = create(name -> DoubleParser.doubleComponent().name(name));
public static final CloudArgument FLOAT = create(name -> FloatParser.floatComponent().name(name));
public static final CloudArgument INTEGER = create(name -> IntegerParser.integerComponent().name(name));
public static final CloudArgument LONG = create(name -> LongParser.longComponent().name(name));
public static final CloudArgument SHORT = create(name -> ShortParser.shortComponent().name(name));
public static final CloudArgument STRING = create(name -> StringParser.stringComponent().name(name));
public static final CloudArgument UUID = create(name -> UUIDParser.uuidComponent().name(name));

private static final Map<String, CloudArgument> TYPES = new HashMap<>();

Expand All @@ -53,13 +55,13 @@ public final class CloudArgument {
}
}

public static CloudArgument create(@NotNull Function<String, CommandArgument.Builder> func) {
public static CloudArgument create(@NotNull Function<String, CommandComponent.Builder> func) {
return new CloudArgument((d) -> {
CommandArgument.Builder builder = func.apply(d.getArgumentName());
CommandComponent.Builder builder = func.apply(d.getArgumentName());
if (d.isOptional()) {
builder.asOptional();
builder.optional();
} else {
builder.asRequired();
builder.required();
}
return builder.build();
});
Expand Down Expand Up @@ -87,17 +89,17 @@ public static Optional<CloudArgument> fromType(@NotNull String type) {
return Optional.ofNullable(TYPES.get(type));
}

private final Function<CloudData, CommandArgument> function;
private final Function<CloudData, CommandComponent> function;

public CloudArgument(@NotNull Function<CloudData, CommandArgument> function) {
public CloudArgument(@NotNull Function<CloudData, CommandComponent> function) {
this.function = Objects.requireNonNull(function);
}

public <T extends CommandArgument> T argument(@NotNull String argName) {
public <T extends CommandComponent> T argument(@NotNull String argName) {
return this.argument(argName, false);
}

public <T extends CommandArgument> T argument(@NotNull String argName, boolean optional) {
public <T extends CommandComponent> T argument(@NotNull String argName, boolean optional) {
Objects.requireNonNull(argName);
return (T) this.function.apply(new CloudData(argName, optional));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.clubobsidian.dynamicgui.api.parser.gui;

import cloud.commandframework.arguments.CommandArgument;
import com.clubobsidian.dynamicgui.api.gui.GuiBuildType;
import com.clubobsidian.dynamicgui.api.parser.function.tree.FunctionTree;
import com.clubobsidian.dynamicgui.api.parser.macro.MacroParser;
import com.clubobsidian.dynamicgui.api.parser.slot.SlotToken;
import org.incendo.cloud.component.CommandComponent;

import java.io.Serializable;
import java.util.Collection;
Expand Down Expand Up @@ -48,7 +48,7 @@ default String parseType(String type) {

List<String> getAlias();

Collection<CommandArgument> getCommandArguments();
Collection<CommandComponent> getCommandArguments();

List<String> getLocations();

Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ allprojects {
version projectVersion
ext {
junitVersion = '5.9.1'
cloudVersion = '1.8.0'
cloudVersion = '2.0.0-rc.2'
wrappyVersion = "3.0.1"
langVersion = "3.12.0"
guiceVersion = "5.1.0"
Expand Down Expand Up @@ -103,18 +103,18 @@ subprojects {
}

repositories {
maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots"}
maven { url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots" }
maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
mavenCentral()
maven { url 'https://jitpack.io' }
maven { url = "https://jitpack.io" }
}

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation 'org.mockito:mockito-core:4.8.0'
compileOnly("cloud.commandframework:cloud-core:$cloudVersion")
testRuntimeOnly("cloud.commandframework:cloud-core:$cloudVersion")
compileOnly("org.incendo:cloud-core:$cloudVersion")
testRuntimeOnly("org.incendo:cloud-core:$cloudVersion")
compileOnly("org.jetbrains:annotations:23.0.0")
compileOnly("org.apache.commons:commons-lang3:$langVersion")
testImplementation("org.apache.commons:commons-lang3:$langVersion")
Expand Down
2 changes: 1 addition & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ dependencies {
compileOnly 'com.github.clubobsidian:foundry:3.0.0'
compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'
compileOnly 'net.md-5:bungeecord-api:1.8-SNAPSHOT'
implementation "cloud.commandframework:cloud-paper:$cloudVersion"
implementation "org.incendo:cloud-paper:2.0.0-beta.8"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,31 @@

package com.clubobsidian.dynamicgui.bukkit.cloud;

import cloud.commandframework.CloudCapability;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager;
import com.clubobsidian.dynamicgui.core.util.ReflectionUtil;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CloudCapability;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.LegacyPaperCommandManager;
import org.incendo.cloud.paper.PaperCommandManager;

import java.util.Set;
import java.util.function.Function;

public class DynamicGuiPaperCommandManager<C> extends PaperCommandManager<C> {
public class DynamicGuiPaperCommandManager<C> extends LegacyPaperCommandManager<C> {

//A hack for not updating the command map for each player every time a new command is registered

private final Set<CloudCapability> capabilities;

public DynamicGuiPaperCommandManager(@NonNull Plugin owningPlugin,
@NonNull Function<CommandTree<C>,
CommandExecutionCoordinator<C>> commandExecutionCoordinator,
@NonNull Function<CommandSender, C> commandSenderMapper,
@NonNull Function<C, CommandSender> backwardsCommandSenderMapper) throws Exception {
super(owningPlugin, commandExecutionCoordinator, commandSenderMapper, backwardsCommandSenderMapper);
public DynamicGuiPaperCommandManager(final @NonNull Plugin owningPlugin,
final @NonNull ExecutionCoordinator<C> commandExecutionCoordinator,
final @NonNull SenderMapper<CommandSender, C> senderMapper) throws Exception {
super(owningPlugin, commandExecutionCoordinator, senderMapper);
this.capabilities = (Set<CloudCapability>) ReflectionUtil
.getFieldByName(CommandManager.class, "capabilities")
.get(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.clubobsidian.dynamicgui.bukkit.inject;

import cloud.commandframework.CommandManager;
import com.clubobsidian.dynamicgui.api.command.GuiCommandSender;
import com.clubobsidian.dynamicgui.api.economy.Economy;
import com.clubobsidian.dynamicgui.api.logger.LoggerWrapper;
Expand All @@ -36,6 +35,7 @@
import com.clubobsidian.dynamicgui.bukkit.manager.world.BukkitLocationManager;
import com.clubobsidian.dynamicgui.core.inject.module.PluginModule;
import com.clubobsidian.dynamicgui.core.manager.cloud.CloudManager;
import org.incendo.cloud.CommandManager;

public class BukkitPluginModule extends PluginModule {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package com.clubobsidian.dynamicgui.bukkit.manager.cloud;

import cloud.commandframework.bukkit.parsers.PlayerArgument;
import com.clubobsidian.dynamicgui.api.command.cloud.CloudArgument;
import com.clubobsidian.dynamicgui.core.manager.cloud.CloudManager;
import org.bukkit.entity.Player;
import org.incendo.cloud.bukkit.parser.PlayerParser;

public class BukkitCloudManager implements CloudManager {

@Override
public CloudArgument createPlayerArg() {
return CloudArgument.create(name -> PlayerArgument.builder(name));
return CloudArgument.create(name -> PlayerParser.playerComponent().name(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

package com.clubobsidian.dynamicgui.bukkit.plugin;

import cloud.commandframework.CommandManager;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager;
import com.clubobsidian.dynamicgui.api.DynamicGui;
import com.clubobsidian.dynamicgui.api.command.GuiCommandSender;
import com.clubobsidian.dynamicgui.api.economy.Economy;
Expand All @@ -44,9 +40,20 @@
import com.clubobsidian.dynamicgui.core.economy.NoOpEconomy;
import com.clubobsidian.dynamicgui.core.logger.JavaLoggerWrapper;
import com.clubobsidian.dynamicgui.core.permission.NoOpPermission;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.brigadier.BrigadierSetting;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.bukkit.internal.BukkitBackwardsBrigadierSenderMapper;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.LegacyPaperCommandManager;
import org.incendo.cloud.paper.PaperCommandManager;
import org.incendo.cloud.setting.ManagerSetting;

import java.util.logging.Level;

Expand Down Expand Up @@ -116,20 +123,30 @@ private void registerListener(Listener listener) {

private CommandManager<GuiCommandSender> createCommandSender() {
try {
PaperCommandManager<GuiCommandSender> commandManager = new DynamicGuiPaperCommandManager<>(this,
CommandExecutionCoordinator.simpleCoordinator(),
BukkitGuiCommandSender::new,
wrappedSender -> wrappedSender.getNativeSender()
LegacyPaperCommandManager<GuiCommandSender> commandManager = new DynamicGuiPaperCommandManager<>(
this,
ExecutionCoordinator.simpleCoordinator(),
new SenderMapper<>() {
@Override
public @NonNull GuiCommandSender map(@NonNull CommandSender base) {
return new BukkitGuiCommandSender(base);
}

@Override
public @NonNull CommandSender reverse(@NonNull GuiCommandSender mapped) {
return mapped.getNativeSender();
}
}
);
//Unfortunately is tied to bukkit so there is no way to do this in core
if (commandManager.hasCapability(CloudBukkitCapabilities.BRIGADIER)) {
commandManager.registerBrigadier();
if (commandManager.hasBrigadierManager()) {
commandManager.brigadierManager().settings().set(BrigadierSetting.FORCE_EXECUTABLE, true);
}
if (commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
commandManager.registerAsynchronousCompletions();
}
commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
commandManager.setSetting(CommandManager.ManagerSettings.OVERRIDE_EXISTING_COMMANDS, true);
commandManager.settings().set(ManagerSetting.ALLOW_UNSAFE_REGISTRATION, true);
commandManager.settings().set(ManagerSetting.OVERRIDE_EXISTING_COMMANDS, true);
return commandManager;
} catch (Exception e) {
e.printStackTrace();
Expand Down
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
api 'org.apache.commons:commons-lang3:3.12.0'
api "com.github.clubobsidian:wrappy:$wrappyVersion"
api 'com.github.ben-manes.caffeine:caffeine:3.1.1'
testImplementation "cloud.commandframework:cloud-core:$cloudVersion"
api "cloud.commandframework:cloud-annotations:$cloudVersion"
testImplementation "org.incendo:cloud-core:$cloudVersion"
api "org.incendo:cloud-annotations:$cloudVersion"
api "io.github.classgraph:classgraph:4.8.149"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

package com.clubobsidian.dynamicgui.core.command;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.meta.SimpleCommandMeta;
import com.clubobsidian.dynamicgui.api.DynamicGui;
import com.clubobsidian.dynamicgui.api.command.CommandRegistrar;
import com.clubobsidian.dynamicgui.api.command.GuiCommandSender;
Expand All @@ -31,6 +26,12 @@
import com.clubobsidian.dynamicgui.api.manager.gui.GuiManager;
import com.google.inject.Injector;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.annotations.AnnotationParser;
import org.incendo.cloud.component.CommandComponent;
import org.incendo.cloud.meta.SimpleCommandMeta;
import org.incendo.cloud.setting.ManagerSetting;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;
Expand All @@ -55,8 +56,8 @@ private CommandRegistrarImpl(CommandManager<GuiCommandSender> commandManager,
this.commandParser = new AnnotationParser<>(this.commandManager,
GuiCommandSender.class,
parserParameters -> SimpleCommandMeta.empty());
this.commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, true);
this.commandManager.setSetting(CommandManager.ManagerSettings.OVERRIDE_EXISTING_COMMANDS, true);
this.commandManager.settings().set(ManagerSetting.ALLOW_UNSAFE_REGISTRATION, true);
this.commandManager.settings().set(ManagerSetting.OVERRIDE_EXISTING_COMMANDS, true);
}

@Override
Expand All @@ -68,20 +69,20 @@ public void registerCommand(@NotNull Class<? extends RegisteredCommand> command)
@Override
public void registerGuiAliasCommand(@NotNull String guiName,
@NotNull String alias,
@NotNull Collection<CommandArgument> arguments) {
@NotNull Collection<CommandComponent> arguments) {
Objects.requireNonNull(guiName);
Objects.requireNonNull(alias);
Objects.requireNonNull(arguments);
this.unregisterCommand(alias);
Command.@NonNull Builder<GuiCommandSender> builder = this.commandManager.commandBuilder(alias);
builder = builder.handler(context -> {
context.getSender().getPlayer()
context.sender().getPlayer()
.ifPresent(playerWrapper -> {
Map<String, String> metadata = new HashMap<>();
for (CommandArgument arg : arguments) {
String argName = arg.getName();
for (CommandComponent arg : arguments) {
String argName = arg.name();
String metaKey = "command_" + argName;
context.getOptional(argName).ifPresent(value -> {
context.optional(argName).ifPresent(value -> {
String metadataValue = EntityManager.get().isPlayer(value) ?
EntityManager.get().createPlayerWrapper(value).getName() :
String.valueOf(value);
Expand All @@ -91,7 +92,7 @@ public void registerGuiAliasCommand(@NotNull String guiName,
GuiManager.get().openGui(playerWrapper, guiName, metadata);
});
});
for (CommandArgument arg : arguments) {
for (CommandComponent arg : arguments) {
builder = builder.argument(arg);
}
this.commandManager.command(builder.build());
Expand Down
Loading

0 comments on commit 182e957

Please sign in to comment.