| 1 | package net.minecraft.world.entity.projectile.throwableitemprojectile;
|
| 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.projectile.ItemSupplier;
|
| 9 | import net.minecraft.world.entity.projectile.ThrowableProjectile;
|
| 10 | import net.minecraft.world.item.Item;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.storage.ValueInput;
|
| 14 | import net.minecraft.world.level.storage.ValueOutput;
|
| 15 |
|
| 16 | public abstract class ThrowableItemProjectile extends ThrowableProjectile implements ItemSupplier {
|
| 17 | private static final EntityDataAccessor<ItemStack> DATA_ITEM_STACK = SynchedEntityData.defineId(
|
| 18 | ThrowableItemProjectile.class, EntityDataSerializers.ITEM_STACK
|
| 19 | );
|
| 20 |
|
| 21 | public ThrowableItemProjectile(final EntityType<? extends ThrowableItemProjectile> type, final Level level) {
|
| 22 | super(type, level);
|
| 23 | }
|
| 24 |
|
| 25 | public ThrowableItemProjectile(
|
| 26 | final EntityType<? extends ThrowableItemProjectile> type, final double x, final double y, final double z, final Level level, final ItemStack itemStack
|
| 27 | ) {
|
| 28 | super(type, x, y, z, level);
|
| 29 | this.setItem(itemStack);
|
| 30 | }
|
| 31 |
|
| 32 | public ThrowableItemProjectile(
|
| 33 | final EntityType<? extends ThrowableItemProjectile> type, final LivingEntity owner, final Level level, final ItemStack itemStack
|
| 34 | ) {
|
| 35 | this(type, owner.getX(), owner.getEyeY() - 0.1F, owner.getZ(), level, itemStack);
|
| 36 | this.setOwner(owner);
|
| 37 | }
|
| 38 |
|
| 39 | public void setItem(final ItemStack source) {
|
| 40 | this.getEntityData().set(DATA_ITEM_STACK, source.copyWithCount(1));
|
| 41 | }
|
| 42 |
|
| 43 | protected abstract Item getDefaultItem();
|
| 44 |
|
| 45 | @Override
|
| 46 | public ItemStack getItem() {
|
| 47 | return this.getEntityData().get(DATA_ITEM_STACK);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 52 | entityData.define(DATA_ITEM_STACK, new ItemStack(this.getDefaultItem()));
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 57 | super.addAdditionalSaveData(output);
|
| 58 | output.store("Item", ItemStack.CODEC, this.getItem());
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 63 | super.readAdditionalSaveData(input);
|
| 64 | this.setItem(input.read("Item", ItemStack.CODEC).orElseGet(() -> new ItemStack(this.getDefaultItem())));
|
| 65 | }
|
| 66 | }
|
| 67 |
|