| 1 | package net.minecraft.world.entity.decoration;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.network.protocol.Packet;
|
| 6 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 7 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 8 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 9 | import net.minecraft.server.level.ServerEntity;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.tags.BlockTags;
|
| 13 | import net.minecraft.world.InteractionHand;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.EntityType;
|
| 17 | import net.minecraft.world.entity.EntityTypes;
|
| 18 | import net.minecraft.world.entity.Leashable;
|
| 19 | import net.minecraft.world.entity.player.Player;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.Items;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 24 | import net.minecraft.world.level.storage.ValueInput;
|
| 25 | import net.minecraft.world.level.storage.ValueOutput;
|
| 26 | import net.minecraft.world.phys.AABB;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class LeashFenceKnotEntity extends BlockAttachedEntity {
|
| 31 | public static final double OFFSET_Y = 0.375;
|
| 32 |
|
| 33 | public LeashFenceKnotEntity(final EntityType<? extends LeashFenceKnotEntity> type, final Level level) {
|
| 34 | super(type, level);
|
| 35 | }
|
| 36 |
|
| 37 | public LeashFenceKnotEntity(final Level level, final BlockPos pos) {
|
| 38 | super(EntityTypes.LEASH_KNOT, level, pos);
|
| 39 | this.setPos(pos.getX(), pos.getY(), pos.getZ());
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void recalculateBoundingBox() {
|
| 48 | this.setPosRaw(this.pos.getX() + 0.5, this.pos.getY() + 0.375, this.pos.getZ() + 0.5);
|
| 49 | double halfWidth = this.getType().getWidth() / 2.0;
|
| 50 | double height = this.getType().getHeight();
|
| 51 | this.setBoundingBox(
|
| 52 | new AABB(this.getX() - halfWidth, this.getY(), this.getZ() - halfWidth, this.getX() + halfWidth, this.getY() + height, this.getZ() + halfWidth)
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 58 | return distance < 1024.0;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void dropItem(final ServerLevel level, final @Nullable Entity causedBy) {
|
| 63 | this.playSound(SoundEvents.LEAD_UNTIED, 1.0F, 1.0F);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 76 | if (this.level().isClientSide()) {
|
| 77 | return InteractionResult.SUCCESS;
|
| 78 | }
|
| 79 |
|
| 80 | if (player.getItemInHand(hand).is(Items.SHEARS)) {
|
| 81 | InteractionResult result = super.interact(player, hand, location);
|
| 82 | if (result instanceof InteractionResult.Success success && success.wasItemInteraction()) {
|
| 83 | return result;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | boolean attachedMob = false;
|
| 88 |
|
| 89 | for (Leashable leashable : Leashable.leashableLeashedTo(player)) {
|
| 90 | if (leashable.canHaveALeashAttachedTo(this)) {
|
| 91 | leashable.setLeashedTo(this, true);
|
| 92 | attachedMob = true;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | boolean anyDropped = false;
|
| 97 | if (!attachedMob && !player.isSecondaryUseActive()) {
|
| 98 | for (Leashable mob : Leashable.leashableLeashedTo(this)) {
|
| 99 | if (mob.canHaveALeashAttachedTo(player)) {
|
| 100 | mob.setLeashedTo(player, true);
|
| 101 | anyDropped = true;
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | if (!attachedMob && !anyDropped) {
|
| 107 | return super.interact(player, hand, location);
|
| 108 | }
|
| 109 |
|
| 110 | this.gameEvent(GameEvent.BLOCK_ATTACH, player);
|
| 111 | this.playSound(SoundEvents.LEAD_TIED);
|
| 112 | return InteractionResult.SUCCESS;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void notifyLeasheeRemoved(final Leashable entity) {
|
| 117 | if (Leashable.leashableLeashedTo(this).isEmpty()) {
|
| 118 | this.discard();
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean survives() {
|
| 124 | return this.level().getBlockState(this.pos).is(BlockTags.FENCES);
|
| 125 | }
|
| 126 |
|
| 127 | public static LeashFenceKnotEntity getOrCreateKnot(final Level level, final BlockPos pos) {
|
| 128 | return getKnot(level, pos).orElseGet(() -> createKnot(level, pos));
|
| 129 | }
|
| 130 |
|
| 131 | public static Optional<LeashFenceKnotEntity> getKnot(final Level level, final BlockPos pos) {
|
| 132 | int x = pos.getX();
|
| 133 | int y = pos.getY();
|
| 134 | int z = pos.getZ();
|
| 135 |
|
| 136 | for (LeashFenceKnotEntity knot : level.getEntitiesOfClass(LeashFenceKnotEntity.class, new AABB(x - 1.0, y - 1.0, z - 1.0, x + 1.0, y + 1.0, z + 1.0))) {
|
| 137 | if (knot.getPos().equals(pos)) {
|
| 138 | return Optional.of(knot);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | return Optional.empty();
|
| 143 | }
|
| 144 |
|
| 145 | public static LeashFenceKnotEntity createKnot(final Level level, final BlockPos pos) {
|
| 146 | LeashFenceKnotEntity knot = new LeashFenceKnotEntity(level, pos);
|
| 147 | level.addFreshEntity(knot);
|
| 148 | return knot;
|
| 149 | }
|
| 150 |
|
| 151 | public void playPlacementSound() {
|
| 152 | this.playSound(SoundEvents.LEAD_TIED, 1.0F, 1.0F);
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity) {
|
| 157 | return new ClientboundAddEntityPacket(this, 0, this.getPos());
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public Vec3 getRopeHoldPosition(final float partialTickTime) {
|
| 162 | return this.getPosition(partialTickTime).add(0.0, 0.2, 0.0);
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | public ItemStack getPickResult() {
|
| 167 | return new ItemStack(Items.LEAD);
|
| 168 | }
|
| 169 | }
|
| 170 |
|