title: How to get player from command in MC Bukkit (Java plugin)
body: <p>I'm trying to &quot;infect&quot; someone with a virus in my minecraft world with a plugin. I have it already done and also it works randomly, like I wanted, but I also want to infect people with a command (like &quot;/infect &quot;. I've been searching for solutions hours and hours but I gave up, so I would like to ask some help. Here's my Main.jar from the plugin:</p>
  <pre><code>
  import java.util.Random;
  
  import java.lang.reflect.Method;  
  
  import org.bukkit.Bukkit;
  import org.bukkit.ChatColor;
  import org.bukkit.command.Command;
  import org.bukkit.command.CommandSender;
  import org.bukkit.entity.Player;
  import org.bukkit.plugin.PluginManager;
  import org.bukkit.plugin.java.JavaPlugin;
  import org.bukkit.potion.PotionEffect;
  import org.bukkit.potion.PotionEffectType;
  
  public class Main extends JavaPlugin{
      
      @Override
      
      
      public void onEnable(){
          
          getLogger().info(&quot;Pandemic has been activated...&quot;);
          PluginManager pm = getServer().getPluginManager();
          firstListener listener = new firstListener(this);
          
          pm.registerEvents(listener, this);
          
          }
      
      public void onDisable(){
          
          getLogger().info(&quot;Pandemic has shut down!&quot;);
          
          }
      
      
      
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          
          Player player = (Player) sender;
          
          
          if (sender instanceof Player) {
              
              String lowerCmd = cmd.getName().toLowerCase();
              
              switch (lowerCmd) {
                  
              case &quot;infect&quot;:
                  
                  firstListener listener = new firstListener(this);
                  
                  listener.infection();
                  
                  return true;
                  
              default:
                  
                  player.sendMessage(&quot;Ese comando no existe&quot;);
                  
                  return true;
                          
              
              }
              
              
          }
      
          player.sendMessage(&quot;Ese comando no existe&quot;);
          
          return true;
          
      }
      
      
  
      
      
  
  }
  </code></pre>
  <p>I have the &quot;infection&quot; function in my listener.jar:</p>
  <pre><code>
  import java.util.Random; 
  import org.bukkit.Bukkit;
  import org.bukkit.ChatColor;
  import org.bukkit.entity.Player;
  import org.bukkit.event.EventHandler;
  import org.bukkit.event.Listener;
  import org.bukkit.event.player.PlayerJoinEvent;
  import org.bukkit.potion.PotionEffect;
  import org.bukkit.potion.PotionEffectType;
  
  public class firstListener implements Listener {
      
      //Constructor
      public firstListener(Main plugin) {
          
          Bukkit.getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin(&quot;Plugin&quot;), new Runnable() {
               
              @Override
              public void run() {
                 
                  Random rand = new Random(); 
                  
                  // Generate random integers in range 0 to 999 
                  int rand_int1 = rand.nextInt(31); 
                  
                  if (rand_int1 == 30) {
                      
                          Player randomPlayer = Bukkit.getOnlinePlayers().stream().skip((int) (Bukkit.getOnlinePlayers().size() * Math.random())).findFirst().orElse(null);
                          
                          randomPlayer.sendMessage(ChatColor.RED+ &quot;Has sido lamentablemente infectado por coronavirus&quot;);
                          
                          randomPlayer.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS,18000, 1));
                          
                          randomPlayer.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,18000, 1));
                          
                          randomPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,18000, 1));
                      
                          
                 
                      }
                  
              }
                  
           
          }, 1L , (long) 9000 * 20);
          
      }
          
          public void infection()
          
          {
              
                  
                      Player selectedPlayer = Bukkit.getPlayer(&quot;yo&quot;);
                      
                      selectedPlayer.sendMessage(ChatColor.RED+ &quot;Has sido lamentablemente infectado por coronavirus&quot;);
                      
                      selectedPlayer.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS,18000, 1));
                      
                      selectedPlayer.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,18000, 1));
                      
                      selectedPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,18000, 1));
              
          }
          
          
          
          
      
      
      //EventHandler
      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent event) {
          
      }
  
  }
  </code></pre>
  <p>Thanks!</p>