Search code examples
javapluginscommandminecraftbukkit

How to get player from command in MC Bukkit (Java plugin)


I'm trying to "infect" 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 "/infect ". 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:


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("Pandemic has been activated...");
        PluginManager pm = getServer().getPluginManager();
        firstListener listener = new firstListener(this);
        
        pm.registerEvents(listener, this);
        
        }
    
    public void onDisable(){
        
        getLogger().info("Pandemic has shut down!");
        
        }
    
    
    
    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 "infect":
                
                firstListener listener = new firstListener(this);
                
                listener.infection();
                
                return true;
                
            default:
                
                player.sendMessage("Ese comando no existe");
                
                return true;
                        
            
            }
            
            
        }
    
        player.sendMessage("Ese comando no existe");
        
        return true;
        
    }
    
    

    
    

}

I have the "infection" function in my listener.jar:


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("Plugin"), 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+ "Has sido lamentablemente infectado por coronavirus");
                        
                        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("yo");
                    
                    selectedPlayer.sendMessage(ChatColor.RED+ "Has sido lamentablemente infectado por coronavirus");
                    
                    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) {
        
    }

}

Thanks!


Solution

  • Do you want to infect a random player once the /infect is fired? or just infect the command sender?

    Here is the code:

    public class infect implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) {
            
            if(sender instanceof Player) {
                Player p = (Player) sender;
                
                // Infect command sender
                if(p.hasPermission("infect")) {
                    
                    /*
                     * Call event here
                     * Like add potion effect
                     */
                    
                }
                
                // Infect target player
                if(p.hasPermission("infect")) {
                    if(args.length >= 1) {
                        try {
                            Player target = Bukkit.getPlayer(args[0]);
                            
                            if(target.isOnline()) {
                                /*
                                * Call event here
                                * Like add potion effect
                                */
                            }
                            
                        } catch (Exception e) {
                            
                        }
                    }
                }
                
                // Infect random player
                if(p.hasPermission("infect")) {
                    int r = new Random().nextInt(Bukkit.getOnlinePlayers().size());
                    
                    List<Player> isO = new ArrayList<Player>();
                    
                    for (Player on : Bukkit.getOnlinePlayers()) {
                        isO.add(on);
                    }
                    
                    if(isO.get(r) != null) {
                        /*
                         * Call event here
                         * Like add potion effect
                         */
                    }
                }
            }
            return true;
        }
    }