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

The following examples show how to use us.myles.ViaVersion.api.protocol.Protocol. 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: 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 #3
Source Project: ViaVersion   Author: ViaVersion   File: WorldPackets.java    License: MIT License 6 votes vote down vote up
public static void register(Protocol protocol) {
    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PARTICLE, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.INT); // 0 - Particle ID
            map(Type.BOOLEAN); // 1 - Long Distance
            map(Type.FLOAT); // 2 - X
            map(Type.FLOAT); // 3 - Y
            map(Type.FLOAT); // 4 - Z
            map(Type.FLOAT); // 5 - Offset X
            map(Type.FLOAT); // 6 - Offset Y
            map(Type.FLOAT); // 7 - Offset Z
            map(Type.FLOAT); // 8 - Particle Data
            map(Type.INT); // 9 - Particle Count
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 27) {
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, wrapper.read(Type.FLAT_ITEM));
                    }
                }
            });
        }
    });
}
 
Example #4
Source Project: ViaVersion   Author: ViaVersion   File: TranslationMappings.java    License: MIT License 6 votes vote down vote up
public TranslationMappings(Protocol protocol) {
    super(protocol);
    mappings.put("block.minecraft.flowing_water", "Flowing Water");
    mappings.put("block.minecraft.flowing_lava", "Flowing Lava");
    mappings.put("block.minecraft.bed", "Bed");
    mappings.put("block.minecraft.bed.not_valid", "Your home bed was missing or obstructed");
    mappings.put("block.minecraft.bed.set_spawn", "Respawn point set");
    mappings.put("block.minecraft.two_turtle_eggs", "Two Turtle Eggs");
    mappings.put("block.minecraft.three_turtle_eggs", "Three Turtle Eggs");
    mappings.put("block.minecraft.four_turtle_eggs", "Four Turtle Eggs");
    mappings.put("block.minecraft.banner", "Banner");
    mappings.put("block.minecraft.wall_banner", "Wall Banner");
    mappings.put("item.minecraft.zombie_pigman_spawn_egg", "Zombie Pigman Spawn Egg");
    mappings.put("item.minecraft.skeleton_skull", "Skeleton Skull");
    mappings.put("item.minecraft.wither_skeleton_skull", "Wither Skeleton Skull");
    mappings.put("item.minecraft.zombie_head", "Zombie Head");
    mappings.put("item.minecraft.creeper_head", "Creeper Head");
    mappings.put("item.minecraft.dragon_head", "Dragon Head");
    mappings.put("entity.minecraft.zombie_pigman", "Zombie Pigman");
    mappings.put("death.fell.accident.water", "%1$s fell out of the water");
    mappings.put("death.attack.netherBed.message", "%1$s was killed by %2$s");
    mappings.put("death.attack.netherBed.link", "Intentional Game Design");
    mappings.put("advancements.husbandry.break_diamond_hoe.title", "Serious Dedication");
    mappings.put("advancements.husbandry.break_diamond_hoe.description", "Completely use up a diamond hoe, and then reevaluate your life choices");
    mappings.put("biome.minecraft.nether", "Nether");
}
 
Example #5
Source Project: ViaRewind   Author: ViaVersion   File: ScoreboardPackets.java    License: MIT License 5 votes vote down vote up
public static void register(Protocol protocol) {
	/*  OUTGOING  */

	//Display Scoreboard
	protocol.registerOutgoing(State.PLAY, 0x38, 0x3D);

	//Scoreboard Objective
	protocol.registerOutgoing(State.PLAY, 0x3F, 0x3B);

	//Scoreboard Team
	protocol.registerOutgoing(State.PLAY, 0x41, 0x3E, new PacketRemapper() {
		@Override
		public void registerMap() {
			map(Type.STRING);
			map(Type.BYTE);
			handler(new PacketHandler() {
				@Override
				public void handle(PacketWrapper packetWrapper) throws Exception {
					byte mode = packetWrapper.get(Type.BYTE, 0);
					if (mode == 0 || mode == 2) {
						packetWrapper.passthrough(Type.STRING);  //Display Name
						packetWrapper.passthrough(Type.STRING);  //Prefix
						packetWrapper.passthrough(Type.STRING);  //Suffix
						packetWrapper.passthrough(Type.BYTE);  //Friendly Flags
						packetWrapper.passthrough(Type.STRING);  //Name Tag Visibility
						packetWrapper.read(Type.STRING);  //Skip Collision Rule
						packetWrapper.passthrough(Type.BYTE);  //Friendly Flags
					}

					if (mode == 0 || mode == 3 || mode == 4) {
						packetWrapper.passthrough(Type.STRING_ARRAY);
					}
				}
			});
		}
	});

	//Update Score
	protocol.registerOutgoing(State.PLAY, 0x42, 0x3C);
}
 
Example #6
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 5 votes vote down vote up
public static void sendToServer(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) {
	try {
		packet.sendToServer(packetProtocol, skipCurrentPipeline, currentThread);
	} catch (CancelException ignored) {
		;
	} catch (Exception ex) {
		ex.printStackTrace();
	}
}
 
Example #7
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 5 votes vote down vote up
public static boolean sendPacket(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) {
	try {
		packet.send(packetProtocol, skipCurrentPipeline, currentThread);
		return true;
	} catch (CancelException ignored) {
		;
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return false;
}
 
Example #8
Source Project: ViaVersion   Author: ViaVersion   File: PacketWrapper.java    License: MIT License 5 votes vote down vote up
/**
 * Send this packet to the associated user.
 * Be careful not to send packets twice.
 * (Sends it after current)
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @param currentThread       Run in the same thread
 * @throws Exception if it fails to write
 */
public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
    if (!isCancelled()) {
        try {
            ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.OUTGOING);
            user().sendRawPacket(output, currentThread);
        } catch (Exception e) {
            if (!PipelineUtil.containsCause(e, CancelException.class)) {
                throw e;
            }
        }
    }
}
 
Example #9
Source Project: ViaVersion   Author: ViaVersion   File: PacketWrapper.java    License: MIT License 5 votes vote down vote up
/**
 * Let the packet go through the protocol pipes and write it to ByteBuf
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @return Packet buffer
 * @throws Exception if it fails to write
 */
private ByteBuf constructPacket(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, Direction direction) throws Exception {
    // Apply current pipeline
    List<Protocol> protocols = new ArrayList<>(user().getProtocolInfo().getPipeline().pipes());
    if (direction == Direction.OUTGOING) {
        // Other way if outgoing
        Collections.reverse(protocols);
    }
    int index = -1;
    for (int i = 0; i < protocols.size(); i++) {
        if (protocols.get(i).getClass().equals(packetProtocol)) {
            index = skipCurrentPipeline ? (i + 1) : (i);
            break;
        }
    }
    if (index == -1) throw new NoSuchElementException(packetProtocol.getCanonicalName());

    // Reset reader before we start
    resetReader();

    // Apply other protocols
    apply(direction, user().getProtocolInfo().getState(), index, protocols);
    // Send
    ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
    writeToBuffer(output);

    return output;
}
 
Example #10
Source Project: ViaVersion   Author: ViaVersion   File: PacketWrapper.java    License: MIT License 5 votes vote down vote up
/**
 * Send this packet to the server.
 *
 * @param packetProtocol      The protocol version of the packet.
 * @param skipCurrentPipeline Skip the current pipeline
 * @param currentThread       Run in the same thread
 * @throws Exception if it fails to write
 */
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception {
    if (!isCancelled()) {
        try {
            ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING);
            user().sendRawPacketToServer(output, currentThread);
        } catch (Exception e) {
            if (!PipelineUtil.containsCause(e, CancelException.class)) {
                throw e;
            }
        }
    }
}
 
Example #11
Source Project: ViaBackwards   Author: ViaVersion   File: WorldPackets1_13_1.java    License: MIT License 5 votes vote down vote up
public static void register(Protocol protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION, Protocol1_13To1_13_1::getNewBlockStateId, Protocol1_13To1_13_1::getNewBlockId);

    protocol.registerOutgoing(ClientboundPackets1_13.CHUNK_DATA, new PacketRemapper() {
        @Override
        public void registerMap() {
            handler(new PacketHandler() {
                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.passthrough(new Chunk1_13Type(clientWorld));

                    for (ChunkSection section : chunk.getSections()) {
                        if (section != null) {
                            for (int i = 0; i < section.getPaletteSize(); i++) {
                                section.setPaletteEntry(i, Protocol1_13To1_13_1.getNewBlockStateId(section.getPaletteEntry(i)));
                            }
                        }
                    }
                }
            });
        }
    });

    blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
    blockRewriter.registerEffect(ClientboundPackets1_13.EFFECT, 1010, 2001, InventoryPackets1_13_1::getOldItemId);
    blockRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, 3, 20, 27, InventoryPackets1_13_1::toClient, Type.FLAT_ITEM, Type.FLOAT);
}
 
Example #12
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 4 votes vote down vote up
public static void sendToServer(PacketWrapper packet, Class<? extends Protocol> packetProtocol) {
	sendToServer(packet, packetProtocol, true);
}
 
Example #13
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 4 votes vote down vote up
public static void sendToServer(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) {
	sendToServer(packet, packetProtocol, skipCurrentPipeline, false);
}
 
Example #14
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 4 votes vote down vote up
public static boolean sendPacket(PacketWrapper packet, Class<? extends Protocol> packetProtocol) {
	return sendPacket(packet, packetProtocol, true);
}
 
Example #15
Source Project: ViaRewind   Author: ViaVersion   File: PacketUtil.java    License: MIT License 4 votes vote down vote up
public static boolean sendPacket(PacketWrapper packet, Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) {
	return sendPacket(packet, packetProtocol, true, false);
}
 
Example #16
Source Project: ViaVersion   Author: ViaVersion   File: ViaSpongeListener.java    License: MIT License 4 votes vote down vote up
public ViaSpongeListener(SpongePlugin plugin, Class<? extends Protocol> requiredPipeline) {
    super(requiredPipeline);
    this.plugin = plugin;
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: ViaBukkitListener.java    License: MIT License 4 votes vote down vote up
public ViaBukkitListener(Plugin plugin, Class<? extends Protocol> requiredPipeline) {
    super(requiredPipeline);
    this.plugin = plugin;
}
 
Example #18
Source Project: ViaVersion   Author: ViaVersion   File: BlockRewriter.java    License: MIT License 4 votes vote down vote up
public BlockRewriter(Protocol protocol, Type<Position> positionType, IdRewriteFunction blockStateRewriter, IdRewriteFunction blockRewriter) {
    this.protocol = protocol;
    this.positionType = positionType;
    this.blockStateRewriter = blockStateRewriter;
    this.blockRewriter = blockRewriter;
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: SoundRewriter.java    License: MIT License 4 votes vote down vote up
public SoundRewriter(Protocol protocol, IdRewriteFunction idRewriter) {
    this.protocol = protocol;
    this.idRewriter = idRewriter;
}
 
Example #20
Source Project: ViaVersion   Author: ViaVersion   File: ItemRewriter.java    License: MIT License 4 votes vote down vote up
public ItemRewriter(Protocol protocol, RewriteFunction toClient, RewriteFunction toServer) {
    this.protocol = protocol;
    this.toClient = toClient;
    this.toServer = toServer;
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: TagRewriter.java    License: MIT License 4 votes vote down vote up
public TagRewriter(Protocol protocol, IdRewriteFunction blockRewriter, IdRewriteFunction itemRewriter, IdRewriteFunction entityRewriter) {
    this.protocol = protocol;
    this.blockRewriter = blockRewriter;
    this.itemRewriter = itemRewriter;
    this.entityRewriter = entityRewriter;
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: ComponentRewriter.java    License: MIT License 4 votes vote down vote up
public ComponentRewriter(Protocol protocol) {
    this.protocol = protocol;
}
 
Example #23
Source Project: ViaVersion   Author: ViaVersion   File: MetadataRewriter.java    License: MIT License 4 votes vote down vote up
protected MetadataRewriter(Protocol protocol, Class<? extends EntityTracker> entityTrackerClass) {
    this.protocol = protocol;
    this.entityTrackerClass = entityTrackerClass;
    protocol.put(this);
}
 
Example #24
Source Project: ViaVersion   Author: ViaVersion   File: ViaListener.java    License: MIT License 4 votes vote down vote up
public ViaListener(Class<? extends Protocol> requiredPipeline) {
    this.requiredPipeline = requiredPipeline;
}
 
Example #25
Source Project: ViaVersion   Author: ViaVersion   File: ViaListener.java    License: MIT License 4 votes vote down vote up
protected Class<? extends Protocol> getRequiredPipeline() {
    return requiredPipeline;
}
 
Example #26
Source Project: ViaVersion   Author: ViaVersion   File: PacketWrapper.java    License: MIT License 4 votes vote down vote up
public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline) throws Exception {
    sendToServer(packetProtocol, skipCurrentPipeline, false);
}
 
Example #27
Source Project: ViaVersion   Author: ViaVersion   File: PacketWrapper.java    License: MIT License 4 votes vote down vote up
public void sendToServer(Class<? extends Protocol> packetProtocol) throws Exception {
    sendToServer(packetProtocol, true);
}
 
Example #28
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 #29
Source Project: ViaVersion   Author: ViaVersion   File: EntityPackets.java    License: MIT License 4 votes vote down vote up
public static void register(Protocol protocol) {
    final PacketHandler metaTypeHandler = wrapper -> {
        for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) {
            metadata.setMetaType(MetaType1_13_2.byId(metadata.getMetaType().getTypeID()));
        }
    };

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Entity UUID
            map(Type.VAR_INT); // 2 - Entity Type
            map(Type.DOUBLE); // 3 - X
            map(Type.DOUBLE); // 4 - Y
            map(Type.DOUBLE); // 5 - Z
            map(Type.BYTE); // 6 - Yaw
            map(Type.BYTE); // 7 - Pitch
            map(Type.BYTE); // 8 - Head Pitch
            map(Type.SHORT); // 9 - Velocity X
            map(Type.SHORT); // 10 - Velocity Y
            map(Type.SHORT); // 11 - Velocity Z
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 12 - Metadata

            handler(metaTypeHandler);
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Type.UUID); // 1 - Player UUID
            map(Type.DOUBLE); // 2 - X
            map(Type.DOUBLE); // 3 - Y
            map(Type.DOUBLE); // 4 - Z
            map(Type.BYTE); // 5 - Yaw
            map(Type.BYTE); // 6 - Pitch
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 7 - Metadata

            handler(metaTypeHandler);
        }
    });

    protocol.registerOutgoing(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() {
        @Override
        public void registerMap() {
            map(Type.VAR_INT); // 0 - Entity ID
            map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST); // 1 - Metadata list

            handler(metaTypeHandler);
        }
    });
}
 
Example #30
Source Project: ViaVersion   Author: ViaVersion   File: ComponentRewriter1_14.java    License: MIT License 4 votes vote down vote up
public ComponentRewriter1_14(Protocol protocol) {
    super(protocol);
}