How to register an event as done while actually cancelling it?

Discussion in 'Spigot Plugin Development' started by Flockshot, May 31, 2017.

  1. Hey guys, i need help in making a plugin of custom eatables. The problem i am having is that the player cannot continuously eat an eatable because once he starts eating it the event is cancelled and so all the custom things happens but if the player does not let go of the right click, He will still be in the event but it is already cancelled so when it tries to eat another bite it won't work.
    I customly decrease the amount from player's inventory and with that all other stuff.
    I tried enabling it in the end but then the vanilla aspect of the eatable will come in effect therefore i need a way to register to the server that PlayerItemConsumeEvent for that player is completed so that player could continuously eat.
    I tried return; but it didn't work.
    Can anyone help me with this? Any Help will be appreciated
     
  2. I'm happy to help but I'll need to see the code
     
  3. Here ya go
    Code (Text):
    @SuppressWarnings("deprecation")
    @EventHandler
    public void onEat(PlayerItemConsumeEvent event) throws IOException
    {
       
        ItemStack plritem = event.getItem();
        String name = event.getItem().getItemMeta().getDisplayName();
        org.bukkit.entity.Player plr = event.getPlayer();

       
       
        if(Main.Apples.containsKey(name))
        {
           
            event.setCancelled(true);
            FileConfiguration c = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder()+"/Eatables", Main.Apples.get(name)+".yml"));
            String path = Main.Apples.get(name);
            path = path.replace(".yml", "");
           
            List<String> lore = plritem.getItemMeta().getLore();
            List<?> clore = c.getList(path+".Lore");

            Iterator<?> its = clore.iterator();
            List<String> tlore = new ArrayList<String>();
            String cur;
            while(its.hasNext())
            {
                cur = ChatColor.translateAlternateColorCodes('&', (String) its.next());
                tlore.add(cur);
            }
           
           
           
            if((plritem.getItemMeta().getLore().isEmpty() && c.getString(path+".Lore").isEmpty()) || isSame(lore, tlore))
            {          
           
                int food = c.getInt(path+".Food");
                double health = c.getDouble(path+".Health");
               
                if(food>0)
                {
                    food = food+plr.getFoodLevel();
                    if(food>20)
                    {                  
                        plr.setFoodLevel(20);
                    }
                    else
                    {
                        plr.setFoodLevel(food);
                    }
                }
               
                if(health>0)
                {
                    health += plr.getHealth();
                    if(health>plr.getMaxHealth())
                    {
                        plr.setHealth(plr.getMaxHealth());
                    }
                    else
                    {
                        plr.setHealth(health);
                    }
                }
               
                if(plritem.getAmount()<=1)
                {
                    if(Bukkit.getVersion().contains("1.7") || Bukkit.getVersion().contains("1.8"))
                    {
                        plr.getInventory().remove(plritem);                      
                    }
                    else if(Bukkit.getVersion().contains("1.9") || Bukkit.getVersion().contains("1.10") || Bukkit.getVersion().contains("1.11") || Bukkit.getVersion().contains("1.12"))
                    {
                       
                        if(plr.getInventory().getItemInMainHand().equals(plritem))
                        {
                            plr.getInventory().remove(plr.getInventory().getItemInMainHand());
                        }
                        else if(plr.getInventory().getItemInOffHand().equals(plritem))
                        {
                            plr.getEquipment().setItemInOffHand(null);
                        }
                    }
                    //plr.getInventory().remove(plritem);
                   
                }
                else
                {
                    int amountofitem = plritem.getAmount()-1;
                   
                   
                    if(Bukkit.getVersion().contains("1.7") || Bukkit.getVersion().contains("1.8"))
                    {
                        plr.getItemInHand().setAmount(amountofitem);                  
                    }
                    else if(Bukkit.getVersion().contains("1.9") || Bukkit.getVersion().contains("1.10") || Bukkit.getVersion().contains("1.11") || Bukkit.getVersion().contains("1.12"))
                    {
                       
                        if(plr.getInventory().getItemInMainHand().equals(plritem))
                        {
                            plr.getInventory().getItemInMainHand().setAmount(amountofitem);
                        }
                        else if(plr.getInventory().getItemInOffHand().equals(plritem))
                        {
                            plr.getInventory().getItemInOffHand().setAmount(amountofitem);                          
                        }                  
                    }          
                }          
            }      
            return;
        }
       
       
    }
     
  4. Mas

    Mas

    I hate to be that guy, but there are quite a few things you could do to improve your code:
    • Don't try and declare a listener with a "throws" declaration, use try/catch blocks to handle exceptions.
    • You never check if the item used in the event has item meta, or whether it has a display name (ItemStack#hasItemMeta and ItemMeta#hasDisplayName).
    • You can just import Player from org.bukkit.entity, no need to do it the long way.
    • Static abuse - referencing "Apples" (which should be apples (lowerCamelCase for fields)) through a static instance instead of passing an instance of the main class to the events class. Try and learn the basics of Object Oriented Programming: https://docs.oracle.com/javase/tutorial/java/javaOO/
    • Using hard coded file separator "/" instead of constant File.separator.
    • Declaring List<?> where List<String> can be used.
    • Unnecessary return statement at the end of method.
     
    • Agree Agree x 1
  5. So to be the guy that actually helps.... :p Easyest way I can think of is dont cancel the event, instead manually remove the vanilla effects of the food (food points, potion effects). Then you add your custom stuff, should work normally.
     
  6. FWIW I believe Java will automatically translate / on Windows machines.

    I could be wrong, but it seems to do that for me (unless all the Windows people using my plugin have just been real silent about how it doesn't work at all... :D)
     
  7. konsolas

    Supporter

    Windows accepts both, but in the interests of portability (somebody makes an operating system that only accepts '\' as the name seperator and compiles a jre for it), File.seperator is "better".

    Notably, linux uses only ':' as the path seperator whereas Windows accepts ';' - this has led to many a class loading issue for those who hardcode classloaders with ';'.
     
    • Informative Informative x 2