Interact Event Issue

Discussion in 'Spigot Plugin Development' started by WildDev, May 22, 2015.

  1. Hey, I'm having a little trouble. I'm trying to get it to loop through all my potions that are in my potionC() method. potionIG is the name of the ArrayList.

    @EventHandler(priority=EventPriority.HIGHEST)
    public void onSplash(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    for(ItemStack pot : potionIG) {
    if (p.getItemInHand() == pot)
    e.setCancelled(true);
    p.sendMessage("§cYou may not throw this potion.");
    return;

    Any idea? I'm trying to get pot by it self, but it interacts with all Materials. Thanks
     
  2. You are missing brackets..

    Try:
    Code (Text):

    @EventHandler(priority=EventPriority.HIGHEST)
    public void onSplash(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    for(ItemStack pot : potionIG) {
    if (p.getItemInHand() == pot){
    e.setCancelled(true);
    p.sendMessage("§cYou may not throw this potion.");
    }
    return;
    }
     
     
  3. No, I removed the brackets on purpose so it doens't look all spammy in my post.
     
  4. Please put your code in a 'code' tag.

    What are you exactly trying to do, I don't get it.
    Use .equals() for pot.
     
  5. Small note: you would want to use isSimilar instead of equals. isSimilar does not check the stack size, and through plugins people are still able to get stacked potions.
     
    • Informative Informative x 1
  6. Code (Text):
    public void potionC() {
            ItemStack instantDamageSplash = new ItemStack(Material.POTION, (short)16396);
            ItemStack instantDamageSplashII = new ItemStack(Material.POTION, (short)16428);
            ItemStack poisonSplash = new ItemStack(Material.POTION, (short)16452); // Poison 1:30
            ItemStack poisonSplashII = new ItemStack(Material.POTION, (short)16420);
            ItemStack regenSplash = new ItemStack(Material.POTION, (short)16449); // Regen 1:30
            ItemStack regenSplashII = new ItemStack(Material.POTION, (short)16417);
            ItemStack regRegenSplash = new ItemStack(Material.POTION, (short)16385); // Regen 0:33
            ItemStack regenDrink = new ItemStack(Material.POTION, (short)8193); // Regen Drink 0:45
            ItemStack regenDrink2 = new ItemStack(Material.POTION, (short)8257); // Regen Drink 2:00
            ItemStack regenDrinkII = new ItemStack(Material.POTION, (short)8225);
            ItemStack weakness = new ItemStack(Material.POTION, (short)16456);
            ItemStack drinkStrength = new ItemStack(Material.POTION, (short)8201); // Str 3:00
            ItemStack drinkStrengthII = new ItemStack(Material.POTION, (short)8233); // Str II 1:30
            ItemStack drinkStrengthII2 = new ItemStack(Material.POTION, (short)8297); // Str II 4:00
            ItemStack drinkStrengthReg = new ItemStack(Material.POTION, (short)8265); // Str 8:00
            ItemStack strengthSplash = new ItemStack(Material.POTION, (short)16393); // Str Splash 2:15
            ItemStack strengthSplashII = new ItemStack(Material.POTION, (short)16425); // Str Splash II 1:07
            ItemStack strengthSplashLong = new ItemStack(Material.POTION, (short)16457); // Str Splash 6:00
            ItemStack strengthSplashII2 = new ItemStack(Material.POTION, (short)16489); // Str Splash II 3:00

            potionIG.add(instantDamageSplash);
            potionIG.add(instantDamageSplashII);
            potionIG.add(poisonSplash);
            potionIG.add(poisonSplashII);
            potionIG.add(regenSplash);
            potionIG.add(regenSplashII);
            potionIG.add(regRegenSplash);
            potionIG.add(regenDrink);
            potionIG.add(regenDrink2);
            potionIG.add(regenDrinkII);
            potionIG.add(weakness);
            potionIG.add(drinkStrength);
            potionIG.add(drinkStrengthII);
            potionIG.add(drinkStrengthII2);
            potionIG.add(drinkStrengthReg);
            potionIG.add(strengthSplash);
            potionIG.add(strengthSplashII);
            potionIG.add(strengthSplashLong);
            potionIG.add(strengthSplashII2);

        }
       


        @EventHandler(priority=EventPriority.HIGHEST)
        public void onSplash(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                for(ItemStack pot : potionIG) {
                    if (p.getItemInHand().isSimilar(pot))
                        e.setCancelled(true);
                    p.sendMessage("§cYou may not throw this potion.");
                    return;
                }
        }
       

       
    }
    Just letting you know, I do have the event registered. And my class does implement Listener. Thanks for the suggestions guys!
     
  7. Still need this. Thanks.
     
  8. Looking at your brackets, I'm curious about why you have:
    Code (Text):
    for(ItemStack pot : potionIG) {
         if (p.getItemInHand().isSimilar(pot))
              e.setCancelled(true);
         p.sendMessage("§cYou may not throw this potion.");
         return;
    }
    instead of
    Code (Text):
    for(ItemStack pot : potionIG) {
         if (p.getItemInHand().isSimilar(pot)){
              e.setCancelled(true);
              p.sendMessage("§cYou may not throw this potion.");
              return;
         }
    }
    If you leave the brackets off of an if() statement, it only applies the if() for the next thing after it (which is setCancelled()). Then it always does the sendMessage() and return;

    If you do the return; every time (since it's outside of the if()), you'll only be checking the first item in potionIG.