Solved Making crafting recipes. Crafting Matrix contains Custom Named Items

Discussion in 'Plugin Development' started by Bamco6657, May 8, 2017.

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

    Bamco6657

    So for a while now, I've been trying to get crafting with custom items to work. I've seen threads on this subject before, but they didn't meet my situation and weren't very helpful. So my crafting recipe looks like this:

    upload_2017-5-8_17-44-47.png

    All of the iron trapped doors lining the top are NOT regular trapped doors, they're custom named like this:

    upload_2017-5-8_17-46-14.png

    So CraftBukkit doesn't allow you to to include ItemMeta for recipe ingredients, so I first made a crafting recipe that includes all of the items:

    Code:java
    1.  
    2. public void MolecularExtractorRecipe() {
    3.  
    4. ItemStack res = new ItemStack(Material.PISTON_BASE, 1);
    5.  
    6. ItemMeta resItemMeta = res.getItemMeta();
    7. resItemMeta.setDisplayName(ChatColor.WHITE + "Molecular Extractor");
    8. res.setItemMeta(resItemMeta);
    9.  
    10. ShapedRecipe resRec = new ShapedRecipe(res);
    11. resRec.shape(
    12.  
    13. "###",
    14. "@?@",
    15. "@/@");
    16.  
    17. resRec.setIngredient('#', Material.IRON_TRAPDOOR);
    18. resRec.setIngredient('@', Material.IRON_INGOT);
    19. resRec.setIngredient('?', Material.FURNACE);
    20. resRec.setIngredient('/', Material.WORKBENCH);
    21. Bukkit.getServer().addRecipe(resRec);
    22. }
    23.  


    By the way, just in case this caused confusion, the product of the recipe is named Molecular Extractor, this shouldn't affect what I'm trying to solve though.

    But if I do this, I'm able to use any type of iron trapped door to craft it, no matter what the ItemMeta is, so I made another Listener class and made a PrepareItemCraftEvent, which looks like this:

    Code:java
    1.  
    2. @EventHandler
    3.  
    4. public void MolecularExtractor(PrepareItemCraftEvent event) {
    5.  
    6. String result = ChatColor.WHITE + "Molecular Extractor";
    7. String indFilter = ChatColor.WHITE + "Industrial Filter";
    8.  
    9. if (event.getRecipe().getResult().hasItemMeta() && result.equals(event.getRecipe().getResult().getItemMeta().getDisplayName())) {
    10. for (ItemStack item : event.getInventory().getMatrix()) {
    11. if (item != null && item.hasItemMeta()) {
    12. if (item.getType().equals(Material.IRON_TRAPDOOR)) {
    13. if(item.getItemMeta().getDisplayName() == indFilter){
    14.  
    15. found1 = true;
    16.  
    17. }
    18. }
    19. }
    20. }
    21. if (!found1) {
    22. event.getInventory().setResult(null);
    23. }
    24. }
    25. }
    26.  


    But when I do this, I can't craft it at all, industrial filters, regular iron trapdoors, both combined, nothing.

    Please help me with this! I've been trying to squash this bug for a while now. If you have any questions, I'd be more than happy to answer them. Thanks! :D
     
  2. Offline

    Zombie_Striker

    @Bamco665
    Your problem is you are comparing string using ==. That compares memory space, not the values of the string. Use == only when you need to see if the instance is the same. If all you care about is the values (like strings), use .equals

    btw, after crafting it once, found will always be true. Set it to false when the event is called, or, better yet, make found a local variable.
     
  3. Offline

    Bamco6657


    Ok, I set it to this:

    Code:java
    1.  
    2. @EventHandlerpublic void MolecularExtractor(PrepareItemCraftEvent event) {
    3.  
    4. boolean found1 = false;
    5.  
    6. String result = ChatColor.WHITE + "Molecular Extractor";
    7. String indFilter = ChatColor.WHITE + "Industrial Filter";
    8.  
    9. if (event.getRecipe().getResult().hasItemMeta() && result.equals(event.getRecipe().getResult().getItemMeta().getDisplayName())) {
    10.  
    11. for (ItemStack item : event.getInventory().getMatrix()) {
    12. if (item != null && item.hasItemMeta()) {
    13. if (item.getType().equals(Material.IRON_TRAPDOOR)) {
    14. if(item.getItemMeta().getDisplayName().equals(indFilter)){
    15.  
    16. found1 = true;
    17.  
    18. }
    19. }
    20. }
    21. }
    22. if (!found1) {
    23. event.getInventory().setResult(null);
    24. }
    25. }
    26. }
    27.  


    I can craft the item now, but now there's a new problem...

    It seems that the code is only checking if at least ONE of the trapdoors are named "Industrial Filter". I want it to check if ALL of the trapdoors are custom named, because now, as long as I have one trapdoor named "Industrial Filter" out of the three, it can craft the Molecular Extractor.
     
  4. Offline

    Zombie_Striker

    @Bamco6657
    1. Change found1 to an int. Let it be equal to 0
    2. Every time a trap door withb the display name is found, add +1 to the found (or, just use "found++;")
    3. If the found is equal to 3, then there are three trap doors with the display name.
     
  5. Offline

    Bamco6657

Thread Status:
Not open for further replies.

Share This Page