Potion Recognition

Discussion in 'Plugin Development' started by Link_Awesomesause, Dec 28, 2015.

Thread Status:
Not open for further replies.
  1. Hello,

    Let me start by describing my problem. I have an InventoryClickEvent checking if a player has selected an option in my Icon-Menu. Then I have created another inventory, where players can put in potions. My problem is this- How do I know what potion they put in.

    Code:
    Code:java
    1. @EventHandler
    2. public void clkEvnt(InventoryClickEvent e) {
    3. Player p = (Player) e.getWhoClicked();
    4. if (e.getClickedInventory().getName().equalsIgnoreCase(mymenu2.getName())) {
    5. e.setCancelled(true);
    6. ItemStack is = e.getCurrentItem();
    7. if (is.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Sample Text")) {
    8. for (int j = 0; j <= 23; j++) {
    9. ItemStack iss = mymenu2.getItem(j);
    10. Potion pot = Potion.fromItemStack(iss); //Returns a unique code.. will be depicted in pic below
    11. mymenu2.clear();
    12. p.closeInventory();
    13. }
    14. }
    15. }
    16. }
     

    Attached Files:

    Last edited: Dec 29, 2015
  2. Offline

    Maxx_Qc

    InventoryCloseEvent, get the contents
     
  3. I tried this, it returned just "POTION".

    I am looking to find the type, and level(1,2 or 3) of the potion.
     
  4. Offline

    elian1203

    e.getClickedItem();
     
  5. Offline

    Xerox262

    Potions all use the Material.POTION material. If you want to check what type the potion is you have to refer to the potion's durability, for example healing potions have the durability of 8197, this is not counting if they have splash, extended, etc.

    Try using PotionEffectTypeWrapper with your item's durability, this should allow you to do object.getType(); (Null checks to make sure that the potion was an actual potion)
     
    Zombie_Striker likes this.
  6. Offline

    Maxx_Qc

  7. Gonna try it. I did something similar while waiting, although it was a long process and very... not good..

    Just in case you are wondering, I did the potion.getEffects().toString().. and then split the string into parts to get my details.. this way however, I was able to get level and duration!

    Potions have ItemMeta?
     
  8. Offline

    Maxx_Qc

  9. It is useful to make a potion, however.. not so, when trying to get the data from it.

    I have solved this issue, here is the code I used, though a little childish.. It works.

    Code:java
    1.  
    2. ItemStack iss;
    3. Potion pot;
    4. String eff;
    5. String[] s1;
    6. String potEffect;
    7. PotionEffectType pett;
    8. String[] ti;
    9. String[] tim;
    10. String[] ttim;
    11. String timmy;
    12. Inventory inventor;
    13.  
    14. if (iss.getType == Material.POTION) {
    15. iss = inventor.getItem(slot_number);
    16. pot = Potion.fromItemStack(iss);
    17. eff = pot.getEffects().toString();
    18. s1 = eff.split("[:]");
    19. potEffect = s1[0].substring(1);
    20. pett = PotionEffectType.getByName(potEffect);
    21. ti = eff.split("[(]");
    22. tim = ti[1].split("[-]");
    23. ttim = tim[0].split("[t]");
    24. timmy = ttim[0];
    25. int time = Integer.parseInt(timmy);
    26. int level = pot.getLevel();
    27. }
    28.  


    Using the code above, I get the duration(in ticks, not seconds), level and the potion effect type. All the details that I need!
     
    Maxx_Qc likes this.
  10. Offline

    Maxx_Qc

Thread Status:
Not open for further replies.

Share This Page