How to Connect a Sign With A Plugin?

Discussion in 'Plugin Development' started by Requadin, May 26, 2012.

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

    Requadin

    Hello everyone, :)
    I'm trying to make a basic plugin which executes a code when player clicks on a button.
    But I don't want it to work on every button, just the ones I want.
    Is it possible to set an ID for the buttons withouth any physical difference between the regular button.

    If it is not, how can i connect a sign with that button?
    And is it possible to connect a sign with a button which are staying on the different sides of a block?

    [wood] [stone][stick]

    Wood: Sign
    Stone: Block
    Stick: Button

    When we build that on a wall, player will only see the button and the stone block. But behind it, there is a sign which tells the button what to do. However player is not going to see it.

    Signs can be better in order to see which button does what. And could you please type the code that i have to use?

    Thank you really much. [meat]
     
  2. Offline

    edocsyl

    You can Save the location of the block and make a Player Interact Event.
    onPlayer Interact
    Check if he is klicking on a block at the location where your button is.
     
  3. Offline

    Requadin

    How do i literally do that? I mean how can I set a location? :eek:
     
  4. Offline

    edocsyl

    Try this.

    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerInteract(PlayerInteractEvent e){
           
            Action a = e.getAction();
            Block b = e.getClickedBlock();
            Player p = e.getPlayer();
            World w = Bukkit.getServer().getWorld("Your Wolrd");
            Location l = new Location(w,123,123,123);
           
            if(a.equals(Action.LEFT_CLICK_BLOCK) || a.equals(Action.RIGHT_CLICK_BLOCK)){
                if(b.getType().equals(Material.STONE_BUTTON)){
                    if(b.getLocation().eqals(l)){
                        //Do something
                    }
                }
            }   
     
        }
    
     
  5. Offline

    Requadin

    Ok, I've tried to do what you have said but now i have a problem like this and i don't know how to fix it. :oops:

    Code:
    package com.github.Requadin.ButtonShop;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class main {
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerInteract(PlayerInteractEvent e){
       
            Action a = e.getAction();
            Block b = e.getClickedBlock();
            Player p = e.getPlayer();
            World w = Bukkit.getServer().getWorld("world");
            Location l = new Location(w,0,70,0);
       
            if(a.equals(Action.LEFT_CLICK_BLOCK) || a.equals(Action.RIGHT_CLICK_BLOCK)){
                if(b.getType().equals(Material.STONE_BUTTON)){
                    if(b.getLocation().equals(l)){
                        if(event.getPlayer().getInventory().contains(Material.GOLD_INGOT)){
                            event.getPlayer().getInventory().remove(new ItemStack(Material.GOLD_INGOT, 1));
                            }
                    }
                }
            }
     
        }
     
    }
    The last part event cannot be resolved.
    if(event.getPlayer() and event.getPlayer()

    When i change it to Event (org.bukkit.event) it says that getPlayer is not true. What i have to do?

    By the way it's not red colored, it says COLOR=#ff0000]event[/COLOR]. In real codes
     
  6. Offline

    ShoukaSeikyo

    Requadin
    You use the variable e for the data of the events, but after, you use event which is null.
    Change "event.getPlayer()" to "e.getPlayer()".
     
  7. Offline

    Requadin

    Ok thank you i changed it hope it works. :D
    (Is it really enough to change it to "e" or do i have to change anything more?) Because it says import org.bukkit.event.Event; is not used.
     
  8. Offline

    ShoukaSeikyo

    In that method, you don't import org.bukkit.event.Event but org.bukkit.event.player.PlayerInteractEvent. They are two different things.
    Just a little optimization of the code : Since you already got the player inside the variable p you should use it instead of recalling the method to get the player.
    e.getPlayer().getInventory(); -> p.getInventory();
     
Thread Status:
Not open for further replies.

Share This Page