Solved SignChange Event

Discussion in 'Plugin Development' started by Mudkiper202, Jul 21, 2016.

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

    Mudkiper202

    I need help i am coding a plugin that includes shopping with signs and i have the money system setup but i am confused?

    1.How would i compare an item they type in to a material.
    2.can i check how many words they type on a line for example Buy 10
    how would i detect that it has 2 arguments Buy and 10
     
    Last edited: Jul 21, 2016
  2. Offline

    N00BHUN73R

    @Mudkiper202
    1. You can use Material.matchMaterial(string); to match the material, check if its null incase they typed it in wrong.
    2. You can sign.getLine(#) then split it by spaces line.split(" "); and then break it up like that.
     
  3. Offline

    andrew4543

    @N00BHUN73R got it right, but wanted to elaborate just a little bit. I didnt know if u knew how to get a sign from a interact event, so i am just going to post anyway
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if(event.getClickedBlock() == null)
            return;
        Block block = event.getClickedBlock();
        Material type = block.getType();
        if (!type.equals(Material.WALL_SIGN) || !type.equals(Material.SIGN_POST))
            return;
        Sign sign = (Sign) block.getState();
    Edit:
    found this resource just a bit later on, might be helpful. Signs!
     
    Last edited: Jul 21, 2016
  4. Offline

    Lordloss

    @andrew4543 First of all, stop spoonfeeding. It is totally wrong to do this, especially in this case. You can read in many threads why its bad to do it. And the code you posted is a wonderful example for this:

    1. No need to create a variable if only used once.
    2. And much more important
    Code:
    if (!type.equals(Material.WALL_SIGN) || !type.equals(Material.SIGN_POST))
    This line will never be false.
     
  5. @Lordloss
    I don't see why that line will never be false? Care to elaborate a bit?
     
  6. Offline

    Lordloss

    Sure, IF type of block is NOT wall_sign OR type of block is NOT sign_post
    and because a block has only one material at a time, it cannot be false.

    Edit: Google Rubberduck-debugging, it will save you hours of time in some situations ;)
     
  7. @Lordloss
    Oh wops, didn't realize he had put an OR, I read it as an AND
     
  8. Offline

    Lordloss

    @AlvinB i think everybody knows this problem. It wont happen to you again if you searched centuries for an error and broke your head with a hammer.
     
  9. Offline

    alaa15

    When it gets confusing simply try flipping all the "not" and the comparison operator to understand if it's what you wanted or if it would work.

    Example,

    Code:
    if (!type.equals(Material.WALL_SIGN) || !type.equals(Material.SIGN_POST))
    Would be

    Code:
    if (type.equals(Material.WALL_SIGN) && type.equals(Material.SIGN_POST))
    Which is impossible.
     
  10. Offline

    andrew4543

    I was just trying to help, i just wrote that without testing. I would have realized my error later on.
     
  11. Offline

    Mudkiper202

    How would i retrieve those 2 arguments?
     
  12. Offline

    Mudkiper202

    I know that but would i save the arguments as a variable for what
     
  13. @Mudkiper202
    To get a line from the sign use
    Code:java
    1. SignChangeEvent.getLine(aLine)
     
  14. Offline

    Mudkiper202

    Yes but i have sign.getLine(1).split(" ");
    I need to get the argument on the left of the split and the argument on the right
     
    Last edited: Jul 22, 2016
  15. @Mudkiper202
    The String.split() method returns an array with the split strings. You just need to get the strings from this array, and remember, arrays start counting from 0, not 1.
     
  16. Offline

    Mudkiper202

    Can you show an example of how you would set this in the array and get
     
  17. @Mudkiper202
    Code:java
    1. String myString = "Will split"
    2. String[] anArray = myString.split(" ");
    3. String firstString = anArray[0]; // will be "Will"
    4. String secondString = anArray[1]; // will be "split"
     
  18. Offline

    Mudkiper202

    Thanks
     
  19. @Mudkiper202
    If your issue has been resolved, please mark the thread as such :)
     
  20. Offline

    Mudkiper202

    how shall i also check that there are 2 args?
     
  21. Offline

    N00BHUN73R

  22. Offline

    Lordloss

    @AlvinB Please read the threads about spoonfeeding. This is basic java which he should know..
     
  23. @Lordloss
    Correct me if I'm wrong, but I don't think what I did is spoon feeding, it is just a general code example of how to use String.split(), it has nothing to do with his code.
     
    bwfcwalshy likes this.
Thread Status:
Not open for further replies.

Share This Page