Skip to content

Commit

Permalink
feat: initial update to cloud 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Mar 17, 2024
1 parent 5d3a984 commit 9b10060
Show file tree
Hide file tree
Showing 70 changed files with 1,212 additions and 1,330 deletions.
10 changes: 9 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import xyz.jpenilla.runpaper.task.RunServer

plugins {
java
idea
alias(libs.plugins.shadow)
alias(libs.plugins.indra.licenser.spotless)
alias(libs.plugins.runPaper)
Expand Down Expand Up @@ -96,6 +97,13 @@ indraSpotlessLicenser {
}
}

idea {
module {
isDownloadJavadoc = true
isDownloadSources = true
}
}

tasks {
assemble {
dependsOn(shadowJar)
Expand Down Expand Up @@ -143,7 +151,7 @@ tasks {
}

listOf(
"cloud.commandframework", // cloud
"org.incendo", // cloud
"com.fasterxml.jackson", // jackson
"com.github.benmanes", // caffeine
"com.google.inject", // guice
Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
minecraft = "1.20.4"
mm-mirror = "0.1.2"
mm-lectern = "0.3.0" # deprecated
cloud = "1.8.4"
cloud = "2.0.0-beta.2"
bstats = "3.0.2"
moonshine = "2.0.4"
guice = "5.1.0"
Expand Down Expand Up @@ -32,8 +32,8 @@ runPaper = "2.2.2"
mm-mirror = { module = "me.machinemaker.mirror:mirror-paper", version.ref = "mm-mirror" }
mm-lectern = { module = "me.machinemaker.lectern:lectern-yaml", version.ref = "mm-lectern" } # deprecated

cloud-paper = { module = "cloud.commandframework:cloud-paper", version.ref = "cloud" }
cloud-extras = { module = "cloud.commandframework:cloud-minecraft-extras", version.ref = "cloud" }
cloud-paper = { module = "org.incendo:cloud-paper", version.ref = "cloud" }
cloud-extras = { module = "org.incendo:cloud-minecraft-extras", version.ref = "cloud" }

guice = { module = "com.google.inject:guice", version.ref = "guice" }
guice-assistedInject = { module = "com.google.inject.extensions:guice-assistedinject", version.ref = "guice" }
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/machinemaker/papertweaks/GlobalListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
package me.machinemaker.papertweaks;

import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.paper.PaperCommandManager;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.paper.PaperCommandManager;
import com.google.inject.Inject;
import me.machinemaker.papertweaks.cloud.dispatchers.CommandDispatcher;
import me.machinemaker.papertweaks.modules.ModuleManager;
Expand Down
47 changes: 25 additions & 22 deletions src/main/java/me/machinemaker/papertweaks/RootCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
package me.machinemaker.papertweaks;

import cloud.commandframework.Command;
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.ArrayList;
Expand All @@ -32,7 +27,6 @@
import me.machinemaker.lectern.ConfigurationNode;
import me.machinemaker.papertweaks.adventure.Components;
import me.machinemaker.papertweaks.cloud.PaperTweaksCommand;
import me.machinemaker.papertweaks.cloud.arguments.ModuleArgument;
import me.machinemaker.papertweaks.cloud.dispatchers.CommandDispatcher;
import me.machinemaker.papertweaks.menus.AbstractConfigurationMenu;
import me.machinemaker.papertweaks.modules.ModuleBase;
Expand All @@ -49,7 +43,13 @@
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.Command;
import org.incendo.cloud.component.DefaultValue;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.key.CloudKey;
import org.incendo.cloud.minecraft.extras.RichDescription;

import static me.machinemaker.papertweaks.cloud.parsers.ParserFactory.moduleDescriptor;
import static net.kyori.adventure.text.Component.join;
import static net.kyori.adventure.text.Component.newline;
import static net.kyori.adventure.text.Component.space;
Expand All @@ -65,11 +65,14 @@
import static net.kyori.adventure.text.format.NamedTextColor.RED;
import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
import static net.kyori.adventure.text.format.TextColor.color;
import static org.incendo.cloud.key.CloudKey.cloudKey;
import static org.incendo.cloud.parser.standard.IntegerParser.integerParser;

@DefaultQualifier(NonNull.class)
public class RootCommand extends PaperTweaksCommand {

private static final int PAGE_SIZE = 6;
private static final CloudKey<ModuleBase> MODULE_BASE_KEY = cloudKey("module", ModuleBase.class);

private final ModuleManager moduleManager;
private final ConfigurationNode modulesConfig;
Expand All @@ -92,37 +95,37 @@ public void registerCommands() {
.handler(this.sync(this::reloadEverything))
).command(this.simple("reload")
.literal("module")
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, translatable("commands.reload.module")) // Override default meta from #simple(String)
.argument(this.argumentFactory.module(true))
.handler(this.sync(context -> context.getSender().sendMessage(this.moduleManager.reloadModule(ModuleArgument.getModule(context).getName()))))
.commandDescription(RichDescription.translatable("commands.reload.module")) // Override default meta from #simple(String)
.required(MODULE_BASE_KEY, moduleDescriptor(this.argumentFactory, true))
.handler(this.sync(context -> context.sender().sendMessage(this.moduleManager.reloadModule(context.get(MODULE_BASE_KEY).getName()))))
).command(this.simple("enable")
.argument(this.argumentFactory.module(false))
.required(MODULE_BASE_KEY, moduleDescriptor(this.argumentFactory, false))
.handler(this.sync(context -> {
final Component enableMsg = this.moduleManager.enableModule(ModuleArgument.getModule(context).getName());
context.getSender().sendMessage(enableMsg);
final Component enableMsg = this.moduleManager.enableModule(context.get(MODULE_BASE_KEY).getName());
context.sender().sendMessage(enableMsg);
this.console.sendMessage(Components.join(PaperTweaks.PLUGIN_PREFIX, enableMsg));
}))
).command(this.simple("disable")
.argument(this.argumentFactory.module(true))
.required(MODULE_BASE_KEY, moduleDescriptor(this.argumentFactory, true))
.handler(this.sync(context -> {
final Component disableMsg = this.moduleManager.disableModule(ModuleArgument.getModule(context).getName());
context.getSender().sendMessage(disableMsg);
final Component disableMsg = this.moduleManager.disableModule(context.get(MODULE_BASE_KEY).getName());
context.sender().sendMessage(disableMsg);
this.console.sendMessage(Components.join(PaperTweaks.PLUGIN_PREFIX, disableMsg));
}))
).command(this.simple("list")
.argument(IntegerArgument.<CommandDispatcher>builder("page").withMin(1).withMax(this.maxPageCount).asOptionalWithDefault(1))
.optional("page", integerParser(1, this.maxPageCount), DefaultValue.constant(1))
.handler(this::sendModuleList)
).command(this.simple("version")
.handler(this::showVersion)
);
}

private Command.Builder<CommandDispatcher> simple(final String name) {
return this.builder.literal(name).meta(MinecraftExtrasMetaKeys.DESCRIPTION, translatable("commands." + name)).permission("vanillatweaks.main." + name);
return this.builder.literal(name).commandDescription(RichDescription.translatable("commands." + name)).permission("vanillatweaks.main." + name);
}

private void reloadEverything(final CommandContext<CommandDispatcher> context) {
final Audience audience = context.getSender();
final Audience audience = context.sender();
this.modulesConfig.reloadAndSave();
// TODO reload more stuff
final ModuleManager.ReloadResult result = this.moduleManager.reloadModules();
Expand All @@ -145,7 +148,7 @@ private void reloadEverything(final CommandContext<CommandDispatcher> context) {
}

private void sendModuleList(final @NonNull CommandContext<CommandDispatcher> context) {
final boolean showAll = context.getSender().hasPermission("vanillatweaks.main.list.all");
final boolean showAll = context.sender().hasPermission("vanillatweaks.main.list.all");
final int page = context.get("page");
final TextComponent.Builder list = text();
final List<ModuleBase> modules = this.moduleManager.getModules().values().stream().filter(module -> showAll || this.moduleManager.getLifecycle(module.getName()).orElseThrow().getState().isRunning()).toList();
Expand All @@ -157,7 +160,7 @@ private void sendModuleList(final @NonNull CommandContext<CommandDispatcher> con
final ModuleState state = lifecycle.get().getState();
if (showAll || state.isRunning()) {
final TextComponent.Builder builder = text().color(color(0x8F8F8F)).append(text(" - "));
if ((state.isRunning() && context.getSender().hasPermission("vanillatweaks.main.disable")) || (!state.isRunning() && context.getSender().hasPermission("vanillatweaks.main.enable"))) {
if ((state.isRunning() && context.sender().hasPermission("vanillatweaks.main.disable")) || (!state.isRunning() && context.sender().hasPermission("vanillatweaks.main.enable"))) {
builder.append(
text("[" + (state.isRunning() ? "■" : "▶") + "]", state.isRunning() ? RED : GREEN)
.hoverEvent(showText(translatable("commands.config.bool-toggle." + state.isRunning(), state.isRunning() ? RED : GREEN, text(moduleBase.getName(), GOLD))))
Expand All @@ -172,7 +175,7 @@ private void sendModuleList(final @NonNull CommandContext<CommandDispatcher> con
list.append(builder).append(newline());
}
}
context.getSender().sendMessage(join(JoinConfiguration.noSeparators(), header, list, AbstractConfigurationMenu.END_LINE));
context.sender().sendMessage(join(JoinConfiguration.noSeparators(), header, list, AbstractConfigurationMenu.END_LINE));
}

private ComponentLike createHeader(final int page, final List<ModuleBase> modules) {
Expand All @@ -194,7 +197,7 @@ private void showVersion(final CommandContext<CommandDispatcher> context) {
.hoverEvent(showText(translatable("commands.version.success.hover", GRAY)))
.clickEvent(copyToClipboard(PaperTweaks.class.getPackage().getImplementationVersion()))
);
context.getSender().sendMessage(component);
context.sender().sendMessage(component);
}

}
92 changes: 44 additions & 48 deletions src/main/java/me/machinemaker/papertweaks/cloud/CloudModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@
*/
package me.machinemaker.papertweaks.cloud;

import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.StaticArgument;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.CloudBukkitCapabilities;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.minecraft.extras.AudienceProvider;
import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler;
import cloud.commandframework.paper.PaperCommandManager;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.LoadingCache;
import com.google.inject.AbstractModule;
Expand All @@ -46,18 +37,25 @@
import me.machinemaker.mirror.MethodInvoker;
import me.machinemaker.mirror.Mirror;
import me.machinemaker.mirror.paper.PaperMirror;
import me.machinemaker.papertweaks.cloud.arguments.ArgumentFactory;
import me.machinemaker.papertweaks.cloud.arguments.PseudoEnumArgument;
import me.machinemaker.papertweaks.cloud.parsers.ParserFactory;
import me.machinemaker.papertweaks.cloud.cooldown.CommandCooldownManager;
import me.machinemaker.papertweaks.cloud.dispatchers.CommandDispatcher;
import me.machinemaker.papertweaks.cloud.dispatchers.CommandDispatcherFactory;
import me.machinemaker.papertweaks.cloud.processors.SimpleSuggestionProcessor;
import me.machinemaker.papertweaks.cloud.parsers.PseudoEnumParser;
import me.machinemaker.papertweaks.cloud.processors.ConditionalCaseInsensitiveSuggestionProcessor;
import me.machinemaker.papertweaks.cloud.processors.post.GamemodePostprocessor;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.SenderMapper;
import org.incendo.cloud.brigadier.CloudBrigadierManager;
import org.incendo.cloud.bukkit.CloudBukkitCapabilities;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler;
import org.incendo.cloud.paper.PaperCommandManager;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.RED;
Expand All @@ -82,15 +80,15 @@ private static Map<String, org.bukkit.command.Command> getCommandMap() {

@Override
protected void configure() {
this.install(new FactoryModuleBuilder().build(ArgumentFactory.class));
this.install(new FactoryModuleBuilder().build(ParserFactory.class));
}

@Provides
@Singleton
CommandCooldownManager<CommandDispatcher, UUID> commandCooldownManager() {
return CommandCooldownManager.create(
CommandDispatcher::getUUID,
(context, cooldown, secondsLeft) -> context.getCommandContext().getSender().sendMessage(text("Cooling down", RED)),
(context, cooldown, secondsLeft) -> context.commandContext().sender().sendMessage(text("Cooling down", RED)),
this.executorService);
}

Expand All @@ -101,25 +99,25 @@ PaperCommandManager<CommandDispatcher> paperCommandManager(final CommandDispatch
final MinecraftExceptionHandler<CommandDispatcher> minecraftExceptionHandler) {
final LoadingCache<CommandSender, CommandDispatcher> senderCache = CacheBuilder.newBuilder().expireAfterAccess(20, TimeUnit.MINUTES).build(commandDispatcherFactory);
try {
final PaperCommandManager<CommandDispatcher> manager = new PaperCommandManager<>(
final PaperCommandManager<CommandDispatcher> manager = new PaperCommandManager<CommandDispatcher>(
this.plugin,
AsynchronousCommandExecutionCoordinator.<CommandDispatcher>builder().build(),
commandSender -> {
try {
return senderCache.get(commandSender);
} catch (final ExecutionException e) {
throw new IllegalArgumentException("Error mapping command sender", e);
}
},
CommandDispatcher::sender
ExecutionCoordinator.asyncCoordinator(),
SenderMapper.create(
commandSender -> {
try {
return senderCache.get(commandSender);
} catch (final ExecutionException e) {
throw new IllegalArgumentException("Error mapping command sender", e);
}
},
CommandDispatcher::sender
)
) {
@Override
public CommandManager<CommandDispatcher> command(final Command<CommandDispatcher> command) {
if (command.getArguments().get(0) instanceof final StaticArgument<?> staticArgument) {
final String main = staticArgument.getName();
if (VANILLA_COMMAND_WRAPPER_CLASS.isInstance(getCommandMap().get(main))) {
getCommandMap().remove(main);
}
public CommandManager<CommandDispatcher> command(final Command<? extends CommandDispatcher> command) {
final String main = command.rootComponent().name();
if (VANILLA_COMMAND_WRAPPER_CLASS.isInstance(getCommandMap().get(main))) {
getCommandMap().remove(main);
}
return super.command(command);
}
Expand All @@ -132,21 +130,19 @@ public CommandManager<CommandDispatcher> command(final Command<CommandDispatcher
manager.registerBrigadier();
}

minecraftExceptionHandler.apply(manager, AudienceProvider.nativeAudience());
minecraftExceptionHandler.registerTo(manager);
commandCooldownManager.registerCooldownManager(manager);
manager.registerCommandPostProcessor(new GamemodePostprocessor());
manager.commandSuggestionProcessor(new SimpleSuggestionProcessor());
manager.suggestionProcessor(ConditionalCaseInsensitiveSuggestionProcessor.instance());

final @Nullable CloudBrigadierManager<CommandDispatcher, ?> brigManager = manager.brigadierManager();
if (brigManager != null) {
brigManager.registerMapping(new TypeToken<PseudoEnumArgument.PseudoEnumParser<CommandDispatcher>>() {}, builder -> {
builder.cloudSuggestions().to(argument -> switch (argument.getStringMode()) {
case QUOTED -> StringArgumentType.string();
case GREEDY -> StringArgumentType.greedyString();
default -> StringArgumentType.word();
});
final CloudBrigadierManager<CommandDispatcher, ?> brigManager = manager.brigadierManager();
brigManager.registerMapping(new TypeToken<PseudoEnumParser<CommandDispatcher>>() {}, builder -> {
builder.cloudSuggestions().to(argument -> switch (argument.getStringMode()) {
case QUOTED -> StringArgumentType.string();
case GREEDY -> StringArgumentType.greedyString();
default -> StringArgumentType.word();
});
}
});

return manager;
} catch (final Exception e) {
Expand All @@ -157,11 +153,11 @@ public CommandManager<CommandDispatcher> command(final Command<CommandDispatcher
@Provides
@Singleton
private MinecraftExceptionHandler<CommandDispatcher> minecraftExceptionHandler() {
return new MinecraftExceptionHandler<CommandDispatcher>()
.withArgumentParsingHandler()
.withCommandExecutionHandler()
.withInvalidSenderHandler()
.withInvalidSyntaxHandler()
.withNoPermissionHandler();
return MinecraftExceptionHandler.<CommandDispatcher>createNative()
.defaultArgumentParsingHandler()
.defaultCommandExecutionHandler()
.defaultInvalidSenderHandler()
.defaultInvalidSyntaxHandler()
.defaultNoPermissionHandler();
}
}
16 changes: 14 additions & 2 deletions src/main/java/me/machinemaker/papertweaks/cloud/MetaKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@
*/
package me.machinemaker.papertweaks.cloud;

import cloud.commandframework.meta.CommandMeta;
import org.bukkit.GameMode;
import org.incendo.cloud.Command;
import org.incendo.cloud.key.CloudKey;

import static org.incendo.cloud.key.CloudKey.cloudKey;

public final class MetaKeys {

public static final CommandMeta.Key<GameMode> GAMEMODE_KEY = CommandMeta.Key.of(GameMode.class, "papertweaks:gamemode");
public static final CloudKey<Void> HIDDEN = cloudKey("papertweaks:hidden");

public static <C> Command.Builder.Applicable<C> hiddenCommand() {
return builder -> {
builder.meta(HIDDEN, null);
return builder;
};
}

public static final CloudKey<GameMode> GAMEMODE_KEY = cloudKey("papertweaks:gamemode", GameMode.class);

private MetaKeys() {
}
Expand Down
Loading

0 comments on commit 9b10060

Please sign in to comment.