| 1 | package net.minecraft.world.entity.decoration;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.network.protocol.Packet;
|
| 8 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 9 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 10 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 11 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 12 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 13 | import net.minecraft.server.level.ServerEntity;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.sounds.SoundEvent;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.tags.DamageTypeTags;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.InteractionHand;
|
| 20 | import net.minecraft.world.InteractionResult;
|
| 21 | import net.minecraft.world.damagesource.DamageSource;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.EntityType;
|
| 24 | import net.minecraft.world.entity.EntityTypes;
|
| 25 | import net.minecraft.world.entity.MoverType;
|
| 26 | import net.minecraft.world.entity.SlotAccess;
|
| 27 | import net.minecraft.world.entity.player.Player;
|
| 28 | import net.minecraft.world.item.ItemStack;
|
| 29 | import net.minecraft.world.item.Items;
|
| 30 | import net.minecraft.world.item.MapItem;
|
| 31 | import net.minecraft.world.level.Level;
|
| 32 | import net.minecraft.world.level.block.Blocks;
|
| 33 | import net.minecraft.world.level.block.DiodeBlock;
|
| 34 | import net.minecraft.world.level.block.state.BlockState;
|
| 35 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 36 | import net.minecraft.world.level.gamerules.GameRules;
|
| 37 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 38 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 39 | import net.minecraft.world.level.storage.ValueInput;
|
| 40 | import net.minecraft.world.level.storage.ValueOutput;
|
| 41 | import net.minecraft.world.phys.AABB;
|
| 42 | import net.minecraft.world.phys.Vec3;
|
| 43 | import org.jspecify.annotations.Nullable;
|
| 44 |
|
| 45 | public class ItemFrame extends HangingEntity {
|
| 46 | private static final EntityDataAccessor<ItemStack> DATA_ITEM = SynchedEntityData.defineId(ItemFrame.class, EntityDataSerializers.ITEM_STACK);
|
| 47 | private static final EntityDataAccessor<Integer> DATA_ROTATION = SynchedEntityData.defineId(ItemFrame.class, EntityDataSerializers.INT);
|
| 48 | public static final int NUM_ROTATIONS = 8;
|
| 49 | private static final float DEPTH = 0.0625F;
|
| 50 | private static final float WIDTH = 0.75F;
|
| 51 | private static final float HEIGHT = 0.75F;
|
| 52 | private static final byte DEFAULT_ROTATION = 0;
|
| 53 | private static final float DEFAULT_DROP_CHANCE = 1.0F;
|
| 54 | private static final boolean DEFAULT_INVISIBLE = false;
|
| 55 | private static final boolean DEFAULT_FIXED = false;
|
| 56 | private float dropChance = 1.0F;
|
| 57 | private boolean fixed = false;
|
| 58 |
|
| 59 | public ItemFrame(final EntityType<? extends ItemFrame> type, final Level level) {
|
| 60 | super(type, level);
|
| 61 | this.setInvisible(false);
|
| 62 | }
|
| 63 |
|
| 64 | public ItemFrame(final Level level, final BlockPos pos, final Direction direction) {
|
| 65 | this(EntityTypes.ITEM_FRAME, level, pos, direction);
|
| 66 | }
|
| 67 |
|
| 68 | public ItemFrame(final EntityType<? extends ItemFrame> type, final Level level, final BlockPos pos, final Direction direction) {
|
| 69 | super(type, level, pos);
|
| 70 | this.setDirection(direction);
|
| 71 | this.setInvisible(false);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 76 | super.defineSynchedData(entityData);
|
| 77 | entityData.define(DATA_ITEM, ItemStack.EMPTY);
|
| 78 | entityData.define(DATA_ROTATION, 0);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected void setDirection(final Direction direction) {
|
| 83 | Objects.requireNonNull(direction);
|
| 84 | super.setDirectionRaw(direction);
|
| 85 | if (direction.getAxis().isHorizontal()) {
|
| 86 | this.setXRot(0.0F);
|
| 87 | this.setYRot(direction.get2DDataValue() * 90);
|
| 88 | } else {
|
| 89 | this.setXRot(-90 * direction.getAxisDirection().getStep());
|
| 90 | this.setYRot(0.0F);
|
| 91 | }
|
| 92 |
|
| 93 | this.xRotO = this.getXRot();
|
| 94 | this.yRotO = this.getYRot();
|
| 95 | this.recalculateBoundingBox();
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected final void recalculateBoundingBox() {
|
| 100 | super.recalculateBoundingBox();
|
| 101 | this.syncPacketPositionCodec(this.getX(), this.getY(), this.getZ());
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected AABB calculateBoundingBox(final BlockPos blockPos, final Direction direction) {
|
| 106 | return this.createBoundingBox(blockPos, direction, this.hasFramedMap());
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | protected AABB getPopBox() {
|
| 111 | return this.createBoundingBox(this.pos, this.getDirection(), false);
|
| 112 | }
|
| 113 |
|
| 114 | private AABB createBoundingBox(final BlockPos blockPos, final Direction direction, final boolean hasFramedMap) {
|
| 115 | float shiftToBlockWall = 0.46875F;
|
| 116 | Vec3 position = Vec3.atCenterOf(blockPos).relative(direction, -0.46875);
|
| 117 | float width = hasFramedMap ? 1.0F : 0.75F;
|
| 118 | float height = hasFramedMap ? 1.0F : 0.75F;
|
| 119 | Direction.Axis axis = direction.getAxis();
|
| 120 | double xSize = axis == Direction.Axis.X ? 0.0625 : width;
|
| 121 | double ySize = axis == Direction.Axis.Y ? 0.0625 : height;
|
| 122 | double zSize = axis == Direction.Axis.Z ? 0.0625 : width;
|
| 123 | return AABB.ofSize(position, xSize, ySize, zSize);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public boolean survives() {
|
| 128 | if (this.fixed) {
|
| 129 | return true;
|
| 130 | }
|
| 131 |
|
| 132 | if (this.hasLevelCollision(this.getPopBox())) {
|
| 133 | return false;
|
| 134 | }
|
| 135 |
|
| 136 | BlockState state = this.level().getBlockState(this.pos.relative(this.getDirection().getOpposite()));
|
| 137 | return state.isSolid() || this.getDirection().getAxis().isHorizontal() && DiodeBlock.isDiode(state) ? this.canCoexist(true) : false;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void move(final MoverType moverType, final Vec3 delta) {
|
| 142 | if (!this.fixed) {
|
| 143 | super.move(moverType, delta);
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public void push(final double xa, final double ya, final double za) {
|
| 149 | if (!this.fixed) {
|
| 150 | super.push(xa, ya, za);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected void onKilled() {
|
| 156 | this.removeFramedMap(this.getItem());
|
| 157 | }
|
| 158 |
|
| 159 | private boolean shouldDamageDropItem(final DamageSource source) {
|
| 160 | return !source.is(DamageTypeTags.IS_EXPLOSION) && !this.getItem().isEmpty();
|
| 161 | }
|
| 162 |
|
| 163 | private static boolean canHurtWhenFixed(final DamageSource source) {
|
| 164 | return source.is(DamageTypeTags.BYPASSES_INVULNERABILITY) || source.isCreativePlayer();
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public boolean hurtClient(final DamageSource source) {
|
| 169 | return this.fixed && !canHurtWhenFixed(source) ? false : !this.isInvulnerableToBase(source);
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 174 | if (!this.fixed) {
|
| 175 | if (this.isInvulnerableToBase(source)) {
|
| 176 | return false;
|
| 177 | } else if (this.shouldDamageDropItem(source)) {
|
| 178 | this.dropItem(level, source.getEntity(), false);
|
| 179 | this.gameEvent(GameEvent.BLOCK_CHANGE, source.getEntity());
|
| 180 | this.playSound(this.getRemoveItemSound(), 1.0F, 1.0F);
|
| 181 | return true;
|
| 182 | } else {
|
| 183 | return super.hurtServer(level, source, damage);
|
| 184 | }
|
| 185 | } else {
|
| 186 | return canHurtWhenFixed(source) && super.hurtServer(level, source, damage);
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | public SoundEvent getRemoveItemSound() {
|
| 191 | return SoundEvents.ITEM_FRAME_REMOVE_ITEM;
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 196 | double size = 16.0;
|
| 197 | size *= 64.0 * getViewScale();
|
| 198 | return distance < size * size;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void dropItem(final ServerLevel level, final @Nullable Entity causedBy) {
|
| 203 | this.playSound(this.getBreakSound(), 1.0F, 1.0F);
|
| 204 | this.dropItem(level, causedBy, true);
|
| 205 | this.gameEvent(GameEvent.BLOCK_CHANGE, causedBy);
|
| 206 | }
|
| 207 |
|
| 208 | public SoundEvent getBreakSound() {
|
| 209 | return SoundEvents.ITEM_FRAME_BREAK;
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public void playPlacementSound() {
|
| 214 | this.playSound(this.getPlaceSound(), 1.0F, 1.0F);
|
| 215 | }
|
| 216 |
|
| 217 | public SoundEvent getPlaceSound() {
|
| 218 | return SoundEvents.ITEM_FRAME_PLACE;
|
| 219 | }
|
| 220 |
|
| 221 | private void dropItem(final ServerLevel level, final @Nullable Entity causedBy, final boolean withFrame) {
|
| 222 | if (!this.fixed) {
|
| 223 | ItemStack itemStack = this.getItem();
|
| 224 | this.setItem(ItemStack.EMPTY);
|
| 225 | if (!level.getGameRules().get(GameRules.ENTITY_DROPS)) {
|
| 226 | if (causedBy == null) {
|
| 227 | this.removeFramedMap(itemStack);
|
| 228 | }
|
| 229 | } else if (causedBy instanceof Player player && player.hasInfiniteMaterials()) {
|
| 230 | this.removeFramedMap(itemStack);
|
| 231 | } else {
|
| 232 | if (withFrame) {
|
| 233 | this.spawnAtLocation(level, this.getFrameItemStackWithData());
|
| 234 | }
|
| 235 |
|
| 236 | if (!itemStack.isEmpty()) {
|
| 237 | itemStack = itemStack.copy();
|
| 238 | this.removeFramedMap(itemStack);
|
| 239 | if (this.random.nextFloat() < this.dropChance) {
|
| 240 | this.spawnAtLocation(level, itemStack);
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | private void removeFramedMap(final ItemStack itemStack) {
|
| 248 | MapId mapId = this.getFramedMapId(itemStack);
|
| 249 | if (mapId != null) {
|
| 250 | MapItemSavedData mapItemSavedData = MapItem.getSavedData(mapId, this.level());
|
| 251 | if (mapItemSavedData != null) {
|
| 252 | mapItemSavedData.removedFromFrame(this.pos, this.getId());
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | public ItemStack getItem() {
|
| 258 | return this.getEntityData().get(DATA_ITEM);
|
| 259 | }
|
| 260 |
|
| 261 | public @Nullable MapId getFramedMapId(final ItemStack itemStack) {
|
| 262 | return itemStack.get(DataComponents.MAP_ID);
|
| 263 | }
|
| 264 |
|
| 265 | public boolean hasFramedMap() {
|
| 266 | return this.getItem().has(DataComponents.MAP_ID);
|
| 267 | }
|
| 268 |
|
| 269 | public void setItem(final ItemStack itemStack) {
|
| 270 | this.setItem(itemStack, true);
|
| 271 | }
|
| 272 |
|
| 273 | public void setItem(ItemStack itemStack, final boolean updateNeighbours) {
|
| 274 | if (!itemStack.isEmpty()) {
|
| 275 | itemStack = itemStack.copyWithCount(1);
|
| 276 | }
|
| 277 |
|
| 278 | this.onItemChanged(itemStack);
|
| 279 | this.getEntityData().set(DATA_ITEM, itemStack);
|
| 280 | if (!itemStack.isEmpty()) {
|
| 281 | this.playSound(this.getAddItemSound(), 1.0F, 1.0F);
|
| 282 | }
|
| 283 |
|
| 284 | if (updateNeighbours && this.pos != null) {
|
| 285 | this.level().updateNeighbourForOutputSignal(this.pos, Blocks.AIR);
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | public SoundEvent getAddItemSound() {
|
| 290 | return SoundEvents.ITEM_FRAME_ADD_ITEM;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public @Nullable SlotAccess getSlot(final int slot) {
|
| 295 | return slot == 0 ? SlotAccess.of(this::getItem, this::setItem) : super.getSlot(slot);
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 300 | super.onSyncedDataUpdated(accessor);
|
| 301 | if (accessor.equals(DATA_ITEM)) {
|
| 302 | this.onItemChanged(this.getItem());
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | private void onItemChanged(final ItemStack item) {
|
| 307 | this.recalculateBoundingBox();
|
| 308 | }
|
| 309 |
|
| 310 | public int getRotation() {
|
| 311 | return this.getEntityData().get(DATA_ROTATION);
|
| 312 | }
|
| 313 |
|
| 314 | public void setRotation(final int rotation) {
|
| 315 | this.setRotation(rotation, true);
|
| 316 | }
|
| 317 |
|
| 318 | private void setRotation(final int rotation, final boolean updateNeighbours) {
|
| 319 | this.getEntityData().set(DATA_ROTATION, rotation % 8);
|
| 320 | if (updateNeighbours && this.pos != null) {
|
| 321 | this.level().updateNeighbourForOutputSignal(this.pos, Blocks.AIR);
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 327 | super.addAdditionalSaveData(output);
|
| 328 | ItemStack currentItem = this.getItem();
|
| 329 | if (!currentItem.isEmpty()) {
|
| 330 | output.store("Item", ItemStack.CODEC, currentItem);
|
| 331 | }
|
| 332 |
|
| 333 | output.putByte("ItemRotation", (byte)this.getRotation());
|
| 334 | output.putFloat("ItemDropChance", this.dropChance);
|
| 335 | output.store("Facing", Direction.LEGACY_ID_CODEC, this.getDirection());
|
| 336 | output.putBoolean("Invisible", this.isInvisible());
|
| 337 | output.putBoolean("Fixed", this.fixed);
|
| 338 | }
|
| 339 |
|
| 340 | @Override
|
| 341 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 342 | super.readAdditionalSaveData(input);
|
| 343 | ItemStack itemStack = input.read("Item", ItemStack.CODEC).orElse(ItemStack.EMPTY);
|
| 344 | ItemStack currentItem = this.getItem();
|
| 345 | if (!currentItem.isEmpty() && !ItemStack.matches(itemStack, currentItem)) {
|
| 346 | this.removeFramedMap(currentItem);
|
| 347 | }
|
| 348 |
|
| 349 | this.setItem(itemStack, false);
|
| 350 | this.setRotation(input.getByteOr("ItemRotation", (byte)0), false);
|
| 351 | this.dropChance = input.getFloatOr("ItemDropChance", 1.0F);
|
| 352 | this.setDirection(input.read("Facing", Direction.LEGACY_ID_CODEC).orElse(Direction.DOWN));
|
| 353 | this.setInvisible(input.getBooleanOr("Invisible", false));
|
| 354 | this.fixed = input.getBooleanOr("Fixed", false);
|
| 355 | }
|
| 356 |
|
| 357 | @Override
|
| 358 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 359 | ItemStack itemStack = player.getItemInHand(hand);
|
| 360 | boolean frameHasItem = !this.getItem().isEmpty();
|
| 361 | boolean hasHeldItem = !itemStack.isEmpty();
|
| 362 | if (this.fixed) {
|
| 363 | return InteractionResult.PASS;
|
| 364 | }
|
| 365 |
|
| 366 | if (!player.level().isClientSide()) {
|
| 367 | if (!frameHasItem) {
|
| 368 | if (hasHeldItem && !this.isRemoved()) {
|
| 369 | MapItemSavedData data = MapItem.getSavedData(itemStack, this.level());
|
| 370 | if (data != null && data.isTrackedCountOverLimit(256)) {
|
| 371 | return InteractionResult.FAIL;
|
| 372 | }
|
| 373 |
|
| 374 | this.setItem(itemStack);
|
| 375 | this.gameEvent(GameEvent.BLOCK_CHANGE, player);
|
| 376 | itemStack.consume(1, player);
|
| 377 | return InteractionResult.SUCCESS;
|
| 378 | } else {
|
| 379 | return InteractionResult.PASS;
|
| 380 | }
|
| 381 | } else {
|
| 382 | this.playSound(this.getRotateItemSound(), 1.0F, 1.0F);
|
| 383 | this.setRotation(this.getRotation() + 1);
|
| 384 | this.gameEvent(GameEvent.BLOCK_CHANGE, player);
|
| 385 | return InteractionResult.SUCCESS;
|
| 386 | }
|
| 387 | } else {
|
| 388 | return !frameHasItem && !hasHeldItem ? InteractionResult.PASS : InteractionResult.SUCCESS;
|
| 389 | }
|
| 390 | }
|
| 391 |
|
| 392 | public SoundEvent getRotateItemSound() {
|
| 393 | return SoundEvents.ITEM_FRAME_ROTATE_ITEM;
|
| 394 | }
|
| 395 |
|
| 396 | public int getAnalogOutput() {
|
| 397 | return this.getItem().isEmpty() ? 0 : this.getRotation() % 8 + 1;
|
| 398 | }
|
| 399 |
|
| 400 | @Override
|
| 401 | public Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity) {
|
| 402 | return new ClientboundAddEntityPacket(this, this.getDirection().get3DDataValue(), this.getPos());
|
| 403 | }
|
| 404 |
|
| 405 | @Override
|
| 406 | public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
|
| 407 | super.recreateFromPacket(packet);
|
| 408 | this.setDirection(Direction.from3DDataValue(packet.getData()));
|
| 409 | }
|
| 410 |
|
| 411 | @Override
|
| 412 | public ItemStack getPickResult() {
|
| 413 | ItemStack framedStack = this.getItem();
|
| 414 | return framedStack.isEmpty() ? this.getFrameItemStackWithData() : framedStack.copy();
|
| 415 | }
|
| 416 |
|
| 417 | protected ItemStack getFrameItemStack() {
|
| 418 | return new ItemStack(Items.ITEM_FRAME);
|
| 419 | }
|
| 420 |
|
| 421 | private ItemStack getFrameItemStackWithData() {
|
| 422 | ItemStack itemStack = this.getFrameItemStack();
|
| 423 | itemStack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
|
| 424 | return itemStack;
|
| 425 | }
|
| 426 |
|
| 427 | @Override
|
| 428 | public float getVisualRotationYInDegrees() {
|
| 429 | Direction frameDirection = this.getDirection();
|
| 430 | int rotationCorrection = frameDirection.getAxis().isVertical() ? 90 * frameDirection.getAxisDirection().getStep() : 0;
|
| 431 | return Mth.wrapDegrees(180 + frameDirection.get2DDataValue() * 90 + this.getRotation() * 45 + rotationCorrection);
|
| 432 | }
|
| 433 | }
|
| 434 |
|