I need help with this.

Discussion in 'Plugin Development' started by anon_22A, Jan 22, 2021.

  1. Offline

    anon_22A

    So, I've been working on a plugin and I want to make it so that every time the player eats food, a random potion effect is given to the player. I have the random potion effect done but I can't figure out how to make an if statement that's like "if the player eats something, do this" and it's really frustrating. Here's the code that I have:
    Code:
    package com.andrei.plugin1;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener{
    
        @Override
        public void onEnable() {
            System.out.println("PLUGIN ENABLED");
           
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onConsume(PlayerItemConsumeEvent e) {
           
            Player player = e.getPlayer();
            ItemStack consumed = e.getItem();
       
            //here is where i'm confused
           
            int chance = rand.nextInt(27);
            if (chance == 0)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 60, 119));
            }
            else if (chance == 1)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 60, 119));
            }
            else if (chance == 2)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 60, 119));
            }
            else if (chance == 3)
            {   
                player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60, 119));
            }
            else if (chance == 4)
            {   
                player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 60, 119));
            }   
            else if (chance == 5)
            {   
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 60, 119));
            }
            else if (chance == 6)
            {   
                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 60, 119));
            }
            else if (chance == 7)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 60, 119));
            }
            else if (chance == 8)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60, 119));
            }
            else if (chance == 9)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 60, 119));
            }
            else if (chance == 10)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 60, 119));
            }
            else if (chance == 11)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 60, 119));
            }
            else if (chance == 12)
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 60, 119));
            }
           
            return;
            }
          }
        }
     
    Last edited by a moderator: Jan 23, 2021
  2. Offline

    Newdel

    Rather than instantly telling you the solution, I'll show you how I got it.
    I looked up when exactly the event is fired.
    So you have to check if the item consumed is food.
    Google: bukkit check if item is food.
    First link leads me to this forum. Scolling down shows a short code snippet with a isEdible() method on the Material class.
    (Optional in this case: recheck Material#isEdible() in docs)
    If you don't know how to get the material from an itemstack, reread what I just wrote.

    Also you should have a look at switch case. https://www.w3schools.com/java/java_switch.asp
    This will make your code more clear and clean imo. At least in this example.

    Oh and don't do that pls.
    1. Bukkit will log it itself when the plugin started
    2. it doesn't make sense to do it at the beginning of the onEnable method because that's the first line, that the plugin executes and it can still crash later in startup
    3. Bukkit has a Logger for, well, doing logs
     
  3. Offline

    Kars

    Better would be a list/array of potion effects and applying potionEffects[chance]. This would prevent the if/switch statement altogether.
     
    Newdel likes this.
  4. Offline

    anon_22A

    how exactly would i do that? i'm a little bit new to programming in java for minecraft ;w;
     
  5. Offline

    Newdel

    You seriously have to learn how to google. I'm not trying to be rude, it's essential for programmers.
    So let's start this again.
    He said a list or array, so you could google "java array" or "java list"
    I won't give you any solution this time so do this on your own.
    When you know how to declare, initialize, add data to and read data from arrays/lists you can easily solve your problem.
    Initalizing and then adding isn't as pretty as adding values while initializing, you could google "java initialize array with values" and get a oneliner.
    Now, you can set the array/list variable to be "final" as well
     
  6. Offline

    anon_22A

    nevermind, i got it working :D thank you guys, it really helped a lot :)
     

Share This Page