Solved Sign Help (Getting text & setting it)

Discussion in 'Plugin Development' started by KingKongC, Jan 19, 2013.

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

    KingKongC

    Yet another help request....

    i would love to know how to get a signs text eg. [SIGN] and when a player right clicks it, It then checks if the top line is [SIGN] and the player has permission (admin.sign) then sets it to this format
    _____________
    | [SIGN] |
    | |
    | Name |
    =============
    |
    |


    PHP:
     Sorry the sign format didn't work
    Also i have looked around before posting 
     
  2. Offline

    Miner_Fil

    For the SignChangeEvent i would use this code

    Code:
    @EventHandler(priority=EventPriority.HIGHEST)
        public void onSignCreate(SignChangeEvent event){
            if(event.getLine(0).equalsIgnoreCase("[SIGN]")){
                event.setLine(3, "Name");
                player.sendMessage(ChatColor.GREEN + "Sign Created!");
            }
    }
    And for the PlayerInteractEvent i would use this code

    Code:
    @EventHandler(priority=EventPriority.HIGHEST)
          public void clickHandler(PlayerInteractEvent e)
          {
            Player player = e.getPlayer();
            if ((e.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
     
            Block clickedBlock = e.getClickedBlock();
            if ((clickedBlock.getType() != Material.SIGN) && (clickedBlock.getType() != Material.SIGN_POST) && (clickedBlock.getType() != Material.WALL_SIGN)) return;
            Sign thisSign = (Sign)clickedBlock.getState();
     
            String[] lines = thisSign.getLines();
            if (lines[0].equalsIgnoreCase("[NAME]")) {
                  if(player.hasPermission("admin.sign")){
                  // Do Stuff
                } else {
                    player.sendMessage(ChatColor.RED + "Sorry, You don't have Permission");
                }
          }
        }
     
  3. Offline

    gomeow

    Don't check for Material.SIGN as that is the inventory item version
     
  4. Offline

    Miner_Fil

    If you can see, I have decompiled Double0Negatives Super Spleef and i see this inside ClickSignEvent.class
    Code:
    Block clickedBlock = e.getClickedBlock();
    if ((clickedBlock.getType() != Material.SIGN) && (clickedBlock.getType() != Material.SIGN_POST) && (clickedBlock.getType() != Material.WALL_SIGN)) return;
    Sign thisSign = (Sign)clickedBlock.getState();
     
  5. Offline

    gomeow

    Then that programmer is wrong. Simple as that.

    I mean it won't destroy the code, it will just never get used
     
  6. Offline

    Miner_Fil

    Try to use the code and see the difference :)
     
  7. Offline

    Major_Derp

    KingKongC
    I would type out some code, but i do not have time right now, But here is some code from my plugin MineProfile, and its pretty simple, you can look through for the parts you need.
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            Action action = event.getAction();
            if(action == Action.RIGHT_CLICK_BLOCK){
                if(block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN){
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    String sline1 = sign.getLine(0);
                    String sline2 = sign.getLine(1);
                    if(sline1.equalsIgnoreCase("[check in]")){
                        if(!(sline2.isEmpty())){
                                if(player.hasPermission("mineprofile.checkin")){
                                    File pluginFolder = new File(Bukkit.getPluginManager().getPlugin("MineProfile").getDataFolder(), "Profiles");
                                    File playerFile = new File(pluginFolder, player.getName()+".yml");
                                    YamlConfiguration playerProfile = new YamlConfiguration();
                                    if(playerFile.exists()){
                                        try {
                                            playerProfile.load(playerFile);
                                            playerProfile.set("Location", sline2);
                                            playerProfile.save(playerFile);
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                        Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.GREEN +  " Has just checked into " + ChatColor.RED + sline2);
                                        player.sendMessage(ChatColor.DARK_GREEN + "You Current Location was Set to " + ChatColor.DARK_RED + sline2);
     
                                    }
                                    else{
                                        player.sendMessage(ChatColor.DARK_GREEN + "You don't have a profile yet, use" + ChatColor.DARK_RED +
                                        " /profile create" + ChatColor.DARK_GREEN + "to create one.");
                                }
                            }
                        }
                    }
                }
            }
        }
       
        @EventHandler
        public void onSignChange(SignChangeEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN){
                String sline1 = event.getLine(0);
                String sline2 = event.getLine(1); // name of the Checkin
                if(sline1.equalsIgnoreCase("[check in]")){
                    if(!(sline2.isEmpty())){
                        if(player.hasPermission("mineprofile.checkin.set")){
                            player.sendMessage(ChatColor.GREEN + "Checkin point " + ChatColor.RED + sline2 + ChatColor.GREEN + " Has been set");
                        }
                        else{
                            player.sendMessage(ChatColor.GREEN + "Im sorry you can't create Checkin Signs. Speak to an Admin if This is a problem");
                            event.setCancelled(true);
                        }
                    }
                    else{
                        player.sendMessage(ChatColor.GREEN + "Second Line of Checkin Sign must have Text on it.");
                    }
                }
            }
        }
     
  8. Offline

    gomeow

    Miner_Fil
    I looked at that code again and it appears that it will only pass the if statement if it is not a sign. Of any type
     
  9. Offline

    KingKongC

    Im looking at the code by Major_Derp and im like wtf
    a. i can't read most of it its not that neat (unlikes gomeow)

    If you could do a simple tutorial on this?
     
  10. Offline

    gomeow

    For the clicking the sign:
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent event) {
    3. Player p = event.getPlayer(); //Get the player
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { //Mke sure the interaction was a right clicking of a block
    5. Material clickedType = event.getClickedBlock().getType(); //Store the type of block clicked
    6. if(clickedType == Material.SIGN_POST || clickedType == Material.WALL_SIGN) { //Check if they clicked a sign
    7. Sign sign = (Sign) event.getClickedBlock().getState(); //Get the sign
    8. String[] lines = sign.getLines(); //Get the lines
    9. if(lines[0].equalsIgnoreCase("[Kill]") { //Check what the first line is (Indexes will go from 0-3)
    10. if(p.hasPermission("sign.use.kill") {
    11. p.setHealth(0); //Kill the player
    12. Bukkit.broadcastMessage(ChatColor.GOLD+p.getName()+" just killed himself with a KILL sign!"); //Tell the world about it
    13. }
    14. }
    15. }
    16. }
    17. }

    Also, when importing the sign, use org.bukkit.block.sign
     
  11. Offline

    KingKongC

    Okay ive done that but these line have syntax errors
    PHP:
              if(lines[0].equalsIgnoreCase("[Kill]"){ //Check what the first line is (Indexes will go from 0-3)
                  
    if(p.hasPermission("sign.use.kill") { 
     
  12. Offline

    gomeow

    What are the errors?

    Also do this with your code:

    [syntax=java]Code here[/syntax]

    Oh Just close the if statements with another ) symbol
     
  13. Offline

    KingKongC

    Okay thanks also there is a warning for the
    Code:java
    1. Player p = event.getPlayer();



    Also i would like to make the [SIGN] code to green text
     
  14. Offline

    gomeow

    Again, error?
     
  15. Offline

    KingKongC

    No just a warning as i said above, Derp
     
  16. Offline

    gomeow

    Which is?
     
  17. Offline

    KingKongC

    The value of the local bariable p is not used

    then again i guess it because i remove the kill code, how do i format sign lines?
     
  18. Offline

    gomeow

    Did you take out the lines where it kills and sends a message? If so, then you may not need that variable
     
  19. Offline

    KingKongC

    Yes i did, could you tell me how to set text too?
     
  20. Offline

    gomeow

  21. Offline

    KingKongC

    Would i set the sign text like this
    Code:java
    1.  
    2. sign.setLine(1,ChatColor.DARK_BLUE + "[" + ChatColor.DARK_AQUA + "Warning" + ChatColor.DARK_BLUE+ "]");
     
  22. Offline

    gomeow

    Yes
     
  23. Offline

    KingKongC

    Thanks for your help going to sutup the plugin.yml and test it
     
Thread Status:
Not open for further replies.

Share This Page