Search in sources :

Example 1 with ClientboundBlockEventPacket

use of net.minecraft.network.protocol.game.ClientboundBlockEventPacket in project NotQuests by AlessioGr.

the class PacketInjector method sendBeaconUpdatePacket.

/* public static void sendMultiBlockChange(Player p, Block block) {
        // Creating some blocks to set
        Location loc = p.getLocation().clone();

        // BlockData is just an object that stores a Location, int and byte
        List<BlockData> blocks = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            blocks.add(new BlockData(loc, block.getType().getId() + 1, (byte) 0));
        }

        // Sending packet
        PacketPlayOutMultiBlockChange packet = new PacketPlayOutMultiBlockChange();
        MultiBlockChangeInfo[] info = new MultiBlockChangeInfo[blocks.size()];
        for (int i = 0; i < blocks.size(); i++) {
            int blockId = blocks.get(i).getTypeId();

            byte horizPos = (byte) (blocks.get(i).getLocation().getBlockX() << 4
                    + blocks.get(i).getLocation().getBlockZ());
            byte heigth = (byte) blocks.get(i).getLocation().getY();
            byte id = (byte) (blockId << 4 | (0 & 15));
            short data = (short) (horizPos + heigth + id);

            info[i] = packet.new MultiBlockChangeInfo(data,
                    net.minecraft.server.v1_8_R3.Block.getByCombinedId(blockId));
        }
        setPrivateObject(PacketPlayOutMultiBlockChange.class, packet, "a",
                new ChunkCoordIntPair(p.getLocation().getChunk().getX(), p.getLocation().getChunk().getZ()));
        setPrivateObject(PacketPlayOutMultiBlockChange.class, packet, "b", info);

        getNMSPlayer(p).playerConnection.sendPacket(packet);

        p.sendMessage("Packet sent");
    }*/
public void sendBeaconUpdatePacket(Player player, Location location, BlockState blockState) {
    Connection connection = getConnection(getServerPlayer(player).connection);
    CraftBlockState craftBlockState = (CraftBlockState) blockState;
    net.minecraft.world.level.block.Block nmsBlock = craftBlockState.getHandle().getBlock();
    BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    player.teleport(location.clone().add(0, 1, 0));
    // player.sendMessage("nmsBlock: " + nmsBlock.getName().getString());
    // BlockPost, Block, Action ID (1=recalculate), Action Type (ignored for beacons)
    ClientboundBlockEventPacket clientboundBlockEventPacket = new ClientboundBlockEventPacket(blockPos, nmsBlock, 1, 1);
    // player.sendMessage("sent!");
    // connection.send(clientboundBlockEventPacket);
    connection.send(clientboundBlockEventPacket, (future) -> {
    // player.sendMessage("Arrived!");
    });
}
Also used : CraftBlockState(org.bukkit.craftbukkit.v1_18_R2.block.CraftBlockState) ClientboundBlockEventPacket(net.minecraft.network.protocol.game.ClientboundBlockEventPacket) Connection(net.minecraft.network.Connection) BlockPos(net.minecraft.core.BlockPos)

Aggregations

BlockPos (net.minecraft.core.BlockPos)1 Connection (net.minecraft.network.Connection)1 ClientboundBlockEventPacket (net.minecraft.network.protocol.game.ClientboundBlockEventPacket)1 CraftBlockState (org.bukkit.craftbukkit.v1_18_R2.block.CraftBlockState)1