Setting a sign line and getting the value

Discussion in 'Plugin Development' started by diamondcodes, Nov 4, 2014.

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

    diamondcodes

    Hello,
    I am wondering how I would get a specific line on a sign, I have
    Code:
                                    if (((Sign) event.getClickedBlock().getState()).getLine(0).equalsIgnoreCase("10$")) {
                                            player.sendMessage("+ 10");
    
    but how would I get the value of the line without having to have it set to a exact value in the code? I want to be able to change the value of the price in game on the sign and send the player a message with that same value

    - Thanks
     
  2. Offline

    mine-care

    Keep a list of sign locations, loop through them if the block is sign cast it and then do your changes on it ;)
    Don't forget to update it.
     
  3. Offline

    Watto

    diamondcodes

    You can just call getLine(LINE_NUMBER) and that returns the String of the line, then cut anything you don't need (leaving the price) and send it to the player
     
  4. Offline

    diamondcodes

    Thanks. 1 question, For sending the player money the Line is a string so it will not work with "EconomyResponse r = Main.econ.depositPlayer" so how would I give them the money with vault? (Giving them the amount of money from the last line on the sign)
     
  5. Offline

    Watto

    diamondcodes

    Strip the string of all text and anything non-numerical (or you will get and exception) and them use Integer.parseInt()
     
  6. Offline

    diamondcodes

    Watto Here is my code,
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4.  
    5. if (event.getClickedBlock().getType() == Material.WALL_SIGN || event.getClickedBlock().getType() == Material.SIGN_POST) {
    6. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    7. if (((Sign) event.getClickedBlock().getState()).getLine(0).equalsIgnoreCase("[SellAll]")) {
    8. if (((Sign) event.getClickedBlock().getState()).getLine(1).equalsIgnoreCase("SomeBlock")) {
    9. if (((Sign) event.getClickedBlock().getState()).getLine(2).equalsIgnoreCase("10")) {
    10. ((Sign) event.getClickedBlock().getState()).getLine(3);
    11. EconomyResponse r = Main.econ.depositPlayer(player.getName(), ((Sign) event.getClickedBlock().getState()).getLine(3).replaceAll("$", "").length());
    12. player.sendMessage("You have gained " + ((Sign) event.getClickedBlock().getState()).getLine(3) + " for selling!");
    13. return;
    14.  
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }
    22.  


    Can you show me what you mean? Please, Thanks

    (First time using Vault/Making a plugin that uses signs)
     
  7. Offline

    Watto

    diamondcodes

    Look up 'regex' and 'non-numeric regex' and it will give you your answer.
    Also rather than using event.getClickedBlock().getState() everytime.
    Just store the Block / BlockState. It looks better and it's easier to read.
     
  8. Offline

    Barinade

    String signLine = "Price: $10";
    String price = signLine.split(": $")[1]; //yes, I went there
    int price = Integer.parseInt(price);
     
  9. Offline

    Watto

    Barinade

    Regex is much better and safer in this case.. Slight changes could cause your method to break.
     
  10. Offline

    Barinade

    That's why you make sure there are no changes lol
     
  11. Offline

    Watto

    Barinade

    ..That's just more pointless code -_-
    Regex = 1 line.
     
  12. Offline

    Barinade

    It's not pointless code, it's code that is going to be done anyways.
    You know what it's coded to say so just use that format to your advantage.
     
Thread Status:
Not open for further replies.

Share This Page