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.Util;
import net.minecraft.world.item.Items;
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 Fireball extends AbstractHurtingProjectile implements ItemSupplier {
    private static final EntityDataAccessor<ItemStack> DATA_ITEM_STACK;
    
    public Fireball(final EntityType<? extends Fireball> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    public Fireball(final EntityType<? extends Fireball> aku, final double double2, final double double3, final double double4, final double double5, final double double6, final double double7, final Level bjw) {
        super(aku, double2, double3, double4, double5, double6, double7, bjw);
    }
    
    public Fireball(final EntityType<? extends Fireball> aku, final LivingEntity akz, final double double3, final double double4, final double double5, final Level bjw) {
        super(aku, akz, double3, double4, double5, bjw);
    }
    
    public void setItem(final ItemStack ben) {
        if (ben.getItem() != Items.FIRE_CHARGE || ben.hasTag()) {
            this.getEntityData().<ItemStack>set(Fireball.DATA_ITEM_STACK, (ItemStack)Util.<T>make((T)ben.copy(), ben -> ben.setCount(1)));
        }
    }
    
    protected ItemStack getItemRaw() {
        return this.getEntityData().<ItemStack>get(Fireball.DATA_ITEM_STACK);
    }
    
    @Override
    public ItemStack getItem() {
        final ItemStack ben2 = this.getItemRaw();
        return ben2.isEmpty() ? new ItemStack(Items.FIRE_CHARGE) : ben2;
    }
    
    @Override
    protected void defineSynchedData() {
        this.getEntityData().<ItemStack>define(Fireball.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(Fireball.class, EntityDataSerializers.ITEM_STACK);
    }
}
