com.comphenix.protocol.wrappers.nbt.NbtCompound Java Examples

The following examples show how to use com.comphenix.protocol.wrappers.nbt.NbtCompound. 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: StructureCUI.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
private void sendNbt(Vector pos, NbtCompound compound) {
    Player player = this.<Player>getPlayer().parent;
    ProtocolManager manager = ProtocolLibrary.getProtocolManager();

    PacketContainer blockNbt = new PacketContainer(PacketType.Play.Server.TILE_ENTITY_DATA);
    blockNbt.getBlockPositionModifier().write(0, new BlockPosition(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ()));
    blockNbt.getIntegers().write(0, 7);
    blockNbt.getNbtModifier().write(0, compound);


    try {
        manager.sendServerPacket(player, blockNbt);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: NBTUtil.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
/**
 * Sets a player profile onto the given tag
 *
 * @param nbt           the tag to set the profile to
 * @param playerProfile the profile to set
 */
public static void setPlayerProfile(NbtCompound nbt, PlayerProfile playerProfile) {
    nbt.put("Id", (playerProfile.getId() == null ? UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerProfile.getName()).getBytes()) : playerProfile.getId()).toString());
    nbt.put("Name", playerProfile.getName());
    if (!nbt.containsKey("Properties")) return;
    NbtCompound properties = nbt.getCompound("Properties");
    NbtList list = properties.getList("textures");
    Map<String, NbtBase> texture = (Map<String, NbtBase>) list.getValue(0);
    for (ProfileProperty property : playerProfile.getProperties()) {
        texture.put("name", NbtFactory.of("name", property.getValue()));
        texture.put("Signature", NbtFactory.of("Signature", property.getSignature()));
        texture.put("Value", NbtFactory.of("Value", property.getValue()));
    }
}
 
Example #3
Source File: SkullPlaceHolders.java    From VoxelGamesLibv2 with MIT License 5 votes vote down vote up
public PacketContainer modifySkull(WrapperPlayServerTileEntityData packet, Player player) {
    NbtCompound nbt = (NbtCompound) packet.getNbtData();

    Location location = new Location(player.getWorld(), packet.getLocation().getX(), packet.getLocation().getY(), packet.getLocation().getZ());

    if (nbt.containsKey("Owner")) {
        NbtCompound owner = nbt.getCompound("Owner");
        if (owner.containsKey("Name")) {
            String name = owner.getString("Name");
            PlayerProfile profile = null;

            String[] args = name.split(":");
            SkullPlaceHolder skullPlaceHolder = placeHolders.get(args[0]);
            if (skullPlaceHolder != null) {
                profile = skullPlaceHolder.apply(name, player, location, args);
            }

            if (profile != null && profile.hasTextures()) {
                NBTUtil.setPlayerProfile(owner, profile);
            } else {
                //log.warning("Error while applying placeholder '" + name + "' null? " + (profile == null) + " textures? " + (profile == null ? "" : profile.hasTextures()));
                NBTUtil.setPlayerProfile(owner, textureHandler.getErrorProfile());
            }

            owner.setName(name);
        }

        // update last seen signs
        Block b = location.getBlock();
        if (!(b.getState() instanceof Skull)) {
            return packet.getHandle();
        }
        Skull skull = (Skull) b.getState();
        lastSeenSkulls.put(location, skull);
    }

    return packet.getHandle();
}
 
Example #4
Source File: StructureCUI.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private NbtCompound constructStructureNbt(int x, int y, int z, int posX, int posY, int posZ, int sizeX, int sizeY, int sizeZ) {
    HashMap<String, Object> tag = new HashMap<>();
    tag.put("name", UUID.randomUUID().toString());
    tag.put("author", "Empire92"); // :D
    tag.put("metadata", "");
    tag.put("x", x);
    tag.put("y", y);
    tag.put("z", z);
    tag.put("posX", posX);
    tag.put("posY", posY);
    tag.put("posZ", posZ);
    tag.put("sizeX", sizeX);
    tag.put("sizeY", sizeY);
    tag.put("sizeZ", sizeZ);
    tag.put("rotation", "NONE");
    tag.put("mirror", "NONE");
    tag.put("mode", "SAVE");
    tag.put("ignoreEntities", true);
    tag.put("powered", false);
    tag.put("showair", false);
    tag.put("showboundingbox", true);
    tag.put("integrity", 1.0f);
    tag.put("seed", 0);
    tag.put("id", "minecraft:structure_block");
    Object nmsTag = BukkitQueue_0.fromNative(FaweCache.asTag(tag));
    return NbtFactory.fromNMSCompound(nmsTag);
}
 
Example #5
Source File: PacketSignPromptFactory.java    From helper with MIT License 4 votes vote down vote up
@Override
public void openPrompt(@Nonnull Player player, @Nonnull List<String> lines, @Nonnull ResponseHandler responseHandler) {
    Location location = player.getLocation().clone();
    location.setY(255);
    Players.sendBlockChange(player, location, Material.WALL_SIGN);

    BlockPosition position = new BlockPosition(location.toVector());
    PacketContainer writeToSign = new PacketContainer(PacketType.Play.Server.TILE_ENTITY_DATA);
    writeToSign.getBlockPositionModifier().write(0, position);
    writeToSign.getIntegers().write(0, 9);
    NbtCompound compound = NbtFactory.ofCompound("");

    for (int i = 0; i < 4; i++) {
        compound.put("Text" + (i + 1), "{\"text\":\"" + (lines.size() > i ? lines.get(i) : "") + "\"}");
    }

    compound.put("id", "minecraft:sign");
    compound.put("x", position.getX());
    compound.put("y", position.getY());
    compound.put("z", position.getZ());

    writeToSign.getNbtModifier().write(0, compound);
    Protocol.sendPacket(player, writeToSign);

    PacketContainer openSign = new PacketContainer(PacketType.Play.Server.OPEN_SIGN_EDITOR);
    openSign.getBlockPositionModifier().write(0, position);
    Protocol.sendPacket(player, openSign);

    // we need to ensure that the callback is only called once.
    final AtomicBoolean active = new AtomicBoolean(true);

    Protocol.subscribe(PacketType.Play.Client.UPDATE_SIGN)
            .filter(e -> e.getPlayer().getUniqueId().equals(player.getUniqueId()))
            .biHandler((sub, event) -> {
                if (!active.getAndSet(false)) {
                    return;
                }

                PacketContainer container = event.getPacket();

                List<String> input = new ArrayList<>(Arrays.asList(container.getStringArrays().read(0)));
                Response response = responseHandler.handleResponse(input);

                if (response == Response.TRY_AGAIN) {
                    // didn't pass, re-send the sign and request another input
                    Schedulers.sync().runLater(() -> {
                        if (player.isOnline()) {
                            openPrompt(player, lines, responseHandler);
                        }
                    }, 1L);
                }

                // cleanup this instance
                sub.close();
                Players.sendBlockChange(player, location, Material.AIR);
            });
}
 
Example #6
Source File: SignPlaceholders.java    From VoxelGamesLibv2 with MIT License 4 votes vote down vote up
public void init() {
    registerPlaceholders();

    // search for already loaded signs
    Bukkit.getWorlds().stream()
            .flatMap(w -> Arrays.stream(w.getLoadedChunks()))
            .flatMap(s -> Arrays.stream(s.getTileEntities()))
            .filter(s -> s instanceof Sign)
            .map(s -> (Sign) s)
            .forEach(s -> lastSeenSigns.put(s.getLocation(), s));

    // modify update packets
    protocolManager.addPacketListener(new PacketAdapter(voxelGamesLib, PacketType.Play.Server.TILE_ENTITY_DATA) {

        @Override
        public void onPacketSending(@Nonnull PacketEvent event) {
            int action = event.getPacket().getIntegers().read(0);
            // 9 = set sign text action
            if (action != 9) {
                return;
            }

            NbtCompound data = (NbtCompound) event.getPacket().getNbtModifier().read(0);
            // read data
            Component[] lines = new Component[4];
            String[] rawLines = new String[4];
            for (int i = 0; i < lines.length; i++) {
                lines[i] = ComponentSerializers.JSON.deserialize(data.getString("Text" + (i + 1)));
                rawLines[i] = ChatUtil.toPlainText(lines[i]);
            }

            // sometimes its a double, sometimes its an int...
            double x;
            double y;
            double z;
            try {
                x = data.getDouble("x");
                y = data.getDouble("y");
                z = data.getDouble("z");
            } catch (ClassCastException ex) {
                x = data.getInteger("x");
                y = data.getInteger("y");
                z = data.getInteger("z");
            }

            Location loc = new Location(event.getPlayer().getWorld(), x, y, z);
            if (event.getPlayer().getLocation().distanceSquared(loc) > 200 * 200) {
                return;
            }

            if (!(rawLines[0].equals("") && rawLines[1].equals("") && rawLines[2].equals("") && rawLines[3].equals(""))) {
                Block b = loc.getBlock();
                if (!(b.getState() instanceof Sign)) {
                    return;
                }
                Sign sign = (Sign) b.getState();
                lastSeenSigns.put(loc, sign);
            }

            Optional<User> user = userHandler.getUser(event.getPlayer().getUniqueId());
            if (!user.isPresent()) {
                return;
            }

            // call sign placeholders
            modifySign(user.get(), loc, rawLines, lines);

            // modify packet
            for (int i = 0; i < lines.length; i++) {
                data.put("Text" + (i + 1), ComponentSerializers.JSON.serialize(lines[i]));
            }
        }
    });

    // update task
    new BukkitRunnable() {

        @Override
        public void run() {
            lastSeenSigns.forEach((loc, sign) -> sign.update());
        }
    }.runTaskTimer(voxelGamesLib, 20, 20);
}