diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-28 00:04:44 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-05-28 00:04:44 +0200 |
commit | 8a5c5d45f87f38d9a7484b73c0505b95e63a21f1 (patch) | |
tree | b4408211d9ee9e9aea315316b1182462d7d1de2e | |
parent | fbd72e2a377a4711190307eed581b8b1d80e3406 (diff) | |
download | ultra-notifier-master.tar.gz ultra-notifier-master.tar.bz2 ultra-notifier-master.zip |
-rw-r--r-- | src/main/java/moe/nea/ultranotifier/mixin/AccessorCommandHandler.java | 18 | ||||
-rw-r--r-- | src/main/kotlin/UltraNotifier.kt | 4 | ||||
-rw-r--r-- | src/main/kotlin/commands/BrigadierPatchbay.kt | 117 | ||||
-rw-r--r-- | src/main/kotlin/commands/Commands.kt | 29 | ||||
-rw-r--r-- | src/main/kotlin/event/CommandRegistrationEvent.kt | 10 | ||||
-rw-r--r-- | src/main/kotlin/event/RegistrationFinishedEvent.kt | 6 | ||||
-rw-r--r-- | src/main/kotlin/event/UltraEvent.kt | 4 |
7 files changed, 178 insertions, 10 deletions
diff --git a/src/main/java/moe/nea/ultranotifier/mixin/AccessorCommandHandler.java b/src/main/java/moe/nea/ultranotifier/mixin/AccessorCommandHandler.java new file mode 100644 index 0000000..9218421 --- /dev/null +++ b/src/main/java/moe/nea/ultranotifier/mixin/AccessorCommandHandler.java @@ -0,0 +1,18 @@ +package moe.nea.ultranotifier.mixin; + +//#if FORGE +//$$import net.minecraft.command.CommandHandler; +//$$import net.minecraft.command.ICommand; +//$$import org.jetbrains.annotations.NotNull; +//$$import org.spongepowered.asm.mixin.Mixin; +//$$import org.spongepowered.asm.mixin.gen.Accessor; +//$$ +//$$import java.util.Set; +//$$ +//$$@Mixin(CommandHandler.class) +//$$public interface AccessorCommandHandler { +//$$ @Accessor("commandSet") +//$$ @NotNull +//$$ Set<@NotNull ICommand> getCommandSet_ultraNotifier(); +//$$} +//#endif
\ No newline at end of file diff --git a/src/main/kotlin/UltraNotifier.kt b/src/main/kotlin/UltraNotifier.kt index dce1fda..f1bb8ff 100644 --- a/src/main/kotlin/UltraNotifier.kt +++ b/src/main/kotlin/UltraNotifier.kt @@ -1,6 +1,7 @@ package moe.nea.ultranotifier import moe.nea.ultranotifier.commands.Commands +import moe.nea.ultranotifier.event.RegistrationFinishedEvent import moe.nea.ultranotifier.init.NeaMixinConfig import java.io.File @@ -18,6 +19,9 @@ object UltraNotifier { logger.info("Loaded ${mixinPlugin.mixins.size} mixins for ${mixinPlugin.mixinPackage}.") } Commands.init() + + RegistrationFinishedEvent().post() + } val configFolder = File("config/ultra-notifier").also { diff --git a/src/main/kotlin/commands/BrigadierPatchbay.kt b/src/main/kotlin/commands/BrigadierPatchbay.kt new file mode 100644 index 0000000..7396b6c --- /dev/null +++ b/src/main/kotlin/commands/BrigadierPatchbay.kt @@ -0,0 +1,117 @@ +package moe.nea.ultranotifier.commands + +//#if FORGE +//$$import com.mojang.brigadier.CommandDispatcher +//$$import com.mojang.brigadier.builder.LiteralArgumentBuilder +//$$import com.mojang.brigadier.tree.CommandNode +//$$import moe.nea.ultranotifier.event.CommandRegistrationEvent +//$$import moe.nea.ultranotifier.event.RegistrationFinishedEvent +//$$import moe.nea.ultranotifier.event.UltraNotifierEvents +//$$import moe.nea.ultranotifier.event.UltraSubscribe +//$$import moe.nea.ultranotifier.mixin.AccessorCommandHandler +//$$import net.minecraft.command.CommandBase +//$$import net.minecraft.command.CommandHandler +//$$import net.minecraft.command.ICommandSender +//$$import net.minecraft.util.BlockPos +//$$import net.minecraft.util.ChatComponentText +//$$import net.minecraft.util.text.ITextComponent +//$$import net.minecraftforge.client.ClientCommandHandler +//$$ +//$$fun CommandHandler.getCommandSet() = (this as AccessorCommandHandler).commandSet_ultraNotifier +//$$ +//$$class BridgedCommandSource( +//$$ val sender: ICommandSender +//$$) : UltraCommandSource { +//$$ override fun sendFeedback(text: ITextComponent) { +//$$ sender.addChatMessage(text) +//$$ } +//$$} +//$$ +//$$class BrigadierCommand( +//$$ val dispatcher: CommandDispatcher<UltraCommandSource>, +//$$ val node: CommandNode<UltraCommandSource> +//$$) : CommandBase() { +//$$ override fun addTabCompletionOptions( +//$$ sender: ICommandSender?, +//$$ args: Array<out String>?, +//$$ pos: BlockPos? +//$$ ): MutableList<String> { +//$$ return super.addTabCompletionOptions(sender, args, pos) +//$$ } +//$$ +//$$ override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean { +//$$ // TODO: proper check here maybe? +//$$ return true +//$$ } +//$$ +//$$ override fun getCommandName(): String { +//$$ return node.name +//$$ } +//$$ +//$$ override fun getCommandUsage(sender: ICommandSender?): String { +//$$ return "" +//$$ } +//$$ +//$$ private fun getCommandLineText(args: Array<out String>) = "${node.name} ${args.joinToString(" ")}".trim() +//$$ +//$$ override fun processCommand(sender: ICommandSender, args: Array<out String>) { +//$$ val source = BridgedCommandSource(sender) +//$$ val results = dispatcher.parse(getCommandLineText(args), source) +//$$ kotlin.runCatching { +//$$ dispatcher.execute(results) +//$$ Unit +//$$ }.recoverCatching { +//$$ source.sendFeedback(ChatComponentText("Could not execute ultra command: ${it.message}")) +//$$ } +//$$ } +//$$} +//$$ +//$$object BrigadierPatchbay { +//$$ +//$$ @UltraSubscribe +//$$ fun onAfterRegistration(event: RegistrationFinishedEvent) { +//$$ fullReload() +//$$ } +//$$ +//$$ @UltraSubscribe +//$$ fun onCommands(event: CommandRegistrationEvent) { +//$$ event.dispatcher +//$$ .register(LiteralArgumentBuilder.literal<UltraCommandSource>("reloadcommands") +//$$ .executes { +//$$ it.source.sendFeedback(literalText("Reloading commands")) +//$$ fullReload() +//$$ it.source.sendFeedback(literalText("Reload completed")) +//$$ 0 +//$$ }) +//$$ } +//$$ +//$$ fun fullReload() { +//$$ val handler = ClientCommandHandler.instance +//$$ unpatch(handler) +//$$ val dispatcher = createDispatcher() +//$$ UltraNotifierEvents.post(CommandRegistrationEvent(dispatcher)) +//$$ patch(handler, dispatcher) +//$$ } +//$$ +//$$ fun createDispatcher() = CommandDispatcher<UltraCommandSource>() +//$$ +//$$ fun unpatch(handler: CommandHandler) { +//$$ handler.getCommandSet() +//$$ .removeIf { +//$$ it is BrigadierCommand +//$$ } +//$$ handler.commands.entries +//$$ .removeIf { +//$$ it.value is BrigadierCommand +//$$ } +//$$ } +//$$ +//$$ fun patch(handler: CommandHandler, dispatcher: CommandDispatcher<UltraCommandSource>) { +//$$ dispatcher.root.children +//$$ .map { BrigadierCommand(dispatcher, it) } +//$$ .forEach(handler::registerCommand) +//$$ } +//$$} +//#endif + + diff --git a/src/main/kotlin/commands/Commands.kt b/src/main/kotlin/commands/Commands.kt index 75047cd..4509fcf 100644 --- a/src/main/kotlin/commands/Commands.kt +++ b/src/main/kotlin/commands/Commands.kt @@ -1,9 +1,10 @@ package moe.nea.ultranotifier.commands -import com.mojang.brigadier.CommandDispatcher import com.mojang.brigadier.builder.LiteralArgumentBuilder import moe.nea.ultranotifier.UltraNotifier +import moe.nea.ultranotifier.event.CommandRegistrationEvent import moe.nea.ultranotifier.event.UltraNotifierEvents +import moe.nea.ultranotifier.event.UltraSubscribe import net.minecraft.text.Text interface CustomSource { @@ -20,29 +21,37 @@ typealias UltraCommandSource = //#endif +fun translatableText(key: String, vararg args: String) = +//#if MC > 11400 + Text.translatable(key, *args) +//#else +//$$ net.minecraft.util.ChatComponentTranslation(key, *args) +//#endif + fun literalText(string: String): Text = -//#if MC >= 11400 +//#if MC > 11400 Text.literal(string) //#else //$$ net.minecraft.util.ChatComponentText(string) //#endif object Commands { - fun registerAll(dispatcher: CommandDispatcher<UltraCommandSource>) { - dispatcher.register(LiteralArgumentBuilder.literal<UltraCommandSource>("hello") - .executes { - it.source.sendFeedback(literalText("Hello World")) - 0 - }) + @UltraSubscribe + fun registerTestCommand(event: CommandRegistrationEvent) { + event.dispatcher.register(LiteralArgumentBuilder.literal<UltraCommandSource>("hello") + .executes { + it.source.sendFeedback(literalText("Hello World")) + 0 + }) } fun init() { UltraNotifierEvents.register(this) //#if FORGE -//$$ +//$$ UltraNotifierEvents.register(BrigadierPatchbay) //#else net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback.EVENT.register { dispatcher, registryAccess -> - registerAll(dispatcher) + UltraNotifierEvents.post(CommandRegistrationEvent(dispatcher)) } //#endif UltraNotifier.logger.info("Initialized command subsystem") diff --git a/src/main/kotlin/event/CommandRegistrationEvent.kt b/src/main/kotlin/event/CommandRegistrationEvent.kt new file mode 100644 index 0000000..49bc637 --- /dev/null +++ b/src/main/kotlin/event/CommandRegistrationEvent.kt @@ -0,0 +1,10 @@ +package moe.nea.ultranotifier.event + +import com.mojang.brigadier.CommandDispatcher +import moe.nea.ultranotifier.commands.UltraCommandSource + +/** + * Fired whenever commands need to be registered. This may be multiple times during each launch. Old commands will be + * automatically unregistered first. + */ +class CommandRegistrationEvent(val dispatcher: CommandDispatcher<UltraCommandSource>) : UltraEvent() diff --git a/src/main/kotlin/event/RegistrationFinishedEvent.kt b/src/main/kotlin/event/RegistrationFinishedEvent.kt new file mode 100644 index 0000000..325e1d8 --- /dev/null +++ b/src/main/kotlin/event/RegistrationFinishedEvent.kt @@ -0,0 +1,6 @@ +package moe.nea.ultranotifier.event + +/** + * Indicates that the registration of all ultra event handlers is done + */ +class RegistrationFinishedEvent : UltraEvent() diff --git a/src/main/kotlin/event/UltraEvent.kt b/src/main/kotlin/event/UltraEvent.kt index 42fa4f2..4f615c1 100644 --- a/src/main/kotlin/event/UltraEvent.kt +++ b/src/main/kotlin/event/UltraEvent.kt @@ -32,6 +32,10 @@ abstract class UltraEvent : setCancelled(true) } + fun post() { + UltraNotifierEvents.post(this) + } + } |