Search in sources :

Example 31 with UserConnection

use of com.viaversion.viaversion.api.connection.UserConnection in project ViaVersion by ViaVersion.

the class BukkitViaLoader method load.

@Override
public void load() {
    // Update Listener
    registerListener(new UpdateListener());
    /* Base Protocol */
    final ViaVersionPlugin plugin = (ViaVersionPlugin) Bukkit.getPluginManager().getPlugin("ViaVersion");
    // Add ProtocolSupport ConnectListener if necessary.
    ClassGenerator.registerPSConnectListener(plugin);
    int serverProtocolVersion = Via.getAPI().getServerVersion().lowestSupportedVersion();
    /* 1.9 client to 1.8 server */
    if (serverProtocolVersion < ProtocolVersion.v1_9.getVersion()) {
        storeListener(new ArmorListener(plugin)).register();
        storeListener(new DeathListener(plugin)).register();
        storeListener(new BlockListener(plugin)).register();
        if (plugin.getConf().isItemCache()) {
            handItemCache = new HandItemCache();
            // Updates player's items :)
            tasks.add(handItemCache.runTaskTimerAsynchronously(plugin, 1L, 1L));
        }
    }
    if (serverProtocolVersion < ProtocolVersion.v1_14.getVersion()) {
        boolean use1_9Fix = plugin.getConf().is1_9HitboxFix() && serverProtocolVersion < ProtocolVersion.v1_9.getVersion();
        if (use1_9Fix || plugin.getConf().is1_14HitboxFix()) {
            try {
                storeListener(new PlayerSneakListener(plugin, use1_9Fix, plugin.getConf().is1_14HitboxFix())).register();
            } catch (ReflectiveOperationException e) {
                Via.getPlatform().getLogger().warning("Could not load hitbox fix - please report this on our GitHub");
                e.printStackTrace();
            }
        }
    }
    if (serverProtocolVersion < ProtocolVersion.v1_15.getVersion()) {
        try {
            Class.forName("org.bukkit.event.entity.EntityToggleGlideEvent");
            storeListener(new EntityToggleGlideListener(plugin)).register();
        } catch (ClassNotFoundException ignored) {
        }
    }
    if (serverProtocolVersion < ProtocolVersion.v1_12.getVersion() && !Boolean.getBoolean("com.viaversion.ignorePaperBlockPlacePatch")) {
        boolean paper = true;
        try {
            // Paper 1.8 ?
            Class.forName("org.github.paperspigot.PaperSpigotConfig");
        } catch (ClassNotFoundException ignored) {
            try {
                // Paper 1.9+ ?
                Class.forName("com.destroystokyo.paper.PaperConfig");
            } catch (ClassNotFoundException alsoIgnored) {
                // Definitely not Paper
                paper = false;
            }
        }
        if (paper) {
            storeListener(new PaperPatch(plugin)).register();
        }
    }
    /* Providers */
    if (serverProtocolVersion < ProtocolVersion.v1_9.getVersion()) {
        Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BukkitViaMovementTransmitter());
        Via.getManager().getProviders().use(HandItemProvider.class, new HandItemProvider() {

            @Override
            public Item getHandItem(final UserConnection info) {
                if (handItemCache != null) {
                    return handItemCache.getHandItem(info.getProtocolInfo().getUuid());
                }
                try {
                    return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), () -> {
                        UUID playerUUID = info.getProtocolInfo().getUuid();
                        Player player = Bukkit.getPlayer(playerUUID);
                        if (player != null) {
                            return HandItemCache.convert(player.getItemInHand());
                        }
                        return null;
                    }).get(10, TimeUnit.SECONDS);
                } catch (Exception e) {
                    Via.getPlatform().getLogger().severe("Error fetching hand item: " + e.getClass().getName());
                    if (Via.getManager().isDebug())
                        e.printStackTrace();
                    return null;
                }
            }
        });
    }
    if (serverProtocolVersion < ProtocolVersion.v1_12.getVersion()) {
        if (plugin.getConf().is1_12QuickMoveActionFix()) {
            Via.getManager().getProviders().use(InventoryQuickMoveProvider.class, new BukkitInventoryQuickMoveProvider());
        }
    }
    if (serverProtocolVersion < ProtocolVersion.v1_13.getVersion()) {
        if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("world")) {
            BukkitBlockConnectionProvider blockConnectionProvider = new BukkitBlockConnectionProvider();
            Via.getManager().getProviders().use(BlockConnectionProvider.class, blockConnectionProvider);
            ConnectionData.blockConnectionProvider = blockConnectionProvider;
        }
    }
}
Also used : Player(org.bukkit.entity.Player) PaperPatch(com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.PaperPatch) BukkitViaMovementTransmitter(com.viaversion.viaversion.bukkit.providers.BukkitViaMovementTransmitter) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BukkitBlockConnectionProvider(com.viaversion.viaversion.bukkit.providers.BukkitBlockConnectionProvider) Item(com.viaversion.viaversion.api.minecraft.item.Item) EntityToggleGlideListener(com.viaversion.viaversion.bukkit.listeners.protocol1_15to1_14_4.EntityToggleGlideListener) BukkitInventoryQuickMoveProvider(com.viaversion.viaversion.bukkit.providers.BukkitInventoryQuickMoveProvider) ArmorListener(com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.ArmorListener) BlockListener(com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.BlockListener) PlayerSneakListener(com.viaversion.viaversion.bukkit.listeners.multiversion.PlayerSneakListener) HandItemProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider) ViaVersionPlugin(com.viaversion.viaversion.ViaVersionPlugin) UpdateListener(com.viaversion.viaversion.bukkit.listeners.UpdateListener) UUID(java.util.UUID) HandItemCache(com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.HandItemCache) DeathListener(com.viaversion.viaversion.bukkit.listeners.protocol1_9to1_8.DeathListener)

Example 32 with UserConnection

use of com.viaversion.viaversion.api.connection.UserConnection in project ViaVersion by ViaVersion.

the class ViaAPIBase method sendRawPacket.

@Override
public void sendRawPacket(UUID uuid, ByteBuf packet) throws IllegalArgumentException {
    if (!isInjected(uuid)) {
        throw new IllegalArgumentException("This player is not controlled by ViaVersion!");
    }
    UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(uuid);
    user.scheduleSendRawPacket(packet);
}
Also used : UserConnection(com.viaversion.viaversion.api.connection.UserConnection)

Example 33 with UserConnection

use of com.viaversion.viaversion.api.connection.UserConnection in project ViaVersion by ViaVersion.

the class BukkitChannelInitializer method afterChannelInitialize.

public static void afterChannelInitialize(Channel channel) {
    UserConnection connection = new UserConnectionImpl(channel);
    new ProtocolPipelineImpl(connection);
    if (PaperViaInjector.PAPER_PACKET_LIMITER) {
        connection.setPacketLimiterEnabled(false);
    }
    // Add our transformers
    HandlerConstructor constructor = ClassGenerator.getConstructor();
    MessageToByteEncoder encoder = constructor.newEncodeHandler(connection, (MessageToByteEncoder) channel.pipeline().get("encoder"));
    ByteToMessageDecoder decoder = constructor.newDecodeHandler(connection, (ByteToMessageDecoder) channel.pipeline().get("decoder"));
    channel.pipeline().replace("encoder", "encoder", encoder);
    channel.pipeline().replace("decoder", "decoder", decoder);
}
Also used : UserConnectionImpl(com.viaversion.viaversion.connection.UserConnectionImpl) HandlerConstructor(com.viaversion.viaversion.classgenerator.generated.HandlerConstructor) ProtocolPipelineImpl(com.viaversion.viaversion.protocol.ProtocolPipelineImpl) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) MessageToByteEncoder(io.netty.handler.codec.MessageToByteEncoder)

Example 34 with UserConnection

use of com.viaversion.viaversion.api.connection.UserConnection in project ViaVersion by ViaVersion.

the class ElytraPatch method onServerConnected.

@EventHandler(priority = EventPriority.HIGH)
public void onServerConnected(ServerConnectedEvent event) {
    UserConnection user = Via.getManager().getConnectionManager().getConnectedClient(event.getPlayer().getUniqueId());
    if (user == null)
        return;
    try {
        if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
            EntityTracker1_9 tracker = user.getEntityTracker(Protocol1_9To1_8.class);
            int entityId = tracker.getProvidedEntityId();
            PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_9.ENTITY_METADATA, null, user);
            wrapper.write(Type.VAR_INT, entityId);
            wrapper.write(Types1_9.METADATA_LIST, Collections.singletonList(new Metadata(0, MetaType1_9.Byte, (byte) 0)));
            wrapper.scheduleSend(Protocol1_9To1_8.class);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) EventHandler(net.md_5.bungee.event.EventHandler)

Example 35 with UserConnection

use of com.viaversion.viaversion.api.connection.UserConnection in project ViaVersion by ViaVersion.

the class PPSSubCmd method execute.

@Override
public boolean execute(ViaCommandSender sender, String[] args) {
    Map<Integer, Set<String>> playerVersions = new HashMap<>();
    int totalPackets = 0;
    int clients = 0;
    long max = 0;
    for (ViaCommandSender p : Via.getPlatform().getOnlinePlayers()) {
        int playerVersion = Via.getAPI().getPlayerVersion(p.getUUID());
        if (!playerVersions.containsKey(playerVersion))
            playerVersions.put(playerVersion, new HashSet<>());
        UserConnection uc = Via.getManager().getConnectionManager().getConnectedClient(p.getUUID());
        if (uc != null && uc.getPacketTracker().getPacketsPerSecond() > -1) {
            playerVersions.get(playerVersion).add(p.getName() + " (" + uc.getPacketTracker().getPacketsPerSecond() + " PPS)");
            totalPackets += uc.getPacketTracker().getPacketsPerSecond();
            if (uc.getPacketTracker().getPacketsPerSecond() > max) {
                max = uc.getPacketTracker().getPacketsPerSecond();
            }
            clients++;
        }
    }
    Map<Integer, Set<String>> sorted = new TreeMap<>(playerVersions);
    sendMessage(sender, "&4Live Packets Per Second");
    if (clients > 1) {
        sendMessage(sender, "&cAverage: &f" + (totalPackets / clients));
        sendMessage(sender, "&cHighest: &f" + max);
    }
    if (clients == 0) {
        sendMessage(sender, "&cNo clients to display.");
    }
    for (Map.Entry<Integer, Set<String>> entry : sorted.entrySet()) sendMessage(sender, "&8[&6%s&8]: &b%s", ProtocolVersion.getProtocol(entry.getKey()).getName(), entry.getValue());
    sorted.clear();
    return true;
}
Also used : ViaCommandSender(com.viaversion.viaversion.api.command.ViaCommandSender) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

UserConnection (com.viaversion.viaversion.api.connection.UserConnection)37 UserConnectionImpl (com.viaversion.viaversion.connection.UserConnectionImpl)20 ProtocolPipelineImpl (com.viaversion.viaversion.protocol.ProtocolPipelineImpl)20 SocketChannel (io.netty.channel.socket.SocketChannel)16 Inject (org.spongepowered.asm.mixin.injection.Inject)15 FabricDecodeHandler (com.viaversion.fabric.common.handler.FabricDecodeHandler)14 FabricEncodeHandler (com.viaversion.fabric.common.handler.FabricEncodeHandler)14 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)5 ProtocolInfo (com.viaversion.viaversion.api.connection.ProtocolInfo)3 EventHandler (net.md_5.bungee.event.EventHandler)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)2 Via (com.viaversion.viaversion.api.Via)2 StorableObject (com.viaversion.viaversion.api.connection.StorableObject)2 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 ProtocolPathEntry (com.viaversion.viaversion.api.protocol.ProtocolPathEntry)2 ClientboundPacketType (com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)2 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)2