|
| 1 | +package dev.simplix.protocolize.velocity.player; |
| 2 | + |
| 3 | +import com.velocitypowered.proxy.connection.MinecraftConnection; |
| 4 | +import com.velocitypowered.proxy.connection.client.InitialInboundConnection; |
| 5 | +import dev.simplix.protocolize.api.*; |
| 6 | +import dev.simplix.protocolize.api.inventory.Inventory; |
| 7 | +import dev.simplix.protocolize.api.inventory.PlayerInventory; |
| 8 | +import dev.simplix.protocolize.api.packet.AbstractPacket; |
| 9 | +import dev.simplix.protocolize.api.player.ProtocolizePlayer; |
| 10 | +import dev.simplix.protocolize.api.providers.ProtocolRegistrationProvider; |
| 11 | +import dev.simplix.protocolize.velocity.packet.VelocityProtocolizePacket; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | + |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +@Slf4j |
| 21 | +public class InitialInboundConnectionProtocolizePlayer implements ProtocolizePlayer { |
| 22 | + |
| 23 | + private static final ProtocolRegistrationProvider REGISTRATION_PROVIDER = Protocolize.protocolRegistration(); |
| 24 | + private static Field connectionField; |
| 25 | + |
| 26 | + static { |
| 27 | + try { |
| 28 | + connectionField = InitialInboundConnection.class.getDeclaredField("connection"); |
| 29 | + connectionField.setAccessible(true); |
| 30 | + } catch (ReflectiveOperationException e) { |
| 31 | + log.error("Unable to initialize InitialInboundConnectionProtocolizePlayer. Maybe your velocity version is unsupported.", e); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private final InitialInboundConnection connection; |
| 36 | + |
| 37 | + public InitialInboundConnectionProtocolizePlayer(InitialInboundConnection connection) { |
| 38 | + this.connection = connection; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public UUID uniqueId() { |
| 43 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public PlayerInventory proxyInventory() { |
| 48 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void sendPacket(Object packet) { |
| 53 | + if (packet instanceof AbstractPacket) { |
| 54 | + VelocityProtocolizePacket pack = (VelocityProtocolizePacket) REGISTRATION_PROVIDER.createPacket((Class<? extends AbstractPacket>) packet.getClass(), |
| 55 | + Protocol.STATUS, PacketDirection.CLIENTBOUND, protocolVersion()); |
| 56 | + if (pack == null) { |
| 57 | + throw new IllegalStateException("Cannot send " + packet.getClass().getName() + " to players with protocol version " + protocolVersion()); |
| 58 | + } |
| 59 | + pack.wrapper((AbstractPacket) packet); |
| 60 | + packet = pack; |
| 61 | + } |
| 62 | + Object finalPacket = packet; |
| 63 | + try { |
| 64 | + MinecraftConnection minecraftConnection = (MinecraftConnection) connectionField.get(connection); |
| 65 | + minecraftConnection.write(finalPacket); |
| 66 | + } catch (IllegalAccessException e) { |
| 67 | + log.error("Unable to send packet to InitialInboundConnectionProtocolizePlayer. Maybe your velocity version is unsupported.", e); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void sendPacketToServer(Object packet) { |
| 73 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Map<Integer, Inventory> registeredInventories() { |
| 78 | + return Collections.emptyMap(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int generateWindowId() { |
| 83 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int protocolVersion() { |
| 88 | + return connection.getProtocolVersion().getProtocol(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public <T> T handle() { |
| 93 | + return (T) connection; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Location location() { |
| 98 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onInteract(Consumer<PlayerInteract> interactConsumer) { |
| 103 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void handleInteract(PlayerInteract interact) { |
| 108 | + throw new IllegalStateException("Not possible for initial inbound connections"); |
| 109 | + } |
| 110 | +} |
0 commit comments