Search in sources :

Example 1 with PacketPlayOutTileEntityDataHandle

use of com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle in project BKCommonLib by bergerhealer.

the class PacketTest method testBlockStateChangePacket.

@Test
public void testBlockStateChangePacket() {
    CommonTagCompound metadata = new CommonTagCompound();
    metadata.putValue("Text1", "a");
    metadata.putValue("Text2", "b");
    metadata.putValue("Text3", "c");
    metadata.putValue("Text4", "d");
    PacketPlayOutTileEntityDataHandle packet = PacketPlayOutTileEntityDataHandle.createNew(IntVector3.of(1, 2, 3), BlockStateType.SIGN, metadata.clone());
    assertEquals(IntVector3.of(1, 2, 3), packet.getPosition());
    assertEquals(BlockStateType.SIGN, packet.getType());
    assertEquals(metadata, packet.getData());
}
Also used : PacketPlayOutTileEntityDataHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) Test(org.junit.Test)

Example 2 with PacketPlayOutTileEntityDataHandle

use of com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle in project BKCommonLib by bergerhealer.

the class BlockStateChangePacketHandler_1_9_3 method enable.

@Override
public void enable() {
    register(PacketType.OUT_TILE_ENTITY_DATA, (player, commonPacket, listener) -> {
        final PacketPlayOutTileEntityDataHandle packet = PacketPlayOutTileEntityDataHandle.createHandle(commonPacket.getHandle());
        CommonTagCompound metadata = packet.getData();
        if (metadata != null) {
            // There's metadata, nothing special needs to be done
            BlockStateChange change = BlockStateChange.deferred(packet.getPosition(), packet.getType(), LogicUtil.constantSupplier(metadata), () -> true);
            // Handle it, if false, cancel the packet entirely
            if (!listener.onBlockChange(player, change)) {
                return false;
            }
        } else {
            // Initialize metadata on first use
            final DeferredSupplier<CommonTagCompound> metadataSupplier = DeferredSupplier.of(CommonTagCompound::new);
            BlockStateChange change = BlockStateChange.deferred(packet.getPosition(), packet.getType(), metadataSupplier, metadataSupplier::isInitialized);
            // Handle it, if false, cancel the packet entirely
            if (!listener.onBlockChange(player, change)) {
                return false;
            }
            // If metadata was created and it's not empty, apply it to the packet
            if (metadataSupplier.isInitialized() && !metadataSupplier.get().isEmpty()) {
                packet.setData(metadataSupplier.get());
            }
        }
        return true;
    });
    register(PacketType.OUT_MAP_CHUNK, (player, commonPacket, listener) -> {
        PacketPlayOutMapChunkHandle packet = PacketPlayOutMapChunkHandle.createHandle(commonPacket.getHandle());
        Iterator<BlockStateChange> iter = packet.getBlockStates().iterator();
        while (iter.hasNext()) {
            BlockStateChange change = iter.next();
            if (!listener.onBlockChange(player, change)) {
                iter.remove();
            }
        }
        return true;
    });
}
Also used : BlockStateChange(com.bergerkiller.bukkit.common.wrappers.BlockStateChange) PacketPlayOutTileEntityDataHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle) CommonTagCompound(com.bergerkiller.bukkit.common.nbt.CommonTagCompound) PacketPlayOutMapChunkHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle)

Aggregations

CommonTagCompound (com.bergerkiller.bukkit.common.nbt.CommonTagCompound)2 PacketPlayOutTileEntityDataHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutTileEntityDataHandle)2 BlockStateChange (com.bergerkiller.bukkit.common.wrappers.BlockStateChange)1 PacketPlayOutMapChunkHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayOutMapChunkHandle)1 Test (org.junit.Test)1