@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(x, y, z); }
@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; } }
public static void update(UserConnection user, Position position) { for (int x = -1; x <= 1; x++) { for (int z = -1; z <= 1; z++) { for (int y = -1; y <= 1; y++) { if (Math.abs(x) + Math.abs(y) + Math.abs(z) != 1) continue; Position pos = new Position(position.getX() + x, position.getY() + y, position.getZ() + z); int blockState = Via.getManager().getProviders().get(BlockConnectionProvider.class).getBlockdata(user, pos); if (!connects(blockState)) continue; int newBlockState = connect(user, pos, blockState); if (newBlockState == blockState) continue; 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, false); } catch (Exception ex) { ex.printStackTrace(); } } } } }
public void storeBlock(UserConnection connection, long x, long y, long z, int blockState) { storeBlock(connection, new Position(x, y, z), blockState); }
@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; } }
public Position getRelative(BlockFace face) { return new Position(this.x + face.getModX(), this.y + face.getModY(), this.z + face.getModZ()); }
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 }
@Override public int getWorldBlockData(UserConnection user, Position position) { if (blockStateIds != null) { UUID uuid = user.get(ProtocolInfo.class).getUuid(); Optional<Player> player = Sponge.getServer().getPlayer(uuid); if (player.isPresent()) { World world = player.get().getWorld(); Optional<Chunk> chunk = world.getChunkAtBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue()); if (chunk.isPresent()) { BlockState b = chunk.get().getBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue()); Integer id = blockStateIds.get(b); if (id == null) { System.out.println("id not found"); } else { return id; } } } } return 0; } }
block = ConnectionData.connect(user, new Position(xOff + x, yOff + y, zOff + z), block); section.setFlatBlock(x, y, z, block); update(user, new Position(xOff - 1, yOff + y, zOff + z)); } else if (x == 15) { update(user, new Position(xOff + 16, yOff + y, zOff + z)); update(user, new Position(xOff + x, yOff + y, zOff - 1)); } else if (z == 15) { update(user, new Position(xOff + x, yOff + y, zOff + 16));
@Override public int connect(UserConnection user, Position position, int blockState) { int blockBelowId = getBlockData(user, position.getRelative(BlockFace.BOTTOM)); if (flowers.containsKey(blockBelowId)) { int blockAboveId = getBlockData(user, position.getRelative(BlockFace.TOP)); if (Via.getConfig().isStemWhenBlockAbove()) { if (blockAboveId == 0) { return flowers.get(blockBelowId); } } else if (!flowers.containsKey(blockAboveId)) { return flowers.get(blockBelowId); } } return blockState; } }
@Override public int getWorldBlockData(UserConnection user, Position position) { UUID uuid = user.get(ProtocolInfo.class).getUuid(); Player player = Bukkit.getPlayer(uuid); if (player != null) { World world = player.getWorld(); int x = (int) (position.getX() >> 4); int z = (int) (position.getZ() >> 4); if (world.isChunkLoaded(x, z)) { Chunk c = getChunk(world, x, z); Block b = c.getBlock(position.getX().intValue(), position.getY().intValue(), position.getZ().intValue()); return b.getTypeId() << 4 | b.getData(); } } return 0; }
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((long) x, (long) y, (long) z); updateBlockEntity(pos, (short) newId, tag, connection); } catch (Exception e) { if (Via.getManager().isDebug()) { Via.getPlatform().getLogger().warning("Block Entity: " + e.getMessage() + ": " + tag); } } } }
protected byte getStates(UserConnection user, Position position, int blockState) { byte states = 0; if (connects(BlockFace.EAST, getBlockData(user, position.getRelative(BlockFace.EAST)))) states |= 1; if (connects(BlockFace.NORTH, getBlockData(user, position.getRelative(BlockFace.NORTH)))) states |= 2; if (connects(BlockFace.SOUTH, getBlockData(user, position.getRelative(BlockFace.SOUTH)))) states |= 4; if (connects(BlockFace.WEST, getBlockData(user, position.getRelative(BlockFace.WEST)))) states |= 8; return states; }
@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(EntityTracker.class) .addBlockInteraction(new Position((long) loc.getX(), (long) loc.getY(), (long) loc.getZ())); } } }
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; }