|
| 1 | +package de.pianoman911.mapengine.common; |
| 2 | + |
| 3 | +import de.pianoman911.mapengine.common.data.MapUpdateData; |
| 4 | +import de.pianoman911.mapengine.common.platform.IListenerBridge; |
| 5 | +import de.pianoman911.mapengine.common.platform.IPlatform; |
| 6 | +import de.pianoman911.mapengine.common.platform.PacketContainer; |
| 7 | +import io.netty.channel.Channel; |
| 8 | +import io.papermc.paper.adventure.PaperAdventure; |
| 9 | +import it.unimi.dsi.fastutil.ints.IntList; |
| 10 | +import net.minecraft.SharedConstants; |
| 11 | +import net.minecraft.core.component.DataComponents; |
| 12 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | +import net.minecraft.network.protocol.Packet; |
| 14 | +import net.minecraft.network.protocol.game.ClientGamePacketListener; |
| 15 | +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; |
| 16 | +import net.minecraft.network.protocol.game.ClientboundBundlePacket; |
| 17 | +import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket; |
| 18 | +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; |
| 19 | +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; |
| 20 | +import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket; |
| 21 | +import net.minecraft.network.syncher.EntityDataAccessor; |
| 22 | +import net.minecraft.network.syncher.EntityDataSerializers; |
| 23 | +import net.minecraft.network.syncher.SynchedEntityData; |
| 24 | +import net.minecraft.server.MinecraftServer; |
| 25 | +import net.minecraft.world.entity.EntityType; |
| 26 | +import net.minecraft.world.entity.PositionMoveRotation; |
| 27 | +import net.minecraft.world.entity.decoration.ItemFrame; |
| 28 | +import net.minecraft.world.item.ItemStack; |
| 29 | +import net.minecraft.world.item.Items; |
| 30 | +import net.minecraft.world.level.saveddata.maps.MapDecoration; |
| 31 | +import net.minecraft.world.level.saveddata.maps.MapId; |
| 32 | +import net.minecraft.world.level.saveddata.maps.MapItemSavedData.MapPatch; |
| 33 | +import net.minecraft.world.phys.Vec3; |
| 34 | +import org.bukkit.Bukkit; |
| 35 | +import org.bukkit.block.BlockFace; |
| 36 | +import org.bukkit.craftbukkit.entity.CraftPlayer; |
| 37 | +import org.bukkit.entity.Player; |
| 38 | +import org.bukkit.event.EventHandler; |
| 39 | +import org.bukkit.event.Listener; |
| 40 | +import org.bukkit.event.player.PlayerJoinEvent; |
| 41 | +import org.bukkit.event.player.PlayerQuitEvent; |
| 42 | +import org.bukkit.map.MapCursor; |
| 43 | +import org.bukkit.map.MapCursorCollection; |
| 44 | +import org.bukkit.plugin.Plugin; |
| 45 | +import org.bukkit.util.BlockVector; |
| 46 | +import org.bukkit.util.Vector; |
| 47 | + |
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.List; |
| 50 | +import java.util.Objects; |
| 51 | +import java.util.Optional; |
| 52 | +import java.util.Set; |
| 53 | +import java.util.UUID; |
| 54 | + |
| 55 | +public class Paper12111Platform implements IPlatform<Packet<ClientGamePacketListener>>, Listener { |
| 56 | + |
| 57 | + private static final EntityDataAccessor<Byte> DATA_SHARED_FLAGS_ID = EntityDataSerializers.BYTE.createAccessor(0); |
| 58 | + private static final EntityDataAccessor<Float> DATA_INTERACTION_BOX_WIDTH_ID = EntityDataSerializers.FLOAT.createAccessor(8); |
| 59 | + private static final EntityDataAccessor<Float> DATA_INTERACTION_BOX_HEIGHT_ID = EntityDataSerializers.FLOAT.createAccessor(9); |
| 60 | + private static final EntityDataAccessor<Boolean> DATA_INTERACTION_BOX_RESPONSIVE_ID = EntityDataSerializers.BOOLEAN.createAccessor(10); |
| 61 | + |
| 62 | + private static final Paper12111SynchedDataBuilder ITEM_FRAME_DATA = Paper12111SynchedDataBuilder.builder() |
| 63 | + .setDataItem(DATA_SHARED_FLAGS_ID, (byte) 0x00) |
| 64 | + .setDataItem(ItemFrame.DATA_ITEM, ItemStack.EMPTY) |
| 65 | + .setDataItem(ItemFrame.DATA_ROTATION, 0); |
| 66 | + private static final Paper12111SynchedDataBuilder INTERACTION_DATA = Paper12111SynchedDataBuilder.builder() |
| 67 | + .setDataItem(DATA_SHARED_FLAGS_ID, (byte) 0x00) |
| 68 | + .setDataItem(DATA_INTERACTION_BOX_WIDTH_ID, 0f) |
| 69 | + .setDataItem(DATA_INTERACTION_BOX_HEIGHT_ID, 0f) |
| 70 | + .setDataItem(DATA_INTERACTION_BOX_RESPONSIVE_ID, false); |
| 71 | + |
| 72 | + private final IListenerBridge bridge; |
| 73 | + |
| 74 | + public Paper12111Platform(Plugin plugin, IListenerBridge bridge) { |
| 75 | + this.bridge = bridge; |
| 76 | + Bukkit.getPluginManager().registerEvents(this, plugin); |
| 77 | + } |
| 78 | + |
| 79 | + @EventHandler |
| 80 | + public void onJoin(PlayerJoinEvent event) { |
| 81 | + Paper12111Listener listener = new Paper12111Listener(event.getPlayer(), this.bridge); |
| 82 | + ((CraftPlayer) event.getPlayer()).getHandle().connection.connection.channel |
| 83 | + .pipeline().addAfter("decoder", "mapengine", listener); |
| 84 | + } |
| 85 | + |
| 86 | + @EventHandler |
| 87 | + public void onQuit(PlayerQuitEvent event) { |
| 88 | + // remove mapengine pipeline handler if the player is still connected |
| 89 | + // while this event is being fired; this prevents errors if the player goes |
| 90 | + // through a second configuration phase |
| 91 | + Channel ch = ((CraftPlayer) event.getPlayer()).getHandle().connection.connection.channel; |
| 92 | + ch.eventLoop().execute(() -> { |
| 93 | + if (ch.isActive()) { |
| 94 | + ch.pipeline().remove("mapengine"); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getDisplayedName() { |
| 101 | + return MinecraftServer.getServer().getServerModName() |
| 102 | + + " " + SharedConstants.getCurrentVersion().name(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void sendPacket(Player player, PacketContainer<Packet<ClientGamePacketListener>> packet) { |
| 107 | + ((CraftPlayer) player).getHandle().connection.connection.channel.write(packet.getPacket()); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void flush(Player player) { |
| 112 | + ((CraftPlayer) player).getHandle().connection.connection.channel.flush(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public PacketContainer<Packet<ClientGamePacketListener>> createMapDataPacket(MapUpdateData data, int mapId, MapCursorCollection cursors) { |
| 117 | + MapId id = new MapId(mapId); |
| 118 | + MapPatch updateData = new MapPatch(data.offsetX(), data.offsetY(), data.width(), data.height(), data.buffer()); |
| 119 | + |
| 120 | + List<MapDecoration> decorations; |
| 121 | + if (cursors != null && cursors.size() > 0) { |
| 122 | + decorations = new ArrayList<>(cursors.size()); |
| 123 | + for (int i = 0; i < cursors.size(); i++) { |
| 124 | + MapCursor cursor = cursors.getCursor(i); |
| 125 | + if (!cursor.isVisible()) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + decorations.add(new MapDecoration(BuiltInRegistries.MAP_DECORATION_TYPE.get(cursor.getRawType()).orElseThrow(), |
| 130 | + cursor.getX(), cursor.getY(), cursor.getDirection(), |
| 131 | + cursor.caption() == null ? Optional.empty() : Optional.of(PaperAdventure.asVanilla(cursor.caption())))); |
| 132 | + } |
| 133 | + } else { |
| 134 | + decorations = null; |
| 135 | + } |
| 136 | + |
| 137 | + return PacketContainer.wrap(this, new ClientboundMapItemDataPacket(id, |
| 138 | + (byte) 0, decorations != null, decorations, updateData)); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public PacketContainer<Packet<ClientGamePacketListener>> createMapEntitySpawnPacket(int entityId, BlockVector pos, BlockFace facing, boolean glowing) { |
| 143 | + int facingIndex = switch (facing) { |
| 144 | + case UP -> 1; |
| 145 | + case NORTH -> 2; |
| 146 | + case SOUTH -> 3; |
| 147 | + case WEST -> 4; |
| 148 | + case EAST -> 5; |
| 149 | + default -> 0; |
| 150 | + }; |
| 151 | + |
| 152 | + return PacketContainer.wrap(this, new ClientboundAddEntityPacket(entityId, UUID.randomUUID(), |
| 153 | + pos.getX(), pos.getY(), pos.getZ(), 0, 0, |
| 154 | + glowing ? EntityType.GLOW_ITEM_FRAME : EntityType.ITEM_FRAME, facingIndex, Vec3.ZERO, 0)); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public PacketContainer<Packet<ClientGamePacketListener>> createMapSetIdPacket(int entityId, int mapId, boolean invisible) { |
| 159 | + SynchedEntityData entityData = ITEM_FRAME_DATA.build(); |
| 160 | + |
| 161 | + ItemStack mapItem = Items.FILLED_MAP.getDefaultInstance(); |
| 162 | + |
| 163 | + mapItem.set(DataComponents.MAP_ID, new MapId(mapId)); |
| 164 | + |
| 165 | + entityData.set(ItemFrame.DATA_ITEM, mapItem); // map item |
| 166 | + |
| 167 | + if (invisible) { |
| 168 | + entityData.set(DATA_SHARED_FLAGS_ID, (byte) 0x20); // invisible |
| 169 | + } |
| 170 | + |
| 171 | + return PacketContainer.wrap(this, new ClientboundSetEntityDataPacket(entityId, Objects.requireNonNull(entityData.packDirty()))); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public PacketContainer<Packet<ClientGamePacketListener>> createRemoveEntitiesPacket(IntList entityIds) { |
| 176 | + return PacketContainer.wrap(this, new ClientboundRemoveEntitiesPacket(entityIds)); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public PacketContainer<?> createInteractionEntitySpawnPacket(int interactionId, Vector pos, BlockFace direction) { |
| 181 | + return PacketContainer.wrap(this, new ClientboundAddEntityPacket(interactionId, UUID.randomUUID(), |
| 182 | + pos.getX(), pos.getY(), pos.getZ(), 0, 0, EntityType.INTERACTION, 0, Vec3.ZERO, 0)); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public PacketContainer<?> createInteractionEntityBlockSizePacket(int interactionId) { |
| 187 | + SynchedEntityData entityData = INTERACTION_DATA.build(); |
| 188 | + |
| 189 | + entityData.set(DATA_INTERACTION_BOX_WIDTH_ID, 1f); |
| 190 | + entityData.set(DATA_INTERACTION_BOX_HEIGHT_ID, 1f); |
| 191 | + entityData.set(DATA_INTERACTION_BOX_RESPONSIVE_ID, true); |
| 192 | + entityData.set(DATA_SHARED_FLAGS_ID, (byte) 0x20); // invisible |
| 193 | + |
| 194 | + return PacketContainer.wrap(this, new ClientboundSetEntityDataPacket(interactionId, Objects.requireNonNull(entityData.packDirty()))); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public PacketContainer<?> createTeleportPacket(int entityId, Vector pos, float yaw, float pitch, boolean onGround) { |
| 199 | + PositionMoveRotation posData = new PositionMoveRotation( |
| 200 | + new Vec3(pos.getX(), pos.getY(), pos.getZ()), Vec3.ZERO, yaw, pitch); |
| 201 | + ClientboundTeleportEntityPacket packet = new ClientboundTeleportEntityPacket( |
| 202 | + entityId, posData, Set.of(), onGround); |
| 203 | + return PacketContainer.wrap(this, packet); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public PacketContainer<?> createItemRotationPacket(int entityId, int rotation) { |
| 208 | + SynchedEntityData entityData = ITEM_FRAME_DATA.build(); |
| 209 | + |
| 210 | + entityData.set(ItemFrame.DATA_ROTATION, rotation); // item rotation (0-7) |
| 211 | + |
| 212 | + return PacketContainer.wrap(this, new ClientboundSetEntityDataPacket(entityId, Objects.requireNonNull(entityData.packDirty()))); |
| 213 | + } |
| 214 | + |
| 215 | + @SuppressWarnings("unchecked") |
| 216 | + @Override |
| 217 | + public void sendBundled(Player player, PacketContainer<?>... packets) { |
| 218 | + List<Packet<? super ClientGamePacketListener>> mcPackets = new ArrayList<>(packets.length); |
| 219 | + for (PacketContainer<?> packetContainer : packets) { |
| 220 | + mcPackets.add((Packet<ClientGamePacketListener>) packetContainer.getPacket()); |
| 221 | + } |
| 222 | + ClientboundBundlePacket packet = new ClientboundBundlePacket(mcPackets); |
| 223 | + ((CraftPlayer) player).getHandle().connection.send(packet); |
| 224 | + } |
| 225 | +} |
0 commit comments