getMaterial method not working?

Discussion in 'Plugin Development' started by PumpMelon, Oct 4, 2016.

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

    PumpMelon

    Hello!
    I'm trying to loop through a list and randomly select an item and give it to a player. getMaterial is deprecated but I don't know a better way to do this as of now.

    http://hastebin.com/zehenihixi.swift

    I know there is a getMaterial(material).getId() method but I saw there was a bug with it and it always returned null. Not sure if this is fixed but its also deprecated.


    Thanks for the help!
     
  2. Offline

    xXkguyXx

    What error are you getting? Have you tried debugging or broadcasting what getMaterial returns when you got it? Does possible-rewards exists in your config?
     
  3. Offline

    Tecno_Wizard

    @PumpMelon, that returns the Material's item ID, which was deprecated in MC 1.8. I'm surprised it has only stopped working now TBH.

    I'm investigating now.

    EDIT: The return should not be null as the ID system still exists. It is safe to use it, but not advised.
     
  4. Offline

    PumpMelon

    Hello,
    So some new updates on this thread, so far the current issues stand. Though I've done some debugging with this as my config file
    Code:
    amount-of-votes: 50
    rewards:
      possible-rewards:
      - 3
      - 5
    
    The 3 and the 5 are suppose to be the ItemIDs,

    When I right clicked the chest, it fired twice giving me the "Sorry, something has gone wrong message" twice - I also made it send me what type I was given and that's the AIR and STONE messages

    [​IMG]
    https://gyazo.com/eece61466e94976f53039ede55574803

    There are no errors in my console at all. Don't know why this is being triggered twice, as I only made one reference call to it here http://hastebin.com/gevaciguvo.swift

    Thanks!
     
  5. Offline

    I Al Istannen

    @PumpMelon
    Why don't you let the user write the material name and then use Material#matchMaterial to get it?

    And I would guess the message was send for every unidentified item in the list ('3' and '5').
    Or it is called twice, once for offhand and once for main hand.
     
  6. Offline

    PumpMelon

    @I Al Istannen
    Thanks for that! matchMaterial seems like a better way to go. Though now when I put say for example "minecraft:bed" in my config its null. Though its one step closer,

    08.10 16:39:39 [Server] ERROR [VoteParty] Error: One of the Item IDs in /plugins/VoteParty/config.yml is null! Disabling plugin! 08.10 16:39:39 [Server] INFO [VoteParty] Disabling VoteParty v1.0

    Though... I'm 99% positive minecraft:bed shouldn't be null, though when I put something like STONE in my config I got the "Something has gone wrong message" aka what I was getting before using matchMaterial.
     
  7. Offline

    I Al Istannen

    @PumpMelon
    You will need to use the Material names here. Just not case sensitive.

    With stone it worked. But your equals makes no sense. What should this do?
    "if(s.equals(m)){"
    This checks if the String is equal to the Material. It will be false every time (as String.class != Material.class).
     
  8. Offline

    PumpMelon

    @I Al Istannen

    Actually, I have one last thing - not sure if this is possible but how would I access another value in the list? Like for example

    Code:
    possible-rewards:
       -COAL 5
    
    The 5 would be how much of that item they should get. Though I'm not sure if thats even possible.. and if it is how would I set myself up to do that?

    Thanks,
     
    Last edited: Oct 9, 2016
    I Al Istannen likes this.
  9. Offline

    PumpMelon

    Bump
    Edited recent post,
     
  10. @PumpMelon
    You can use the String#split() method, passing in " " (a space) as the argument. This will give you an array of all the text in the string, split at all spaces. So in the "COAL 5" example, string.split(" ") would give you an array of {"COAL", "5"} which you could then work with.
     
    I Al Istannen likes this.
  11. Offline

    I Al Istannen

    @AlvinB @PumpMelon
    Just don't assume the size of the resulting array. If the user didn't input it correctly, it may be of size 1.

    To "work with [it]", you can use Integer.parseInt or Short.parseShort or probably even Byte.parseByte. Just make sure you catch the NumberFormatException that might occur.
     
  12. @I Al Istannen
    Or if you want to avoid nasty inefficient exceptions, you can use regex matching!
    Code:java
    1. if (string.matches("[0-9]*")) {
    2. Integer.parseInt(string);
    3. }
     
  13. Offline

    I Al Istannen

    @AlvinB
    Exceptions are only inefficient if they occur. Your regex is slow everytime (needs to be compiled, and then matched. Takes it's time). And Exceptions aren't that slow anymore. They should not be used to structure the programs flow, but in this case I would argue they are okay.

    But apart from that, numbers are special.
    Your regex will be okay with "", your regex will not allow "+5".
     
  14. @I Al Istannen
    I'm aware that the regex isn't flawless, and that the occasional exception doesn't cause your program much harm, I mostly wanted to bring up that there are other options than the catching NumberFormatExceptions.

    Also, is this regex better? :p
    Code:java
    1. if (string.matches("[\-+0-9]*") && !string.isEmpty()) {
    2. Integer.parseInt(string);
    3. }


    @I Al Istannen
    Alright, you have convinced me :p

    Also, I guess we should stop derailing this thread by talking about integer parsing..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 25, 2017
  15. Offline

    I Al Istannen

    @AlvinB
    You can just use the '+' sign instead of the kleene star (yes, I learned that recently. Yes, I needed to show it xD).
    This will remove the need to check for "string.isEmpty()".

    Ideally you would make a private static variable with the pattern:
    Code:java
    1. private static final Pattern INTEGER_PATTERN = Pattern.compile("[\-+0-9]*");


    Then just create a matcher, which is faster and the RegEx is in one defined place.

    Your Regex can still overflow. Try this: "1111111111111111111111111111111" and see what happens. You can use curly brackets instead of the plus or star to prevent this: "[1-9]{1,5}" will match one to five numbers.

    Using this you can narrow it down.
    The max value for an integer is "2147483647", which has 10 digits. But "9999999999" has that too and will overflow the int.
    This means you will either be too strict (allow only nine digits) or allow overflows to happen.

    Honstly, use exceptions here.
     
  16. Offline

    PumpMelon

    @AlvinB @I Al Istannen
    Okay, so I've tried this though it's not working out to well.
    http://hastebin.com/gotuzawogi.java

    When I tried running it, the first time nothing happened - second time it said one of the items in my config was null
    I know the first value in my config is not null, so I'm guessing that it doesn't know what the number was and determined that as null. How would I only gain access to the first content of the index for my picked_reward string? I think that's causing it to be null.

    Here what my config looked like
    Code:
    possible-rewards:
       - COAL 5
       - DIAMOND 5
    #etc..
    
    Thanks for the help
     
  17. Offline

    Tecno_Wizard

    Guessing will get you nowhere.

    If you really want to figure out the issue, do some of your own debugging.

    Where do things go wrong? Is the string being pulled from the config null? Is the function that reads the String returning null? Is it my code or Bukkit's code that is the origin of the null? If it's Bukkit's code, why is the data that I am putting into the id to material conversion not being accepted? All of this can be determined with a few sysouts.

    Becoming a good programmer is more than knowing what to do. You're learning fast that programming is 20% writing code, 40% coming up with good strategies on how to write your code (DO THIS, PEOPLE!), and 40% fixing the inevitable bugs in your code. No one is perfect at this. Very rarely can you write a large algorithm such as this and expect it to "just work". Believe me, most of us are actually surprised when stuff works the first time. So please, don't ask us for help until you have exhausted your own knowledge of how to fix the issue. It's how you become a better programmer.

    SIDE EDIT: I'm currently teaching myself the Android SDK and I'm finding myself going through this process again. As soon as something doesn't work, I want to go Google the solution, but that isn't going to help anything. I keep reminding myself that the key to learning isn't Google. It's trying to fix your own mistakes and tragically failing until you figure out what you screwed up. Eventually, I have to Google stuff, but it makes it more of a learning experience than just copying the answer.
     
    Last edited: Oct 11, 2016
Thread Status:
Not open for further replies.

Share This Page