Skip to content

Commit

Permalink
port and drop 1.20.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Jul 10, 2024
1 parent ff9d660 commit c2ecbbd
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 35 deletions.
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pluginManagement {
}

plugins {
id("dev.kikugie.stonecutter") version "0.4"
id("dev.kikugie.stonecutter") version "0.4.2"
}


Expand All @@ -27,7 +27,7 @@ extensions.configure<StonecutterSettings> {
kotlinController = true
centralScript = "build.gradle.kts"
shared {
versions("1.20.1", "1.20.4", "1.20.6", "1.21")
versions("1.20.4", "1.20.6", "1.21")
vcsVersion = "1.21"
}
create(rootProject)
Expand Down
33 changes: 28 additions & 5 deletions src/main/kotlin/dev/nyon/autodrop/AutoDrop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.nyon.autodrop.config.*
import dev.nyon.autodrop.config.screen.root.ArchiveScreen
import dev.nyon.autodrop.extensions.DataComponentPatchSerializer
import dev.nyon.autodrop.extensions.ItemSerializer
import dev.nyon.autodrop.extensions.StoredComponents
import dev.nyon.konfig.config.config
import dev.nyon.konfig.config.loadConfig
import dev.nyon.konfig.config.saveConfig
Expand All @@ -16,16 +17,17 @@ import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.KeyMapping
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.screens.inventory.InventoryScreen
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.network.chat.Component
import net.minecraft.network.chat.Style
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.inventory.ClickType
import net.minecraft.world.item.Item
import net.minecraft.world.item.enchantment.ItemEnchantments
import org.lwjgl.glfw.GLFW
import kotlin.time.Duration.Companion.milliseconds

//? if >=1.20.5
import net.minecraft.world.item.enchantment.ItemEnchantments

lateinit var mcScope: CoroutineScope
lateinit var minecraft: Minecraft

Expand Down Expand Up @@ -70,25 +72,46 @@ object AutoDrop : ClientModInitializer {
val isValid = currentItems.any { identifier ->
val typeValid = identifier.type == null || itemStack.item == identifier.type
val amountValid = itemStack.count >= identifier.amount
val componentValid =

/*? if >=1.21 {*/val componentValid =
identifier.components.isEmpty || identifier.components.entrySet().all { (key, component) ->
val identifierComponent = component.get()
val itemComponent = itemStack.get(key) ?: return@all false

if (identifierComponent as? ItemEnchantments != null && itemComponent as? ItemEnchantments != null) {
return@all identifierComponent.entrySet()
.all { entry -> // How tf is minecraft too bad to create a simple equals check - or it's me?
.all { entry -> // How tf is minecraft too bad to create a simple equals check - or is it me?
itemComponent.entrySet().map { it.key.value().description.string }
.contains(entry.key.value().description.string)
}
}

itemComponent == identifierComponent
}
/*?} else if >=1.20.5 {*//*val componentValid = identifier.components.isEmpty || identifier.components.all { component ->
val identifierComponent = component.value
val itemComponent = itemStack.get(component.type) ?: return@all false
if (identifierComponent as? ItemEnchantments != null && itemComponent as? ItemEnchantments != null) {
return@all identifierComponent.entrySet()
.all { entry -> // How tf is minecraft too bad to create a simple equals check - or is it me?
itemComponent.entrySet().map { it.key.value().descriptionId }
.contains(entry.key.value().descriptionId)
}
}
itemComponent == identifierComponent
}
*//*?} else {*/
/*val componentValid = identifier.components.allKeys.all { key ->
if (itemStack.tag == null) return@all false
itemStack.tag!!.get(key) == identifier.components.get(key)
}*//*?}*/
typeValid && amountValid && componentValid
}


if (!isValid) return@forEach

minecraft.gameMode?.handleInventoryMouseClick(
Expand All @@ -109,7 +132,7 @@ object AutoDrop : ClientModInitializer {
config(FabricLoader.getInstance().configDir.resolve("autodrop.json"), 1, Config(), jsonBuilder = {
serializersModule = SerializersModule {
contextual(Item::class, ItemSerializer)
contextual(DataComponentPatch::class, DataComponentPatchSerializer)
contextual(StoredComponents::class, DataComponentPatchSerializer)
}
}) { jsonTree, version -> migrate(jsonTree, version) }
config = loadConfig<Config>()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/nyon/autodrop/config/Config.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.nyon.autodrop.config

import dev.nyon.autodrop.extensions.StoredComponents
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.world.item.Item

/**
Expand Down Expand Up @@ -59,5 +59,5 @@ data class Archive(
*/
@Serializable
data class ItemIdentifier(
var type: @Contextual Item?, var components: @Contextual DataComponentPatch, var amount: Int
var type: @Contextual Item?, var components: @Contextual StoredComponents, var amount: Int
)
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/nyon/autodrop/config/ConfigHandler.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.nyon.autodrop.config

import dev.nyon.autodrop.extensions.emptyStoredComponents
import dev.nyon.autodrop.extensions.resourceLocation
import kotlinx.serialization.json.*
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.core.registries.BuiltInRegistries

var config: Config = Config()
Expand Down Expand Up @@ -34,7 +34,7 @@ internal fun migrate(
archiveObject["items"]?.jsonArray?.map secMap@{ content ->
val resourceLocation = resourceLocation(content.jsonPrimitive.contentOrNull ?: return null)
return@secMap ItemIdentifier(
BuiltInRegistries.ITEM.get(resourceLocation), DataComponentPatch.EMPTY, 1
BuiltInRegistries.ITEM.get(resourceLocation), emptyStoredComponents, 1
)
}?.toMutableList() ?: return null,
archiveObject["lockedSlots"]?.jsonArray?.map secMap@{ content ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class ModifyIdentifierScreen(private val parent: ArchiveScreen, private val item
Screen(screenComponent("modify.title")) {
private val matcher: () -> Boolean = {
(itemEditBox.value.isBlank() || BuiltInRegistries.ITEM.getOptional(resourceLocation(itemEditBox.value)).isPresent) && kotlin.runCatching {
DataComponentPatchSerializer.toPatch(
componentsEditBox.value
)
DataComponentPatchSerializer.toPatch(componentsEditBox.value)
}.isSuccess && amountEditBox.value.toIntOrNull().let { it != null && it in 0 .. 64 }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import dev.nyon.autodrop.config.reloadArchiveProperties
import dev.nyon.autodrop.config.screen.create.CreateArchiveScreen
import dev.nyon.autodrop.config.screen.ignored.IgnoredSlotsScreen
import dev.nyon.autodrop.config.screen.modify.ModifyIdentifierScreen
import dev.nyon.autodrop.extensions.emptyStoredComponents
import dev.nyon.autodrop.extensions.screenComponent
import dev.nyon.konfig.config.saveConfig
import net.minecraft.ChatFormatting
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Button
import net.minecraft.client.gui.screens.Screen
import net.minecraft.core.component.DataComponentPatch
import dev.nyon.autodrop.minecraft as internalMinecraft

const val INNER_PAD = 5
Expand Down Expand Up @@ -54,7 +54,7 @@ class ArchiveScreen(private val parent: Screen?) : Screen(screenComponent("title
}.build().also { addWidget(it) }

private val addIdentifierButton = Button.builder(screenComponent("identifier")) {
val newIdentifier = ItemIdentifier(null, DataComponentPatch.EMPTY, 1)
val newIdentifier = ItemIdentifier(null, emptyStoredComponents, 1)

selected.entries.add(newIdentifier)
internalMinecraft.setScreen(
Expand All @@ -72,6 +72,7 @@ class ArchiveScreen(private val parent: Screen?) : Screen(screenComponent("title

override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) {
renderBackground(guiGraphics, mouseX, mouseY, tickDelta)

archivesWidget.render(guiGraphics, mouseX, mouseY, tickDelta)

// render archive item list, if empty, render info
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/dev/nyon/autodrop/extensions/Nbt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.nyon.autodrop.extensions

import net.minecraft.nbt.CompoundTag

/*? if >=1.20.5 {*/
import net.minecraft.core.component.DataComponentMap
import net.minecraft.core.component.DataComponentPatch
/*?}*/

typealias StoredComponents = /*? if >=1.21 {*/ DataComponentPatch /*?} else if >=1.20.5 {*/ /*DataComponentMap *//*?} else {*/ /*CompoundTag *//*?}*/

val emptyStoredComponents: StoredComponents = /*? if >=1.21 {*/ DataComponentPatch.EMPTY /*?} else if >=1.20.5 {*/ /*DataComponentMap.EMPTY *//*?} else {*/ /*CompoundTag() *//*?}*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import net.minecraft.resources.ResourceLocation

fun resourceLocation(location: String): ResourceLocation {
//? if >=1.20.6
return ResourceLocation.bySeparator(location, ':')
return ResourceLocation.tryParse(location)!!

//? if <1.20.6
/*return ResourceLocation(location)*/
Expand Down
42 changes: 34 additions & 8 deletions src/main/kotlin/dev/nyon/autodrop/extensions/Serializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.minecraft.commands.arguments.item.ItemParser
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.core.registries.Registries
import net.minecraft.data.registries.VanillaRegistries
import net.minecraft.nbt.NbtOps
import net.minecraft.world.item.Item

/*? if >=1.20.5 {*/
import kotlin.jvm.optionals.getOrNull
import net.minecraft.nbt.NbtOps
/*?}*/

object ItemSerializer : KSerializer<Item> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("item", PrimitiveKind.STRING)
Expand All @@ -32,20 +35,30 @@ object ItemSerializer : KSerializer<Item> {
}
}

object DataComponentPatchSerializer : KSerializer<DataComponentPatch> {
object DataComponentPatchSerializer : KSerializer<StoredComponents> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("components", PrimitiveKind.STRING)
private val registryAccess = minecraft.connection?.registryAccess() ?: VanillaRegistries.createLookup()

/*? if >=1.20.5 {*/
private val itemParser = ItemParser(registryAccess)
private val dynamicOps = registryAccess.createSerializationContext(NbtOps.INSTANCE)
/*?}*/

fun toPatch(decoded: String): DataComponentPatch {
fun toPatch(decoded: String): StoredComponents {
/*? if >=1.20.5 {*/
val correctString = if (decoded.startsWith('[')) "stone$decoded" else decoded
val result = itemParser.parse(StringReader(correctString))
return result.components
/*?} else {*/
/*val correctString = if (decoded.startsWith('{')) "stone$decoded" else decoded
val result = ItemParser.parseForItem(registryAccess.lookupOrThrow(Registries.ITEM), StringReader(correctString))
return result.nbt ?: emptyStoredComponents
*//*?}*/
}

@Suppress("UNCHECKED_CAST")
fun toString(patch: DataComponentPatch): String {
@Suppress("UNCHECKED_CAST", "KotlinRedundantDiagnosticSuppress")
fun toString(patch: StoredComponents): String {
/*? if >=1.21 {*/
val stringMap = patch.entrySet().mapNotNull { (type, value) ->
val resourceLocation = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type)
val encoded =
Expand All @@ -55,14 +68,27 @@ object DataComponentPatchSerializer : KSerializer<DataComponentPatch> {
else "$resourceLocation=$encoded"
}
return stringMap.toString()
/*?} else if >=1.20.5 {*/
/*val stringMap = patch.mapNotNull { component ->
val resourceLocation = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(component.type)
val encoded =
(component.type.codec() as? com.mojang.serialization.Encoder<Any>)?.encodeStart(dynamicOps, component.value)
?.result()?.getOrNull()
return@mapNotNull if (resourceLocation == null || encoded == null) null
else "$resourceLocation=$encoded"
}
return stringMap.toString()
*//*?} else {*/
/*return patch.asString
*//*?}*/
}

override fun deserialize(decoder: Decoder): DataComponentPatch {
override fun deserialize(decoder: Decoder): StoredComponents {
val decoded = decoder.decodeString()
return toPatch(decoded)
}

override fun serialize(encoder: Encoder, value: DataComponentPatch) {
override fun serialize(encoder: Encoder, value: StoredComponents) {
encoder.encodeString(toString(value))
}
}
8 changes: 0 additions & 8 deletions versions/1.20.1/gradle.properties

This file was deleted.

2 changes: 1 addition & 1 deletion versions/1.20.4/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mcVersionRange=>=1.20.3 <=1.20.4
supportedMcVersions=1.20.4
deps.parchment=
deps.fapi=0.97.0+1.20.4
deps.yacl=3.4.2+1.20.4-fabric
deps.yacl=3.5.0+1.20.4-fabric
deps.modMenu=9.2.0-beta.2
javaVer=17
2 changes: 1 addition & 1 deletion versions/1.20.6/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mcVersionRange=>=1.20.5 <=1.20.6
supportedMcVersions=1.20.5,1.20.6
deps.parchment=
deps.fapi=0.97.8+1.20.6
deps.yacl=3.4.2+1.20.5-fabric
deps.yacl=3.5.0+1.20.6-fabric
deps.modMenu=10.0.0-beta.1
javaVer=21

0 comments on commit c2ecbbd

Please sign in to comment.