1.14.4 Get the command executed when a Sign is right-clicked

Discussion in 'Spigot Plugin Development' started by TheKingElessar, Dec 6, 2020.

  1. [I put 1.14.4 but really just any version]


    I'm trying to get the command that's executed when a sign is right-clicked. Minecraft signs can execute actions when right-clicked, as seen here. I want to get that command when it's executed. I thought maybe I could get it from the org.bukkit.block.Sign object on a PlayerInteractEvent, but I can't find any way to get the sign action, only the text

    Is there a way to do this?

    Code (Java):
    public void onSignInteract(PlayerInteractEvent event)
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign)
            Sign clickedSign = (Sign) event.getClickedBlock().getState();
            // Can I get the sign's action from this object?
  2. This is stored in the sings NBT data. Which is complicated and you should research how to get that.
  3. I'm assuming he want's to also know what sign it came from, and if it came from a sign. So that wouldn't work in this case.
  4. Code (Java):
    /data merge block ~ ~1 ~ {Text1:'{"text":"Minecraft Tools","clickEvent":{"action":"run_command","value":"CommandNameHere"}}'}
    That is the command generated for 1.16 using the website you linked.

    I believe the NMS for getting that would be something along the lines of this:
    Code (Java):
    net.minecraft.server.ItemStack nmsItem = CraftItemStack.asNMSCopy(itemStack);

    String command = ((NBTTagCompound) ((NBTTagCompound) nmsItem.getTag().get("Text1")).get("clickEvent")).getString("value");
    This is untested though, so you might need to fiddle around with it.
    • Useful Useful x 1
  5. Great, thanks! I'll try that out. Since that uses NMS, I have to use reflection to support multiple versions, right?
  6. Yea, if it’s a public plugin
    • Like Like x 1
  7. Not sure if this applies to you, but I remember when I did something similar with right-clicking a sign and it would send 2 calls to the method for some reason. I found that it would do a duplicate call if I was holding an item in my hand when I right-clicked the sign. Not sure if I was doing something wrong but I just wanted to mention it just in case.
  8. It’s because you have 2 hands now so if you have to use an if to check if it’s the main player’s hand
  9. Ah. That makes sense. Thanks!
  10. Also consider that for versions lower than 1.14, the website you linked generates a different format. If you plan on supporting those too, I advise you use abstraction instead (better performance and readability anyway).