Doing a random

Discussion in 'Spigot Plugin Development' started by xUbalubex, Jun 1, 2018.

  1. Code (Text):
    public void InfectByMob(Player p, EntityDamageByEntityEvent e , int InfectByMobHigh, int InfectByMobLow)
        {
            InfectByMobHigh = this.getConfig().getInt("InfectChanceHigh");
            InfectByMobLow = this.getConfig().getInt("InfectChanceLow");
           
            double chance = (Math.random()* InfectByMobHigh) + InfectByMobLow;
            if(chance > InfectByMobHigh) {
                chance = InfectByMobHigh;
            }
            if(e.getEntity() instanceof Player)
            {
                if(Zombies = true)
                {
                    if(e.getDamager() instanceof Zombie)
                    {
                        if(chance == ZombieChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(Silverfish = true)
                {
                    if(e.getDamager() instanceof Silverfish)
                    {
                        if(chance == SilverfishChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(Spider = true)
                {
                    if(e.getDamager() instanceof Spider)
                    {
                        if(chance == SpiderChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(e.getDamager() instanceof Player)
                {
                    Entity Attacker = e.getDamager();
                    Entity Victim = e.getEntity();
                   
                    if(chance == PlayerChance)
                    {
                        InfectPlayer(p);
                        Attacker.sendMessage(ChatColor.GREEN + "You infected " + Victim);
                    }
                }
            }
           
        }
    This is my code. I am trying to infect a player if they get hit by a zombie, spider, silverfish, or a player. But it wont work.
    Heres the plugin yml
    https://pastebin.com/AuU5b3Eq
     
  2. Code (Text):
    public void InfectByMob(EntityDamageByEntityEvent e)
        {
    Code (Text):
    public void InfectByMob(Player p, EntityDamageByEntityEvent e , int InfectByMobHigh, int InfectByMobLow)
        {
    First of all your class should implements listener, add @EventHandler before your event and register it. Also remember that you cannot do something like this:
    Code (Text):
    public void InfectByMob(Player p, EntityDamageByEntityEvent e , int InfectByMobHigh, int InfectByMobLow)
        {
    But you must do this:
    Code (Text):
    public void InfectByMob(EntityDamageByEntityEvent e)
        {
     
  3. You should handle them using listeners. If you have register listeners then could you please send the code of the listener
     
  4. We can make the method that handles the EntityDamageByEntityEvent and get the player and the InfectByMobHigh and low and call the method using those parameters.
     
  5. Code (Text):
    package sm.uba;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Silverfish;
    import org.bukkit.entity.Zombie;
    import org.bukkit.entity.Spider;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class main extends JavaPlugin implements Listener
    {

        //Symptoms
        boolean Nasua = true;
        boolean Hunger = true;
        boolean Weakness = true;
        boolean Slowness = true;
        boolean Unlucky = true;
        boolean DeathCause = true;
       
        int DiseaseStrength;
       
        //Infect Cause Mobs
        boolean Zombies = true;
        boolean Silverfish = true;
        boolean Spider = true;
       
        int ZombieChance;
        int SilverfishChance;
        int SpiderChance;
        int PlayerChance;
       
        int InfectByMobHigh;
        int InfectByMobLow;
       
        public void onEnable()
        {
            Zombies = this.getConfig().getBoolean("Zombies");
            Silverfish = this.getConfig().getBoolean("Silverfish");
            Spider = this.getConfig().getBoolean("Spider");
           
            ZombieChance = this.getConfig().getInt("ZombieChance");
            SilverfishChance = this.getConfig().getInt("SilverfishChance");
            SpiderChance = this.getConfig().getInt("SpiderChance");
            PlayerChance = this.getConfig().getInt("PlayerChance");
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
           
        }
       
        public void InfectByMob(Player p, EntityDamageByEntityEvent e , int InfectByMobHigh, int InfectByMobLow)
        {
            InfectByMobHigh = this.getConfig().getInt("InfectChanceHigh");
            InfectByMobLow = this.getConfig().getInt("InfectChanceLow");
           
            double chance = (Math.random()* InfectByMobHigh) + InfectByMobLow;
            if(chance > InfectByMobHigh) {
                chance = InfectByMobHigh;
            }
            if(e.getEntity() instanceof Player)
            {
                if(Zombies = true)
                {
                    if(e.getDamager() instanceof Zombie)
                    {
                        if(chance == ZombieChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(Silverfish = true)
                {
                    if(e.getDamager() instanceof Silverfish)
                    {
                        if(chance == SilverfishChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(Spider = true)
                {
                    if(e.getDamager() instanceof Spider)
                    {
                        if(chance == SpiderChance)
                        {
                            InfectPlayer(p);
                        }
                    }
                }
                if(e.getDamager() instanceof Player)
                {
                    Entity Attacker = e.getDamager();
                    Entity Victim = e.getEntity();
                   
                    if(chance == PlayerChance)
                    {
                        InfectPlayer(p);
                        Attacker.sendMessage(ChatColor.GREEN + "You infected " + Victim);
                    }
                }
            }
           
        }
       
        public void InfectPlayer(Player p)
        {
            Nasua = this.getConfig().getBoolean("Nasua");
            Hunger = this.getConfig().getBoolean("Hunger");
            Weakness = this.getConfig().getBoolean("Weak");
            Slowness = this.getConfig().getBoolean("Slow");
            Unlucky = this.getConfig().getBoolean("Unluck");
            DeathCause = this.getConfig().getBoolean("DeathCause");
           
            DiseaseStrength = this.getConfig().getInt("DiseaseStrength");
           
            Location location = p.getLocation();
           
            p.sendMessage(ChatColor.RED + this.getConfig().getString("InfectedMessage"));
            p.playSound(location, Sound.ENTITY_ENDERDRAGON_GROWL, 3.0F, 0.5F);
           
            if(Nasua == true){
                p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 1000000, DiseaseStrength));
                if(Hunger == true) {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 1000000, DiseaseStrength));
                    if(Weakness == true) {
                        p.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 1000000, DiseaseStrength));
                        if(Slowness == true) {
                            p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 1000000, DiseaseStrength));
                            if(Unlucky == true) {
                                p.addPotionEffect(new PotionEffect(PotionEffectType.UNLUCK, 1000000, DiseaseStrength));
                                if(DeathCause == true) {
                                    p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 1000000, DiseaseStrength));
                                }
                            }
                        }
                    }
                }
            }
        }
        public void Cure(Player p)
        {
            p.removePotionEffect(PotionEffectType.HUNGER);
            p.removePotionEffect(PotionEffectType.CONFUSION);
            p.removePotionEffect(PotionEffectType.WEAKNESS);
            p.removePotionEffect(PotionEffectType.SLOW);
            p.removePotionEffect(PotionEffectType.UNLUCK);
            p.removePotionEffect(PotionEffectType.WITHER);
           
            p.sendMessage(ChatColor.BLUE + "You have been cured!");
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            if (cmd.getName().equalsIgnoreCase("infect") && sender instanceof Player) {

            Player player = (Player) sender;

            //Arg0 = first word entered after command, Arg1 = second word, etc.
            int length = args.length;
           
            if (length == 1) {
               
                boolean playerFound = false;
               
               
                for (Player playerToInfect : Bukkit.getServer().getOnlinePlayers()) {
                    if(playerToInfect.getName().equalsIgnoreCase(args[0])) {
                        InfectPlayer(playerToInfect);
                        player.sendMessage(ChatColor.GREEN +  playerToInfect.getName() + " was infected!");
                        playerFound = true;
                        break;
                    }
                }
               
                if (playerFound == false) {
                    player.sendMessage(ChatColor.RED + args[0] + " was not found");
                    return true;
                }
               
            } else player.sendMessage(ChatColor.RED + "Incorrect Arguements! :(");
           
            return true;

        }
            if(cmd.getName().equalsIgnoreCase("Cure"))
            {
                Player player = (Player) sender;

                //Arg0 = first word entered after command, Arg1 = second word, etc.
                int length = args.length;
               
                if (length == 1) {
                   
                    boolean playerFound = false;
                   
                   
                    for (Player playerToCure : Bukkit.getServer().getOnlinePlayers()) {
                        if(playerToCure.getName().equalsIgnoreCase(args[0])) {
                            Cure(playerToCure);
                            player.sendMessage(ChatColor.GREEN +  playerToCure.getName() + " was cured!");
                            playerFound = true;
                            break;
                        }
                    }
                   
                    if (playerFound == false) {
                        player.sendMessage(ChatColor.RED + args[0] + " was not found");
                        return true;
                    }
                   
                } else player.sendMessage(ChatColor.RED + "Incorrect Arguements! :(");
               
                return true;
            }
            return false;
           
        }
        public void SpreadDisease(EntityDamageByEntityEvent e)
        {
            SpreadDisease(e);
        }
    }
     
    This is my whole class
    Code (Text):
        public void SpreadDisease(EntityDamageByEntityEvent e)
        {
            SpreadDisease(e);
        }
    This is the listener
     

  6. Maybe this will fix your problem.
    Add this method to your class. I hope it will work.

    Code (Java):

        @EventHandler // Register this method as a function in the class
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
            System.out.println("Damage Event fired!"); // just to debug you can remove it. it just only check if your method is working or no.
            Entity entity = e.getEntity(); // the entity which got damage
            if(entity.getType() != EntityType.PLAYER) return; // if the entity is not player then cancel the method
            Player p = (Player) e.getEntity();
            int infectByMobHigh = 0; // get from the config
            int infectByMobLow = 0; // get from the config
            InfectByMob(p, e, infectByMobHigh, infectByMobLow);
        }

     
  7. I am back from school. So I did it. and the console said [18:44:32] [Server thread/INFO]: Damage Event fired! which is a good thing. But I wont get infected. Is It because the int of mob high and low is 0 and it won't get any number?

    I just checked if that was the case but no.
     
  8. Nevermind! It did work! I tried changing the high and low values from the config to guarentee I would get infected and it worked! Thanks! And Extra thanks for the notes!
     
  9. No problem. I'm glad that I resolved your problem.
    Just make sure to make this post to be "Solved" so everyone will see that everything is okay here.
     
  10. Before marking as solved, I'd like to remark that you're doing a concern more than doing a random.

    Code (Text):
    public void InfectByMob(Player p, EntityDamageByEntityEvent e , int InfectByMobHigh, int InfectByMobLow)
    Method names and variable names should be in lowerCamelCase as per the Java Naming Convention.

    Code (Text):
    if(Zombies = true)
    Code (Text):
    if(Silverfish = true)
    Code (Text):
    if(Spider = true)
    These will always return true because you are setting the variable. You should be using the equality operator(==) instead of the assignment operator.

    Code (Text):
    if(chance > InfectByMobHigh) {
                chance = InfectByMobHigh;
            }
    Consistency issues. You should only really follow one style of coding, i.e. placement of braces. This may be subtle, but it foes affect the readbility of it.

    Code (Text):
    if(e.getDamager() instanceof Player)
                {
                    Entity Attacker = e.getDamager();
    DRY principle, i.e. Don't Repeat Yourself. Set a variable for the Entity before the conditional.

    These issues follows throughout your entire class.