Problem with "getPlayer(args[0])" Need Help ========================================================

Discussion in 'Plugin Development' started by 08jne01, Jun 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    08jne01

    I am making a fakeOp plugin for those annoying people who say they are from planet minecraft but i have an error under .getPlayer(args[0]) And i can't solve it.
    Code:
            public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args){
                Player player = (Player)sender;
                Player target = player.getWorld().getPlayer(args[0]);
                if(commandLabel.equalsIgnoreCase("FakeOp")) {
                    target.sendMessage(ChatColor.YELLOW + "You are now op!");
                }
                return false;
         
                }
     
  2. theres n getPlayer() at World
     
  3. Offline

    08jne01

    What do you mean?
     
  4. Offline

    Mitsugaru

    Wouldn't it be more around the lines of:

    Code:
    Player target = plugin.getServer().getPlayer(name);
    if(target != null && target.isOnline())
    {
      //do stuff
    }
     
  5. You cant just assume the player has entered the parameter you are searching for, wrap your code in an args check
    Code:
    if(args.length == 1){}
    So...
    Code:
      public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args){
               if(!(sender instanceof Player){
                    return true;
               }           
               Player player = (Player)sender;
                if(args.length == 1){
                Player target = player.getWorld().getPlayer(args[0]);
                if(commandLabel.equalsIgnoreCase("FakeOp")) {
                    target.sendMessage(ChatColor.YELLOW + "You are now op!");
                }
                return false;
    }
       
                }
     
  6. Offline

    08jne01

    None of that fixes my error the error is under the .getPlayer(args[0]); and it says to remove the args[0]
     
  7. Offline

    Mitsugaru

    Well, yeah, I just wanted to solve his problem of getting the player. The thing is that his code is automatically assuming that the target player is in the same world as the command sender, which isn't always going to true.

    Also, doing the instanceof Player check makes it so that the command cannot be issued as console, when it should be possible regardless if the command was issued from console or player.

    Which is why I had posted that he use his plugin instance to grab the server and attempt to get the player that way, rather than through the world.
     
  8. Offline

    08jne01

    Ok now how do i solve the problem of getting the player from args[0]
     
  9. Offline

    Mitsugaru

    Well, that was already answered:
    The only method from World is getPlayers(), which doesn't have any arguments. You need to change your approach, and I offered one possible solution:
    Code:
    Player target = plugin.getServer().getPlayer(name);
    Where, "name" would be replaced by args[0], provided it was given. "plugin" is an instance of your plugin class that inherits JavaPlugin (and anything else that you have).
     
  10. Offline

    08jne01

    what is the variable 'plugin'
     
  11. Offline

    Gravity

    Were all those equal signs necessary?
     
    Mitsugaru likes this.
  12. Offline

    Mitsugaru

    It's your plugin...?

    Here, try this instead:
    Code:
    Player target = Bukkit.getServer().getPlayer(name);
    Should work relatively the same... in theory.
     
  13. Offline

    08jne01

    Sorry i am reletivly new to Java But thank you

    Sorry but it still doesn't work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  14. Yeah sorry, we posted at the same time so I didn't see your post until after I'd posted
     
  15. Offline

    Mitsugaru

    What doesn't work? Can you post an updated code snippet? And describe the issue.
     
  16. And post the error you keep saying you have.
     
  17. Try

    Bukkit.getPlayer(args[0]);
     
  18. Offline

    08jne01

    Code:
            public boolean onCommand1(CommandSender sender, Command cmd, String commandLabel, String[] args){
                if(!(sender instanceof Player)){
                    return true;
                }
                if(args.length == 1){
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if(commandLabel.equalsIgnoreCase("FakeOp")) {
                    target.sendMessage(ChatColor.YELLOW + "You are now op!");
                }
             
                }
                return false;
            }
    This is what i have but it doesn't do anything when i use the command and a player name. Now i get no error in eclipse but it doesn't work
    also in the server it gave this error when i used the command from the console:

    Code:
    2012-06-09 23:00:50 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'fakeop' in plugin FlyPlugin v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
    at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:475)
    at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.jne.FlyPlugin.FlyPlugin.onCommand(FlyPlugin.java:30)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 7 more
    There was no server error when i typed it in game but nothing happened.
     
  19. Offline

    Mitsugaru

    The following class works for me. Tested it on my dev server. Made a few changes, such as name auto-complete and making the message look more like the actual op message. Tested through both player and console sending the command.
    Code:
    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.java.JavaPlugin;
     
    public class FakeOp extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            // Register command
            getCommand("fakeop").setExecutor(this);
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args)
        {
            if (!label.equalsIgnoreCase("fakeop"))
            {
                return false;
            }
            if (args.length == 1)
            {
                String name = expandName(args[0]);
                if (name != null)
                {
                    Player target = Bukkit.getServer().getPlayer(name);
                    if (target != null && target.isOnline())
                    {
                        target.sendMessage(ChatColor.GRAY + "(" + sender.getName()
                                + ": Opping " + target.getName() + ")");
                    }
                    return true;
                }
                else
                {
                    sender.sendMessage("Unknown player: " + args[0]);
                }
            }
            return false;
        }
     
        /**
        * Attempts to look up full name based on who's on the server Given a
        * partial name
        *
        * @author Frigid, edited by Raphfrk and petteyg359
        */
        public String expandName(String Name)
        {
            int m = 0;
            String Result = "";
            for (int n = 0; n < Bukkit.getServer().getOnlinePlayers().length; n++)
            {
                String str = Bukkit.getServer().getOnlinePlayers()[n].getName();
                if (str.matches("(?i).*" + Name + ".*"))
                {
                    m++;
                    Result = str;
                    if (m == 2)
                    {
                        return null;
                    }
                }
                if (str.equalsIgnoreCase(Name))
                    return str;
            }
            if (m == 1)
                return Result;
            if (m > 1)
            {
                return null;
            }
            return Name;
        }
    }
     
  20. The error says:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.jne.FlyPlugin.FlyPlugin.onCommand(FlyPlugin.java:30)

    Problem is as FlyPlugin.java at line 30, have a look there :)

    However, you say that code doesn't do anything, must be because it's named onCommand1, Bukkit will only call "onCommand" from a command executor.
    If you want a class for each command you can use getCommand("cmdname").setExecutor(new YourCmdExecutor()); and in that class you can have an onCommand() method that'll only trigger for cmdname so you don't have to filter out commands.
     
  21. Offline

    08jne01

    Thank you. I have learned much!
     
  22. Offline

    Kenazi

    I don't know if you have figured it out yet, but if you haven't, try this instead.
    The error is that the world cant get a player, but the server can. (its at least my theory)

    This is how I would do it, but note that im not a pro guy, so it may be incorrect.

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){

    Player player = (Player) sender;
    Player target = player.getServer().getPlayer(args[0]);

    if(commandLabel.equalsIgnoreCase("FakeOp")) {
    target.sendMessage(ChatColor.YELLOW + "You are now op!");
    }
    return true;
    }
     
  23. Kenazi, that's not it because the error is not within any code he posted because that code didn't trigger at all due to:
    The error is in his FlyPlugin.java at line 30, which I have no clue what's there.
     
  24. Offline

    Technius

    I strongly suggest telling the sender that you sent the message to the target...
     
  25. commandLabel.equalsIgnoreCase("FakeOp")
    must be
    cmd.getName().equalsIgnoreCase("FakeOp")


    Read this: http://forums.bukkit.org/threads/co...ouldnt-just-return-if-instanceof-player.3186/
     
Thread Status:
Not open for further replies.

Share This Page