Java Code Examples for us.myles.ViaVersion.api.PacketWrapper#send()

The following examples show how to use us.myles.ViaVersion.api.PacketWrapper#send() . 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 File: ElytraPatch.java    From ViaVersion with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onServerConnected(ServerConnectedEvent event) {
    UserConnection user = Via.getManager().getConnection(event.getPlayer().getUniqueId());
    if (user == null) return;

    try {
        if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
            int entityId = user.get(EntityTracker1_9.class).getProvidedEntityId();

            PacketWrapper wrapper = new PacketWrapper(0x39, 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.send(Protocol1_9To1_8.class);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: ArmorListener.java    From ViaVersion with MIT License 6 votes vote down vote up
public void sendArmorUpdate(Player player) {
    // Ensure that the player is on our pipe
    if (!isOnPipe(player)) return;

    int armor = 0;
    for (ItemStack stack : player.getInventory().getArmorContents()) {
        armor += ArmorType.findById(stack.getTypeId()).getArmorPoints();
    }

    PacketWrapper wrapper = new PacketWrapper(0x4B, null, getUserConnection(player));
    try {
        wrapper.write(Type.VAR_INT, player.getEntityId()); // Player ID
        wrapper.write(Type.INT, 1); // only 1 property
        wrapper.write(Type.STRING, "generic.armor");
        wrapper.write(Type.DOUBLE, 0D); //default 0 armor
        wrapper.write(Type.VAR_INT, 1); // 1 modifier
        wrapper.write(Type.UUID, ARMOR_ATTRIBUTE); // armor modifier uuid
        wrapper.write(Type.DOUBLE, (double) armor); // the modifier value
        wrapper.write(Type.BYTE, (byte) 0);// the modifier operation, 0 is add number

        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: ElytraPatch.java    From ViaVersion with MIT License 6 votes vote down vote up
@Subscribe(order = PostOrder.LAST)
public void onServerConnected(ServerConnectedEvent event) {
    UserConnection user = Via.getManager().getConnection(event.getPlayer().getUniqueId());
    if (user == null) return;

    try {
        if (user.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
            int entityId = user.get(EntityTracker1_9.class).getProvidedEntityId();

            PacketWrapper wrapper = new PacketWrapper(0x39, 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.send(Protocol1_9To1_8.class);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: ConnectionData.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void update(UserConnection user, Position position) {
    for (BlockFace face : BlockFace.values()) {
        Position pos = position.getRelative(face);
        int blockState = blockConnectionProvider.getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
        ConnectionHandler handler = connectionHandlerMap.get(blockState);
        if (handler == null) continue;

        int newBlockState = handler.connect(user, pos, blockState);
        PacketWrapper blockUpdatePacket = new PacketWrapper(0x0B, null, user);
        blockUpdatePacket.write(Type.POSITION, pos);
        blockUpdatePacket.write(Type.VAR_INT, newBlockState);
        try {
            blockUpdatePacket.send(Protocol1_13To1_12_2.class, true, true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
 
Example 5
Source File: EntityTracker.java    From ViaRewind with MIT License 6 votes vote down vote up
public void sendMetadataBuffer(int entityId) {
	if (!this.metadataBuffer.containsKey(entityId)) return;
	if (entityReplacements.containsKey(entityId)) {
		entityReplacements.get(entityId).updateMetadata(this.metadataBuffer.remove(entityId));
	} else {
		PacketWrapper wrapper = new PacketWrapper(0x1C, null, this.getUser());
		wrapper.write(Type.VAR_INT, entityId);
		wrapper.write(Types1_8.METADATA_LIST, this.metadataBuffer.get(entityId));
		MetadataRewriter.transform(this.getClientEntityTypes().get(entityId), this.metadataBuffer.get(entityId));
		if (!this.metadataBuffer.get(entityId).isEmpty()) {
			try {
				wrapper.send(Protocol1_8TO1_9.class);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		this.metadataBuffer.remove(entityId);
	}
}
 
Example 6
Source File: EntityTracker1_9.java    From ViaVersion with MIT License 5 votes vote down vote up
public void setSecondHand(int entityID, Item item) {
    PacketWrapper wrapper = new PacketWrapper(0x3C, null, getUser());
    wrapper.write(Type.VAR_INT, entityID);
    wrapper.write(Type.VAR_INT, 1); // slot
    wrapper.write(Type.ITEM, item);
    try {
        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: ShoulderTracker.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void update() {
    PacketWrapper wrapper = new PacketWrapper(0x0F, null, getUser());

    wrapper.write(Type.COMPONENT, Protocol1_9To1_8.fixJson(generateString()));
    wrapper.write(Type.BYTE, (byte) 2);

    try {
        wrapper.send(Protocol1_11_1To1_12.class);
    } catch (Exception e) {
        ViaBackwards.getPlatform().getLogger().severe("Failed to send the shoulder indication");
        e.printStackTrace();
    }
}
 
Example 8
Source File: BungeePlugin.java    From ViaBackwards with MIT License 5 votes vote down vote up
private void sendRespawn(UserConnection connection, int dimension) {
    PacketWrapper packet = new PacketWrapper(ClientboundPackets1_15.RESPAWN.ordinal(), null, connection);
    packet.write(Type.INT, dimension);
    packet.write(Type.LONG, 0L);
    packet.write(Type.UNSIGNED_BYTE, (short) 0);
    packet.write(Type.STRING, "default");
    try {
        packet.send(Protocol1_15_2To1_16.class, true, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: Sponge4ArmorListener.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendArmorUpdate(Player player) {
    // Ensure that the player is on our pipe
    if (!isOnPipe(player.getUniqueId())) return;


    int armor = 0;
    armor += calculate(player.getHelmet());
    armor += calculate(player.getChestplate());
    armor += calculate(player.getLeggings());
    armor += calculate(player.getBoots());

    PacketWrapper wrapper = new PacketWrapper(0x4B, null, getUserConnection(player.getUniqueId()));
    try {
        wrapper.write(Type.VAR_INT, getEntityId(player)); // Player ID
        wrapper.write(Type.INT, 1); // only 1 property
        wrapper.write(Type.STRING, "generic.armor");
        wrapper.write(Type.DOUBLE, 0D); //default 0 armor
        wrapper.write(Type.VAR_INT, 1); // 1 modifier
        wrapper.write(Type.UUID, ARMOR_ATTRIBUTE); // armor modifier uuid
        wrapper.write(Type.DOUBLE, (double) armor); // the modifier value
        wrapper.write(Type.BYTE, (byte) 0);// the modifier operation, 0 is add number

        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: CommonBoss.java    From ViaVersion with MIT License 5 votes vote down vote up
private void sendPacketConnection(UserConnection conn, PacketWrapper wrapper) {
    if (conn.getProtocolInfo() == null || !conn.getProtocolInfo().getPipeline().contains(Protocol1_9To1_8.class)) {
        connections.remove(conn);
        return;
    }
    try {
        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: BlockEntityProvider.java    From ViaVersion with MIT License 5 votes vote down vote up
private void sendBlockChange(UserConnection user, Position position, int blockId) throws Exception {
    PacketWrapper wrapper = new PacketWrapper(0x0B, null, user);
    wrapper.write(Type.POSITION, position);
    wrapper.write(Type.VAR_INT, blockId);

    wrapper.send(Protocol1_13To1_12_2.class, true, true);
}
 
Example 12
Source File: BlockItemPackets1_13.java    From ViaBackwards with MIT License 5 votes vote down vote up
private static void flowerPotSpecialTreatment(UserConnection user, int blockState, Position position) throws Exception {
    if (FlowerPotHandler.isFlowah(blockState)) {
        BackwardsBlockEntityProvider beProvider = Via.getManager().getProviders().get(BackwardsBlockEntityProvider.class);

        CompoundTag nbt = beProvider.transform(user, position, "minecraft:flower_pot");

        // Remove the flowerpot
        PacketWrapper blockUpdateRemove = new PacketWrapper(0x0B, null, user);
        blockUpdateRemove.write(Type.POSITION, position);
        blockUpdateRemove.write(Type.VAR_INT, 0);
        blockUpdateRemove.send(Protocol1_12_2To1_13.class, true);

        // Create the flowerpot
        PacketWrapper blockCreate = new PacketWrapper(0x0B, null, user);
        blockCreate.write(Type.POSITION, position);
        blockCreate.write(Type.VAR_INT, toOldId(blockState));
        blockCreate.send(Protocol1_12_2To1_13.class, true);

        // Send a block entity update
        PacketWrapper wrapper = new PacketWrapper(0x09, null, user);
        wrapper.write(Type.POSITION, position);
        wrapper.write(Type.UNSIGNED_BYTE, (short) 5);
        wrapper.write(Type.NBT, nbt);
        wrapper.send(Protocol1_12_2To1_13.class, true);

    }
}
 
Example 13
Source File: EntityTracker1_9.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendTeamPacket(boolean add, boolean now) {
    PacketWrapper wrapper = new PacketWrapper(0x41, null, getUser());
    wrapper.write(Type.STRING, "viaversion"); // Use viaversion as name
    if (add) {
        // add
        if (!teamExists) {
            wrapper.write(Type.BYTE, (byte) 0); // make team
            wrapper.write(Type.STRING, "viaversion");
            wrapper.write(Type.STRING, ""); // prefix
            wrapper.write(Type.STRING, ""); // suffix
            wrapper.write(Type.BYTE, (byte) 0); // friendly fire
            wrapper.write(Type.STRING, ""); // nametags
            wrapper.write(Type.STRING, "never"); // collision rule :)
            wrapper.write(Type.BYTE, (byte) 0); // color
        } else {
            wrapper.write(Type.BYTE, (byte) 3);
        }
        wrapper.write(Type.STRING_ARRAY, new String[]{getUser().getProtocolInfo().getUsername()});
    } else {
        wrapper.write(Type.BYTE, (byte) 1); // remove team
    }
    teamExists = add;
    try {
        wrapper.send(Protocol1_9To1_8.class, true, now);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: CommandBlockProvider.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendPermission(UserConnection user) throws Exception {
    if (!isEnabled())
        return;
    PacketWrapper wrapper = new PacketWrapper(0x1B, null, user); // Entity status

    wrapper.write(Type.INT, user.get(EntityTracker1_9.class).getProvidedEntityId()); // Entity ID
    wrapper.write(Type.BYTE, (byte) 26); // Hardcoded op permission level

    wrapper.send(Protocol1_9To1_8.class);

    user.get(CommandBlockStorage.class).setPermissions(true);
}
 
Example 15
Source File: EntityTracker1_14.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public void onExternalJoinGame(int playerEntityId) {
    super.onExternalJoinGame(playerEntityId);

    PacketWrapper setViewDistance = new PacketWrapper(0x41, null, getUser());
    setViewDistance.write(Type.VAR_INT, WorldPackets.SERVERSIDE_VIEW_DISTANCE);
    try {
        setViewDistance.send(Protocol1_14To1_13_2.class, true, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 16
Source File: BlockEntity.java    From ViaVersion with MIT License 5 votes vote down vote up
private static void updateBlockEntity(Position pos, short id, CompoundTag tag, UserConnection connection) throws Exception {
    PacketWrapper wrapper = new PacketWrapper(0x09, null, connection);
    wrapper.write(Type.POSITION, pos);
    wrapper.write(Type.UNSIGNED_BYTE, id);
    wrapper.write(Type.NBT, tag);
    wrapper.send(Protocol1_9_1_2To1_9_3_4.class, false);
}
 
Example 17
Source File: Sponge5ArmorListener.java    From ViaVersion with MIT License 5 votes vote down vote up
public void sendArmorUpdate(Player player) {
    // Ensure that the player is on our pipe
    if (!isOnPipe(player.getUniqueId())) return;


    int armor = 0;
    armor += calculate(player.getHelmet());
    armor += calculate(player.getChestplate());
    armor += calculate(player.getLeggings());
    armor += calculate(player.getBoots());

    PacketWrapper wrapper = new PacketWrapper(0x4B, null, getUserConnection(player.getUniqueId()));
    try {
        wrapper.write(Type.VAR_INT, getEntityId(player)); // Player ID
        wrapper.write(Type.INT, 1); // only 1 property
        wrapper.write(Type.STRING, "generic.armor");
        wrapper.write(Type.DOUBLE, 0D); //default 0 armor
        wrapper.write(Type.VAR_INT, 1); // 1 modifier
        wrapper.write(Type.UUID, ARMOR_ATTRIBUTE); // armor modifier uuid
        wrapper.write(Type.DOUBLE, (double) armor); // the modifier value
        wrapper.write(Type.BYTE, (byte) 0);// the modifier operation, 0 is add number

        wrapper.send(Protocol1_9To1_8.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 18
Source File: PacketUtil.java    From ViaRewind with 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 19
Source File: ConnectionData.java    From ViaVersion with MIT License 4 votes vote down vote up
public static void updateChunkSectionNeighbours(UserConnection user, int chunkX, int chunkZ, int chunkSectionY) {
    for (int chunkDeltaX = -1; chunkDeltaX <= 1; chunkDeltaX++) {
        for (int chunkDeltaZ = -1; chunkDeltaZ <= 1; chunkDeltaZ++) {
            if (Math.abs(chunkDeltaX) + Math.abs(chunkDeltaZ) == 0) continue;

            List<BlockChangeRecord> updates = new ArrayList<>();

            if (Math.abs(chunkDeltaX) + Math.abs(chunkDeltaZ) == 2) { // Corner
                for (int blockY = chunkSectionY * 16; blockY < chunkSectionY * 16 + 16; blockY++) {
                    int blockPosX = chunkDeltaX == 1 ? 0 : 15;
                    int blockPosZ = chunkDeltaZ == 1 ? 0 : 15;
                    updateBlock(user,
                            new Position(
                                    ((chunkX + chunkDeltaX) << 4) + blockPosX,
                                    (short) blockY,
                                    ((chunkZ + chunkDeltaZ) << 4) + blockPosZ
                            ),
                            updates
                    );
                }
            } else {
                for (int blockY = chunkSectionY * 16; blockY < chunkSectionY * 16 + 16; blockY++) {
                    int xStart;
                    int xEnd;
                    int zStart;
                    int zEnd;
                    if (chunkDeltaX == 1) {
                        xStart = 0;
                        xEnd = 2;
                        zStart = 0;
                        zEnd = 16;
                    } else if (chunkDeltaX == -1) {
                        xStart = 14;
                        xEnd = 16;
                        zStart = 0;
                        zEnd = 16;
                    } else if (chunkDeltaZ == 1) {
                        xStart = 0;
                        xEnd = 16;
                        zStart = 0;
                        zEnd = 2;
                    } else {
                        xStart = 0;
                        xEnd = 16;
                        zStart = 14;
                        zEnd = 16;
                    }
                    for (int blockX = xStart; blockX < xEnd; blockX++) {
                        for (int blockZ = zStart; blockZ < zEnd; blockZ++) {
                            updateBlock(user,
                                    new Position(
                                            ((chunkX + chunkDeltaX) << 4) + blockX,
                                            (short) blockY,
                                            ((chunkZ + chunkDeltaZ) << 4) + blockZ),
                                    updates
                            );
                        }
                    }
                }
            }

            if (!updates.isEmpty()) {
                PacketWrapper wrapper = new PacketWrapper(0x0F, null, user);
                wrapper.write(Type.INT, chunkX + chunkDeltaX);
                wrapper.write(Type.INT, chunkZ + chunkDeltaZ);
                wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, updates.toArray(EMPTY_RECORDS));
                try {
                    wrapper.send(Protocol1_13To1_12_2.class, true, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
Example 20
Source File: EntityToggleGlideListener.java    From ViaVersion with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void entityToggleGlide(EntityToggleGlideEvent event) {
    if (!(event.getEntity() instanceof Player)) return;

    Player player = (Player) event.getEntity();
    if (!isOnPipe(player)) return;

    // Cancelling can only be done by updating the player's metadata
    if (event.isGliding() && event.isCancelled()) {
        PacketWrapper packet = new PacketWrapper(0x44, null, getUserConnection(player));
        try {
            packet.write(Type.VAR_INT, player.getEntityId());

            byte bitmask = 0;
            // Collect other metadata for the mitmask
            if (player.getFireTicks() > 0) {
                bitmask |= 0x01;
            }
            if (player.isSneaking()) {
                bitmask |= 0x02;
            }
            // 0x04 is unused
            if (player.isSprinting()) {
                bitmask |= 0x08;
            }
            if (swimmingMethodExists && player.isSwimming()) {
                bitmask |= 0x10;
            }
            if (player.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
                bitmask |= 0x20;
            }
            if (player.isGlowing()) {
                bitmask |= 0x40;
            }

            // leave 0x80 as 0 to stop gliding
            packet.write(Types1_14.METADATA_LIST, Arrays.asList(new Metadata(0, MetaType1_14.Byte, bitmask)));
            packet.send(Protocol1_15To1_14_4.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}