Search in sources :

Example 1 with BlockFace

use of com.viaversion.viaversion.api.minecraft.BlockFace in project ViaVersion by ViaVersion.

the class ChestConnectionHandler method connect.

@Override
public int connect(UserConnection user, Position position, int blockState) {
    BlockFace facing = chestFacings.get(blockState);
    byte states = 0;
    states |= (facing.ordinal() << 2);
    boolean trapped = trappedChests.contains(blockState);
    if (trapped)
        states |= 16;
    int relative;
    if (chestFacings.containsKey(relative = getBlockData(user, position.getRelative(BlockFace.NORTH))) && trapped == trappedChests.contains(relative)) {
        states |= facing == BlockFace.WEST ? 1 : 2;
    } else if (chestFacings.containsKey(relative = getBlockData(user, position.getRelative(BlockFace.SOUTH))) && trapped == trappedChests.contains(relative)) {
        states |= facing == BlockFace.EAST ? 1 : 2;
    } else if (chestFacings.containsKey(relative = getBlockData(user, position.getRelative(BlockFace.WEST))) && trapped == trappedChests.contains(relative)) {
        states |= facing == BlockFace.NORTH ? 2 : 1;
    } else if (chestFacings.containsKey(relative = getBlockData(user, position.getRelative(BlockFace.EAST))) && trapped == trappedChests.contains(relative)) {
        states |= facing == BlockFace.SOUTH ? 2 : 1;
    }
    Integer newBlockState = connectedStates.get(states);
    return newBlockState == null ? blockState : newBlockState;
}
Also used : BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace)

Example 2 with BlockFace

use of com.viaversion.viaversion.api.minecraft.BlockFace in project ViaVersion by ViaVersion.

the class ConnectionData method init.

public static void init() {
    if (!Via.getConfig().isServersideBlockConnections())
        return;
    Via.getPlatform().getLogger().info("Loading block connection mappings ...");
    JsonObject mapping1_13 = MappingDataLoader.loadData("mapping-1.13.json", true);
    JsonObject blocks1_13 = mapping1_13.getAsJsonObject("blockstates");
    for (Entry<String, JsonElement> blockState : blocks1_13.entrySet()) {
        int id = Integer.parseInt(blockState.getKey());
        String key = blockState.getValue().getAsString();
        idToKey.put(id, key);
        keyToId.put(key, id);
    }
    connectionHandlerMap = new Int2ObjectOpenHashMap<>(3650, .99F);
    if (!Via.getConfig().isReduceBlockStorageMemory()) {
        blockConnectionData = new Int2ObjectOpenHashMap<>(1146, .99F);
        JsonObject mappingBlockConnections = MappingDataLoader.loadData("blockConnections.json");
        for (Entry<String, JsonElement> entry : mappingBlockConnections.entrySet()) {
            int id = keyToId.get(entry.getKey());
            BlockData blockData = new BlockData();
            for (Entry<String, JsonElement> type : entry.getValue().getAsJsonObject().entrySet()) {
                String name = type.getKey();
                JsonObject object = type.getValue().getAsJsonObject();
                boolean[] data = new boolean[6];
                for (BlockFace value : BlockFace.values()) {
                    String face = value.toString().toLowerCase(Locale.ROOT);
                    if (object.has(face)) {
                        data[value.ordinal()] = object.getAsJsonPrimitive(face).getAsBoolean();
                    }
                }
                blockData.put(name, data);
            }
            if (entry.getKey().contains("stairs")) {
                blockData.put("allFalseIfStairPre1_12", new boolean[6]);
            }
            blockConnectionData.put(id, blockData);
        }
    }
    JsonObject blockData = MappingDataLoader.loadData("blockData.json");
    JsonArray occluding = blockData.getAsJsonArray("occluding");
    for (JsonElement jsonElement : occluding) {
        occludingStates.add(keyToId.get(jsonElement.getAsString()).intValue());
    }
    List<ConnectorInitAction> initActions = new ArrayList<>();
    initActions.add(PumpkinConnectionHandler.init());
    initActions.addAll(BasicFenceConnectionHandler.init());
    initActions.add(NetherFenceConnectionHandler.init());
    initActions.addAll(WallConnectionHandler.init());
    initActions.add(MelonConnectionHandler.init());
    initActions.addAll(GlassConnectionHandler.init());
    initActions.add(ChestConnectionHandler.init());
    initActions.add(DoorConnectionHandler.init());
    initActions.add(RedstoneConnectionHandler.init());
    initActions.add(StairConnectionHandler.init());
    initActions.add(FlowerConnectionHandler.init());
    initActions.addAll(ChorusPlantConnectionHandler.init());
    initActions.add(TripwireConnectionHandler.init());
    initActions.add(SnowyGrassConnectionHandler.init());
    initActions.add(FireConnectionHandler.init());
    if (Via.getConfig().isVineClimbFix()) {
        initActions.add(VineConnectionHandler.init());
    }
    for (String key : keyToId.keySet()) {
        WrappedBlockData wrappedBlockData = WrappedBlockData.fromString(key);
        for (ConnectorInitAction action : initActions) {
            action.check(wrappedBlockData);
        }
    }
    if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("packet")) {
        blockConnectionProvider = new PacketBlockConnectionProvider();
        Via.getManager().getProviders().register(BlockConnectionProvider.class, blockConnectionProvider);
    }
}
Also used : PacketBlockConnectionProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.providers.PacketBlockConnectionProvider) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement)

Example 3 with BlockFace

use of com.viaversion.viaversion.api.minecraft.BlockFace in project ViaVersion by ViaVersion.

the class WorldPackets method setNonFullLight.

private static void setNonFullLight(Chunk chunk, ChunkSection section, int ySection, int x, int y, int z) {
    int skyLight = 0;
    int blockLight = 0;
    for (BlockFace blockFace : BlockFace.values()) {
        NibbleArray skyLightArray = section.getLight().getSkyLightNibbleArray();
        NibbleArray blockLightArray = section.getLight().getBlockLightNibbleArray();
        int neighbourX = x + blockFace.modX();
        int neighbourY = y + blockFace.modY();
        int neighbourZ = z + blockFace.modZ();
        if (blockFace.modX() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourX == 16 || neighbourX == -1)
                continue;
        } else if (blockFace.modY() != 0) {
            if (neighbourY == 16 || neighbourY == -1) {
                if (neighbourY == 16) {
                    ySection += 1;
                    neighbourY = 0;
                } else {
                    ySection -= 1;
                    neighbourY = 15;
                }
                if (ySection == 16 || ySection == -1)
                    continue;
                ChunkSection newSection = chunk.getSections()[ySection];
                if (newSection == null)
                    continue;
                skyLightArray = newSection.getLight().getSkyLightNibbleArray();
                blockLightArray = newSection.getLight().getBlockLightNibbleArray();
            }
        } else if (blockFace.modZ() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourZ == 16 || neighbourZ == -1)
                continue;
        }
        if (blockLightArray != null && blockLight != 15) {
            int neighbourBlockLight = blockLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourBlockLight == 15) {
                blockLight = 14;
            } else if (neighbourBlockLight > blockLight) {
                // lower light level by one
                blockLight = neighbourBlockLight - 1;
            }
        }
        if (skyLightArray != null && skyLight != 15) {
            int neighbourSkyLight = skyLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourSkyLight == 15) {
                if (blockFace.modY() == 1) {
                    // Keep 15 if block is exposed to sky
                    skyLight = 15;
                    continue;
                }
                skyLight = 14;
            } else if (neighbourSkyLight > skyLight) {
                // lower light level by one
                skyLight = neighbourSkyLight - 1;
            }
        }
    }
    if (skyLight != 0) {
        if (!section.getLight().hasSkyLight()) {
            byte[] newSkyLight = new byte[2028];
            section.getLight().setSkyLight(newSkyLight);
        }
        section.getLight().getSkyLightNibbleArray().set(x, y, z, skyLight);
    }
    if (blockLight != 0) {
        section.getLight().getBlockLightNibbleArray().set(x, y, z, blockLight);
    }
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 4 with BlockFace

use of com.viaversion.viaversion.api.minecraft.BlockFace in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sign Position
            map(Type.POSITION);
            // 1 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 2 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 3 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 4 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Effect ID
            map(Type.INT);
            // 1 - Position
            map(Type.POSITION);
            // 2 - Data
            map(Type.INT);
            // 3 - Disable relative volume
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    id = Effect.getNewId(id);
                    wrapper.set(Type.INT, 0, id);
                }
            });
            // Rewrite potion effect as it changed to use a dynamic registry
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 2002) {
                        int data = wrapper.get(Type.INT, 1);
                        int newData = ItemRewriter.getNewEffectID(data);
                        wrapper.set(Type.INT, 1, newData);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.NAMED_SOUND, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sound Name
            map(Type.STRING);
            // 1 - Sound Category ID
            // Everything else get's written through
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String name = wrapper.get(Type.STRING, 0);
                    SoundEffect effect = SoundEffect.getByName(name);
                    int catid = 0;
                    String newname = name;
                    if (effect != null) {
                        catid = effect.getCategory().getId();
                        newname = effect.getNewName();
                    }
                    wrapper.set(Type.STRING, 0, newname);
                    // Write Category ID
                    wrapper.write(Type.VAR_INT, catid);
                    if (effect != null && effect.isBreaksound()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        // Position X
                        int x = wrapper.passthrough(Type.INT);
                        // Position Y
                        int y = wrapper.passthrough(Type.INT);
                        // Position Z
                        int z = wrapper.passthrough(Type.INT);
                        if (tracker.interactedBlockRecently((int) Math.floor(x / 8.0), (int) Math.floor(y / 8.0), (int) Math.floor(z / 8.0))) {
                            wrapper.cancel();
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.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);
                    ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
                    Chunk chunk = wrapper.read(new Chunk1_8Type(clientWorld));
                    long chunkHash = ClientChunks.toLong(chunk.getX(), chunk.getZ());
                    // Check if the chunk should be handled as an unload packet
                    if (chunk.isFullChunk() && chunk.getBitmask() == 0) {
                        wrapper.setPacketType(ClientboundPackets1_9.UNLOAD_CHUNK);
                        wrapper.write(Type.INT, chunk.getX());
                        wrapper.write(Type.INT, chunk.getZ());
                        // Remove commandBlocks on chunk unload
                        CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                        provider.unloadChunk(wrapper.user(), chunk.getX(), chunk.getZ());
                        clientChunks.getLoadedChunks().remove(chunkHash);
                        // Unload the empty chunks
                        if (Via.getConfig().isChunkBorderFix()) {
                            for (BlockFace face : BlockFace.HORIZONTAL) {
                                int chunkX = chunk.getX() + face.modX();
                                int chunkZ = chunk.getZ() + face.modZ();
                                if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                    PacketWrapper unloadChunk = wrapper.create(ClientboundPackets1_9.UNLOAD_CHUNK);
                                    unloadChunk.write(Type.INT, chunkX);
                                    unloadChunk.write(Type.INT, chunkZ);
                                    unloadChunk.send(Protocol1_9To1_8.class);
                                }
                            }
                        }
                    } else {
                        Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
                        wrapper.write(chunkType, chunk);
                        clientChunks.getLoadedChunks().add(chunkHash);
                        // Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
                        if (Via.getConfig().isChunkBorderFix()) {
                            for (BlockFace face : BlockFace.HORIZONTAL) {
                                int chunkX = chunk.getX() + face.modX();
                                int chunkZ = chunk.getZ() + face.modZ();
                                if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                    PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                                    Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
                                    emptyChunk.write(chunkType, c);
                                    emptyChunk.send(Protocol1_9To1_8.class);
                                }
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.MAP_BULK_CHUNK, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Cancel the packet from being sent
                wrapper.cancel();
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
                Chunk[] chunks = wrapper.read(new ChunkBulk1_8Type(clientWorld));
                Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
                // Split into multiple chunk packets
                for (Chunk chunk : chunks) {
                    PacketWrapper chunkData = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                    chunkData.write(chunkType, chunk);
                    chunkData.send(Protocol1_9To1_8.class);
                    clientChunks.getLoadedChunks().add(ClientChunks.toLong(chunk.getX(), chunk.getZ()));
                    // Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
                    if (Via.getConfig().isChunkBorderFix()) {
                        for (BlockFace face : BlockFace.HORIZONTAL) {
                            int chunkX = chunk.getX() + face.modX();
                            int chunkZ = chunk.getZ() + face.modZ();
                            if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                                Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
                                emptyChunk.write(chunkType, c);
                                emptyChunk.send(Protocol1_9To1_8.class);
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Block Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT (Might not be present)
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    if (action == 1) {
                        // Update Spawner
                        CompoundTag tag = wrapper.get(Type.NBT, 0);
                        if (tag != null) {
                            if (tag.contains("EntityId")) {
                                String entity = (String) tag.get("EntityId").getValue();
                                CompoundTag spawn = new CompoundTag();
                                spawn.put("id", new StringTag(entity));
                                tag.put("SpawnData", spawn);
                            } else {
                                // EntityID does not exist
                                CompoundTag spawn = new CompoundTag();
                                // Make spawners show up as empty when no EntityId is given.
                                spawn.put("id", new StringTag("AreaEffectCloud"));
                                tag.put("SpawnData", spawn);
                            }
                        }
                    }
                    if (action == 2) {
                        // Update Command Block
                        CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                        provider.addOrUpdateBlock(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0));
                        // To prevent window issues don't send updates
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.UPDATE_SIGN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sign Position
            map(Type.POSITION);
            // 1 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 2 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 3 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 4 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_DIGGING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Action
            map(Type.VAR_INT);
            // Position
            map(Type.POSITION);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int status = wrapper.get(Type.VAR_INT, 0);
                    if (status == 6)
                        wrapper.cancel();
                }
            });
            // Blocking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int status = wrapper.get(Type.VAR_INT, 0);
                    if (status == 5 || status == 4 || status == 3) {
                        EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        if (entityTracker.isBlocking()) {
                            entityTracker.setBlocking(false);
                            if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
                                entityTracker.setSecondHand(null);
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.USE_ITEM, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int hand = wrapper.read(Type.VAR_INT);
                    // Wipe the input buffer
                    wrapper.clearInputBuffer();
                    // First set this packet ID to Block placement
                    wrapper.setId(0x08);
                    wrapper.write(Type.POSITION, new Position(-1, (short) -1, -1));
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 255);
                    // Write item in hand
                    Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
                    // Blocking patch
                    if (Via.getConfig().isShieldBlocking()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        // Check if the shield is already there or if we have to give it here
                        boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand();
                        // Method to identify the sword in hand
                        boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand() : item != null && Protocol1_9To1_8.isSword(item.identifier());
                        if (isSword) {
                            if (hand == 0) {
                                if (!tracker.isBlocking()) {
                                    tracker.setBlocking(true);
                                    // Check if the shield is already in the offhand
                                    if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
                                        // Set shield in offhand when interacting with main hand
                                        Item shield = new DataItem(442, (byte) 1, (short) 0, null);
                                        tracker.setSecondHand(shield);
                                    }
                                }
                            }
                            // Use the main hand to trigger the blocking
                            boolean blockUsingMainHand = Via.getConfig().isNoDelayShieldBlocking() && !showShieldWhenSwordInHand;
                            if (blockUsingMainHand && hand == 1 || !blockUsingMainHand && hand == 0) {
                                wrapper.cancel();
                            }
                        } else {
                            if (!showShieldWhenSwordInHand) {
                                // Remove the shield from the offhand
                                tracker.setSecondHand(null);
                            }
                            tracker.setBlocking(false);
                        }
                    }
                    wrapper.write(Type.ITEM, item);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Block Face
            map(Type.VAR_INT, Type.UNSIGNED_BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 2 - Hand
                    final int hand = wrapper.read(Type.VAR_INT);
                    if (hand != 0)
                        wrapper.cancel();
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
                    // 3 - Item
                    wrapper.write(Type.ITEM, item);
                }
            });
            // 4 - X
            map(Type.UNSIGNED_BYTE);
            // 5 - Y
            map(Type.UNSIGNED_BYTE);
            // 6 - Z
            map(Type.UNSIGNED_BYTE);
            // Register block place to fix sounds
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int face = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    if (face == 255)
                        return;
                    Position p = wrapper.get(Type.POSITION, 0);
                    int x = p.x();
                    int y = p.y();
                    int z = p.z();
                    switch(face) {
                        case 0:
                            y--;
                            break;
                        case 1:
                            y++;
                            break;
                        case 2:
                            z--;
                            break;
                        case 3:
                            z++;
                            break;
                        case 4:
                            x--;
                            break;
                        case 5:
                            x++;
                            break;
                    }
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addBlockInteraction(new Position(x, y, z));
                }
            });
            // Handle CommandBlocks
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                    Position pos = wrapper.get(Type.POSITION, 0);
                    Optional<CompoundTag> tag = provider.get(wrapper.user(), pos);
                    // Send the Update Block Entity packet if present
                    if (tag.isPresent()) {
                        PacketWrapper updateBlockEntity = PacketWrapper.create(ClientboundPackets1_9.BLOCK_ENTITY_DATA, null, wrapper.user());
                        updateBlockEntity.write(Type.POSITION, pos);
                        updateBlockEntity.write(Type.UNSIGNED_BYTE, (short) 2);
                        updateBlockEntity.write(Type.NBT, tag.get());
                        updateBlockEntity.scheduleSend(Protocol1_9To1_8.class);
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Effect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.Effect) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Item(com.viaversion.viaversion.api.minecraft.item.Item) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) ClientboundPackets1_8(com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8) ItemRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter) SoundEffect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) Position(com.viaversion.viaversion.api.minecraft.Position) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol(com.viaversion.viaversion.api.protocol.Protocol) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) Via(com.viaversion.viaversion.api.Via) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ServerboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9) Optional(java.util.Optional) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) ClientboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) SoundEffect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) Position(com.viaversion.viaversion.api.minecraft.Position) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) Type(com.viaversion.viaversion.api.type.Type) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 5 with BlockFace

use of com.viaversion.viaversion.api.minecraft.BlockFace in project ViaVersion by ViaVersion.

the class ConnectionData method update.

public static void update(UserConnection user, Position position) {
    for (BlockFace face : BlockFace.values()) {
        Position pos = position.getRelative(face);
        int blockState = blockConnectionProvider.getBlockData(user, pos.x(), pos.y(), pos.z());
        ConnectionHandler handler = connectionHandlerMap.get(blockState);
        if (handler == null)
            continue;
        int newBlockState = handler.connect(user, pos, blockState);
        PacketWrapper blockUpdatePacket = PacketWrapper.create(ClientboundPackets1_13.BLOCK_CHANGE, null, user);
        blockUpdatePacket.write(Type.POSITION, pos);
        blockUpdatePacket.write(Type.VAR_INT, newBlockState);
        try {
            blockUpdatePacket.send(Protocol1_13To1_12_2.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Position(com.viaversion.viaversion.api.minecraft.Position) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace)

Aggregations

BlockFace (com.viaversion.viaversion.api.minecraft.BlockFace)6 Position (com.viaversion.viaversion.api.minecraft.Position)2 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)2 ArrayList (java.util.ArrayList)2 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Via (com.viaversion.viaversion.api.Via)1 BaseChunk (com.viaversion.viaversion.api.minecraft.chunks.BaseChunk)1 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)1 NibbleArray (com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)1 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 Protocol (com.viaversion.viaversion.api.protocol.Protocol)1 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 Type (com.viaversion.viaversion.api.type.Type)1