How do you execute code when a block is placed

Discussion in 'Plugin Development' started by WAZANATOR3998, Dec 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    WAZANATOR3998

    I want it to check if a sign has a anticancer string in it. But what is the santax to start executing code when a block (sign) is placed.
     
  2. Offline

    makskay

    Take a look at the Event API reference. Basically, you register Listeners for various types of Events (in this case, you'd want to listen for BlockPlaceEvents), and react appropriately to these Events depending on what you want to do.
     
  3. Offline

    WAZANATOR3998

    Can you give me an example of code with the sign included? So like when someone places a sign enters text then it executes code
     
  4. Offline

    gomeow

    In your onEnable:
    Code:java
    1. this.getServer().getPluginManager().registerEvents(this, this);

    Anywhere in your main class:
    Code:java
    1. @EventHandler
    2. public void onPlace(SignUpdateEvent event) {
    3. Sign sign = (Sign) event.getBlock().getState();
    4. if(sign.getLine(0).equalsIgnoreCase("gomeow") {
    5. //code to be ran if the top line was gomeow.
    6. }
    7. }

    Make sure you import org.bukkit.block.sign not org.bukkit.material.sign!
     
  5. Offline

    WAZANATOR3998

    Thank you so much :D
     
  6. Offline

    fireblast709

    SignUpdateEvent... SignChangeEvent?
     
  7. Offline

    CevinWa

    Remeber if you have changed the lines in the Sign when it's placed. You have to update it.
     
  8. Offline

    gomeow

    Yes, I was going out of memory, haven't done too much with signs
     
  9. Offline

    nisovin

    You don't need to access the Sign object, you can (and should) just use the methods on the event object.
     
Thread Status:
Not open for further replies.

Share This Page