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 File: SkullHandler.java    From ViaVersion with 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 #2
Source File: BedHandler.java    From ViaVersion with 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 #3
Source File: BlockEntityProvider.java    From ViaVersion with 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 #4
Source File: WorldPackets.java    From ViaVersion with 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 #5
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 #6
Source File: CommandBlockStorage.java    From ViaVersion with 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 #7
Source File: DoorConnectionHandler.java    From ViaVersion with 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 #8
Source File: ChestConnectionHandler.java    From ViaVersion with 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 #9
Source File: FlowerConnectionHandler.java    From ViaVersion with 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 #10
Source File: BackwardsBlockEntityProvider.java    From ViaBackwards with 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 #11
Source File: RedstoneConnectionHandler.java    From ViaVersion with 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 #12
Source File: BlockListener.java    From ViaVersion with 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 #13
Source File: WallConnectionHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
private int getBlockFaces(UserConnection user, Position position) {
    int blockFaces = 0;
    for (int i = 0; i < BLOCK_FACES.length; i++) {
        if (isWall(getBlockData(user, position.getRelative(BLOCK_FACES[i])))) {
            blockFaces |= 1 << i;
        }
    }
    return blockFaces;
}
 
Example #14
Source File: WallConnectionHandler.java    From ViaVersion with 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;
}
 
Example #15
Source File: AbstractStempConnectionHandler.java    From ViaVersion with 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 #16
Source File: RedstoneConnectionHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    short b = 0;
    b |= connects(user, position, BlockFace.EAST);
    b |= connects(user, position, BlockFace.NORTH) << 2;
    b |= connects(user, position, BlockFace.SOUTH) << 4;
    b |= connects(user, position, BlockFace.WEST) << 6;
    b |= powerMappings.get(blockState) << 8;
    return connectedBlockStates.getOrDefault(b, blockState);
}
 
Example #17
Source File: ChorusPlantConnectionHandler.java    From ViaVersion with 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 #18
Source File: AbstractFenceConnectionHandler.java    From ViaVersion with 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 #19
Source File: GlassConnectionHandler.java    From ViaVersion with 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 #20
Source File: TripwireConnectionHandler.java    From ViaVersion with 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 #21
Source File: SnowyGrassConnectionHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public int connect(UserConnection user, Position position, int blockState) {
    int blockUpId = getBlockData(user, position.getRelative(BlockFace.TOP));
    Integer newId = grassBlocks.get(new Pair<>(blockState, snows.contains(blockUpId)));
    if (newId != null) {
        return newId;
    }
    return blockState;
}
 
Example #22
Source File: ConnectionData.java    From ViaVersion with 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 File: ConnectionData.java    From ViaVersion with 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 #24
Source File: VineConnectionHandler.java    From ViaVersion with 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 #25
Source File: BackwardsBlockEntityProvider.java    From ViaBackwards with MIT License 5 votes vote down vote up
/**
 * Transform blocks to block entities!
 *
 * @param user     The user
 * @param position The position of the block entity
 * @param id       The block entity id
 */
public CompoundTag transform(UserConnection user, Position position, String id) throws Exception {
    CompoundTag tag = new CompoundTag("");
    tag.put(new StringTag("id", id));
    tag.put(new IntTag("x", Math.toIntExact(position.getX())));
    tag.put(new IntTag("y", Math.toIntExact(position.getY())));
    tag.put(new IntTag("z", Math.toIntExact(position.getZ())));

    return this.transform(user, position, tag);
}
 
Example #26
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 #27
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 #28
Source File: BackwardsBlockStorage.java    From ViaBackwards with MIT License 5 votes vote down vote up
public void checkAndStore(Position position, int block) {
    if (!WHITELIST.contains(block)) {
        // Remove if not whitelisted
        blocks.remove(position);
        return;
    }

    blocks.put(position, block);
}
 
Example #29
Source File: EntityPackets1_14.java    From ViaBackwards with MIT License 5 votes vote down vote up
@Override
protected void addTrackedEntity(PacketWrapper wrapper, int entityId, EntityType type) throws Exception {
    super.addTrackedEntity(wrapper, entityId, type);

    // Cache the position for every newly tracked entity
    if (type == Entity1_14Types.EntityType.PAINTING) {
        final Position position = wrapper.get(Type.POSITION, 0);
        positionHandler.cacheEntityPosition(wrapper, position.getX(), position.getY(), position.getZ(), true, false);
    } else if (wrapper.getId() != 0x25) { // ignore join game
        positionHandler.cacheEntityPosition(wrapper, true, false);
    }
}
 
Example #30
Source File: FireConnectionHandler.java    From ViaVersion with 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);
}