Inventory check ?

Discussion in 'Plugin Development' started by evilguy4000, Aug 6, 2013.

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

    evilguy4000

    Hello It's me again guys,
    I'm making a plugin that works with a currency that is specified in a yml file but i'm having troubles with the if statement that checks for the specified items in the inventory. I've tried everything but it still doesn't work so I checked if all the code before that if statement is working, and it does ;)
    So does anybody know how i can solve this. if you can you would make me a very happy man ;)

    So here is my code ;)
    Code:java
    1. package be.PandaPorgramming.AnvilRepair;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener {
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17.  
    18. @Override
    19. public void onEnable(){
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. this.logger.info(pdfFile.getName() + " Has Been Enabled!");
    22. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    23. getConfig().options().copyDefaults(true);
    24. saveConfig();
    25. }
    26. @Override
    27. public void onDisable(){
    28. PluginDescriptionFile pdfFile = this.getDescription();
    29. this.logger.info(pdfFile.getName() + " Has been Disabled!");
    30.  
    31.  
    32.  
    33. }
    34.  
    35. @EventHandler
    36. public void onClick(PlayerInteractEvent e){
    37. if(e.getPlayer().hasPermission("AR.use")){
    38. if( e.getAction() == Action.LEFT_CLICK_BLOCK){
    39. if(e.getClickedBlock().getType() == Material.ANVIL){
    40. Material material = Material.getMaterial(getConfig().getString("Currency"));
    41. int amount = this.getConfig().getInt("Amount");
    42. if(e.getPlayer().getInventory().getContents().equals(new ItemStack(material, amount))){
    43. e.getPlayer().sendMessage("it works!");
    44. }
    45.  
    46.  
    47.  
    48.  
    49.  
    50. }
    51.  
    52.  
    53. }
    54. }
    55. }
    56. }
    57.  
    58.  
    59.  
    60.  
    61.  


    PS. There isn't an error code so you don't need to ask after that ;)
     
  2. Offline

    Steffion

    Instead of using .equals try using .contains?
     
  3. Offline

    Tarestudio

    evilguy4000
    getContents() gives you an Array or List of ItemStacks. That will never be equal to a single ItemStack with an given amount.
    Loop through the getContents, check Material (getType().equals(Material)) and that amount is equal or greater then your amount.
     
  4. Offline

    evilguy4000

    Tarestudio
    How do you mean looping through the get contents?
     
  5. Offline

    Tarestudio

    evilguy4000
    ItemStack[] inv = getInventory().getContents();
    for (int i = 0; i < inv.length; i++) {
    if (inv != null) {
    ...
    But better use a thing Steffion mentioned
    if(e.getPlayer().getInventory().contains(material, amount)){
     
  6. Offline

    evilguy4000

Thread Status:
Not open for further replies.

Share This Page