Domanda

I am working in Bukkit, and basically I need to make a method so that when a sign is right-clicked, it does something. In this case it'll be an inventory of kits, but I'll cross that bridge when I come to it.

Right now I am stumped as to how to right-click signs to get them to do things. I figure I should probably get the right-clicked block, check if it's a sign, then check what's written on the sign. However, I don't know how to identify it as a sign, as I'm confused at the presence of 2 sign materials, SIGN and SIGN_POST. After that, do I need to call some special event?

If you know of a good reference for me, feel free to post that in a comment. But I checked everywhere and all I find is the stupid SignChangeEvent, which is NOT what I need. All help is greatly appreciated!

È stato utile?

Soluzione

You should use the block type IDs for confirming that it's a sign. There are two ids for signs, one being the sign that's on a wall (68) and the other being the sign that's standing on a post (63). Use the PlayerInteractEvent to check if the player right clicked the sign. Check if the player right clicked a block. Check if the block that was right clicked has an ID of 63 or 68. Cast the block-state to a sign. Do whatever you want to do. If you change the text on the sign, make sure you update it afterward, otherwise the text won't show up. Here's the Player Listener function you should use.

    @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
        if(event.getClickedBlock().getTypeId() == 63 || event.getClickedBlock().getTypeId() == 68) {

            Sign sign = (Sign) event.getClickedBlock().getState();
            sign.setLine(0, "Boo Yeah");
            sign.update();

            // Do other stuff if you need to
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top