minecraft-src/net/minecraft/world/LockCode.java
2025-07-04 02:00:41 +03:00

39 lines
1.4 KiB
Java

package net.minecraft.world;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
public record LockCode(ItemPredicate predicate) {
public static final LockCode NO_LOCK = new LockCode(ItemPredicate.Builder.item().build());
public static final Codec<LockCode> CODEC = ItemPredicate.CODEC.xmap(LockCode::new, LockCode::predicate);
public static final String TAG_LOCK = "lock";
public boolean unlocksWith(ItemStack stack) {
return this.predicate.test(stack);
}
public void addToTag(CompoundTag compoundTag, HolderLookup.Provider provider) {
if (this != NO_LOCK) {
DataResult<Tag> dataResult = CODEC.encode(this, provider.createSerializationContext(NbtOps.INSTANCE), new CompoundTag());
dataResult.result().ifPresent(tag -> compoundTag.put("lock", tag));
}
}
public static LockCode fromTag(CompoundTag compoundTag, HolderLookup.Provider provider) {
if (compoundTag.contains("lock", 10)) {
DataResult<Pair<LockCode, Tag>> dataResult = CODEC.decode(provider.createSerializationContext(NbtOps.INSTANCE), compoundTag.get("lock"));
if (dataResult.isSuccess()) {
return dataResult.getOrThrow().getFirst();
}
}
return NO_LOCK;
}
}