Solved PrepareItemCraftEvent

Discussion in 'Spigot Plugin Development' started by Lifeonblack, May 24, 2017.

  1. So I am in trouble can you give me a hand? there is no error at all but let me tell you how it's been a problem

    so here is my event
    Code (Java):
    @EventHandler(priority=EventPriority.HIGH)
        public void onCraft(PrepareItemCraftEvent e) {
            if(e.getInventory().getResult() == null) {
                return;
            }
            if(ca.getConfig().getKeys(false) == null) {
                return;
            }
            Set<String> sets = ca.getConfig().getKeys(false);
            ItemStack item = ca.getConfig().getItemStack(sets + ".Product");
            if(e.getInventory().getResult() == item && ca.getConfig().getString(sets + ".Type").equals("Shaped")) {
                for(int i = 0; i < 9; i++) {
                    if(e.getInventory().getItem(i) != null) {
                        if(e.getInventory().getItem(i).hasItemMeta()) {
                            ItemStack tocheck = ca.getConfig().getItemStack(sets + ".Slot" + i);
                            if(e.getInventory().getItem(i).getItemMeta().getDisplayName().equals(tocheck.getItemMeta().getDisplayName())) {
                                ItemStack result = ca.getConfig().getItemStack(sets + ".Product");
                                e.getInventory().setResult(result);
                            }
                        }else {
                            ItemStack recipe = ca.getConfig().getItemStack(sets + ".Slot" + i);
                            if(e.getInventory().getItem(i) == recipe) {
                                ItemStack result = ca.getConfig().getItemStack(sets + ".Product");
                                e.getInventory().setResult(result);
                            }
                        }
                    }
                }
            }
        }
    then this is a sample of a data I used
    Code (Java):

    new:
      Slot5:
        ==: org.bukkit.inventory.ItemStack
        type: GRAVEL
      Product:
        ==: org.bukkit.inventory.ItemStack
        type: DIRT
        damage: 2
      Enabled: true
      Type: Shaped
    fab:
      Slot5:
        ==: org.bukkit.inventory.ItemStack
        type: GRAVEL
        meta:
          ==: ItemMeta
          meta-type: UNSPECIFIC
          display-name: handsome
      Product:
        ==: org.bukkit.inventory.ItemStack
        type: STAINED_CLAY
        damage: 5
        meta:
          ==: ItemMeta
          meta-type: UNSPECIFIC
          display-name: cool
      Enabled: true
      Type: Shaped
    when I use gravel at the slot 5 with the name handsome it gives me the product on first which is type: DIRT
    so I am asking every custom name in config will be compared in this event so I can sort the results even the type of item is not different.

    if necessary this is ca
    Code (Java):
    ConfigAccessor ca = ConfigAccessor.getInstance();
    or if you have any idea how I set the itemstacks or datas to flatfile so I can search and get datas to it mroe easy and more handy.
     
    #1 Lifeonblack, May 24, 2017
    Last edited: May 24, 2017
  2. bump :) sorry for interruption.
     
  3. Add some Println's to basically everything and find where it stops, or if it's even called. Also just saying, this is probably something that can be done in a way cleaner fashion with Gson.
     
  4. I used this code
    Code (Java):
    Set<String> set = ca.getConfig().getKeys(false);
            for(String sets : set) {
                if(ca.getConfig().getString(sets + ".Type").equals("Shaped")) {
                    System.out.println("shaped");
                    return;
                }
                for(int i = 0; i < e.getInventory().getSize(); i++) {
                    if(e.getInventory().getItem(i) != null) {
                        System.out.println("not null");
                        ItemStack tocheck = null;
                        try {
                         tocheck = ca.getConfig().getItemStack(sets + ".Slot" + i);
                        }catch(NullPointerException npe) {}
                        if(e.getInventory().getItem(i).equals(tocheck)) {
                            System.out.println("equal");
                            ItemStack product = ca.getConfig().getItemStack(sets + ".Product");
                            System.out.println("set product");
                            e.getInventory().setResult(product);
                        }
                    }
                }
            }
    it stopped on shaped
     
  5. ofcourse it will :) because the recipe I put in is shaped so it returns :( I fixed it thanks for help.
     
    • Like Like x 1
  6. Why are you returning inside a loop? I think you want to continue instead so it can finish looping the rest of the set.
     
  7. I figured it out late.
     
  8. @Mr.Midnight sorry for this sudden interruption but is there any alternative methods with this: v
    Code (Java):
    if(e.getInventory().getItem(i).contains(tocheck)) {//to check is itemstack
    // i is 0 to 8
     
  9. You should honestly look into Gson rather than manually serializing stuff. And if you must, can you send me all the current code and what's wrong with the current method?
     
  10. there is no such method as ItemStack#contains
     
  11. I have this kind of code I used Inventory#contains method but it doesn't run the sysout
    Code (Java):
    public void onCraft2(PrepareItemCraftEvent e) {
            if(e.getInventory().getResult() == null) {
                return;
            }
            if(ca.getConfig().getKeys(false) == null) {
                return;
            }
            Set<String> set = ca.getConfig().getKeys(false);
            for(String sets : set) {
                if(!ca.getConfig().getString(sets + ".Type").equals("Shapeless")) {
                    return;
                }
                for(int i = 0; i < e.getInventory().getSize(); i++) {
                    if(e.getInventory().getItem(i) != null) {
                        ItemStack tocheck = null;
                        try {
                         tocheck = ca.getConfig().getItemStack(sets + ".Slot" + i);
                        }catch(NullPointerException npe) {}
                        if(e.getInventory().contains(tocheck)) {
                            System.out.println("contains");
                            ItemStack product = ca.getConfig().getItemStack(sets + ".Product");
                            e.getInventory().setResult(product);
                        }
                    }
                }
            }
        }
    or it is not allowed to have multiple high prioritized event?
     
  12. Probably because the inventory doesn't contain that exact item and amount of that item? Do you just want to check the type?
     
  13. I am checking the ItemMeta actually so I'd rather used ItemStack so it will compare the material type also.
     
  14. I got it working tried to sort all types of recipe to a new yml
    so Shaped.yml
    Shapeless.yml
    Furnace.yml
     
  15. Nice, and remember that Gson suggestion ;)
     
  16. I am going to study about that also, it is my first time to hear that it can used to plugin.
     
  17. It's actually compiled with Spigot, so you don't even need to import a new Library.
     
    • Agree Agree x 1