| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.UUID;
|
| 6 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 7 | import net.minecraft.core.UUIDUtil;
|
| 8 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 9 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 10 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import net.minecraft.world.InteractionHand;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.damagesource.DamageSource;
|
| 16 | import net.minecraft.world.entity.player.Player;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.material.PushReaction;
|
| 19 | import net.minecraft.world.level.storage.ValueInput;
|
| 20 | import net.minecraft.world.level.storage.ValueOutput;
|
| 21 | import net.minecraft.world.phys.AABB;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | /**
|
| 26 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 27 | */
|
| 28 | public class Interaction extends Entity implements Attackable, Targeting {
|
| 29 | private static final EntityDataAccessor<Float> DATA_WIDTH_ID = SynchedEntityData.defineId(Interaction.class, EntityDataSerializers.FLOAT);
|
| 30 | private static final EntityDataAccessor<Float> DATA_HEIGHT_ID = SynchedEntityData.defineId(Interaction.class, EntityDataSerializers.FLOAT);
|
| 31 | private static final EntityDataAccessor<Boolean> DATA_RESPONSE_ID = SynchedEntityData.defineId(Interaction.class, EntityDataSerializers.BOOLEAN);
|
| 32 | private static final String TAG_WIDTH = "width";
|
| 33 | private static final String TAG_HEIGHT = "height";
|
| 34 | private static final String TAG_ATTACK = "attack";
|
| 35 | private static final String TAG_INTERACTION = "interaction";
|
| 36 | private static final String TAG_RESPONSE = "response";
|
| 37 | private static final float DEFAULT_WIDTH = 1.0F;
|
| 38 | private static final float DEFAULT_HEIGHT = 1.0F;
|
| 39 | private static final boolean DEFAULT_RESPONSE = false;
|
| 40 | private Interaction.@Nullable PlayerAction attack;
|
| 41 | private Interaction.@Nullable PlayerAction interaction;
|
| 42 |
|
| 43 | public Interaction(final EntityType<?> type, final Level level) {
|
| 44 | super(type, level);
|
| 45 | this.noPhysics = true;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 50 | entityData.define(DATA_WIDTH_ID, 1.0F);
|
| 51 | entityData.define(DATA_HEIGHT_ID, 1.0F);
|
| 52 | entityData.define(DATA_RESPONSE_ID, false);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 57 | this.setWidth(input.getFloatOr("width", 1.0F));
|
| 58 | this.setHeight(input.getFloatOr("height", 1.0F));
|
| 59 | this.attack = input.read("attack", Interaction.PlayerAction.CODEC).orElse(null);
|
| 60 | this.interaction = input.read("interaction", Interaction.PlayerAction.CODEC).orElse(null);
|
| 61 | this.setResponse(input.getBooleanOr("response", false));
|
| 62 | this.setBoundingBox(this.makeBoundingBox());
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 67 | output.putFloat("width", this.getWidth());
|
| 68 | output.putFloat("height", this.getHeight());
|
| 69 | output.storeNullable("attack", Interaction.PlayerAction.CODEC, this.attack);
|
| 70 | output.storeNullable("interaction", Interaction.PlayerAction.CODEC, this.interaction);
|
| 71 | output.putBoolean("response", this.getResponse());
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 76 | super.onSyncedDataUpdated(accessor);
|
| 77 | if (DATA_HEIGHT_ID.equals(accessor) || DATA_WIDTH_ID.equals(accessor)) {
|
| 78 | this.refreshDimensions();
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public boolean canBeHitByProjectile() {
|
| 84 | return false;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public boolean isPickable() {
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public PushReaction getPistonPushReaction() {
|
| 94 | return PushReaction.IGNORE_ENTITY;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean isIgnoringBlockTriggers() {
|
| 99 | return true;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public boolean skipAttackInteraction(final Entity source) {
|
| 104 | if (source instanceof Player player) {
|
| 105 | this.attack = new Interaction.PlayerAction(player.getUUID(), this.level().getGameTime());
|
| 106 | if (player instanceof ServerPlayer serverPlayer) {
|
| 107 | CriteriaTriggers.PLAYER_HURT_ENTITY.trigger(serverPlayer, this, player.damageSources().generic(), 1.0F, 1.0F, false);
|
| 108 | }
|
| 109 |
|
| 110 | return !this.getResponse();
|
| 111 | } else {
|
| 112 | return false;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 118 | return false;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 123 | if (this.level().isClientSide()) {
|
| 124 | return this.getResponse() ? InteractionResult.SUCCESS : InteractionResult.CONSUME;
|
| 125 | }
|
| 126 |
|
| 127 | this.interaction = new Interaction.PlayerAction(player.getUUID(), this.level().getGameTime());
|
| 128 | return InteractionResult.CONSUME;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public void tick() {
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public @Nullable LivingEntity getLastAttacker() {
|
| 137 | return this.attack != null ? this.level().getPlayerByUUID(this.attack.player()) : null;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public @Nullable LivingEntity getTarget() {
|
| 142 | return this.interaction != null ? this.level().getPlayerByUUID(this.interaction.player()) : null;
|
| 143 | }
|
| 144 |
|
| 145 | /**
|
| 146 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 147 | */
|
| 148 | public final void setWidth(final float width) {
|
| 149 | this.entityData.set(DATA_WIDTH_ID, width);
|
| 150 | }
|
| 151 |
|
| 152 | /**
|
| 153 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 154 | */
|
| 155 | public final float getWidth() {
|
| 156 | return this.entityData.get(DATA_WIDTH_ID);
|
| 157 | }
|
| 158 |
|
| 159 | /**
|
| 160 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 161 | */
|
| 162 | public final void setHeight(final float width) {
|
| 163 | this.entityData.set(DATA_HEIGHT_ID, width);
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 168 | */
|
| 169 | public final float getHeight() {
|
| 170 | return this.entityData.get(DATA_HEIGHT_ID);
|
| 171 | }
|
| 172 |
|
| 173 | /**
|
| 174 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 175 | */
|
| 176 | public final void setResponse(final boolean response) {
|
| 177 | this.entityData.set(DATA_RESPONSE_ID, response);
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 182 | */
|
| 183 | public final boolean getResponse() {
|
| 184 | return this.entityData.get(DATA_RESPONSE_ID);
|
| 185 | }
|
| 186 |
|
| 187 | private EntityDimensions getDimensions() {
|
| 188 | return EntityDimensions.scalable(this.getWidth(), this.getHeight());
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public EntityDimensions getDimensions(final Pose pose) {
|
| 193 | return this.getDimensions();
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | protected AABB makeBoundingBox(final Vec3 position) {
|
| 198 | return this.getDimensions().makeBoundingBox(position);
|
| 199 | }
|
| 200 |
|
| 201 | private record PlayerAction(UUID player, long timestamp) {
|
| 202 | public static final Codec<Interaction.PlayerAction> CODEC = RecordCodecBuilder.create(
|
| 203 | i -> i.group(
|
| 204 | UUIDUtil.CODEC.fieldOf("player").forGetter(Interaction.PlayerAction::player),
|
| 205 | Codec.LONG.fieldOf("timestamp").forGetter(Interaction.PlayerAction::timestamp)
|
| 206 | )
|
| 207 | .apply(i, Interaction.PlayerAction::new)
|
| 208 | );
|
| 209 | }
|
| 210 | }
|
| 211 |
|