| 1 | package net.minecraft.world.entity.projectile.hurtingprojectile;
|
| 2 |
|
| 3 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 4 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 5 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 6 | import net.minecraft.world.entity.EntityType;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.entity.SlotAccess;
|
| 9 | import net.minecraft.world.entity.projectile.ItemSupplier;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.Items;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.storage.ValueInput;
|
| 14 | import net.minecraft.world.level.storage.ValueOutput;
|
| 15 | import net.minecraft.world.phys.Vec3;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public abstract class Fireball extends AbstractHurtingProjectile implements ItemSupplier {
|
| 19 | private static final float MIN_CAMERA_DISTANCE_SQUARED = 12.25F;
|
| 20 | private static final EntityDataAccessor<ItemStack> DATA_ITEM_STACK = SynchedEntityData.defineId(Fireball.class, EntityDataSerializers.ITEM_STACK);
|
| 21 |
|
| 22 | public Fireball(final EntityType<? extends Fireball> type, final Level level) {
|
| 23 | super(type, level);
|
| 24 | }
|
| 25 |
|
| 26 | public Fireball(final EntityType<? extends Fireball> type, final double x, final double y, final double z, final Vec3 direction, final Level level) {
|
| 27 | super(type, x, y, z, direction, level);
|
| 28 | }
|
| 29 |
|
| 30 | public Fireball(final EntityType<? extends Fireball> type, final LivingEntity mob, final Vec3 direction, final Level level) {
|
| 31 | super(type, mob, direction, level);
|
| 32 | }
|
| 33 |
|
| 34 | public void setItem(final ItemStack source) {
|
| 35 | if (source.isEmpty()) {
|
| 36 | this.getEntityData().set(DATA_ITEM_STACK, this.getDefaultItem());
|
| 37 | } else {
|
| 38 | this.getEntityData().set(DATA_ITEM_STACK, source.copyWithCount(1));
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void playEntityOnFireExtinguishedSound() {
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public ItemStack getItem() {
|
| 48 | return this.getEntityData().get(DATA_ITEM_STACK);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 53 | entityData.define(DATA_ITEM_STACK, this.getDefaultItem());
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 58 | super.addAdditionalSaveData(output);
|
| 59 | output.store("Item", ItemStack.CODEC, this.getItem());
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 64 | super.readAdditionalSaveData(input);
|
| 65 | this.setItem(input.read("Item", ItemStack.CODEC).orElse(this.getDefaultItem()));
|
| 66 | }
|
| 67 |
|
| 68 | private ItemStack getDefaultItem() {
|
| 69 | return new ItemStack(Items.FIRE_CHARGE);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public @Nullable SlotAccess getSlot(final int slot) {
|
| 74 | return slot == 0 ? SlotAccess.of(this::getItem, this::setItem) : super.getSlot(slot);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 79 | return this.tickCount < 2 && distance < 12.25 ? false : super.shouldRenderAtSqrDistance(distance);
|
| 80 | }
|
| 81 | }
|
| 82 |
|