| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.component.DataComponentGetter;
|
| 5 | import net.minecraft.core.component.DataComponentMap;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.network.chat.ComponentSerialization;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.Nameable;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.storage.ValueInput;
|
| 16 | import net.minecraft.world.level.storage.ValueOutput;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class EnchantingTableBlockEntity extends BlockEntity implements Nameable {
|
| 20 | private static final Component DEFAULT_NAME = Component.translatable("container.enchant");
|
| 21 | public int time;
|
| 22 | public float flip;
|
| 23 | public float oFlip;
|
| 24 | public float flipT;
|
| 25 | public float flipA;
|
| 26 | public float open;
|
| 27 | public float oOpen;
|
| 28 | public float rot;
|
| 29 | public float oRot;
|
| 30 | public float tRot;
|
| 31 | private static final RandomSource RANDOM = RandomSource.create();
|
| 32 | private @Nullable Component name;
|
| 33 |
|
| 34 | public EnchantingTableBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 35 | super(BlockEntityTypes.ENCHANTING_TABLE, worldPosition, blockState);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected void saveAdditional(final ValueOutput output) {
|
| 40 | super.saveAdditional(output);
|
| 41 | output.storeNullable("CustomName", ComponentSerialization.CODEC, this.name);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void loadAdditional(final ValueInput input) {
|
| 46 | super.loadAdditional(input);
|
| 47 | this.name = parseCustomNameSafe(input, "CustomName");
|
| 48 | }
|
| 49 |
|
| 50 | public static void bookAnimationTick(final Level level, final BlockPos worldPosition, final BlockState state, final EnchantingTableBlockEntity entity) {
|
| 51 | entity.oOpen = entity.open;
|
| 52 | entity.oRot = entity.rot;
|
| 53 | Player player = level.getNearestPlayer(worldPosition.getX() + 0.5, worldPosition.getY() + 0.5, worldPosition.getZ() + 0.5, 3.0, false);
|
| 54 | if (player != null) {
|
| 55 | double xd = player.getX() - (worldPosition.getX() + 0.5);
|
| 56 | double zd = player.getZ() - (worldPosition.getZ() + 0.5);
|
| 57 | entity.tRot = (float)Mth.atan2(zd, xd);
|
| 58 | entity.open += 0.1F;
|
| 59 | if (entity.open < 0.5F || RANDOM.nextInt(40) == 0) {
|
| 60 | float old = entity.flipT;
|
| 61 |
|
| 62 | do {
|
| 63 | entity.flipT = entity.flipT + (RANDOM.nextInt(4) - RANDOM.nextInt(4));
|
| 64 | } while (old == entity.flipT);
|
| 65 | }
|
| 66 | } else {
|
| 67 | entity.tRot += 0.02F;
|
| 68 | entity.open -= 0.1F;
|
| 69 | }
|
| 70 |
|
| 71 | while (entity.rot >= (float) Math.PI) {
|
| 72 | entity.rot -= (float) (Math.PI * 2);
|
| 73 | }
|
| 74 |
|
| 75 | while (entity.rot < (float) -Math.PI) {
|
| 76 | entity.rot += (float) (Math.PI * 2);
|
| 77 | }
|
| 78 |
|
| 79 | while (entity.tRot >= (float) Math.PI) {
|
| 80 | entity.tRot -= (float) (Math.PI * 2);
|
| 81 | }
|
| 82 |
|
| 83 | while (entity.tRot < (float) -Math.PI) {
|
| 84 | entity.tRot += (float) (Math.PI * 2);
|
| 85 | }
|
| 86 |
|
| 87 | float rotDir = entity.tRot - entity.rot;
|
| 88 |
|
| 89 | while (rotDir >= (float) Math.PI) {
|
| 90 | rotDir -= (float) (Math.PI * 2);
|
| 91 | }
|
| 92 |
|
| 93 | while (rotDir < (float) -Math.PI) {
|
| 94 | rotDir += (float) (Math.PI * 2);
|
| 95 | }
|
| 96 |
|
| 97 | entity.rot += rotDir * 0.4F;
|
| 98 | entity.open = Mth.clamp(entity.open, 0.0F, 1.0F);
|
| 99 | entity.time++;
|
| 100 | entity.oFlip = entity.flip;
|
| 101 | float diff = (entity.flipT - entity.flip) * 0.4F;
|
| 102 | float max = 0.2F;
|
| 103 | diff = Mth.clamp(diff, -0.2F, 0.2F);
|
| 104 | entity.flipA = entity.flipA + (diff - entity.flipA) * 0.9F;
|
| 105 | entity.flip = entity.flip + entity.flipA;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public Component getName() {
|
| 110 | return this.name != null ? this.name : DEFAULT_NAME;
|
| 111 | }
|
| 112 |
|
| 113 | public void setCustomName(final @Nullable Component name) {
|
| 114 | this.name = name;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public @Nullable Component getCustomName() {
|
| 119 | return this.name;
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 124 | super.applyImplicitComponents(components);
|
| 125 | this.name = components.get(DataComponents.CUSTOM_NAME);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 130 | super.collectImplicitComponents(components);
|
| 131 | components.set(DataComponents.CUSTOM_NAME, this.name);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 136 | output.discard("CustomName");
|
| 137 | }
|
| 138 | }
|
| 139 |
|