forked from FabricMC/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.20.5] Add back custom data ingredient (FabricMC#3642)
* [1.20.5] Add back custom data ingredient --------- Co-authored-by: modmuss50 <[email protected]>
- Loading branch information
Showing
4 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...rc/main/java/net/fabricmc/fabric/impl/recipe/ingredient/builtin/CustomDataIngredient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.impl.recipe.ingredient.builtin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.datafixers.util.Either; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.component.type.NbtComponent; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.StringNbtReader; | ||
import net.minecraft.network.RegistryByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.codec.PacketCodecs; | ||
import net.minecraft.recipe.Ingredient; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.dynamic.Codecs; | ||
|
||
import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient; | ||
import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredientSerializer; | ||
|
||
public class CustomDataIngredient implements CustomIngredient { | ||
public static final CustomIngredientSerializer<CustomDataIngredient> SERIALIZER = new Serializer(); | ||
private final Ingredient base; | ||
private final NbtCompound nbt; | ||
|
||
public CustomDataIngredient(Ingredient base, NbtCompound nbt) { | ||
if (nbt == null || nbt.isEmpty()) throw new IllegalArgumentException("NBT cannot be null; use components ingredient for strict matching"); | ||
|
||
this.base = base; | ||
this.nbt = nbt; | ||
} | ||
|
||
@Override | ||
public boolean test(ItemStack stack) { | ||
if (!base.test(stack)) return false; | ||
|
||
NbtComponent nbt = stack.get(DataComponentTypes.CUSTOM_DATA); | ||
|
||
return nbt != null && nbt.matches(this.nbt); | ||
} | ||
|
||
@Override | ||
public List<ItemStack> getMatchingStacks() { | ||
List<ItemStack> stacks = new ArrayList<>(List.of(base.getMatchingStacks())); | ||
stacks.replaceAll(stack -> { | ||
ItemStack copy = stack.copy(); | ||
copy.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, existingNbt -> NbtComponent.of(existingNbt.copyNbt().copyFrom(this.nbt))); | ||
return copy; | ||
}); | ||
stacks.removeIf(stack -> !base.test(stack)); | ||
return stacks; | ||
} | ||
|
||
@Override | ||
public boolean requiresTesting() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public CustomIngredientSerializer<?> getSerializer() { | ||
return SERIALIZER; | ||
} | ||
|
||
private Ingredient getBase() { | ||
return base; | ||
} | ||
|
||
private NbtCompound getNbt() { | ||
return nbt; | ||
} | ||
|
||
private static class Serializer implements CustomIngredientSerializer<CustomDataIngredient> { | ||
private static final Identifier ID = new Identifier("fabric", "custom_data"); | ||
|
||
// Supports decoding the NBT as a string as well as the object. | ||
private static final Codec<NbtCompound> NBT_CODEC = Codecs.xor( | ||
Codec.STRING, NbtCompound.CODEC | ||
).flatXmap(either -> either.map(s -> { | ||
try { | ||
return DataResult.success(StringNbtReader.parse(s)); | ||
} catch (CommandSyntaxException e) { | ||
return DataResult.error(e::getMessage); | ||
} | ||
}, DataResult::success), nbtCompound -> DataResult.success(Either.left(nbtCompound.asString()))); | ||
|
||
private static final Codec<CustomDataIngredient> ALLOW_EMPTY_CODEC = createCodec(Ingredient.ALLOW_EMPTY_CODEC); | ||
private static final Codec<CustomDataIngredient> DISALLOW_EMPTY_CODEC = createCodec(Ingredient.DISALLOW_EMPTY_CODEC); | ||
|
||
private static final PacketCodec<RegistryByteBuf, CustomDataIngredient> PACKET_CODEC = PacketCodec.tuple( | ||
Ingredient.PACKET_CODEC, CustomDataIngredient::getBase, | ||
PacketCodecs.NBT_COMPOUND, CustomDataIngredient::getNbt, | ||
CustomDataIngredient::new | ||
); | ||
|
||
private static Codec<CustomDataIngredient> createCodec(Codec<Ingredient> ingredientCodec) { | ||
return RecordCodecBuilder.create(instance -> | ||
instance.group( | ||
ingredientCodec.fieldOf("base").forGetter(CustomDataIngredient::getBase), | ||
NBT_CODEC.fieldOf("nbt").forGetter(CustomDataIngredient::getNbt) | ||
).apply(instance, CustomDataIngredient::new) | ||
); | ||
} | ||
|
||
@Override | ||
public Identifier getIdentifier() { | ||
return ID; | ||
} | ||
|
||
@Override | ||
public Codec<CustomDataIngredient> getCodec(boolean allowEmpty) { | ||
return allowEmpty ? ALLOW_EMPTY_CODEC : DISALLOW_EMPTY_CODEC; | ||
} | ||
|
||
@Override | ||
public PacketCodec<RegistryByteBuf, CustomDataIngredient> getPacketCodec() { | ||
return PACKET_CODEC; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters