us.myles.ViaVersion.api.protocol.ProtocolRegistry Java Examples

The following examples show how to use us.myles.ViaVersion.api.protocol.ProtocolRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: ViaVersion   Author: ViaVersion   File: BungeeServerHandler.java    License: MIT License 6 votes vote down vote up
@EventHandler(priority = 120)
public void onServerConnect(ServerConnectEvent e) {
    if (e.isCancelled()) {
        return;
    }

    UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId());
    if (user == null) return;
    if (!user.has(BungeeStorage.class)) {
        user.put(new BungeeStorage(user, e.getPlayer()));
    }

    int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName());
    List<Pair<Integer, Protocol>> protocols = ProtocolRegistry.getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId);

    // Check if ViaVersion can support that version
    try {
        //Object pendingConnection = getPendingConnection.invoke(e.getPlayer());
        Object handshake = getHandshake.invoke(e.getPlayer().getPendingConnection());
        setProtocol.invoke(handshake, protocols == null ? user.getProtocolInfo().getProtocolVersion() : protocolId);
    } catch (InvocationTargetException | IllegalAccessException e1) {
        e1.printStackTrace();
    }
}
 
Example #2
Source Project: ViaVersion   Author: ViaVersion   File: SpongeChannelInitializer.java    License: MIT License 6 votes vote down vote up
@Override
protected void initChannel(Channel channel) throws Exception {
    // Ensure ViaVersion is loaded
    if (ProtocolRegistry.SERVER_PROTOCOL != -1
            && channel instanceof SocketChannel) { // channel can be LocalChannel on internal server
        UserConnection info = new UserConnection((SocketChannel) channel);
        // init protocol
        new ProtocolPipeline(info);
        // Add originals
        this.method.invoke(this.original, channel);
        // Add our transformers
        MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) channel.pipeline().get("encoder"));
        ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) channel.pipeline().get("decoder"));
        SpongePacketHandler chunkHandler = new SpongePacketHandler(info);

        channel.pipeline().replace("encoder", "encoder", encoder);
        channel.pipeline().replace("decoder", "decoder", decoder);
        channel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
    } else {
        this.method.invoke(this.original, channel);
    }
}
 
Example #3
Source Project: ViaVersion   Author: ViaVersion   File: VelocityViaLoader.java    License: MIT License 6 votes vote down vote up
@Override
public void load() {
    Object plugin = VelocityPlugin.PROXY.getPluginManager()
            .getPlugin("viaversion").flatMap(PluginContainer::getInstance).get();

    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        Via.getManager().getProviders().use(MovementTransmitterProvider.class, new VelocityMovementTransmitter());
        Via.getManager().getProviders().use(BossBarProvider.class, new VelocityBossBarProvider());
        VelocityPlugin.PROXY.getEventManager().register(plugin, new ElytraPatch());
    }

    Via.getManager().getProviders().use(VersionProvider.class, new VelocityVersionProvider());
    // We probably don't need a EntityIdProvider because velocity sends a Join packet on server change
    // We don't need main hand patch because Join Game packet makes client send hand data again

    VelocityPlugin.PROXY.getEventManager().register(plugin, new UpdateListener());
    VelocityPlugin.PROXY.getEventManager().register(plugin, new VelocityServerHandler());

    int pingInterval = ((VelocityViaConfig) Via.getPlatform().getConf()).getVelocityPingInterval();
    if (pingInterval > 0) {
        Via.getPlatform().runRepeatingSync(
                new ProtocolDetectorService(),
                pingInterval * 20L);
    }
}
 
Example #4
Source Project: ViaVersion   Author: ViaVersion   File: VelocityServerHandler.java    License: MIT License 6 votes vote down vote up
@Subscribe
public void preServerConnect(ServerPreConnectEvent e) {
    try {
        UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId());
        if (user == null) return;
        if (!user.has(VelocityStorage.class)) {
            user.put(new VelocityStorage(user, e.getPlayer()));
        }

        int protocolId = ProtocolDetectorService.getProtocolId(e.getOriginalServer().getServerInfo().getName());
        List<Pair<Integer, Protocol>> protocols = ProtocolRegistry.getProtocolPath(user.getProtocolInfo().getProtocolVersion(), protocolId);

        // Check if ViaVersion can support that version
        Object connection = getMinecraftConnection.invoke(e.getPlayer());
        setNextProtocolVersion.invoke(connection, ProtocolVersion.getProtocolVersion(protocols == null
                ? user.getProtocolInfo().getProtocolVersion()
                : protocolId));

    } catch (IllegalAccessException | InvocationTargetException e1) {
        e1.printStackTrace();
    }
}
 
Example #5
Source Project: ViaFabric   Author: ViaVersion   File: VRViaAPI.java    License: MIT License 5 votes vote down vote up
@Override
public SortedSet<Integer> getSupportedVersions() {
    SortedSet<Integer> outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions());
    outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols());

    return outputSet;
}
 
Example #6
Source Project: ViaRewind   Author: ViaVersion   File: ViaRewindPlatform.java    License: MIT License 5 votes vote down vote up
default void init(ViaRewindConfig config) {
	ViaRewind.init(this, config);

	String version = ViaRewind.class.getPackage().getImplementationVersion();
	Via.getManager().getSubPlatforms().add(version != null ? version : "UNKNOWN");

	ProtocolRegistry.registerProtocol(new Protocol1_8TO1_9(), Collections.singletonList(ProtocolVersion.v1_8.getId()), ProtocolVersion.v1_9.getId());
	ProtocolRegistry.registerProtocol(new Protocol1_7_6_10TO1_8(), Collections.singletonList(ProtocolVersion.v1_7_6.getId()), ProtocolVersion.v1_8.getId());
	ProtocolRegistry.registerProtocol(new Protocol1_7_0_5to1_7_6_10(), Collections.singletonList(ProtocolVersion.v1_7_1.getId()), ProtocolVersion.v1_7_6.getId());
}
 
Example #7
Source Project: ViaVersion   Author: ViaVersion   File: BukkitViaAPI.java    License: MIT License 5 votes vote down vote up
private int getExternalVersion(Player player) {
    if (!isProtocolSupport()) {
        return ProtocolRegistry.SERVER_PROTOCOL;
    } else {
        return ProtocolSupportUtil.getProtocolVersion(player);
    }
}
 
Example #8
Source Project: ViaVersion   Author: ViaVersion   File: BukkitViaAPI.java    License: MIT License 5 votes vote down vote up
@Override
public SortedSet<Integer> getSupportedVersions() {
    SortedSet<Integer> outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions());
    outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols());

    return outputSet;
}
 
Example #9
Source Project: ViaVersion   Author: ViaVersion   File: BukkitInventoryQuickMoveProvider.java    License: MIT License 5 votes vote down vote up
@Override
public boolean registerQuickMoveAction(short windowId, short slotId, short actionId, UserConnection userConnection) {
    if (!supported) {
        return false;
    }
    if (slotId < 0) { // clicked out of inv slot
        return false;
    }
    if (windowId == 0) {
        /**
         * windowId is always 0 for player inventory.
         * This has almost definitely something to do with the offhand slot.
         */
        if (slotId >= 36 && slotId <= 44) {
            int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
            // this seems to be working just fine.
            if (protocolId == ProtocolVersion.v1_8.getId()) {
                return false;
            }
        }
    }
    ProtocolInfo info = userConnection.getProtocolInfo();
    UUID uuid = info.getUuid();
    BukkitInventoryUpdateTask updateTask = updateTasks.get(uuid);
    final boolean registered = updateTask != null;
    if (!registered) {
        updateTask = new BukkitInventoryUpdateTask(this, uuid);
        updateTasks.put(uuid, updateTask);
    }
    // http://wiki.vg/index.php?title=Protocol&oldid=13223#Click_Window
    updateTask.addItem(windowId, slotId, actionId);
    if (!registered) {
        Via.getPlatform().runSync(updateTask, 5L);
    }
    return true;
}
 
Example #10
Source Project: ViaVersion   Author: ViaVersion   File: BukkitInventoryQuickMoveProvider.java    License: MIT License 5 votes vote down vote up
private boolean isSupported() {
    int protocolId = ProtocolRegistry.SERVER_PROTOCOL;
    if (protocolId >= ProtocolVersion.v1_8.getId() && protocolId <= ProtocolVersion.v1_11_1.getId()) {
        return true; // 1.8-1.11.2
    }
    // this is not needed on 1.12+ servers
    return false;
}
 
Example #11
Source Project: ViaVersion   Author: ViaVersion   File: PlayerSneakListener.java    License: MIT License 5 votes vote down vote up
public PlayerSneakListener(ViaVersionPlugin plugin, boolean is1_9Fix, boolean is1_14Fix) throws ReflectiveOperationException {
    super(plugin, null);
    this.is1_9Fix = is1_9Fix;
    this.is1_14Fix = is1_14Fix;

    final String packageName = plugin.getServer().getClass().getPackage().getName();
    getHandle = Class.forName(packageName + ".entity.CraftPlayer").getMethod("getHandle");

    final Class<?> entityPlayerClass = Class.forName(packageName
            .replace("org.bukkit.craftbukkit", "net.minecraft.server") + ".EntityPlayer");
    try {
        setSize = entityPlayerClass.getMethod("setSize", Float.TYPE, Float.TYPE);
    } catch (NoSuchMethodException e) {
        // Don't catch this one
        setSize = entityPlayerClass.getMethod("a", Float.TYPE, Float.TYPE);
    }


    // From 1.9 upwards the server hitbox is set in every entity tick, so we have to reset it everytime
    if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_9.getId()) {
        sneaking = new WeakHashMap<>();
        useCache = true;
        plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
            @Override
            public void run() {
                for (Map.Entry<Player, Boolean> entry : sneaking.entrySet()) {
                    setHeight(entry.getKey(), entry.getValue() ? HEIGHT_1_14 : HEIGHT_1_9);
                }
            }
        }, 1, 1);
    }

    // Suffocation removal only required for 1.14+ clients.
    if (is1_14Fix) {
        sneakingUuids = new HashSet<>();
    }
}
 
Example #12
Source Project: ViaVersion   Author: ViaVersion   File: BungeeViaAPI.java    License: MIT License 5 votes vote down vote up
@Override
public SortedSet<Integer> getSupportedVersions() {
    SortedSet<Integer> outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions());
    outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols());

    return outputSet;
}
 
Example #13
Source Project: ViaVersion   Author: ViaVersion   File: BungeeViaLoader.java    License: MIT License 5 votes vote down vote up
@Override
public void load() {
    // Listeners
    registerListener(plugin);
    registerListener(new UpdateListener());
    registerListener(new BungeeServerHandler());

    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        registerListener(new ElytraPatch());
    }

    // Providers
    Via.getManager().getProviders().use(VersionProvider.class, new BungeeVersionProvider());
    Via.getManager().getProviders().use(EntityIdProvider.class, new BungeeEntityIdProvider());

    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        Via.getManager().getProviders().use(MovementTransmitterProvider.class, new BungeeMovementTransmitter());
        Via.getManager().getProviders().use(BossBarProvider.class, new BungeeBossBarProvider());
        Via.getManager().getProviders().use(MainHandProvider.class, new BungeeMainHandProvider());
    }

    if (plugin.getConf().getBungeePingInterval() > 0) {
        tasks.add(plugin.getProxy().getScheduler().schedule(
                plugin,
                new ProtocolDetectorService(plugin),
                0, plugin.getConf().getBungeePingInterval(),
                TimeUnit.SECONDS
        ));
    }
}
 
Example #14
Source Project: ViaVersion   Author: ViaVersion   File: SpongeViaLoader.java    License: MIT License 5 votes vote down vote up
@Override
public void load() {
    // Update Listener
    registerListener(new UpdateListener());

    /* 1.9 client to 1.8 server */
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        try {
            Class.forName("org.spongepowered.api.event.entity.DisplaceEntityEvent");
            storeListener(new Sponge4ArmorListener()).register();
        } catch (ClassNotFoundException e) {
            storeListener(new Sponge5ArmorListener(plugin)).register();
        }
        storeListener(new DeathListener(plugin)).register();
        storeListener(new BlockListener(plugin)).register();

        if (plugin.getConf().isItemCache()) {
            tasks.add(Via.getPlatform().runRepeatingSync(new HandItemCache(), 2L)); // Updates players items :)
            HandItemCache.CACHE = true;
        }
    }

    /* Providers */
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        Via.getManager().getProviders().use(BulkChunkTranslatorProvider.class, new SpongeViaBulkChunkTranslator());
        Via.getManager().getProviders().use(MovementTransmitterProvider.class, new SpongeViaMovementTransmitter());

        Via.getManager().getProviders().use(HandItemProvider.class, new HandItemProvider() {
            @Override
            public Item getHandItem(final UserConnection info) {
                if (HandItemCache.CACHE) {
                    return HandItemCache.getHandItem(info.getProtocolInfo().getUuid());
                } else {
                    return super.getHandItem(info);
                }
            }
        });
    }
}
 
Example #15
Source Project: ViaVersion   Author: ViaVersion   File: SpongeViaAPI.java    License: MIT License 5 votes vote down vote up
@Override
public SortedSet<Integer> getSupportedVersions() {
    SortedSet<Integer> outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions());
    outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols());

    return outputSet;
}
 
Example #16
Source Project: ViaVersion   Author: ViaVersion   File: VelocityViaAPI.java    License: MIT License 5 votes vote down vote up
@Override
public SortedSet<Integer> getSupportedVersions() {
    SortedSet<Integer> outputSet = new TreeSet<>(ProtocolRegistry.getSupportedVersions());
    outputSet.removeAll(Via.getPlatform().getConf().getBlockedProtocols());

    return outputSet;
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: ViaManager.java    License: MIT License 5 votes vote down vote up
public void init() {
    if (System.getProperty("ViaVersion") != null) {
        // Reload?
        platform.onReload();
    }
    // Check for updates
    if (platform.getConf().isCheckForUpdates()) {
        UpdateUtil.sendUpdateMessage();
    }

    // Force class load
    ProtocolRegistry.init();

    // Inject
    try {
        injector.inject();
    } catch (Exception e) {
        platform.getLogger().severe("ViaVersion failed to inject:");
        e.printStackTrace();
        return;
    }

    // Mark as injected
    System.setProperty("ViaVersion", platform.getPluginVersion());

    for (Runnable listener : enableListeners) {
        listener.run();
    }
    enableListeners = null;

    // If successful
    platform.runSync(this::onServerLoaded);
}
 
Example #18
Source Project: ViaFabric   Author: ViaVersion   File: MixinMultiplayerScreen.java    License: MIT License 4 votes vote down vote up
@Unique
private boolean isSupported(int protocol) {
    return ProtocolRegistry.getProtocolPath(ProtocolRegistry.SERVER_PROTOCOL, protocol) != null
            || ProtocolRegistry.SERVER_PROTOCOL == protocol;
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: SpongeViaAPI.java    License: MIT License 4 votes vote down vote up
@Override
public int getPlayerVersion(UUID uuid) {
    if (!isInjected(uuid))
        return ProtocolRegistry.SERVER_PROTOCOL;
    return Via.getManager().getConnection(uuid).getProtocolInfo().getProtocolVersion();
}
 
Example #20
Source Project: ViaVersion   Author: ViaVersion   File: BaseProtocol.java    License: MIT License 4 votes vote down vote up
@Override
protected void registerPackets() {
    /* Incoming Packets */

    // Handshake Packet
    registerIncoming(State.HANDSHAKE, 0x00, 0x00, new PacketRemapper() {
        @Override
        public void registerMap() {
            // select right protocol
            map(Type.VAR_INT); // 0 - Client Protocol Version
            map(Type.STRING); // 1 - Server Address
            map(Type.UNSIGNED_SHORT); // 2 - Server Port
            map(Type.VAR_INT); // 3 - Next State
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int protVer = wrapper.get(Type.VAR_INT, 0);
                    int state = wrapper.get(Type.VAR_INT, 1);

                    ProtocolInfo info = wrapper.user().getProtocolInfo();
                    info.setProtocolVersion(protVer);
                    // Ensure the server has a version provider
                    if (Via.getManager().getProviders().get(VersionProvider.class) == null) {
                        wrapper.user().setActive(false);
                        return;
                    }
                    // Choose the pipe
                    int protocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user());
                    info.setServerProtocolVersion(protocol);
                    List<Pair<Integer, Protocol>> protocols = null;

                    // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it)
                    if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) {
                        protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol);
                    }

                    ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline();
                    if (protocols != null) {
                        for (Pair<Integer, Protocol> prot : protocols) {
                            pipeline.add(prot.getValue());
                            // Ensure mapping data has already been loaded
                            ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass());
                        }
                        wrapper.set(Type.VAR_INT, 0, protocol);
                    }

                    // Add Base Protocol
                    pipeline.add(ProtocolRegistry.getBaseProtocol(protocol));

                    // Change state
                    if (state == 1) {
                        info.setState(State.STATUS);
                    }
                    if (state == 2) {
                        info.setState(State.LOGIN);
                    }
                }
            });
        }
    });
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: VersionProvider.java    License: MIT License 4 votes vote down vote up
public int getServerProtocol(UserConnection connection) throws Exception {
    return ProtocolRegistry.SERVER_PROTOCOL;
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: ViaManager.java    License: MIT License 4 votes vote down vote up
public void onServerLoaded() {
    // Load Server Protocol
    try {
        ProtocolRegistry.SERVER_PROTOCOL = injector.getServerProtocolVersion();
    } catch (Exception e) {
        platform.getLogger().severe("ViaVersion failed to get the server protocol!");
        e.printStackTrace();
    }
    // Check if there are any pipes to this version
    if (ProtocolRegistry.SERVER_PROTOCOL != -1) {
        platform.getLogger().info("ViaVersion detected server version: " + ProtocolVersion.getProtocol(ProtocolRegistry.SERVER_PROTOCOL));
        if (!ProtocolRegistry.isWorkingPipe()) {
            platform.getLogger().warning("ViaVersion does not have any compatible versions for this server version!");
            platform.getLogger().warning("Please remember that ViaVersion only adds support for versions newer than the server version.");
            platform.getLogger().warning("If you need support for older versions you may need to use one or more ViaVersion addons too.");
            platform.getLogger().warning("In that case please read the ViaVersion resource page carefully, and if you're");
            platform.getLogger().warning("still unsure, feel free to join our Discord-Server for further assistance.");
        }
    }
    // Load Listeners / Tasks
    ProtocolRegistry.onServerLoaded();

    // Load Platform
    loader.load();
    // Common tasks
    mappingLoadingTask = Via.getPlatform().runRepeatingSync(() -> {
        if (ProtocolRegistry.checkForMappingCompletion()) {
            platform.cancelTask(mappingLoadingTask);
            mappingLoadingTask = null;
        }
    }, 10L);
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
        if (Via.getConfig().isSimulatePlayerTick()) {
            Via.getPlatform().runRepeatingSync(new ViaIdleThread(), 1L);
        }
    }
    if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_13.getId()) {
        if (Via.getConfig().get1_13TabCompleteDelay() > 0) {
            Via.getPlatform().runRepeatingSync(new TabCompleteThread(), 1L);
        }
    }

    // Refresh Versions
    ProtocolRegistry.refreshVersions();
}
 
Example #23
Source Project: ViaBackwards   Author: ViaVersion   File: BukkitPlugin.java    License: MIT License 4 votes vote down vote up
private void onServerLoaded() {
    if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_14.getId()) {
        BukkitViaLoader loader = (BukkitViaLoader) Via.getManager().getLoader();
        loader.storeListener(new LecternInteractListener(this)).register();
    }
}
 
Example #24
Source Project: ViaBackwards   Author: ViaVersion   File: BackwardsProtocol.java    License: MIT License 4 votes vote down vote up
/**
 * Waits for the given protocol to be loaded to then asynchronously execute the runnable for this protocol.
 */
protected void executeAsyncAfterLoaded(Class<? extends Protocol> protocolClass, Runnable runnable) {
    ProtocolRegistry.addMappingLoaderFuture(getClass(), protocolClass, runnable);
}