Two AIR ItemStacks don't equal

Discussion in 'Plugin Development' started by bensku, May 1, 2014.

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

    bensku

    Title explains my problem quite well. It may sound very strange, and actually it's strange. .toString() representation is same for both ItemStacks, but when I do stack1.equals(stack2), it returns false.
    Code:java
    1. public class CraftEventListener implements Listener {
    2. @EventHandler(priority = EventPriority.HIGHEST) //Highest due to CraftingListener
    3. public void onPrepareCraft(PrepareItemCraftEvent event) {
    4. Bukkit.getLogger().info("Debug: PrepareCraftItemEvent catched");
    5.  
    6. ItemStack[] items = event.getInventory().getContents();
    7. ArrayList<CustomRecipe> recipes = RecipeRegistry.getRecipes();
    8.  
    9. Bukkit.getLogger().info("Debug: items is " + items.toString());
    10. Bukkit.getLogger().info("Debug: recipes is " + recipes.toString());
    11.  
    12. for (int i = 0; i < recipes.size(); i++) {
    13. CustomRecipe recipe = recipes.get(i);
    14. Bukkit.getLogger().info("Debug: recipe is " + recipe.toString());
    15.  
    16. boolean same = true;
    17. for (int i2 = 1; i2 <= 9; i2++) {
    18. Bukkit.getLogger().info("Debug: for loop: i2");
    19.  
    20. if ( !items[i2].equals(recipe.getReagent(i2)) ) { //PROBLEM HERE
    21. Bukkit.getLogger().info("Debug: break for i2");
    22. Bukkit.getLogger().info("Debug: i2 is " + i2);
    23. Bukkit.getLogger().info("Debug: reagent should be " +
    24. recipe.getReagent(i2));
    25. Bukkit.getLogger().info("Debug: reagent is " + items[i2]);
    26.  
    27. //If at least one of items isn't same, break
    28. same = false;
    29. break;
    30. }
    31.  
    32. }
    33. if ( same ) {
    34. //All reagents are same than in recipe, set the result
    35. event.getInventory().setResult(recipe.getResult());
    36. return;
    37. }
    38. }
    39. }
    40. }
    41.  

    This the part of code where the problem appears. It's part of my own plugin's unreleased dev-version, which should allow custom crafting with custom items as ingredients (called reagents in code).
    When I run this code, I get output:
    Code:
    [21:04:32 INFO]: Debug: for loop: i2 //For loop have started, all fine now...
    [21:04:32 INFO]: Debug: break for i2
    [21:04:32 INFO]: Debug: i2 is 1 //This isn't related to my problem
    [21:04:32 INFO]: Debug: reagent should be ItemStack{AIR x 0}
    [21:04:32 INFO]: Debug: reagent is ItemStack{AIR x 0}
    So no errors appears. As you can see, the items[i2] and recipe.getReagent(i2) looks exactly same as Strings... But they aren't, as the Debug: break for i2 tells.

    I can't even imagine what could create this kind of problem, so I need to ask help.
     
Thread Status:
Not open for further replies.

Share This Page