Solved PlayerItemConsumeEvent issues.

Discussion in 'Plugin Development' started by OTF Catastrophe, Jun 9, 2016.

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

    OTF Catastrophe

    Issue:
    I've been looking for an hour to find a solution that would work but I've tried everything I could find on the internet. I'm having an issue trying to use an else statement with the event iteself.


    Code:
    package me.SirApathy.sheepcraft;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    
    import me.SirApathy.sheepcraft.config.FileManager;
    import me.SirApathy.sheepcraft.Main;
    
    public class ItemListener implements Listener
    {
     
        static String[] instance;
     
        public static String[] getInstance()
        {
       
            return instance;
           
        }
     
        public static Map<String, String> inParkour = new HashMap();
       
        public ItemListener (Main plugin)
        {
           
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
           
        }
       
        @EventHandler (priority=EventPriority.HIGH)
        public void onDrinkEvent(PlayerItemConsumeEvent pice)
        {
           
            String prefix = FileManager.getInstance().getMainConfig().getString("sheepcraft.prefix");
           
            if (pice.getItem() == null)
            {
               
                return;
                       
            }
           
            if (Main.players.containsKey(pice.getPlayer().getName()))
            {
           
                if ((pice.getItem().getType().equals(Material.POTION)))
                {
                   
                    if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.TRUE))
                   
                        pice.setCancelled(true);
                        pice.getPlayer().sendMessage(prefix.replace("&", "§") + "§cPotions are disabled in the parkour!");
                   
               
                    if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.FALSE))
    
                   
                        return;
                   
                }
               
            }
       
        }
       
    }
    
    As you can see, I try to keep my code clean, so hopefully it's easy for people to understand. I'm trying to make it so when a player drinks a potion of any type while in parkour it will be disabled or not disabled depending on if the config setting disablePotion is set to false or true. I'm using a HashMap to basically detect if a player is in the parkour or not. I managed to get it so you can drink any potion outside of parkour, but when you get in parkour, whether the disablePotions setting is set to true or false, it still blocks it no matter what. Is there any solution to fixing this issue? I'll happily take any suggestions thank you!
     
  2. Offline

    N00BHUN73R

    @OTF Catastrophe
    Change this:
    Code:
     if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.TRUE))
                  
                        pice.setCancelled(true);
                        pice.getPlayer().sendMessage(prefix.replace("&", "§") + "§cPotions are disabled in the parkour!");
    To This:
    Code:
     if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.TRUE)) {
                  
                        pice.setCancelled(true);
                        pice.getPlayer().sendMessage(prefix.replace("&", "§") + "§cPotions are disabled in the parkour!");
    }
    Notice the brackets you need in order to do that.
     
  3. Offline

    OTF Catastrophe

    I've tried many many different solutions, more than I can really list. But that was one that hasn't worked for me. I tried putting both the if statements in there own brackets but with no success. Just to confirm that I did what you said, this is the event snippet of the code.


    Code:
    (More up here.)
        @EventHandler (priority=EventPriority.HIGH)
        public void onDrinkEvent(PlayerItemConsumeEvent pice)
        {
           
            String prefix = FileManager.getInstance().getMainConfig().getString("sheepcraft.prefix");
           
            if (pice.getItem() == null)
            {
               
                return;
                       
            }
           
            if (Main.players.containsKey(pice.getPlayer().getName()))
            {
           
                if ((pice.getItem().getType().equals(Material.POTION)))
                {
                   
                    if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.TRUE))
                    {
                   
                        pice.setCancelled(true);
                        pice.getPlayer().sendMessage(prefix.replace("&", "§") + "§cPotions are disabled in the parkour!");
                       
                    }
                               
                    if (FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.FALSE))
                       
                        return;
                   
                }
               
            }
       
        }
       
    }
    
     
  4. Offline

    Lordloss

    Code:
    FileManager.getInstance().getMainConfig().getBoolean("disablePotions", Boolean.TRUE)
    Im pretty sure this is your issue. Simply remove the second argument, Boolean.TRUE. With getBoolean the config API allready knows that you want a bool from the config. And the if statement knows also that it is a boolean ;)

    EDIT: Sorry i have to correct myself. The second argument you put in is the default value if the configuration section you check for is not found.
     
  5. Offline

    OTF Catastrophe

    Ahh thank you a bunch. I tend to overthink a lot when I get into the code. I was using disablePotions when I should have put sheepcraft.disablePotion. Thank you @Lordloss .
     
    Lordloss likes this.
Thread Status:
Not open for further replies.

Share This Page