Skip to content

Commit

Permalink
Add a simplified version of NBT Crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Nov 5, 2023
1 parent 8cabc31 commit d827305
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,16 @@ tasks {
}
}

test {
useJUnitPlatform()
license {
rule(file("codeformat/QUILT_MODIFIED_HEADER"))
rule(file("codeformat/HEADER"))

license {
rule(file("codeformat/QUILT_MODIFIED_HEADER"))
rule(file("codeformat/HEADER"))
include("**//*.java")
include("**//*.kt")
}

include("**//*.java")
include("**//*.kt")
}
test {
useJUnitPlatform()
}

shadowJar {
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
loader_version=0.14.23

# Mod Properties
mod_version = 1.4
mod_version = 1.4.1
mod_loader = Fabric
maven_group = net.frozenblock
archives_base_name = FrozenLib

# Dependencies
fabric_api_version=0.90.0+1.20.2
fabric_kotlin_version=1.10.11+kotlin.1.9.20
fabric_kotlin_version=1.10.13+kotlin.1.9.20
# https://github.com/LlamaLad7/MixinExtras/releases
mixin_extras_version=0.2.1-beta.2
fabric_asm_version=v2.3
toml4j_version=0.7.2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 FrozenBlock
* This file is part of FrozenLib.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/

package net.frozenblock.lib.recipe.mixin;

import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipeCodecs;
import org.spongepowered.asm.mixin.Final;
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.Redirect;
import java.util.Optional;
import java.util.function.Function;

/**
* Adds {@link CompoundTag} data support to crafting recipes.
* @since 1.4.1
*/
@Mixin(CraftingRecipeCodecs.class)
public class CraftingRecipeCodecsMixin {

@Shadow
@Final
private static Codec<Item> ITEM_NONAIR_CODEC;

@Redirect(method = "<clinit>", at = @At(value = "INVOKE", target = "Lcom/mojang/serialization/codecs/RecordCodecBuilder;create(Ljava/util/function/Function;)Lcom/mojang/serialization/Codec;", ordinal = 0))
private static Codec<ItemStack> newItemStackCodec(Function<RecordCodecBuilder.Instance<ItemStack>, ? extends App<RecordCodecBuilder.Mu<ItemStack>, ItemStack>> builder) {
return RecordCodecBuilder.create(instance ->
instance.group(
ITEM_NONAIR_CODEC.fieldOf("item").forGetter(ItemStack::getItem),
ExtraCodecs.strictOptionalField(ExtraCodecs.POSITIVE_INT, "count", 1).forGetter(ItemStack::getCount),
CompoundTag.CODEC.optionalFieldOf("data").forGetter(stack -> Optional.ofNullable(stack.getTag()))
).apply(instance, ItemStack::new)
);
}
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"mixin/frozenlib.menu.mixins.json",
"mixin/frozenlib.mobcategory.mixins.json",
"mixin/frozenlib.modmenu.mixins.json",
"mixin/frozenlib.recipe.mixins.json",
"mixin/frozenlib.screenshake.mixins.json",
"mixin/frozenlib.shovel.mixins.json",
"mixin/frozenlib.sound.mixins.json",
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/mixin/frozenlib.recipe.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"minVersion": "0.8",
"package": "net.frozenblock.lib.recipe.mixin",
"compatibilityLevel": "JAVA_17",
"injectors": {
"defaultRequire": 1
},
"mixins": [
"CraftingRecipeCodecsMixin"
]
}

0 comments on commit d827305

Please sign in to comment.