playerInteractEvent for signs

Discussion in 'Plugin Development' started by tylerthecreeper1, Feb 20, 2014.

Thread Status:
Not open for further replies.
  1. Hi. I am making a plugin that can show you the contents of a kit defined by right clicking a sign.(either in its own config, or essentials kits, i dont know how to hook other plugins so..)

    I am having trouble with the playerInteractEvent, I need it to open an inventory GUI. Below is my code
    Code:
        @EventHandler
        public void signInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        Block block = e.getClickedBlock();
        if(block.getType() == Material.SIGN || block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) {
        Sign sign = (Sign) e.getClickedBlock().getState();
        if(sign.getLine(0).contains("[KitDisplay]")) {
            p.openInventory(inv);
       
        }
        }
    I am also having trouble with the sign showing this: http://prntscr.com/2tyjpk
    I need to be able to type
    [kitdisplay]
    <kitname>
    and it shows what is in the picture. I already have working the top line, changing the color..
    Can someone please write this code for me?

    Thanks!
     
  2. Offline

    superguy9

    May I see your 'inv' variable code?
     
  3. Offline

    shawn99

    tylerthecreeper1

    heres some line of code that i use to add colors and test for lines WITH Colors

    Bukkit/java is picky over anything

    testing for a sign with no color tags in the code will not work if there is colors on the sign

    &1[Kits]
    will be in code for ChatColor.BLUE

    This is NEEDED to have it work with colors

    heres 2 parts of code that i use to make signs appear text and also test wether that text is the right color on the sign when right clicked

    Code:
    //TEsting for right click event
    //With the colors
    //if i did not show the colors in this code it would not do anything
     
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Block b = e.getClickedBlock();
                //PaintBall
                if(b.getType() == Material.WALL_SIGN || b.getType() == Material.SIGN_POST){
                    Sign sign = (Sign) b.getState();
                    String[] lines = sign.getLines();
                    if(lines[0].equalsIgnoreCase(ChatColor.DARK_BLUE+"[PB]") && lines[1].equalsIgnoreCase(ChatColor.GREEN+"Join")&& lines[3].equalsIgnoreCase(ChatColor.GREEN+"Running")){
                        p.chat("/pb join");
                    }
     
    //Code for making the signs
    //this tests for line 0 to have [pb]
    // and line 1 to have Join on it
    //if it does it will then make the sign with
    // ChatColor.BLUE+"[PB]"
    //and ChatColor.GREEN+"Join"
    //on line 0 and 1
     
    @EventHandler
        public void onSignCreate(SignChangeEvent sign)
        {
            Player p = sign.getPlayer();
            //PaintBall signs
            if(sign.getLine(0).equalsIgnoreCase("[PB]") && sign.getLine(1).equalsIgnoreCase("Join"))
            {
                p.sendMessage(ChatColor.GREEN+"Sign has been created");
                sign.setLine(0, ChatColor.DARK_BLUE + "[PB]");
                sign.setLine(1, ChatColor.GREEN+ "Join");
                sign.setLine(3, ChatColor.GREEN+"Running");
             
            }
    -Shawn

    Make sure u define the color in the rightclick event like i have if u want the colors on the signs to work propoly
     
  4. superguy9
    Code:
        public void openGUI(Player p){
            inv = Bukkit.createInventory(null, 54, ChatColor.DARK_RED + "KitDisplay"); //format: null, size of inventory, "GUI name"
            p.openInventory(inv);
     
  5. Offline

    superguy9

    Then instead of using
    Code:java
    1. p.openInventory(inv);
    use
    Code:java
    1. openGUI(p);
     
  6. Offline

    superguy9

    I assumed it worked then? :)

    If so, I'll make it a learning opportunity. :p
    What you were doing was opening a null inventory, which most likely would've given you an error. However, using that method you made initialized that variable. What you were basically doing was opening a nonexistent inventory, but using the openGUI(Player p) method, you opened an existing inventory.

    If you have any other questions, feel free to PM me about them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  7. superguy9 Thanks a lot. I haven't tested it yet, I took a break from working on that plugin for now :p
     
  8. Offline

    superguy9

    OK, tell me if you have any more issues!
     
  9. superguy9 That worked, tested yesterday. Now I just can't figure out preventing a player from breaking the sign if they do not have the permission. Me and my friend have been messing around with the code using teamviewer,

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockDamageEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. if(e.getBlock().getType() == Material.SIGN ){
    6. if(p.hasPermission("kitdisplay.destroy")){
    7.  
    8. }else{
    9. p.sendMessage(ChatColor.RED + "[Error] " + ChatColor.GOLD + "You do not have perrmision to remove displays!");
    10. e.setCancelled(false);
    11. e.setInstaBreak(false);
    12. }
    13. }
    14. }
    15. }
    16.  
     
  10. Offline

    superguy9

    Sorry for the late response.
    I believe the method that you're looking for is something like: e.getBlock().breakNaturally() or something along those lines. And you need to change the event to BlockBreakEvent instead of BlockDamageEvent.
     
Thread Status:
Not open for further replies.

Share This Page