us.myles.ViaVersion.api.minecraft.Position Java Examples

The following examples show how to use us.myles.ViaVersion.api.minecraft.Position. 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: CommandBlockStorage.java    License: MIT License 6 votes vote down vote up
public Optional<CompoundTag> getCommandBlock(Position position) {
    Pair<Integer, Integer> chunkCoords = getChunkCoords(position);

    Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords);
    if (blocks == null)
        return Optional.empty();

    CompoundTag tag = blocks.get(position);
    if (tag == null)
        return Optional.empty();

    tag = tag.clone();
    tag.put(new ByteTag("powered", (byte) 0));
    tag.put(new ByteTag("auto", (byte) 0));
    tag.put(new ByteTag("conditionMet", (byte) 0));
    return Optional.of(tag);
}
 
Example #2
Source Project: ViaVersion   Author: ViaVersion   File: SkullHandler.java    License: MIT License 6 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));

    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an head update packet, but there is no head! O_o " + tag);
        return -1;
    }

    int id = storage.get(position).getOriginal();
    if (id >= SKULL_WALL_START && id <= SKULL_END) {
        Tag skullType = tag.get("SkullType");
        if (skullType != null) {
            id += ((Number) tag.get("SkullType").getValue()).intValue() * 20;
        }
        if (tag.contains("Rot")) {
            id += ((Number) tag.get("Rot").getValue()).intValue();
        }
    } else {
        Via.getPlatform().getLogger().warning("Why does this block have the skull block entity? " + tag);
        return -1;
    }

    return id;
}
 
Example #3
Source Project: ViaVersion   Author: ViaVersion   File: BedHandler.java    License: MIT License 6 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));

    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an bed color update packet, but there is no bed! O_o " + tag);
        return -1;
    }

    //                                              RED_BED + FIRST_BED
    int blockId = storage.get(position).getOriginal() - 972 + 748;

    Tag color = tag.get("color");
    if (color != null) {
        blockId += (((Number) color.getValue()).intValue() * 16);
    }

    return blockId;
}
 
Example #4
Source Project: ViaVersion   Author: ViaVersion   File: BlockEntityProvider.java    License: MIT License 6 votes vote down vote up
/**
 * Transforms the BlockEntities to blocks!
 *
 * @param user       UserConnection instance
 * @param position   Block Position - WARNING: Position is null when called from a chunk
 * @param tag        BlockEntity NBT
 * @param sendUpdate send a block change update
 * @return new block id
 * @throws Exception Gotta throw that exception
 */
public int transform(UserConnection user, Position position, CompoundTag tag, boolean sendUpdate) throws Exception {
    Tag idTag = tag.get("id");
    if (idTag == null) return -1;

    String id = (String) idTag.getValue();
    BlockEntityHandler handler = handlers.get(id);
    if (handler == null) {
        if (Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag);
        }
        return -1;
    }

    int newBlock = handler.transform(user, tag);

    if (sendUpdate && newBlock != -1) {
        sendBlockChange(user, position, newBlock);
    }

    return newBlock;
}
 
Example #5
Source Project: ViaVersion   Author: ViaVersion   File: WorldPackets.java    License: MIT License 6 votes vote down vote up
private static int checkStorage(UserConnection user, Position position, int newId) {
    BlockStorage storage = user.get(BlockStorage.class);
    if (storage.contains(position)) {
        BlockStorage.ReplacementData data = storage.get(position);

        if (data.getOriginal() == newId) {
            if (data.getReplacement() != -1) {
                return data.getReplacement();
            }
        } else {
            storage.remove(position);
            // Check if the new id has to be stored
            if (storage.isWelcome(newId))
                storage.store(position, newId);
        }
    } else if (storage.isWelcome(newId))
        storage.store(position, newId);
    return newId;
}
 
Example #6
Source Project: ViaVersion   Author: ViaVersion   File: ConnectionData.java    License: 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 #7
Source Project: ViaVersion   Author: ViaVersion   File: FlowerConnectionHandler.java    License: MIT License 6 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    int blockBelowId = getBlockData(user, position.getRelative(BlockFace.BOTTOM));
    int connectBelow = flowers.get(blockBelowId);
    if (connectBelow != 0) {
        int blockAboveId = getBlockData(user, position.getRelative(BlockFace.TOP));
        if (Via.getConfig().isStemWhenBlockAbove()) {
            if (blockAboveId == 0) {
                return connectBelow;
            }
        } else if (!flowers.containsKey(blockAboveId)) {
            return connectBelow;
        }
    }
    return blockState;
}
 
Example #8
Source Project: ViaVersion   Author: ViaVersion   File: DoorConnectionHandler.java    License: MIT License 6 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    DoorData doorData = doorDataMap.get(blockState);
    if (doorData == null) return blockState;
    short s = 0;
    s |= (doorData.getType() & 0x7) << 6;
    if (doorData.isLower()) {
        DoorData upperHalf = doorDataMap.get(getBlockData(user, position.getRelative(BlockFace.TOP)));
        if (upperHalf == null) return blockState;
        s |= 1;
        if (doorData.isOpen()) s |= 2;
        if (upperHalf.isPowered()) s |= 4;
        if (upperHalf.isRightHinge()) s |= 8;
        s |= doorData.getFacing().ordinal() << 4;
    } else {
        DoorData lowerHalf = doorDataMap.get(getBlockData(user, position.getRelative(BlockFace.BOTTOM)));
        if (lowerHalf == null) return blockState;
        if (lowerHalf.isOpen()) s |= 2;
        if (doorData.isPowered()) s |= 4;
        if (doorData.isRightHinge()) s |= 8;
        s |= lowerHalf.getFacing().ordinal() << 4;
    }

    Integer newBlockState = connectedStates.get(s);
    return newBlockState == null ? blockState : newBlockState;
}
 
Example #9
Source Project: ViaVersion   Author: ViaVersion   File: ChestConnectionHandler.java    License: MIT License 6 votes vote down vote up
@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;
}
 
Example #10
Source Project: ViaVersion   Author: ViaVersion   File: RedstoneConnectionHandler.java    License: MIT License 6 votes vote down vote up
private int connects(UserConnection user, Position position, BlockFace side) {
    final Position relative = position.getRelative(side);
    int blockState = getBlockData(user, relative);
    if (connects(side, blockState)) {
        return 1; //side
    }
    int up = getBlockData(user, relative.getRelative(BlockFace.TOP));
    if (redstone.contains(up) && !ConnectionData.occludingStates.contains(getBlockData(user, position.getRelative(BlockFace.TOP)))) {
        return 2; //"up"
    }
    int down = getBlockData(user, relative.getRelative(BlockFace.BOTTOM));
    if (redstone.contains(down) && !ConnectionData.occludingStates.contains(getBlockData(user, relative))) {
        return 1; //side
    }
    return 0; //none
}
 
Example #11
Source Project: ViaBackwards   Author: ViaVersion   File: BackwardsBlockEntityProvider.java    License: MIT License 6 votes vote down vote up
/**
 * Transform blocks to block entities!
 *
 * @param user     The user
 * @param position The position of the block entity
 * @param tag      The block entity tag
 */
public CompoundTag transform(UserConnection user, Position position, CompoundTag tag) throws Exception {
    String id = (String) tag.get("id").getValue();
    BackwardsBlockEntityHandler handler = handlers.get(id);
    if (handler == null) {
        if (Via.getManager().isDebug()) {
            ViaBackwards.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag);
        }
        return tag;
    }

    BackwardsBlockStorage storage = user.get(BackwardsBlockStorage.class);
    Integer blockId = storage.get(position);
    if (blockId == null) {
        if (Via.getManager().isDebug()) {
            ViaBackwards.getPlatform().getLogger().warning("Handled BlockEntity does not have a stored block :( " + id + " full tag: " + tag);
        }
        return tag;
    }

    return handler.transform(user, blockId, tag);
}
 
Example #12
Source Project: ViaVersion   Author: ViaVersion   File: BlockListener.java    License: MIT License 5 votes vote down vote up
@Listener
public void placeBlock(ChangeBlockEvent.Place e, @Root Player player) {
    if (isOnPipe(player.getUniqueId())) {
        Location loc = e.getTransactions().get(0).getFinal().getLocation().get();
        getUserConnection(player.getUniqueId())
                .get(EntityTracker1_9.class)
                .addBlockInteraction(new Position(loc.getBlockX(), (short) loc.getBlockY(), loc.getBlockZ()));
    }
}
 
Example #13
Source Project: ViaVersion   Author: ViaVersion   File: BlockListener.java    License: MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void placeBlock(BlockPlaceEvent e) {
    if (isOnPipe(e.getPlayer())) {
        Block b = e.getBlockPlaced();
        getUserConnection(e.getPlayer())
                .get(EntityTracker1_9.class)
                .addBlockInteraction(new Position(b.getX(), (short) b.getY(), b.getZ()));
    }
}
 
Example #14
Source Project: ViaVersion   Author: ViaVersion   File: Position1_14Type.java    License: MIT License 5 votes vote down vote up
@Override
public Position read(ByteBuf buffer) {
    long val = buffer.readLong();

    long x = (val >> 38);
    long y = val << 52 >> 52;
    long z = val << 26 >> 38;

    return new Position((int) x, (short) y, (int) z);
}
 
Example #15
Source Project: ViaVersion   Author: ViaVersion   File: PositionType.java    License: MIT License 5 votes vote down vote up
@Override
public Position read(ByteBuf buffer) {
    long val = buffer.readLong();
    long x = (val >> 38); // signed
    long y = (val >> 26) & 0xfff; // unsigned
    // this shifting madness is used to preserve sign
    long z = (val << 38) >> 38; // signed

    return new Position((int) x, (short) y, (int) z);
}
 
Example #16
Source Project: ViaVersion   Author: ViaVersion   File: BlockEntity.java    License: MIT License 5 votes vote down vote up
public static void handle(List<CompoundTag> tags, UserConnection connection) {
    for (CompoundTag tag : tags) {
        try {
            if (!tag.contains("id"))
                throw new Exception("NBT tag not handled because the id key is missing");

            String id = (String) tag.get("id").getValue();
            if (!types.containsKey(id))
                throw new Exception("Not handled id: " + id);

            int newId = types.get(id);
            if (newId == -1)
                continue;

            int x = (int) tag.get("x").getValue();
            int y = (int) tag.get("y").getValue();
            int z = (int) tag.get("z").getValue();

            Position pos = new Position(x, (short) y, z);

            updateBlockEntity(pos, (short) newId, tag, connection);
        } catch (Exception e) {
            if (Via.getManager().isDebug()) {
                Via.getPlatform().getLogger().warning("Block Entity: " + e.getMessage() + ": " + tag);
            }
        }
    }
}
 
Example #17
Source Project: ViaVersion   Author: ViaVersion   File: BlockEntity.java    License: 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 #18
Source Project: ViaVersion   Author: ViaVersion   File: CommandBlockStorage.java    License: MIT License 5 votes vote down vote up
public void addOrUpdateBlock(Position position, CompoundTag tag) {
    Pair<Integer, Integer> chunkPos = getChunkCoords(position);

    if (!storedCommandBlocks.containsKey(chunkPos))
        storedCommandBlocks.put(chunkPos, new ConcurrentHashMap<>());

    Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkPos);

    if (blocks.containsKey(position))
        if (blocks.get(position).equals(tag))
            return;

    blocks.put(position, tag);
}
 
Example #19
Source Project: ViaVersion   Author: ViaVersion   File: BlockEntityProvider.java    License: 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 #20
Source Project: ViaVersion   Author: ViaVersion   File: VineConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    if (isAttachedToBlock(user, position)) return blockState;

    Position upperPos = position.getRelative(BlockFace.TOP);
    int upperBlock = getBlockData(user, upperPos);
    if (vines.contains(upperBlock) && isAttachedToBlock(user, upperPos)) return blockState;

    // Map to air if not attached to block, and upper block is also not a vine attached to a block
    return 0;
}
 
Example #21
Source Project: ViaVersion   Author: ViaVersion   File: ConnectionData.java    License: MIT License 5 votes vote down vote up
public static void updateBlock(UserConnection user, Position pos, List<BlockChangeRecord> records) {
    int blockState = blockConnectionProvider.getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
    ConnectionHandler handler = getConnectionHandler(blockState);
    if (handler == null) return;

    int newBlockState = handler.connect(user, pos, blockState);
    records.add(new BlockChangeRecord((short) (((pos.getX() & 0xF) << 4) | (pos.getZ() & 0xF)), pos.getY(), newBlockState));
}
 
Example #22
Source Project: ViaVersion   Author: ViaVersion   File: ConnectionData.java    License: MIT License 5 votes vote down vote up
public static void connectBlocks(UserConnection user, Chunk chunk) {
    long xOff = chunk.getX() << 4;
    long zOff = chunk.getZ() << 4;

    for (int i = 0; i < chunk.getSections().length; i++) {
        ChunkSection section = chunk.getSections()[i];
        if (section == null) continue;

        boolean willConnect = false;

        for (int p = 0; p < section.getPaletteSize(); p++) {
            int id = section.getPaletteEntry(p);
            if (ConnectionData.connects(id)) {
                willConnect = true;
                break;
            }
        }
        if (!willConnect) continue;

        long yOff = i << 4;

        for (int y = 0; y < 16; y++) {
            for (int z = 0; z < 16; z++) {
                for (int x = 0; x < 16; x++) {
                    int block = section.getFlatBlock(x, y, z);

                    ConnectionHandler handler = ConnectionData.getConnectionHandler(block);
                    if (handler != null) {
                        block = handler.connect(user, new Position(
                                (int) (xOff + x),
                                (short) (yOff + y),
                                (int) (zOff + z)
                        ), block);
                        section.setFlatBlock(x, y, z, block);
                    }
                }
            }
        }
    }
}
 
Example #23
Source Project: ViaVersion   Author: ViaVersion   File: FireConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    byte states = 0;
    if (flammableBlocks.contains(getBlockData(user, position.getRelative(BlockFace.EAST)))) states |= 1;
    if (flammableBlocks.contains(getBlockData(user, position.getRelative(BlockFace.NORTH)))) states |= 2;
    if (flammableBlocks.contains(getBlockData(user, position.getRelative(BlockFace.SOUTH)))) states |= 4;
    if (flammableBlocks.contains(getBlockData(user, position.getRelative(BlockFace.TOP)))) states |= 8;
    if (flammableBlocks.contains(getBlockData(user, position.getRelative(BlockFace.WEST)))) states |= 16;
    return connectedBlocks.get(states);
}
 
Example #24
Source Project: ViaVersion   Author: ViaVersion   File: GlassConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
protected byte getStates(UserConnection user, Position position, int blockState) {
    byte states = super.getStates(user, position, blockState);
    if (states != 0) return states;

    ProtocolInfo protocolInfo = user.getProtocolInfo();
    return protocolInfo.getServerProtocolVersion() <= 47
            && protocolInfo.getServerProtocolVersion() != -1 ? 0xF : states;
}
 
Example #25
Source Project: ViaVersion   Author: ViaVersion   File: TripwireConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    TripwireData tripwireData = tripwireDataMap.get(blockState);
    if (tripwireData == null) return blockState;
    byte b = 0;
    if (tripwireData.isAttached()) b |= 1;
    if (tripwireData.isDisarmed()) b |= 2;
    if (tripwireData.isPowered()) b |= 4;

    int east = getBlockData(user, position.getRelative(BlockFace.EAST));
    int north = getBlockData(user, position.getRelative(BlockFace.NORTH));
    int south = getBlockData(user, position.getRelative(BlockFace.SOUTH));
    int west = getBlockData(user, position.getRelative(BlockFace.WEST));

    if (tripwireDataMap.containsKey(east) || tripwireHooks.get(east) == BlockFace.WEST) {
        b |= 8;
    }
    if (tripwireDataMap.containsKey(north) || tripwireHooks.get(north) == BlockFace.SOUTH) {
        b |= 16;
    }
    if (tripwireDataMap.containsKey(south) || tripwireHooks.get(south) == BlockFace.NORTH) {
        b |= 32;
    }
    if (tripwireDataMap.containsKey(west) || tripwireHooks.get(west) == BlockFace.EAST) {
        b |= 64;
    }

    Integer newBlockState = connectedBlocks.get(b);
    return newBlockState == null ? blockState : newBlockState;
}
 
Example #26
Source Project: ViaVersion   Author: ViaVersion   File: AbstractFenceConnectionHandler.java    License: MIT License 5 votes vote down vote up
protected byte getStates(UserConnection user, Position position, int blockState) {
    byte states = 0;
    boolean pre1_12 = user.getProtocolInfo().getServerProtocolVersion() < ProtocolVersion.v1_12.getId();
    if (connects(BlockFace.EAST, getBlockData(user, position.getRelative(BlockFace.EAST)), pre1_12)) states |= 1;
    if (connects(BlockFace.NORTH, getBlockData(user, position.getRelative(BlockFace.NORTH)), pre1_12)) states |= 2;
    if (connects(BlockFace.SOUTH, getBlockData(user, position.getRelative(BlockFace.SOUTH)), pre1_12)) states |= 4;
    if (connects(BlockFace.WEST, getBlockData(user, position.getRelative(BlockFace.WEST)), pre1_12)) states |= 8;
    return states;
}
 
Example #27
Source Project: ViaVersion   Author: ViaVersion   File: StairConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    StairData stairData = stairDataMap.get(blockState);
    if (stairData == null) return blockState;

    short s = 0;
    if (stairData.isBottom()) s |= 1;
    s |= getShape(user, position, stairData) << 1;
    s |= stairData.getType() << 4;
    s |= stairData.getFacing().ordinal() << 9;

    Integer newBlockState = connectedBlocks.get(s);
    return newBlockState == null ? blockState : newBlockState;
}
 
Example #28
Source Project: ViaVersion   Author: ViaVersion   File: ChorusPlantConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
protected byte getStates(UserConnection user, Position position, int blockState) {
    byte states = super.getStates(user, position, blockState);
    if (connects(BlockFace.TOP, getBlockData(user, position.getRelative(BlockFace.TOP)), false)) states |= 16;
    if (connects(BlockFace.BOTTOM, getBlockData(user, position.getRelative(BlockFace.BOTTOM)), false)) states |= 32;
    return states;
}
 
Example #29
Source Project: ViaVersion   Author: ViaVersion   File: AbstractStempConnectionHandler.java    License: MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    if (blockState != baseStateId) {
        return blockState;
    }
    for (BlockFace blockFace : BLOCK_FACES) {
        if (blockId.contains(getBlockData(user, position.getRelative(blockFace)))) {
            return stemps.get(blockFace);
        }
    }
    return baseStateId;
}
 
Example #30
Source Project: ViaVersion   Author: ViaVersion   File: WallConnectionHandler.java    License: MIT License 5 votes vote down vote up
public boolean up(UserConnection user, Position position) {
    if (isWall(getBlockData(user, position.getRelative(BlockFace.BOTTOM))) || isWall(getBlockData(user, position.getRelative(BlockFace.TOP))))
        return true;
    int blockFaces = getBlockFaces(user, position);
    if (blockFaces == 0 || blockFaces == 0xF) return true;
    for (int i = 0; i < BLOCK_FACES.length; i++) {
        if ((blockFaces & (1 << i)) != 0 && (blockFaces & (1 << OPPOSITES[i])) == 0) return true;
    }
    return false;
}