Solved InteractEvent Problem

Discussion in 'Plugin Development' started by xxPatterson, Jul 29, 2015.

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

    xxPatterson

    Code:
    @EventHandler
        public boolean PlayerUse(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Sign s = (Sign) e.getClickedBlock().getState();
            int coal = getAmount(e.getPlayer(),263);
            ItemStack coalI = new ItemStack(Material.COAL);
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getClickedBlock().getState() instanceof Sign) {
                if (s.getLine(2).equalsIgnoreCase("§cSell Coa1l"));
                if (coal == 0);
                p.sendMessage("§b[§rRevengePrison§b]§cYou don't have any coal!");
                return true;
                }
                if (coal > 0);
                Main.econ.depositPlayer(p.getName(), coal*500);
                p.getInventory().remove(Material.COAL);
                p.sendMessage("§b[§rRevengePrison§b]§a $" + coal*500 + " added to your account!");
                return true;
                }
            return false;
    
                    }
    The issue I am having is that it only says "You don't have any coal!" even if player has coal in their inventory
     
  2. Offline

    schwabfl

    can we see the getAmount() method?
     
  3. Offline

    xxPatterson

    Code:
        @EventHandler
        public void PlayerUse(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Sign s = (Sign) e.getClickedBlock().getState();
            ItemStack coalI = new ItemStack(Material.COAL, 64);
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (e.getClickedBlock().getState() instanceof Sign) {
                if (s.getLine(2).equalsIgnoreCase("§cSell Coa1l"));
                if (p.getInventory().contains(Material.COAL));
                Main.econ.depositPlayer(p.getName(), 32000);
                p.getInventory().remove(coalI);
                p.sendMessage("§b[§rRevengePrison§b]§a $32,000 added to your account!");
                }
            }
    
                    }
    I tried it with this so it would sell in stacks of 64 but it still gives player 32k even if they don't have any coal

    This also gives play 32k no matter what is on the sign

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  4. Offline

    Zombie_Striker

    This is why it's always 32K

    Fix these lines

    The way to check the amount of coal is to use the following:
    Code:
    int wantedamount = 64;
    
    int amount = 0;
    ItemStack[] tempItemstacks = new ItemStack[32];
    ItemStack leftover= null;
    int amountleftover = 0;
    for(ItemStack i : player#getInventory().getContents()){
    if(amount >= wantedamount){
    break;
    }
    if(i != null && i.getType() == Material.Coal){
    if(amount + i.getAmount() >= wantedamount){
    leftover = i;
    amountleftover = wantedamount-amount;
    amount += amountleftover;
    }else{
    amount += i.getAmount();
    }
    }
    
    for(int i = 0; i < tempItemStacks.lengh;i++){
    Player#getInventory().remove(tempItemstacks[i]);
    }
    if(leftover != null){
    leftover.setAmount(leftover.getAmount()-amountleftover);
    }
    //either Player#updateInventory() or Player#getInventory().update();
    
     
  5. Offline

    schwabfl

    Code:
    if (s.getLine(2).equalsIgnoreCase("§cSell Coa1l"));
    if (p.getInventory().contains(Material.COAL));
    
    if you put a semi colon after an if statement, you could just remove the entire line.
    Learn the basic java syntax and you'll see why your code doesn't run as wanted
     
Thread Status:
Not open for further replies.

Share This Page