Solved Give player NBT sign

Discussion in 'Spigot Plugin Development' started by Smikkel_Bakje, Feb 6, 2020.

  1. Hi there!

    I wanted to give a player a "pre-written" sign with NBT. Is there a way to do this in spigot?

    My current code:
    Code (Text):
            ItemStack item = new ItemStack(Material.SIGN, 1);

            net.minecraft.server.v1_12_R1.ItemStack stack = CraftItemStack.asNMSCopy(item);
            NBTTagCompound compound = stack.hasTag() ? stack.getTag() : new NBTTagCompound();
            NBTTagList BlockEntityTag = new NBTTagList();

            NBTTagCompound text1 = new NBTTagCompound();
            NBTTagCompound text1t = new NBTTagCompound();
            text1t.setString("text", "Test");
            text1.set("Text1", text1t);

            BlockEntityTag.add(text1);
            compound.set("BlockEntityTag", BlockEntityTag);
            stack.setTag(compound);

            item = CraftItemStack.asBukkitCopy(stack);

            p.getInventory().addItem(item);
    The code just gives me a sign (with an nbt-tag in the lore), but when I place it it just acts like a normal sign.

    Thanks in advance,
    Smikkel_Bakje
     
  2. Should be like that. If not working look into the deprecated Sign (MaterialData) class.

    Code (Text):
    ItemStack signStack = new ItemStack(Material.SIGN);

    ItemMeta itemMeta = signStack.getItemMeta();

    if(itemMeta instanceof BlockStateMeta){
        BlockStateMeta blockStateMeta = (BlockStateMeta) itemMeta;
        BlockState blockState = blockStateMeta.getBlockState();
       
        if(blockState instanceof Sign){
            Sign sign = (Sign) blockState;
           
            sign.setLine(0, "Yolo");
            sign.setLine(1, "Yolo");
            sign.setLine(2, "Yolo");
            sign.setLine(3, "Yolo");
            sign.update();
           
            blockStateMeta.setBlockState(sign);
            signStack.setItemMeta(blockStateMeta);
        }
       
    }
     
    • Like Like x 1
    • Useful Useful x 1
  3. This works! Thanks for the help!