Please Help me! Bug in my Code

Discussion in 'Plugin Development' started by xXblack_lionXx, Sep 4, 2017.

Thread Status:
Not open for further replies.
  1. I coded a plugin which is supposed to be used for trading with other players.
    Whatever here's my code:

    Code:
    @EventHandler
        public void InventoryClickEvent (InventoryClickEvent ev) {
            System.out.println("step 1");
            Location loc = ev.getInventory().getLocation();
            World w = ((Dropper) ev.getClickedInventory()).getWorld();
            Block bl = w.getBlockAt(loc);
            String fckdch = ev.getClickedInventory().getTitle();
            if(fckdch == "container.dropper" ) {
                System.out.println("step2");
                System.out.println(ev.getAction());
                if(ev.getAction() == InventoryAction.PLACE_ALL) {
                    System.out.println("step3");
                    int blx = bl.getX();
                    int bly = bl.getY();
                    int blz = bl.getZ();
                    Block bls = w.getBlockAt(blx, bly, blz - 1);
                    Block blu = w.getBlockAt(blx, bly - 1, blz);
                    if (blu instanceof Chest) {
                        System.out.println("step4");
                        if (bls instanceof Sign) {
                            System.out.println("step5");
                            String cost = ((Sign) bls).getLine(1);
                            String id = ((Sign) bls).getLine(2);
                            String amount = ((Sign) bls).getLine(3);
                            int costint = Integer.parseInt(cost);
                            int amountint = Integer.parseInt(amount);
                            if(ev.getCurrentItem().getType() == Material.DIAMOND){
                                System.out.println("step6");
                                int payment = ev.getCurrentItem().getAmount();
                                int transactions = payment/costint;
                                int paymentdel = transactions * costint;
                                int paymentback = payment - paymentdel;
                                int transactionamount = amountint * transactions;
                                Inventory bluinv = ((Chest) blu).getInventory();
                                boolean accessible = bluinv.contains(Material.DIAMOND, transactionamount);
                                if(accessible == true){
                                    System.out.println("step7");
                                    Inventory blinv = ((Dropper) bl).getInventory();
                                    blinv.clear();
                                    blinv.addItem(new ItemStack(Material.DIAMOND, paymentback));
                                    blinv.addItem(new ItemStack(Material.valueOf(id), transactionamount));
                                    bluinv.removeItem(new ItemStack(Material.valueOf(id),transactionamount));
                                }
    
                            }
                        }
                    }
                }
            }
        }
    I know I'm not a genius with this code and there are thousands better ways to do what I want to do here, but I don't care about that I'm still learning.
    When i tried it on my Server I got this message after triggering the event:
    I can't look what are the 15 more's but I hope you guys can help me anyways!

    Thanks a lot for looking at this and hopefully for answers!

    Ohh and I'm pretty sure the fault is in the first few lines

    Code:
    System.out.println("step 1");
            Location loc = ev.getInventory().getLocation();
            World w = ((Dropper) ev.getClickedInventory()).getWorld();
            Block bl = w.getBlockAt(loc);
            String fckdch = ev.getClickedInventory().getTitle();
     
  2. Offline

    Zombie_Striker

    @xXblack_lionXx

    You need to make sure the inventory instance is an instance of Dropper before casting it to a Dropper. Currently, you are casting it regardless of if it is a dropper.
     
  3. Thanks a lot!!
    Such a stupid mistake xD
     
  4. Offline

    LRK

    Please mark this thread as solved if this fixed you problem.
     
  5. how do I do that?
     
Thread Status:
Not open for further replies.

Share This Page