Solved Making /seen

Discussion in 'Plugin Development' started by random_username, Nov 13, 2013.

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

    random_username

    Hello, I am trying to make a /seen command, but I don't know how to get an offline player's IP, and also how to display the moment when they were last seen in a string (if it is possible), not in a long. The IP address part is throwing errors in the console, line 41. Here is my code:
    Code:java
    1. package me.kait18.playercommands.commands;
    2.  
    3. import java.net.InetSocketAddress;
    4.  
    5. import me.kait18.playercommands.Main;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.OfflinePlayer;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13.  
    14. public class seen implements CommandExecutor{
    15.  
    16. private Main main;
    17.  
    18. public seen(Main main)
    19. {
    20. this.main = main;
    21. }
    22.  
    23. @Override
    24. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    25. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    26. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    27. if(cmd.getName().equalsIgnoreCase("seen")){
    28. if(Sender.hasPermission("pcommands.seen")){
    29. if(args.length != 1){
    30. Sender.sendMessage(prefix + "§3Correct usage: /seen player");
    31. }if(args.length == 1){
    32. Player target = Bukkit.getPlayer(args[0]);
    33. if(target != null){
    34. if(target.isOnline()){
    35. Sender.sendMessage(prefix + "§3" + args[0] + "is §aOnline!\n§3First joined: " + target.getFirstPlayed() + "\nIP: " + target.getAddress().getAddress().toString() + "\nBanned: " + target.isBanned());
    36.  
    37. }
    38. }if(target == null){
    39. OfflinePlayer targetoff = (Player) Bukkit.getServer().getOfflinePlayer(args[0]);
    40. if(targetoff.hasPlayedBefore()){
    41. Sender.sendMessage(prefix + "§3" + args[0] + "is §3Offline since " + targetoff.getLastPlayed() + "\n§3First joined: " + targetoff.getFirstPlayed() + "\nIP: " + targetoff.getAddress().getAddress().toString() + "\nBanned: " + targetoff.isBanned());
    42. }else{
    43. Sender.sendMessage(prefix + "§cPlayer has not played before. Make sure casing is correct.");
    44. }
    45. }
    46. }
    47. }else{
    48. Sender.sendMessage(prefix + noperm);
    49. }
    50. }
    51. return false;
    52. }
    53.  
    54. }
    55.  

    Any Idea on how to solve the IP for an offline player? :)
     
  2. Offline

    Windy Day

    Well, I have not really worked with this before and you are not giving us the errors you are getting but on line 41 you are doing .getAddress().getAddress(). Never mind, it does the same thing just removes the port. People would be able to help better if you gave the error
     
  3. Offline

    random_username

    Here is the error :) http://pastebin.com/2dhCL5au
     
  4. Offline

    Windy Day

    Well as I said I'm haven't worked with this type of stuff in bukkit much so what I say may be wrong, in which case I'm sorry. However, it appears you just can't get an address from someone that is offline so what I would do to bypass this (and I think that essentials and plugins like this work this way) is listen for when a player joins. Then check if they have played before. If they haven't then add them file were you add their name then their address after it is made a string. So that it looks something like:
    random_username: 123.123.123.123:22565
    then you can get their name from the config and get their address while they are offline. Also it would be a good idea so that not only do you check if they have already joined but if their address is the same. And if it isn't update it.
     
  5. Offline

    Aljed_The_Legit

    You might wanna use Bukkit.getPlayer(args[0].toLowerCase());
     
  6. Offline

    random_username

    Then, any ideas in how to put in a file a player's name, and his IP? This way, as Windy Day said, I will be able to get the player's IP for the /seen command.
     
  7. Offline

    xize

    random_username
    the problem is that offline players aren't really supported some methods do but not all.
    what I do is saving a file when the player quits with I save his name, ip, also the time when they leaved.
    then when I use /seen playername I will check if that file exists with that name and then load up the information.

    edit
    because I didn't read the post above me you could save files like
    Code:
    try {
    File f = new File(yourplugin.getDataFolder() + File.Seperator + "players" + File.Seperator + p.getName() + ".yml");
    if(f.exists()) {
     FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    con.set("name", p.getName());
    con.set("time", System.timeCurrentMillies());
    con.save(f);
    } else {
    FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    con.set("name", p.getName());
    con.set("time", System.timeCurrentMillies());
    con.save(f);
    }
    } catch(Exception e) {
    e.printStacktrace();
    }
    
    loading is almost the same principe but then you would use .get();

    you could set a System.currentTimeMillies(); into a date by using Date date = new Date(yourMillies);
     
  8. Offline

    random_username

    Yes, I know I can't use those methods with offline players :) But my question is, how can I add that data you are saying to a file? Maybe in the playerquitevent?
     
  9. Offline

    amhokies

    Make a yml file for each player.
     
  10. Offline

    random_username

    but how would I create those files when a player leaves the game? :)
     
  11. Offline

    xize

    random_username
    ive updated my code on my earlier tag:p
    it could be good to use it in the PlayerQuitEvent and PlayerKickEvent though I know you use likely getConfig() but this only works for config.yml not custom configs;)
     
  12. Offline

    amhokies

    You forgot to create the file if it doesn't exist :)
     
  13. Offline

    xize

    amhokies
    it automaticly does:), but for folders when using /seen for the first time you really need to check if the dir exists.
     
  14. Offline

    amhokies

    Then your code seems kind of redundant. You're doing the exact same thing whether the file exists or not.
     
  15. Offline

    random_username

    amhokies
    xize
    So...:
    Code:java
    1. //Quit Messages
    2. @EventHandler
    3. public void onPlayerQuit(PlayerQuitEvent event){
    4. Player player = event.getPlayer();
    5. if(main.getConfig().getBoolean("enableleavemessages") == true){
    6. event.setQuitMessage(main.getConfig().getString("onLeave").replace("&", "§").replace("%p", player.getName()));
    7. }try {
    8. File f = new File(main.getDataFolder() + File.separator + "players" + File.separator + player.getName() + ".yml");
    9. if(f.exists()) {
    10. FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    11. Date date = new Date(System.currentTimeMillis());
    12. con.set("name", player.getName());
    13. con.set("time", date);
    14. con.save(f);
    15. } else {
    16. FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    17. Date date = new Date(System.currentTimeMillis());
    18. con.set("name", player.getName());
    19. con.set("time", date);
    20. con.save(f);
    21. }
    22. } catch(Exception e) {
    23. e.printStackTrace();
    24. }
    25. }

    Inside my events class??
     
  16. Offline

    xize

    amhokies
    not really, if the file already exists you might want to change the values in it by using newer ones.
     
  17. Offline

    random_username

    And how can I do this?
     
  18. Offline

    xize

    random_username
    almost good, best way how I would do it is just using con.set("time", System.currentTimeMillies()); but it doesn't make a really big difference though (I hope) if I'm correct the Date object can be saved as a string.

    edit
    it goes automatic, if the file exists it only changes the values in that file.
    if the file doesn't exists it creates a new file which get saved through con.save(f);
     
  19. Offline

    random_username

    wait, shouldn't the file be created in the on-enable??
     
  20. Offline

    xize

    random_username
    best way is to do it when they get kicked or quited though:p
     
  21. Offline

    random_username

    and on my other class called seen, how do I get the data stored in the events class? :)
     
  22. Offline

    xize

    random_username
    Code:
    try {
      File f = new File(yourPlugin.getDataFolder() + File.Seperator + "players" + File.Seperator + args[0] +".yml");
      if(f.exists()) {
          FileConfiguration con = YamlConfiguration.loadConfiguration(f);
          Date date = new Date(con.getLong("time"));
    //do something more
      } else {
         sender.sendMessage("no player data found");
      }
    } catch(Exception e) {
       e.printStackTrace();
    }
    
    edit
    though I recommend to save and load files as toLowerCase() so you have less case sensitivity
     
  23. Offline

    random_username

    xize
    I got the /seen when the player is online to work perfectly, but when I log out, this is what /seen says:
    Last seen: 0
    IP: 127.0.0.1
    Location: 5
    The IP is the only thing right, as I am joining with "localhost". Anyway, when I am online, it says:
    Last seen: online
    IP: 127.0.0.1
    Location xxx,xxx,xxx
    replace xxx with my current location x,y,z's values. How can I fix the message when I am offline? Here is the code of the Playerquitevent, and my seen class:
    PlayerQuitEvent:
    Code:java
    1. @EventHandler
    2. public void onPlayerQuit(PlayerQuitEvent event){
    3. Player player = event.getPlayer();
    4. if(main.getConfig().getBoolean("enableleavemessages") == true){
    5. event.setQuitMessage(main.getConfig().getString("onLeave").replace("&", "§").replace("%p", player.getName()));
    6. }try {
    7. File f = new File(main.getDataFolder() + File.separator + "players" + File.separator + player.getName() + ".yml");
    8. if(f.exists()) {
    9. FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    10. Date date = new Date(System.currentTimeMillis());
    11. Location loc = player.getLocation();
    12. con.set("name", player.getName());
    13. con.set("time", date);
    14. con.set("ip", player.getAddress().getAddress().toString());
    15. con.set("loc", loc.getX() + loc.getY() + loc.getZ()) ;
    16. con.save(f);
    17. } else {
    18. FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    19. Date date = new Date(System.currentTimeMillis());
    20. Location loc = player.getLocation();
    21. con.set("name", player.getName());
    22. con.set("time", date);
    23. con.set("ip", player.getAddress().getAddress().toString());
    24. con.set("loc", loc.getX() + loc.getY() + loc.getZ()) ;
    25. con.save(f);
    26. }
    27. } catch(Exception e) {
    28. e.printStackTrace();
    29. }
    30. }

    Seen:
    Code:java
    1. public class seen implements CommandExecutor {
    2. private Main main;
    3.  
    4. public seen(Main main)
    5. {
    6. this.main = main;
    7. }
    8. @Override
    9. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    10. String noperm = main.getConfig().getString("NoPermission").replace("&", "§");
    11. String prefix = main.getConfig().getString("Prefix").replace("&", "§");
    12. if(cmd.getName().equalsIgnoreCase("seen")){
    13. if(Sender.hasPermission("pcommands.seen")){
    14. if(args.length == 1){
    15. Player targeton = Bukkit.getPlayer(args[0]);
    16. if(targeton != null){
    17. if(targeton.isOnline()){
    18. Location loc = targeton.getLocation();
    19. Sender.sendMessage(prefix + "§a" + args[0] + " details:\n" + "§3Last Seen: §aOnline!" + "\n§3IP: " + targeton.getAddress().getAddress().toString().replace("/", "") + "\n§3Location: " + df.format(loc.getX()) + "," + df.format(loc.getY()) + "," + df.format(loc.getZ()));
    20. }
    21. }else{
    22. OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]) ;
    23. try {
    24. File f = new File(main.getDataFolder() + File.separator + "players" + File.separator + args[0] +".yml");
    25. if(f.exists()) {
    26. FileConfiguration con = YamlConfiguration.loadConfiguration(f);
    27. Sender.sendMessage(prefix + "§a" + args[0] + " details:\n" + "§3Last Seen: " + con.getLong("time") + "\n§3IP: " + con.getString("ip").replace("/", "") + "\n§3Location: " + df.format(con.getDouble("loc")));
    28. } else {
    29. Sender.sendMessage(prefix + "§3no player data found");
    30. }
    31. } catch(Exception e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }
    36. }else{
    37. Sender.sendMessage(prefix + noperm);
    38. }
    39. }
    40. return false;
    41. }
    42. }

    Any ideas on how to solve it? Thanks for all the help btw :)
     
  24. Offline

    xize

    I just did a look how the location got saved but why not serialize it? it seems you put everything to one coordinate:p

    edit
    also you are saving a Date object in 'time' while you load it as a Long type which is wrong try loading that value as a string.
     
  25. Offline

    random_username

    Thanks, making the date a string worked! About the location, I made it a string by using:
    Code:java
    1. String location = "§3" + df.format(loc.getX()) + "," + df.format(loc.getY()) + "," + df.format(loc.getZ());

    and It worked! Thanks for the help, marking thread as solved :D
     
Thread Status:
Not open for further replies.

Share This Page