diff options
author | nea <romangraef@gmail.com> | 2022-08-27 02:37:34 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-08-27 02:37:34 +0200 |
commit | 2019473c50dfe3f1c6fd071d123d49d43bc180c4 (patch) | |
tree | 56ae9b81672582bfab2993f6b4a110c8a98e6682 /src/main/kotlin/moe/nea | |
parent | 973c13e84f8e091a6a920779103d96559dcd32a8 (diff) | |
download | Firmament-2019473c50dfe3f1c6fd071d123d49d43bc180c4.tar.gz Firmament-2019473c50dfe3f1c6fd071d123d49d43bc180c4.tar.bz2 Firmament-2019473c50dfe3f1c6fd071d123d49d43bc180c4.zip |
move commands to its own place
Diffstat (limited to 'src/main/kotlin/moe/nea')
4 files changed, 133 insertions, 28 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt index 35671a3..4868fbf 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/NotEnoughUpdates.kt @@ -1,22 +1,18 @@ package moe.nea.notenoughupdates -import com.mojang.brigadier.Command import com.mojang.brigadier.CommandDispatcher -import io.github.cottonmc.cotton.gui.client.CottonClientScreen import io.ktor.client.* import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* import io.ktor.serialization.kotlinx.json.* import kotlinx.coroutines.* import kotlinx.serialization.json.Json +import moe.nea.notenoughupdates.commands.registerNeuCommand import moe.nea.notenoughupdates.dbus.NEUDbusObject -import moe.nea.notenoughupdates.gui.repoGui import moe.nea.notenoughupdates.repo.RepoManager import moe.nea.notenoughupdates.util.ConfigHolder -import moe.nea.notenoughupdates.util.ScreenUtil.setScreenLater import net.fabricmc.api.ClientModInitializer import net.fabricmc.api.ModInitializer -import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents @@ -24,7 +20,6 @@ import net.fabricmc.loader.api.FabricLoader import net.fabricmc.loader.api.Version import net.fabricmc.loader.api.metadata.ModMetadata import net.minecraft.command.CommandRegistryAccess -import net.minecraft.text.Text import org.apache.logging.log4j.LogManager import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder import java.nio.file.Files @@ -70,24 +65,7 @@ object NotEnoughUpdates : ModInitializer, ClientModInitializer { @Suppress("UNUSED_PARAMETER") _ctx: CommandRegistryAccess ) { - dispatcher.register(ClientCommandManager.literal("neureload") - .then(ClientCommandManager.literal("fetch").executes { - it.source.sendFeedback(Text.literal("Trying to redownload the repository")) // TODO better reporting - RepoManager.launchAsyncUpdate() - Command.SINGLE_SUCCESS - }) - .executes { - it.source.sendFeedback(Text.translatable("notenoughupdates.repo.reload.disk")) - RepoManager.reload() - Command.SINGLE_SUCCESS - }) - dispatcher.register( - ClientCommandManager.literal("neu") - .then(ClientCommandManager.literal("repo").executes { - setScreenLater(CottonClientScreen(repoGui())) - Command.SINGLE_SUCCESS - }) - ) + registerNeuCommand(dispatcher) } override fun onInitialize() { diff --git a/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt b/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt new file mode 100644 index 0000000..c21eab8 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/commands/dsl.kt @@ -0,0 +1,75 @@ +package moe.nea.notenoughupdates.commands + +import com.mojang.brigadier.arguments.ArgumentType +import com.mojang.brigadier.builder.ArgumentBuilder +import com.mojang.brigadier.builder.LiteralArgumentBuilder +import com.mojang.brigadier.builder.RequiredArgumentBuilder +import com.mojang.brigadier.context.CommandContext +import moe.nea.notenoughupdates.util.iterate +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import java.lang.reflect.ParameterizedType + + +typealias DefaultSource = FabricClientCommandSource + +fun literal( + name: String, + block: LiteralArgumentBuilder<DefaultSource>.() -> Unit +): LiteralArgumentBuilder<DefaultSource> = + LiteralArgumentBuilder.literal<DefaultSource>(name).also(block) + +data class TypeSafeArg<T : Any>(val name: String, val argument: ArgumentType<T>) { + val argClass by lazy { + argument.javaClass + .iterate<Class<in ArgumentType<T>>> { + it.superclass + } + .map { + it.genericSuperclass + } + .filterIsInstance<ParameterizedType>() + .find { it.rawType == ArgumentType::class.java }!! + .let { it.actualTypeArguments[0] as Class<*> } + } + + @JvmName("getWithThis") + fun <S> CommandContext<S>.get(): T = + get(this) + + + fun <S> get(ctx: CommandContext<S>): T { + return ctx.getArgument(name, argClass) as T + } +} + + +fun <T : Any> argument( + name: String, + argument: ArgumentType<T>, + block: RequiredArgumentBuilder<DefaultSource, T>.(TypeSafeArg<T>) -> Unit +): RequiredArgumentBuilder<DefaultSource, T> = + RequiredArgumentBuilder.argument<DefaultSource, T>(name, argument).also { block(it, TypeSafeArg(name, argument)) } + +fun <T : ArgumentBuilder<DefaultSource, T>, AT : Any> T.thenArgument( + name: String, + argument: ArgumentType<AT>, + block: RequiredArgumentBuilder<DefaultSource, AT>.(TypeSafeArg<AT>) -> Unit +): T = then(argument(name, argument, block)) + + +fun <T : ArgumentBuilder<DefaultSource, T>> T.thenLiteral( + name: String, + block: LiteralArgumentBuilder<DefaultSource>.() -> Unit +): T = + then(literal(name, block)) + +fun <T : ArgumentBuilder<DefaultSource, T>> T.then(node: ArgumentBuilder<DefaultSource, *>, block: T.() -> Unit): T = + then(node).also(block) + +fun <T : ArgumentBuilder<DefaultSource, T>> T.thenExecute(block: CommandContext<DefaultSource>.() -> Unit): T = + executes { + block(it) + 1 + } + + diff --git a/src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt b/src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt new file mode 100644 index 0000000..1e59d78 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/commands/rome.kt @@ -0,0 +1,42 @@ +package moe.nea.notenoughupdates.commands + +import com.mojang.brigadier.CommandDispatcher +import io.github.cottonmc.cotton.gui.client.CottonClientScreen +import moe.nea.notenoughupdates.gui.repoGui +import moe.nea.notenoughupdates.repo.RepoManager +import moe.nea.notenoughupdates.util.ScreenUtil.setScreenLater +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource +import net.minecraft.text.Text + + +fun neuCommand() = literal("neu") { + thenLiteral("reload") { + thenLiteral("fetch") { + thenExecute { + source.sendFeedback(Text.literal("Trying to redownload the repository")) // TODO better reporting + RepoManager.launchAsyncUpdate() + } + } + thenExecute { + source.sendFeedback(Text.translatable("notenoughupdates.repo.reload.disk")) + RepoManager.reload() + } + } + thenLiteral("repo") { + thenExecute { + setScreenLater(CottonClientScreen(repoGui())) + } + } +} + + +fun registerNeuCommand(dispatcher: CommandDispatcher<FabricClientCommandSource>) { + val neu = dispatcher.register(neuCommand()) + dispatcher.register(literal("alsoneu") { + redirect(neu) + }) +} + + + + diff --git a/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt index 1cf6efc..19ae606 100644 --- a/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt +++ b/src/main/kotlin/moe/nea/notenoughupdates/repo/ItemCache.kt @@ -18,12 +18,19 @@ import net.minecraft.nbt.NbtCompound import net.minecraft.nbt.NbtOps import net.minecraft.text.Text import net.minecraft.util.Identifier +import java.io.PrintWriter +import java.nio.file.Path import java.util.concurrent.ConcurrentHashMap +import kotlin.io.path.absolutePathString +import kotlin.io.path.writer object ItemCache : IReloadable { - val cache: MutableMap<String, ItemStack> = ConcurrentHashMap() - val df = Schemas.getFixer() + val dfuLog = Path.of("logs/dfulog.txt") + private val cache: MutableMap<String, ItemStack> = ConcurrentHashMap() + private val df = Schemas.getFixer() + private val dfuHandle = PrintWriter(dfuLog.writer()) var isFlawless = true + private set private fun NEUItem.get10809CompoundTag(): NbtCompound = NbtCompound().apply { put("tag", LegacyTagParser.parse(nbttag)) @@ -41,8 +48,10 @@ object ItemCache : IReloadable { 2975 ).value as NbtCompound } catch (e: Exception) { - NotEnoughUpdates.logger.error("Failed to datafixer an item", e) + if (isFlawless) + NotEnoughUpdates.logger.error("Failed to run data fixer an item. Check ${dfuLog.absolutePathString()} for more information") isFlawless = false + e.printStackTrace(dfuHandle) null } @@ -76,11 +85,12 @@ object ItemCache : IReloadable { var job: Job? = null override fun reload(repository: NEURepository) { - cache.clear() val j = job if (j != null && j.isActive) { j.cancel() } + cache.clear() + isFlawless = true job = NotEnoughUpdates.coroutineScope.launch { val items = repository.items?.items |