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> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> aku, final double double2, final double double3, final double double4, final Level bjw) {
        super(aku, double2, double3, double4, bjw);
    }
    
    public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> aku, final LivingEntity akz, final Level bjw) {
        super(aku, akz, bjw);
    }
    
    public void setItem(final ItemStack ben) {
        if (ben.getItem() != this.getDefaultItem() || ben.hasTag()) {
            this.getEntityData().<ItemStack>set(ThrowableItemProjectile.DATA_ITEM_STACK, (ItemStack)Util.<T>make((T)ben.copy(), ben -> ben.setCount(1)));
        }
    }
    
    protected abstract Item getDefaultItem();
    
    protected ItemStack getItemRaw() {
        return this.getEntityData().<ItemStack>get(ThrowableItemProjectile.DATA_ITEM_STACK);
    }
    
    @Override
    public ItemStack getItem() {
        final ItemStack ben2 = this.getItemRaw();
        return ben2.isEmpty() ? new ItemStack(this.getDefaultItem()) : ben2;
    }
    
    @Override
    protected void defineSynchedData() {
        this.getEntityData().<ItemStack>define(ThrowableItemProjectile.DATA_ITEM_STACK, ItemStack.EMPTY);
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag jt) {
        super.addAdditionalSaveData(jt);
        final ItemStack ben3 = this.getItemRaw();
        if (!ben3.isEmpty()) {
            jt.put("Item", ben3.save(new CompoundTag()));
        }
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag jt) {
        super.readAdditionalSaveData(jt);
        final ItemStack ben3 = ItemStack.of(jt.getCompound("Item"));
        this.setItem(ben3);
    }
    
    static {
        DATA_ITEM_STACK = SynchedEntityData.<ItemStack>defineId(ThrowableItemProjectile.class, EntityDataSerializers.ITEM_STACK);
    }
}
