From c0eb625a9b937bcb9256e98afb80d32443daa795 Mon Sep 17 00:00:00 2001 From: inglettronald Date: Fri, 9 Jun 2023 16:55:24 -0500 Subject: 1.20 refactor --- .../dulkirfabric/mixin/io/HandledScreenMixin.java | 48 +++++++++++++++++ .../com/dulkirfabric/mixin/render/ScreenMixin.java | 9 ++++ .../dulkirfabric/mixin/render/TooltipMixin.java | 8 +++ .../kotlin/com/dulkirfabric/DulkirModFabric.kt | 1 - src/main/kotlin/com/dulkirfabric/Registrations.kt | 3 +- .../kotlin/com/dulkirfabric/config/DulkirConfig.kt | 60 +++++++++++----------- .../kotlin/com/dulkirfabric/config/ListHelper.kt | 4 -- .../config/serializations/KeySerializer.kt | 22 ++++++++ .../com/dulkirfabric/events/ClientTickEvent.kt | 9 ++-- .../dulkirfabric/events/InventoryKeyPressEvent.kt | 5 ++ .../com/dulkirfabric/events/SlotRenderEvent.kt | 29 +++++++++++ .../com/dulkirfabric/events/WidgetInitEvent.kt | 24 --------- .../com/dulkirfabric/events/WorldLoadEvent.kt | 8 --- .../dulkirfabric/events/base/CancellableEvent.kt | 25 +++++++++ .../kotlin/com/dulkirfabric/events/base/Event.kt | 9 ++++ src/main/resources/dulkirmod-fabric.mixins.json | 1 + src/main/resources/fabric.mod.json | 4 +- 17 files changed, 191 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/dulkirfabric/mixin/io/HandledScreenMixin.java create mode 100644 src/main/java/com/dulkirfabric/mixin/render/TooltipMixin.java create mode 100644 src/main/kotlin/com/dulkirfabric/config/serializations/KeySerializer.kt create mode 100644 src/main/kotlin/com/dulkirfabric/events/InventoryKeyPressEvent.kt create mode 100644 src/main/kotlin/com/dulkirfabric/events/SlotRenderEvent.kt delete mode 100644 src/main/kotlin/com/dulkirfabric/events/WidgetInitEvent.kt delete mode 100644 src/main/kotlin/com/dulkirfabric/events/WorldLoadEvent.kt create mode 100644 src/main/kotlin/com/dulkirfabric/events/base/CancellableEvent.kt create mode 100644 src/main/kotlin/com/dulkirfabric/events/base/Event.kt (limited to 'src') diff --git a/src/main/java/com/dulkirfabric/mixin/io/HandledScreenMixin.java b/src/main/java/com/dulkirfabric/mixin/io/HandledScreenMixin.java new file mode 100644 index 0000000..430faa0 --- /dev/null +++ b/src/main/java/com/dulkirfabric/mixin/io/HandledScreenMixin.java @@ -0,0 +1,48 @@ +package com.dulkirfabric.mixin.io; + +import com.dulkirfabric.events.InventoryKeyPressEvent; +import com.dulkirfabric.events.SlotRenderEvent; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.screen.slot.Slot; +import net.minecraft.screen.slot.SlotActionType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(HandledScreen.class) +public class HandledScreenMixin { + @Inject(method = "keyPressed", at = @At(value = "INVOKE", + target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;handleHotbarKeyPressed(II)Z", + shift = At.Shift.BEFORE), cancellable = true) + public void onKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { + if (new InventoryKeyPressEvent(keyCode, scanCode, modifiers).post()) { + cir.setReturnValue(true); + } + } + + /**@Inject(method = "onMouseClick(Lnet/minecraft/screen/slot/Slot;IILnet/minecraft/screen/slot/SlotActionType;)V", + at = @At("HEAD"), cancellable = true) + public void onMouseClickedSlot(Slot slot, int slotId, int button, SlotActionType actionType, CallbackInfo ci) { + if (IsSlotProtectedEvent.shouldBlockInteraction(slot)) { + ci.cancel(); + } + }*/ + + @Inject(method = "render", at = @At(value = "INVOKE", + target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawSlot(Lnet/minecraft/client/gui/DrawContext;Lnet/minecraft/screen/slot/Slot;)V", + shift = At.Shift.AFTER), locals = LocalCapture.CAPTURE_FAILHARD) + public void onAfterDrawSlot(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci, int i, int j, int k, Slot slot) { + SlotRenderEvent.After event = new SlotRenderEvent.After(context, slot, mouseX, mouseY, delta); + event.post(); + } + + @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;drawSlot(Lnet/minecraft/client/gui/DrawContext;Lnet/minecraft/screen/slot/Slot;)V", shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD) + public void onBeforeDrawSlot(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci, int i, int j, int k, Slot slot) { + SlotRenderEvent.Before event = new SlotRenderEvent.Before(context, slot, mouseX, mouseY, delta); + event.post(); + } +} diff --git a/src/main/java/com/dulkirfabric/mixin/render/ScreenMixin.java b/src/main/java/com/dulkirfabric/mixin/render/ScreenMixin.java index a29cfbc..56ed0f9 100644 --- a/src/main/java/com/dulkirfabric/mixin/render/ScreenMixin.java +++ b/src/main/java/com/dulkirfabric/mixin/render/ScreenMixin.java @@ -13,6 +13,7 @@ package com.dulkirfabric.mixin.render; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Drawable; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; @@ -20,10 +21,18 @@ import net.minecraft.client.gui.screen.Screen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(Screen.class) public abstract class ScreenMixin { @Shadow protected abstract T addDrawableChild(T drawableElement); + + @Inject(method = "resize", at = @At("HEAD")) + private void beforeResizeScreen(MinecraftClient client, int width, int height, CallbackInfo ci) { + + } } \ No newline at end of file diff --git a/src/main/java/com/dulkirfabric/mixin/render/TooltipMixin.java b/src/main/java/com/dulkirfabric/mixin/render/TooltipMixin.java new file mode 100644 index 0000000..b484885 --- /dev/null +++ b/src/main/java/com/dulkirfabric/mixin/render/TooltipMixin.java @@ -0,0 +1,8 @@ +package com.dulkirfabric.mixin.render; + +import net.minecraft.client.gui.tooltip.Tooltip; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(Tooltip.class) +public class TooltipMixin { +} diff --git a/src/main/kotlin/com/dulkirfabric/DulkirModFabric.kt b/src/main/kotlin/com/dulkirfabric/DulkirModFabric.kt index 95e3eb2..e32e50a 100644 --- a/src/main/kotlin/com/dulkirfabric/DulkirModFabric.kt +++ b/src/main/kotlin/com/dulkirfabric/DulkirModFabric.kt @@ -14,7 +14,6 @@ package com.dulkirfabric import com.dulkirfabric.config.DulkirConfig -import com.dulkirfabric.events.WidgetInitEvent import meteordevelopment.orbit.EventBus import meteordevelopment.orbit.EventHandler import net.fabricmc.api.ModInitializer diff --git a/src/main/kotlin/com/dulkirfabric/Registrations.kt b/src/main/kotlin/com/dulkirfabric/Registrations.kt index 1d99640..8a90f7b 100644 --- a/src/main/kotlin/com/dulkirfabric/Registrations.kt +++ b/src/main/kotlin/com/dulkirfabric/Registrations.kt @@ -40,8 +40,7 @@ object Registrations { fun registerEvents() { // Register Custom Tick event, so we can use it like 1.8.9 forge ClientTickEvents.START_CLIENT_TICK.register( - ClientTickEvents.StartTick { _ -> EVENT_BUS.post(ClientTickEvent.get()) } + ClientTickEvents.StartTick { _ -> ClientTickEvent.post() } ) - // WorldLoadFinishedEvent TODO } } \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/config/DulkirConfig.kt b/src/main/kotlin/com/dulkirfabric/config/DulkirConfig.kt index 9aeb190..a34016d 100644 --- a/src/main/kotlin/com/dulkirfabric/config/DulkirConfig.kt +++ b/src/main/kotlin/com/dulkirfabric/config/DulkirConfig.kt @@ -10,25 +10,27 @@ * * You may add additional accurate notices of copyright ownership. */ - +@file:UseSerializers(com.dulkirfabric.config.serializations.KeySerializer::class) package com.dulkirfabric.config import com.dulkirfabric.DulkirModFabric.mc import com.dulkirfabric.config.ListHelper.mkKeyField import com.dulkirfabric.config.ListHelper.mkStringField -import com.google.gson.Gson import kotlinx.serialization.Serializable -import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.annotation.SerializedName +import kotlinx.serialization.UseSerializers +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json import me.shedaniel.clothconfig2.api.ConfigBuilder import net.minecraft.client.gui.screen.Screen import net.minecraft.client.util.InputUtil +import net.minecraft.client.util.InputUtil.UNKNOWN_KEY import net.minecraft.text.LiteralTextContent import net.minecraft.text.MutableText import net.minecraft.text.Text import net.minecraft.util.Formatting import net.minecraft.util.Identifier import java.io.File -import java.util.* class DulkirConfig { @@ -46,15 +48,15 @@ class DulkirConfig { val entryBuilder = builder.entryBuilder() val general = builder.getOrCreateCategory(Text.literal("General")) general.addEntry( - entryBuilder.startBooleanToggle(Text.literal("Custom Inventory Scale Toggle"), invScaleBool) + entryBuilder.startBooleanToggle(Text.literal("Custom Inventory Scale Toggle"), configOptions.invScaleBool) .setTooltip(Text.literal("WAHOO!")) - .setSaveConsumer { newValue -> invScaleBool = newValue } + .setSaveConsumer { newValue -> configOptions.invScaleBool = newValue } .build() ) general.addEntry( - entryBuilder.startIntSlider(Text.literal("Inventory Scale"), inventoryScale, 1, 5) + entryBuilder.startIntSlider(Text.literal("Inventory Scale"), configOptions.inventoryScale, 1, 5) .setTooltip(Text.literal("Size of GUI whenever you're in an inventory screen")) - .setSaveConsumer { newValue -> inventoryScale = newValue } + .setSaveConsumer { newValue -> configOptions.inventoryScale = newValue } .build() ) @@ -62,8 +64,8 @@ class DulkirConfig { shortcuts.addEntry( ListHelper.mkConfigList( Text.literal("Macros"), - ListHelper.Holder::macros, - { Macro(InputUtil.UNKNOWN_KEY, "") }, + configOptions::macrosList, + { Macro(UNKNOWN_KEY, "") }, Text.literal("Macro"), { value -> listOf( @@ -79,11 +81,9 @@ class DulkirConfig { } data class ConfigOptions( - @SerializedName("testOption") - val invScaleBool: Boolean, - - @SerializedName("inventoryScale") - val inventoryScale: Int + var invScaleBool: Boolean = true, + var inventoryScale: Int = 1, + var macrosList: List = listOf(Macro(UNKNOWN_KEY, "")) ) @Serializable @@ -96,38 +96,36 @@ class DulkirConfig { * Object for storing all the actual config values that will be used in doing useful stuff with the config */ companion object ConfigVars { - var invScaleBool: Boolean = true - var inventoryScale: Int = 1 - var value: List> = listOf(Pair(1, 2), Pair(3, 4)) - + var configOptions = ConfigOptions() private fun saveConfig() { - val gson = Gson() - val configOptions = ConfigOptions( - invScaleBool, - inventoryScale) - val json = gson.toJson(configOptions) + val json = Json { + prettyPrint = true + ignoreUnknownKeys = true + encodeDefaults = true + } val configDirectory = File(mc.runDirectory, "config") if (!configDirectory.exists()) { configDirectory.mkdir() } val configFile = File(configDirectory, "dulkirConfig.json") - configFile.writeText(json) + configFile.writeText(json.encodeToString(configOptions)) } fun loadConfig() { - val gson = Gson() val configDir = File(mc.runDirectory, "config") if (!configDir.exists()) return val configFile = File(configDir, "dulkirConfig.json") if (configFile.exists()) { - val json = configFile.readText() - val configOptions = gson.fromJson(json, ConfigOptions::class.java) - - invScaleBool = configOptions.invScaleBool - inventoryScale = configOptions.inventoryScale + val json = Json { + prettyPrint = true + ignoreUnknownKeys = true + encodeDefaults = true + } + configOptions = json.decodeFromString(configFile.readText()) } + } } diff --git a/src/main/kotlin/com/dulkirfabric/config/ListHelper.kt b/src/main/kotlin/com/dulkirfabric/config/ListHelper.kt index 3476bcc..8f50943 100644 --- a/src/main/kotlin/com/dulkirfabric/config/ListHelper.kt +++ b/src/main/kotlin/com/dulkirfabric/config/ListHelper.kt @@ -35,10 +35,6 @@ object ListHelper { ) } - object Holder { - var macros = listOf(DulkirConfig.Macro(InputUtil.UNKNOWN_KEY, "Hello World")) - } - fun ConfigEntryBuilder.mkStringField(text: Text, prop: KMutableProperty0) = startStrField(text, prop.get()) .setSaveConsumer { prop.set(it) } .setDefaultValue("") diff --git a/src/main/kotlin/com/dulkirfabric/config/serializations/KeySerializer.kt b/src/main/kotlin/com/dulkirfabric/config/serializations/KeySerializer.kt new file mode 100644 index 0000000..a737bf0 --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/config/serializations/KeySerializer.kt @@ -0,0 +1,22 @@ +package com.dulkirfabric.config.serializations + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import net.minecraft.client.util.InputUtil +import org.lwjgl.glfw.GLFW + +object KeySerializer: KSerializer { + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InputUtil.Key", PrimitiveKind.INT) + + override fun deserialize(decoder: Decoder): InputUtil.Key { + return InputUtil.fromKeyCode(GLFW.GLFW_KEY_UNKNOWN, decoder.decodeInt()) + } + + override fun serialize(encoder: Encoder, value: InputUtil.Key) { + encoder.encodeInt(value.code) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/ClientTickEvent.kt b/src/main/kotlin/com/dulkirfabric/events/ClientTickEvent.kt index f24fdf7..d8184f1 100644 --- a/src/main/kotlin/com/dulkirfabric/events/ClientTickEvent.kt +++ b/src/main/kotlin/com/dulkirfabric/events/ClientTickEvent.kt @@ -1,8 +1,5 @@ package com.dulkirfabric.events -object ClientTickEvent { - @JvmStatic - fun get(): ClientTickEvent { - return this - } -} \ No newline at end of file +import com.dulkirfabric.events.base.Event + +object ClientTickEvent: Event() \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/InventoryKeyPressEvent.kt b/src/main/kotlin/com/dulkirfabric/events/InventoryKeyPressEvent.kt new file mode 100644 index 0000000..f4f9e13 --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/events/InventoryKeyPressEvent.kt @@ -0,0 +1,5 @@ +package com.dulkirfabric.events + +import com.dulkirfabric.events.base.CancellableEvent + +data class InventoryKeyPressEvent(val keyCode: Int, val scanCode: Int, val modifiers: Int): CancellableEvent() \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/SlotRenderEvent.kt b/src/main/kotlin/com/dulkirfabric/events/SlotRenderEvent.kt new file mode 100644 index 0000000..3670998 --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/events/SlotRenderEvent.kt @@ -0,0 +1,29 @@ +package com.dulkirfabric.events + +import com.dulkirfabric.events.base.CancellableEvent +import net.minecraft.client.gui.DrawContext +import net.minecraft.screen.slot.Slot + +interface SlotRenderEvent { + val context: DrawContext + val slot: Slot + val mouseX: Int + val mouseY: Int + val delta: Float + + data class Before( + override val context: DrawContext, override val slot: Slot, + override val mouseX: Int, + override val mouseY: Int, + override val delta: Float + ) : CancellableEvent(), + SlotRenderEvent + + data class After( + override val context: DrawContext, override val slot: Slot, + override val mouseX: Int, + override val mouseY: Int, + override val delta: Float + ) : CancellableEvent(), + SlotRenderEvent +} \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/WidgetInitEvent.kt b/src/main/kotlin/com/dulkirfabric/events/WidgetInitEvent.kt deleted file mode 100644 index 9a6eebb..0000000 --- a/src/main/kotlin/com/dulkirfabric/events/WidgetInitEvent.kt +++ /dev/null @@ -1,24 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * If it is not possible or desirable to put the notice in a particular - * file, then You may include the notice in a location (such as a LICENSE - * file in a relevant directory) where a recipient would be likely to look - * for such a notice. - * - * You may add additional accurate notices of copyright ownership. - */ - -package com.dulkirfabric.events - -object WidgetInitEvent { - var initialized = false - - @JvmStatic - fun get(initialized: Boolean): WidgetInitEvent { - this.initialized = initialized - return this - } -} \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/WorldLoadEvent.kt b/src/main/kotlin/com/dulkirfabric/events/WorldLoadEvent.kt deleted file mode 100644 index 99039a8..0000000 --- a/src/main/kotlin/com/dulkirfabric/events/WorldLoadEvent.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.dulkirfabric.events - -object WorldLoadEvent { - @JvmStatic - fun get(): WorldLoadEvent { - return this - } -} \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/base/CancellableEvent.kt b/src/main/kotlin/com/dulkirfabric/events/base/CancellableEvent.kt new file mode 100644 index 0000000..1c1ddcd --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/events/base/CancellableEvent.kt @@ -0,0 +1,25 @@ +package com.dulkirfabric.events.base + +import com.dulkirfabric.DulkirModFabric +import meteordevelopment.orbit.ICancellable + +abstract class CancellableEvent: ICancellable { + + var cancelled: Boolean = false + + override fun isCancelled(): Boolean { + return cancelled + } + + override fun setCancelled(cancelled: Boolean) { + this.cancelled = cancelled + } + + /** + * Posts a given event to the bus and returns whether the user wishes to cancel it + */ + fun post(): Boolean { + DulkirModFabric.EVENT_BUS.post(this) + return cancelled + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/dulkirfabric/events/base/Event.kt b/src/main/kotlin/com/dulkirfabric/events/base/Event.kt new file mode 100644 index 0000000..7671199 --- /dev/null +++ b/src/main/kotlin/com/dulkirfabric/events/base/Event.kt @@ -0,0 +1,9 @@ +package com.dulkirfabric.events.base + +import com.dulkirfabric.DulkirModFabric + +abstract class Event { + fun post() { + DulkirModFabric.EVENT_BUS.post(this) + } +} \ No newline at end of file diff --git a/src/main/resources/dulkirmod-fabric.mixins.json b/src/main/resources/dulkirmod-fabric.mixins.json index 3bd03a1..dc450d3 100644 --- a/src/main/resources/dulkirmod-fabric.mixins.json +++ b/src/main/resources/dulkirmod-fabric.mixins.json @@ -8,6 +8,7 @@ "client": [ "render.GameMenuScreenMixin", "render.GameRendererMixin", + "io.HandledScreenMixin", "render.ScreenMixin" ] } \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index e9fb65d..070d281 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -28,9 +28,9 @@ ], "depends": { "fabricloader": ">=0.14.21", - "minecraft": "~1.19.4", + "minecraft": "~1.20", "java": ">=17", - "fabric-api": "*", + "fabric-api": "0.83.0+1.20", "fabric-language-kotlin": ">=1.8.21" }, "suggests": { -- cgit