Returning sender's info not player's?

Discussion in 'Plugin Development' started by WALK2222, Apr 14, 2013.

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

    WALK2222

    My plugin continues to return the senders information rather than the requested players information. For example, I want to find the requested player's IP Address with a command, however, it continues to return the senders IP Address. (see code below)
    Code:
    package com.github.walk2222;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.ChatColor;
     
    public class PlayerIp {
     
        public static String getip(Player player, CommandSender sender){
     
            String playerIp = player.getAddress().getHostString();
            if(player != null){
                sender.sendMessage(ChatColor.AQUA + "IP Address: "+ChatColor.GREEN + playerIp);
            }
            return playerIp;   
        }
    }
    
    And when I try to find the requested player's hunger level, the same exact thing happens. I get the sender's hunger level.
    Code:
    package com.github.walk2222;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class PlayerHunger {
        public static int gethunger(Player player, CommandSender sender){
            int playerhunger = player.getFoodLevel();
            sender.sendMessage(ChatColor.AQUA + "Hunger: "+ChatColor.GREEN + playerhunger+"/20");
            return playerhunger;
        }
    }
    
    However, when I try to get the requested player's location, that works just fine. I don't get the sender's location, only the requested location (code below)
    Code:
    package com.github.walk2222;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class PlayerLocation {
     
     
        public static void getlocation(Player playerlocation, CommandSender sender){
            int px = (int) playerlocation.getLocation().getX();
            int py = (int) playerlocation.getLocation().getY();
            int pz = (int) playerlocation.getLocation().getZ();
            Player playerloc = Bukkit.getPlayer(sender.getName());
     
            playerloc.sendMessage(ChatColor.AQUA + "X: "+ChatColor.GREEN +px + ChatColor.AQUA +" Y: "+ChatColor.GREEN +py+ ChatColor.AQUA +" Z: "+ChatColor.GREEN +pz);
        }
    }


    Anyone have an idea on how to fix this?
    Thanks!
     
  2. Offline

    Nitnelave

    Show us the code where you call those methods.
     
  3. Offline

    WALK2222

    alright

    Code:
    package com.github.walk2222;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.*;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class Main extends JavaPlugin {
     
    //Enabling plugin message
    @Override
    public void onEnable(){
    getLogger().info("has been enabled");
    }
     
    //Player info command
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
    if(cmd.getName().equalsIgnoreCase("playerinfo")){
    Player player = Bukkit.getPlayer(sender.getName());
     
    if(args.length == 0){
    //If no arguments were entered
    player.sendMessage("Error:  You must enter a username");
    return false;
    }else if(args.length == 1){
     
    //Get target player from variable
    Player target = sender.getServer().getPlayer(args[0]);
     
    if(target == null){
    //If the player is offline
    player.sendMessage("That person is NOT online!");
    }else{
    //If the player is online
    //Start calling information methods
     
    PlayerLocation.getlocation(target, sender);
    PlayerIp.getip(player, sender);
    PlayerHealth.gethealth(player, sender);
    PlayerWorld.getworld(player, sender);
    PlayerDistance.getdistance(player, sender);
    PlayerHunger.gethunger(player, sender);
    PlayerOp.getop(player, sender);
    }
     
    }else{
    //If too many arguments were entered
    player.sendMessage("Error:  You have entered incorrect parameters");
    }
    }
    return false;
    }
    //Disabling plugin message
    @Override
    public void onDisable(){
    getLogger().info("has been disabled");
    }
    }
     
     
     
    
    I start calling the methods where I commented "start calling information methods"
     
  4. Offline

    Qwahchees

    Wouldn't you do target.getLocation()?
     
  5. Offline

    Rocoty

    The thing is, your sender and your player has the same IP.
    Code:
    Player player = Bukkit.getPlayer(sender.getName());
    When doing that all you did was getting the name of the sender (which is the player passing the command), and finding the player with that name, AKA getting the player entity of the sender (which could also be done by casting).

    I must ask you, why do you expect to get a different IP? Compare it to eating a fruit which tastes good. You learn that the fruit is called apple. You will now immediately recognize this object as an apple, but it does not taste differently for that reason. It's still a fruit, but also an apple.

    I hope I didn't confuse you with that analogy. Tell me if it helped or not :)
     
  6. Offline

    WALK2222

    I'm sorry I still don't understand what to do :c
     
  7. Offline

    Rocoty

    I must first know what you want to achieve in order to help you.
     
  8. Offline

    WALK2222

    As said in the op, I just want all the information requested (such as player's IP address, player's health) to be sent to the command sender. As of now, the plugin is finding the command sender's information, not the requested player's information
     
  9. Offline

    afistofirony

    WALK2222

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
        if(cmd.getName().equalsIgnoreCase("playerinfo")){
        Player player = Bukkit.getPlayer(sender.getName()); // This will mess up if you have a console, and is entirely unnecessary.
     
        if(args.length == 0){
            //If no arguments were entered
            player.sendMessage("Error:  You must enter a username");
            return false;
      } else if(args.length == 1){
     
            //Get target player from variable
            Player target = sender.getServer().getPlayer(args[0]);
     
            if(target == null){
            //If the player is offline
            player.sendMessage("That person is NOT online!");
          } else {
            //If the player is online, start calling information methods.
     
            PlayerLocation.getlocation(target, sender); // Gets the location of the *target*
            PlayerIp.getip(player, sender); // Gets the location of the *player* (the sender)
            PlayerHealth.gethealth(player, sender); // Same as above - applies to all of the ones below
            PlayerWorld.getworld(player, sender);
            PlayerDistance.getdistance(player, sender);
            PlayerHunger.gethunger(player, sender);
            PlayerOp.getop(player, sender);
        }
    }
    Like everyone else said, you're basically getting the sender for every one except for getLocation (which is why that one's working correctly). You need to go through all of those calls and change 'player' to 'target' so it gets the target's info. That should fix your issue. ;)

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
        if(cmd.getName().equalsIgnoreCase("playerinfo")){
            Player p = (Player) sender;
            switch(args.length){
                case 0:
                      sender.sendMessage(ChatColor.RED + "No player specified, showing you your own stats.");
                      break;
                case 1:
                      p = sender.getServer().getPlayer(args[0]);
                      break;
                default:
                      sender.sendMessage(ChatColor.RED + "Too many arguments; showing you your own stats.");
          } if(p != null){
                PlayerLocation.getlocation(p, sender);
                PlayerIp.getip(p, sender);
                PlayerHealth.gethealth(p, sender);
                PlayerWorld.getworld(p, sender);
                PlayerDistance.getdistance(p, sender);
                PlayerHunger.gethunger(p, sender);
                PlayerOp.getop(p, sender);
            } else sender.sendMessage(ChatColor.RED + "That player is not online.");
        } 
    PS: You should put all of those utilities into one class so it's more compact. :p
     
    justcool393 likes this.
  10. Offline

    justcool393

    Code:
    PlayerLocation.getlocation(target, sender);
    PlayerIp.getip(target, sender);
    PlayerHealth.gethealth(target, sender);
    PlayerWorld.getworld(target, sender);
    PlayerDistance.getdistance(target, sender);
    PlayerHunger.gethunger(target, sender);
    You are trying to pass in the player variable for the get ip, health, world, distance, and hunger methods, but for the getlocation method, you need to send in target instead :) That player variable seems to be your sender.
     
  11. Offline

    WALK2222

    thank you!
     
Thread Status:
Not open for further replies.

Share This Page