|
| 1 | +package fr.maxlego08.essentials.nms.v1_21_11; |
| 2 | + |
| 3 | +import com.mojang.math.Transformation; |
| 4 | +import fr.maxlego08.essentials.api.EssentialsPlugin; |
| 5 | +import fr.maxlego08.essentials.api.hologram.Hologram; |
| 6 | +import fr.maxlego08.essentials.api.hologram.HologramType; |
| 7 | +import fr.maxlego08.essentials.api.hologram.configuration.BlockHologramConfiguration; |
| 8 | +import fr.maxlego08.essentials.api.hologram.configuration.HologramConfiguration; |
| 9 | +import fr.maxlego08.essentials.api.hologram.configuration.ItemHologramConfiguration; |
| 10 | +import fr.maxlego08.essentials.api.hologram.configuration.TextHologramConfiguration; |
| 11 | +import fr.maxlego08.essentials.api.utils.ReflectionUtils; |
| 12 | +import fr.maxlego08.essentials.api.utils.SafeLocation; |
| 13 | +import io.papermc.paper.adventure.PaperAdventure; |
| 14 | +import net.minecraft.network.protocol.Packet; |
| 15 | +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; |
| 16 | +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; |
| 17 | +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; |
| 18 | +import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket; |
| 19 | +import net.minecraft.network.syncher.EntityDataAccessor; |
| 20 | +import net.minecraft.network.syncher.SynchedEntityData; |
| 21 | +import net.minecraft.server.level.ServerEntity; |
| 22 | +import net.minecraft.server.level.ServerLevel; |
| 23 | +import net.minecraft.server.level.ServerPlayer; |
| 24 | +import net.minecraft.util.Brightness; |
| 25 | +import net.minecraft.world.entity.Display; |
| 26 | +import net.minecraft.world.entity.EntityType; |
| 27 | +import net.minecraft.world.entity.PositionMoveRotation; |
| 28 | +import net.minecraft.world.item.ItemDisplayContext; |
| 29 | +import net.minecraft.world.item.ItemStack; |
| 30 | +import net.minecraft.world.level.block.Block; |
| 31 | +import org.bukkit.craftbukkit.CraftWorld; |
| 32 | +import org.bukkit.craftbukkit.block.CraftBlockType; |
| 33 | +import org.bukkit.craftbukkit.entity.CraftPlayer; |
| 34 | +import org.bukkit.entity.Player; |
| 35 | +import org.joml.Quaternionf; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Set; |
| 39 | + |
| 40 | +public class CraftHologram extends Hologram { |
| 41 | + |
| 42 | + private Display display = null; |
| 43 | + |
| 44 | + public CraftHologram(EssentialsPlugin plugin, HologramType hologramType, HologramConfiguration configuration, String fileName, String name, SafeLocation location) { |
| 45 | + super(plugin, hologramType, name, fileName, location, configuration); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void create(Player player) { |
| 50 | + ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle(); |
| 51 | + ServerEntity serverEntity = new ServerEntity(serverPlayer.level(), display, 0, false, null, Set.of()); |
| 52 | + send(player, new ClientboundAddEntityPacket(display, serverEntity)); |
| 53 | + |
| 54 | + this.update(player); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void delete(Player player) { |
| 59 | + this.send(player, new ClientboundRemoveEntitiesPacket(display.getId())); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void update(Player player) { |
| 64 | + |
| 65 | + send(player, new ClientboundTeleportEntityPacket(display.getId(), PositionMoveRotation.of(display), Set.of(), display.onGround)); |
| 66 | + |
| 67 | + if (display instanceof Display.TextDisplay textDisplay) { |
| 68 | + textDisplay.setText(PaperAdventure.asVanilla(getComponentText(player))); |
| 69 | + } |
| 70 | + |
| 71 | + final var values = new ArrayList<SynchedEntityData.DataValue<?>>(); |
| 72 | + for (final var item : ((SynchedEntityData.DataItem<?>[]) ReflectionUtils.getValue(display.getEntityData(), "itemsById"))) { |
| 73 | + values.add(item.value()); |
| 74 | + } |
| 75 | + send(player, new ClientboundSetEntityDataPacket(display.getId(), values)); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void create() { |
| 80 | + ServerLevel serverLevel = ((CraftWorld) location.getLocation().getWorld()).getHandle(); |
| 81 | + switch (hologramType) { |
| 82 | + case BLOCK -> this.display = new Display.BlockDisplay(EntityType.BLOCK_DISPLAY, serverLevel); |
| 83 | + case ITEM -> this.display = new Display.ItemDisplay(EntityType.ITEM_DISPLAY, serverLevel); |
| 84 | + case TEXT -> this.display = new Display.TextDisplay(EntityType.TEXT_DISPLAY, serverLevel); |
| 85 | + } |
| 86 | + |
| 87 | + display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.class, "DATA_TRANSFORMATION_INTERPOLATION_DURATION_ID"), 1); // Transformation duration |
| 88 | + display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.class, "DATA_TRANSFORMATION_INTERPOLATION_START_DELTA_TICKS_ID"), 0); // Interpolation start |
| 89 | + |
| 90 | + this.updateLocation(); |
| 91 | + this.update(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void updateLocation() { |
| 96 | + display.setPosRaw(location.getX(), location.getY(), location.getZ()); |
| 97 | + display.setYRot(location.getYaw()); |
| 98 | + display.setXRot(location.getPitch()); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void update() { |
| 103 | + |
| 104 | + display.setBillboardConstraints(switch (this.configuration.getBillboard()) { |
| 105 | + case FIXED -> Display.BillboardConstraints.FIXED; |
| 106 | + case VERTICAL -> Display.BillboardConstraints.VERTICAL; |
| 107 | + case HORIZONTAL -> Display.BillboardConstraints.HORIZONTAL; |
| 108 | + case CENTER -> Display.BillboardConstraints.CENTER; |
| 109 | + }); |
| 110 | + |
| 111 | + if (display instanceof Display.TextDisplay textDisplay && configuration instanceof TextHologramConfiguration textHologramConfiguration) { |
| 112 | + |
| 113 | + display.getEntityData().set((EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.TextDisplay.class, "DATA_LINE_WIDTH_ID"), Hologram.LINE_WIDTH); |
| 114 | + |
| 115 | + var backgroundColor = (EntityDataAccessor<Integer>) ReflectionUtils.getStaticValue(Display.TextDisplay.class, "DATA_BACKGROUND_COLOR_ID"); |
| 116 | + var background = textHologramConfiguration.getBackground(); |
| 117 | + int backgroundValue = (background == null) ? Display.TextDisplay.INITIAL_BACKGROUND : (background == Hologram.TRANSPARENT) ? 0 : background.value() | 0xC8000000; |
| 118 | + display.getEntityData().set(backgroundColor, backgroundValue); |
| 119 | + |
| 120 | + byte flags = textDisplay.getFlags(); |
| 121 | + flags = (byte) (textHologramConfiguration.isTextShadow() ? (flags | Display.TextDisplay.FLAG_SHADOW) : (flags & ~Display.TextDisplay.FLAG_SHADOW)); |
| 122 | + flags = (byte) (textHologramConfiguration.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.LEFT ? (flags | Display.TextDisplay.FLAG_ALIGN_LEFT) : (flags & ~Display.TextDisplay.FLAG_ALIGN_LEFT)); |
| 123 | + flags = (byte) (textHologramConfiguration.isSeeThrough() ? (flags | Display.TextDisplay.FLAG_SEE_THROUGH) : (flags & ~Display.TextDisplay.FLAG_SEE_THROUGH)); |
| 124 | + flags = (byte) (textHologramConfiguration.getTextAlignment() == org.bukkit.entity.TextDisplay.TextAlignment.RIGHT ? (flags | Display.TextDisplay.FLAG_ALIGN_RIGHT) : (flags & ~Display.TextDisplay.FLAG_ALIGN_RIGHT)); |
| 125 | + textDisplay.setFlags(flags); |
| 126 | + } else if (display instanceof Display.ItemDisplay itemDisplay && configuration instanceof ItemHologramConfiguration itemHologramConfiguration) { |
| 127 | + |
| 128 | + var context = switch (itemHologramConfiguration.getItemDisplayTransform()) { |
| 129 | + case THIRDPERSON_LEFTHAND -> ItemDisplayContext.THIRD_PERSON_LEFT_HAND; |
| 130 | + case THIRDPERSON_RIGHTHAND -> ItemDisplayContext.THIRD_PERSON_RIGHT_HAND; |
| 131 | + case FIRSTPERSON_LEFTHAND -> ItemDisplayContext.FIRST_PERSON_LEFT_HAND; |
| 132 | + case FIRSTPERSON_RIGHTHAND -> ItemDisplayContext.FIRST_PERSON_RIGHT_HAND; |
| 133 | + default -> ItemDisplayContext.valueOf(itemHologramConfiguration.getItemDisplayTransform().name()); |
| 134 | + }; |
| 135 | + itemDisplay.setItemTransform(context); |
| 136 | + itemDisplay.setItemStack(ItemStack.fromBukkitCopy(itemHologramConfiguration.getItemStack())); |
| 137 | + } else if (display instanceof Display.BlockDisplay blockDisplay && configuration instanceof BlockHologramConfiguration blockData) { |
| 138 | + |
| 139 | + Block block = CraftBlockType.bukkitToMinecraft(blockData.getMaterial()); |
| 140 | + blockDisplay.setBlockState(block.defaultBlockState()); |
| 141 | + } |
| 142 | + |
| 143 | + if (this.configuration.getBrightness() != null) { |
| 144 | + display.setBrightnessOverride(new Brightness(this.configuration.getBrightness().getBlockLight(), this.configuration.getBrightness().getSkyLight())); |
| 145 | + } |
| 146 | + |
| 147 | + display.setTransformation(new Transformation(this.configuration.getTranslation(), new Quaternionf(), this.configuration.getScale(), new Quaternionf())); |
| 148 | + |
| 149 | + display.setShadowRadius(this.configuration.getShadowRadius()); |
| 150 | + display.setShadowStrength(this.configuration.getShadowStrength()); |
| 151 | + } |
| 152 | + |
| 153 | + private void send(Player player, Packet<?> packet) { |
| 154 | + ((CraftPlayer) player).getHandle().connection.send(packet); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments