While item is in slot

Discussion in 'Plugin Development' started by khave, Apr 8, 2014.

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

    khave

    So basically what I'm trying to get at is that I want something to happen when an item is in a slot (For example slot 9). I want it to keep happening, so I can't just use the code: p.getInventory().getItem(18).getType == Material
    I've managed to figure out that I probably need to use the InventoryClickEvent, but I really suck at Inventory logic.
    Thank you for helping me in advance ^^
     
  2. Offline

    St3venAU

    How often do you want "something to happen" if it's not really often (like every tick or something) then you could have a repeating task that checks all player's 9 slots and does something if they have the item there. It might also help to say what you want to happen because you may be able to set it up in another listener that will check the 9 slot.

    The other way is to check for the player placing the item in their 9 slot in InventoryClickEvent and start a repeating task that cancels itself as soon as the item is not in the 9 slot.
     
  3. Offline

    khave


    I know that, but the problem is I don't know how to check if the item is placed in the 9th slot of the inventory.
     
  4. Offline

    St3venAU

    You could check the different combination of ways a player can get an item into their 9 slot (left click it there, right click it there, hover over the item and press 9, drag a stack of that item across their 7-8-9 slots for example... there are lots of ways). So I think the easier way is just to schedule a task for 1 tick after the inventoryclickevent and check then if player.getIntentory().getItem(9) is your item.
     
  5. Offline

    khave

    If anyone else is having difficulties I made a bit of a test code (I haven't worked it out fully) using the tip from St3venAU
    Here it is:
    Code:java
    1. package me.khave.MyServer.me.khave.Items;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.inventory.InventoryClickEvent;
    9. import org.bukkit.event.inventory.InventoryDragEvent;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.inventory.meta.ItemMeta;
    12. import org.bukkit.potion.PotionEffect;
    13. import org.bukkit.potion.PotionEffectType;
    14.  
    15. import java.util.ArrayList;
    16.  
    17. public class Health implements Listener {
    18.  
    19. @EventHandler
    20. public void playerInventory(InventoryClickEvent e){
    21. ItemStack SpeedRing = new ItemStack(Material.EMERALD);
    22. ItemMeta SpeedRingim = SpeedRing.getItemMeta();
    23. SpeedRingim.setDisplayName(ChatColor.GREEN + "Speed Crystal");
    24. ArrayList<String> SpeedRingtt = new ArrayList<String>();
    25. SpeedRingtt.add(ChatColor.GRAY + "By having this item in your crystal slot");
    26. SpeedRingtt.add(ChatColor.GRAY + "you will be able to be faster!");
    27. SpeedRingim.setLore(SpeedRingtt);
    28. SpeedRing.setItemMeta(SpeedRingim);
    29.  
    30. Player p = (Player) e.getWhoClicked();
    31.  
    32. //if(e.getCursor() == null && !e.getCursor().getItemMeta().hasDisplayName() && !e.getCursor().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Speed Crystal")) return;
    33.  
    34. if(!e.getCursor().getType().equals(Material.AIR) && e.getCursor().getItemMeta().hasDisplayName() && e.getSlot() == 9 && e.getCursor().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Speed Crystal")){
    35. p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 20*15, 3));
    36. }
    37. //TODO: Make a: "if the item is taken away"
    38. }
    39. }


    It still needs a bit of tweaking and I've added a TODO, to make sure it works. Ask me if you need the whole code in the future, cause I will probably have made it by then ;3
     
Thread Status:
Not open for further replies.

Share This Page