Solved Potion to ItemStack - Short durability incorrect?

Discussion in 'Plugin Development' started by elementalgodz11, Jun 15, 2014.

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

    elementalgodz11

    I have made an instance of the Potion class with my custom item, but it is giving the ItemStack the wrong durability value:

    Current Code (open)
    Code:java
    1. @SuppressWarnings("deprecation")
    2. private ItemStack stringToPotion(String string) {
    3.  
    4. Potion potion = null;
    5. string = string.toLowerCase();
    6.  
    7. if (string.startsWith("swp")) {
    8. potion = new Potion(PotionType.SPEED);
    9. } else {
    10. return null;
    11. }
    12.  
    13. if ((!(string.endsWith("s"))) && (!(string.endsWith("1"))) && (!(string.endsWith("1e"))) && (!(string.endsWith("2")))) {
    14. return null;
    15. }
    16.  
    17. if (string.endsWith("s")) {
    18. string = string.replace("s", "");
    19. potion.setSplash(true);
    20. }
    21.  
    22. if (string.endsWith("1")) {
    23. potion.setLevel(1);
    24. } else if (string.endsWith("2")) {
    25. potion.setLevel(2);
    26. } else if (string.endsWith("1e")) {
    27. potion.setHasExtendedDuration(true);
    28. }
    29.  
    30. System.out.println(potion.toDamageValue() + "<DAMAGEVALUE");
    31. return potion.toItemStack(1);
    32.  
    33. }


    So for this case if I use the input string of 'swp2' it should return an ItemStack of Speed, with the level modifier of 2, which it does, but with the incorrect durability. Instead of a durability of '8226' as it should be here, it is instead a durability of 34:
    test.png

    If there is an alternative way of doing this I'd like to know, because I think it may be a problem with the Potion class as it works fine for potions that are splashable.

    Thanks.
     
  2. elementalgodz11 Why does it matter? You get your potion which is essentially the same - maybe it's just not including the base potion in the DV
     
  3. Offline

    elementalgodz11

    AdamQpzm

    When you obtain a potion the correct way such as brewing it, the data value will be correct to such as a wiki would show,

    therefore if I tried to get the ItemStack using the stringToPotion method, it will not recognise it as a potion because the string is not equal to the actual potions data value
     
  4. Offline

    AoH_Ruthless

    elementalgodz11
    I believe 34 is actually correct, seeing as the first potion that isn't a bottle starts with data value 8192, then increments from there. So, if you can just add 8192 to the data value and you should be fine, although it won't matter at all.

    Edit: Yep: Tested in-game: /give AoH_Ruthless 373:34 gives me a swiftness II potion.
     
    elementalgodz11 likes this.
  5. Offline

    elementalgodz11

    AoH_Ruthless

    Thank you!

    Would this work for splashable potions too as I noticed they generate correctly.

    If so, what pattern could I attempt to correct this? Check if the durability is less than 1000 or it returns true for the isSplashable boolean before adding 8192?
     
  6. Offline

    AoH_Ruthless

    elementalgodz11
    Splash potions I don't think work. If you notice, their durability is 16,000+ so I don't know the algorithm. Check if the durability is less than, say, 500 and then add 8192.
     
  7. AoH_Ruthless Sure checking isSplashable or if the value is less than 8192 makes more sense, right? :p
     
    elementalgodz11 and AoH_Ruthless like this.
  8. Offline

    AoH_Ruthless

    AdamQpzm likes this.
  9. Offline

    elementalgodz11

    Thank you guys.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page