Skip to content

Commit

Permalink
fix: try to fix custom data
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Apr 26, 2024
1 parent 801cfaf commit 97cc106
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2024
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.invoker;

import net.minecraft.component.DataComponentType;
import net.minecraft.component.DataComponentTypes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

import java.util.function.UnaryOperator;

@Mixin(DataComponentTypes.class)
public interface DataComponentTypesInvoker {
@Invoker("register")
static <T> DataComponentType<T> invokeRegister(String id, UnaryOperator<DataComponentType.Builder<T>> builderOperator) {
throw new AssertionError();
}
}
14 changes: 12 additions & 2 deletions src/main/kotlin/one/oktw/galaxy/item/CustomItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ package one.oktw.galaxy.item
import net.minecraft.component.DataComponentTypes
import net.minecraft.component.type.AttributeModifiersComponent
import net.minecraft.component.type.CustomModelDataComponent
import net.minecraft.component.type.NbtComponent
import net.minecraft.component.type.UnbreakableComponent
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import one.oktw.galaxy.mixin.invoker.DataComponentTypesInvoker
import one.oktw.galaxy.util.CustomRegistry
import one.oktw.galaxy.util.Registrable

abstract class CustomItem(override val identifier: Identifier, private val baseItem: Item, private val modelData: Int) : Registrable {
companion object {
val registry = CustomRegistry<CustomItem>()

val galaxyDataComponent = DataComponentTypesInvoker.invokeRegister("galaxy_data") { builder ->
builder.codec(NbtComponent.CODEC)
}

init {
Button
Gui
Expand Down Expand Up @@ -67,8 +73,12 @@ abstract class CustomItem(override val identifier: Identifier, private val baseI
set(DataComponentTypes.CUSTOM_MODEL_DATA, CustomModelDataComponent(modelData))
set(DataComponentTypes.UNBREAKABLE, UnbreakableComponent(false))
set(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent(listOf<AttributeModifiersComponent.Entry>(), false))
set(DataComponentTypes.CUSTOM_NAME, this@CustomItem.getName())
writeCustomNbt(getOrCreateSubNbt("GalaxyData"))
set(DataComponentTypes.ITEM_NAME, this@CustomItem.getName())

// Galaxy Data
val galaxyNbt = CustomItemHelper.getNbt(this) ?: NbtComponent.DEFAULT.copyNbt()
writeCustomNbt(galaxyNbt)
set(galaxyDataComponent, NbtComponent.of(galaxyNbt))
}.also { if (cacheable) cacheItemStack = it.copy() }
}
}
11 changes: 9 additions & 2 deletions src/main/kotlin/one/oktw/galaxy/item/CustomItemHelper.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
* Copyright (C) 2018-2024
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand All @@ -19,11 +19,18 @@
package one.oktw.galaxy.item

import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtCompound
import net.minecraft.util.Identifier
import one.oktw.galaxy.item.CustomItem.Companion.galaxyDataComponent

object CustomItemHelper {
fun getNbt(itemStack: ItemStack): NbtCompound? {
val galaxyData = itemStack.get(galaxyDataComponent)
return galaxyData?.copyNbt()
}

fun getItem(itemStack: ItemStack): CustomItem? {
val customNbt = itemStack.getSubNbt("GalaxyData")
val customNbt = getNbt(itemStack)

return customNbt?.getString("CustomItemIdentifier")?.let(Identifier::tryParse)
?.let(CustomItem.registry::get)
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"tweak.mixin.json",
"sponge.realtime.mixin.json",
"accessor.mixin.json",
"recipe.mixin.json"
"recipe.mixin.json",
"invoker.mixin.json"
],
"depends": {
"minecraft": "1.20.x",
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/invoker.mixin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"package": "one.oktw.galaxy.mixin.invoker",
"compatibilityLevel": "JAVA_21",
"mixins": [
"DataComponentTypesInvoker"
],
"client": [],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 97cc106

Please sign in to comment.