| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 5 | import net.minecraft.core.component.DataComponents;
|
| 6 | import net.minecraft.nbt.CompoundTag;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.world.InteractionHand;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.ItemUtils;
|
| 14 | import net.minecraft.world.item.Items;
|
| 15 | import net.minecraft.world.item.component.CustomData;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 |
|
| 18 | public interface Bucketable {
|
| 19 | boolean fromBucket();
|
| 20 |
|
| 21 | void setFromBucket(final boolean fromBucket);
|
| 22 |
|
| 23 | void saveToBucketTag(final ItemStack bucket);
|
| 24 |
|
| 25 | void loadFromBucketTag(final CompoundTag tag);
|
| 26 |
|
| 27 | ItemStack getBucketItemStack();
|
| 28 |
|
| 29 | SoundEvent getPickupSound();
|
| 30 |
|
| 31 | @Deprecated
|
| 32 | static void saveDefaultDataToBucketTag(final Mob entity, final ItemStack bucket) {
|
| 33 | bucket.copyFrom(DataComponents.CUSTOM_NAME, entity);
|
| 34 | CustomData.update(DataComponents.BUCKET_ENTITY_DATA, bucket, tag -> {
|
| 35 | if (entity.isNoAi()) {
|
| 36 | tag.putBoolean("NoAI", entity.isNoAi());
|
| 37 | }
|
| 38 |
|
| 39 | if (entity.isSilent()) {
|
| 40 | tag.putBoolean("Silent", entity.isSilent());
|
| 41 | }
|
| 42 |
|
| 43 | if (entity.isNoGravity()) {
|
| 44 | tag.putBoolean("NoGravity", entity.isNoGravity());
|
| 45 | }
|
| 46 |
|
| 47 | if (entity.hasGlowingTag()) {
|
| 48 | tag.putBoolean("Glowing", entity.hasGlowingTag());
|
| 49 | }
|
| 50 |
|
| 51 | if (entity.isPermanentlyInvulnerable()) {
|
| 52 | tag.putBoolean("Invulnerable", entity.isPermanentlyInvulnerable());
|
| 53 | }
|
| 54 |
|
| 55 | if (entity.isPersistenceRequired()) {
|
| 56 | tag.putBoolean("PersistenceRequired", entity.isPersistenceRequired());
|
| 57 | }
|
| 58 |
|
| 59 | tag.putFloat("Health", entity.getHealth());
|
| 60 | });
|
| 61 | }
|
| 62 |
|
| 63 | @Deprecated
|
| 64 | static void loadDefaultDataFromBucketTag(final Mob entity, final CompoundTag tag) {
|
| 65 | tag.getBoolean("NoAI").ifPresent(entity::setNoAi);
|
| 66 | tag.getBoolean("Silent").ifPresent(entity::setSilent);
|
| 67 | tag.getBoolean("NoGravity").ifPresent(entity::setNoGravity);
|
| 68 | tag.getBoolean("Glowing").ifPresent(entity::setGlowingTag);
|
| 69 | tag.getBoolean("Invulnerable").ifPresent(entity::setPermanentlyInvulnerable);
|
| 70 | tag.getBoolean("PersistenceRequired").ifPresent(required -> {
|
| 71 | if (required) {
|
| 72 | entity.setPersistenceRequired();
|
| 73 | }
|
| 74 | });
|
| 75 | tag.getFloat("Health").ifPresent(entity::setHealth);
|
| 76 | }
|
| 77 |
|
| 78 | static <T extends LivingEntity & Bucketable> Optional<InteractionResult> bucketMobPickup(final Player player, final InteractionHand hand, final T pickupEntity) {
|
| 79 | ItemStack itemStack = player.getItemInHand(hand);
|
| 80 | if (pickupEntity.canBePickedUpWithBucket(itemStack) && pickupEntity.isAlive()) {
|
| 81 | pickupEntity.playSound(pickupEntity.getPickupSound(), 1.0F, 1.0F);
|
| 82 | ItemStack bucket = pickupEntity.getBucketItemStack();
|
| 83 | pickupEntity.saveToBucketTag(bucket);
|
| 84 | ItemStack result = ItemUtils.createFilledResult(itemStack, player, bucket, false);
|
| 85 | player.setItemInHand(hand, result);
|
| 86 | Level level = pickupEntity.level();
|
| 87 | if (!level.isClientSide() && player instanceof ServerPlayer serverPlayer) {
|
| 88 | CriteriaTriggers.FILLED_BUCKET.trigger(serverPlayer, bucket);
|
| 89 | }
|
| 90 |
|
| 91 | if (pickupEntity instanceof Leashable leashable) {
|
| 92 | leashable.dropLeash();
|
| 93 | }
|
| 94 |
|
| 95 | pickupEntity.discard();
|
| 96 | return Optional.of(InteractionResult.SUCCESS);
|
| 97 | } else {
|
| 98 | return Optional.empty();
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | default boolean canBePickedUpWithBucket(final ItemStack itemStack) {
|
| 103 | return itemStack.getItem() == Items.WATER_BUCKET;
|
| 104 | }
|
| 105 | }
|
| 106 |
|