package net.minecraft.world.entity.projectile;

import net.minecraft.world.entity.Entity;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Item;
import net.minecraft.Util;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.network.syncher.EntityDataAccessor;

public abstract class ThrowableItemProjectile extends ThrowableProjectile implements ItemSupplier {
    private static final EntityDataAccessor<ItemStack> DATA_ITEM_STACK;
    
    public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> aoq, final Level bqb) {
        super(aoq, bqb);
    }
    
    public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> aoq, final double double2, final double double3, final double double4, final Level bqb) {
        super(aoq, double2, double3, double4, bqb);
    }
    
    public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> aoq, final LivingEntity aoy, final Level bqb) {
        super(aoq, aoy, bqb);
    }
    
    public void setItem(final ItemStack bki) {
        if (bki.getItem() != this.getDefaultItem() || bki.hasTag()) {
            this.getEntityData().<ItemStack>set(ThrowableItemProjectile.DATA_ITEM_STACK, (ItemStack)Util.<T>make((T)bki.copy(), bki -> bki.setCount(1)));
        }
    }
    
    protected abstract Item getDefaultItem();
    
    protected ItemStack getItemRaw() {
        return this.getEntityData().<ItemStack>get(ThrowableItemProjectile.DATA_ITEM_STACK);
    }
    
    @Override
    public ItemStack getItem() {
        final ItemStack bki2 = this.getItemRaw();
        return bki2.isEmpty() ? new ItemStack(this.getDefaultItem()) : bki2;
    }
    
    @Override
    protected void defineSynchedData() {
        this.getEntityData().<ItemStack>define(ThrowableItemProjectile.DATA_ITEM_STACK, ItemStack.EMPTY);
    }
    
    public void addAdditionalSaveData(final CompoundTag le) {
        super.addAdditionalSaveData(le);
        final ItemStack bki3 = this.getItemRaw();
        if (!bki3.isEmpty()) {
            le.put("Item", bki3.save(new CompoundTag()));
        }
    }
    
    public void readAdditionalSaveData(final CompoundTag le) {
        super.readAdditionalSaveData(le);
        final ItemStack bki3 = ItemStack.of(le.getCompound("Item"));
        this.setItem(bki3);
    }
    
    static {
        DATA_ITEM_STACK = SynchedEntityData.<ItemStack>defineId(ThrowableItemProjectile.class, EntityDataSerializers.ITEM_STACK);
    }
}
