Something went wrong. Try again.
Because why would Drowned have infinitely many tridents? Forked from gitlab.com/ananasem/one-trident-throw-only
Something went wrong. Try again.
one-trident-throw-only src main java eu comosus ananas onetridentthrowonly component OriginalTridentOwner.java
3.3 kB · 77 lines
Java
at dev/26.3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778package eu.comosus.ananas.onetridentthrowonly.component;
import com.mojang.serialization.Codec;import com.mojang.serialization.MapCodec;import com.mojang.serialization.codecs.RecordCodecBuilder;import io.netty.buffer.ByteBuf;import net.minecraft.core.UUIDUtil;import net.minecraft.core.component.DataComponentMap;import net.minecraft.core.component.DataComponents;import net.minecraft.nbt.CompoundTag;import net.minecraft.network.codec.ByteBufCodecs;import net.minecraft.network.codec.StreamCodec;import net.minecraft.util.ExtraCodecs;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.component.CustomData;
import java.util.Optional;import java.util.UUID;
public record OriginalTridentOwner(String name, UUID playerUUID) { public static final Codec<OriginalTridentOwner> CODEC = RecordCodecBuilder.create( instance -> instance.group( ExtraCodecs.PLAYER_NAME.fieldOf("name").forGetter(OriginalTridentOwner::name), UUIDUtil.CODEC.fieldOf("id").forGetter(OriginalTridentOwner::playerUUID) ) .apply(instance, OriginalTridentOwner::new) );
private static final MapCodec<OriginalTridentOwner> INNER_OBJECT_CODEC = RecordCodecBuilder.mapCodec( instance -> instance.group( ExtraCodecs.PLAYER_NAME.fieldOf("name").forGetter(OriginalTridentOwner::name), UUIDUtil.CODEC.fieldOf("id").forGetter(OriginalTridentOwner::playerUUID) ) .apply(instance, OriginalTridentOwner::new) );
public static final MapCodec<OriginalTridentOwner> OBJECT_CODEC = INNER_OBJECT_CODEC.fieldOf("original_trident_owner");
public static final StreamCodec<ByteBuf, OriginalTridentOwner> STREAM_CODEC = StreamCodec.composite( ByteBufCodecs.STRING_UTF8, OriginalTridentOwner::name, UUIDUtil.STREAM_CODEC, OriginalTridentOwner::playerUUID, OriginalTridentOwner::new );
public OriginalTridentOwner(CompoundTag compoundTag) { this(compoundTag.getString("name").orElseThrow(), compoundTag.read("id", UUIDUtil.CODEC).orElseThrow()); }
public OriginalTridentOwner getOriginalOwner(ItemStack stack) { DataComponentMap itemComponents = stack.getComponents(); CustomData customData = itemComponents.get(DataComponents.CUSTOM_DATA); if (customData == null || customData.isEmpty()) { return null; } Optional<OriginalTridentOwner> name = customData.copyTag().read(OBJECT_CODEC); return name.orElse(null); }
public DataComponentMap getComponentMap() { CompoundTag compoundTag = new CompoundTag(); compoundTag.putString("name", this.name); compoundTag.store("id", UUIDUtil.CODEC, this.playerUUID); return DataComponentMap.builder() .set(DataComponents.CUSTOM_DATA, CustomData.of(compoundTag)) .build(); }
public CompoundTag getCompoundTag() { CompoundTag compoundTag = new CompoundTag(); compoundTag.putString("name", this.name); compoundTag.store("id", UUIDUtil.CODEC, this.playerUUID); return compoundTag; }}