[SOLVED] Custom getPlayer function not working.

Discussion in 'Plugin Development' started by Cirno, Jun 30, 2012.

  1. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I can't override the /kill command at all. Is there a way around it besides going throug listening via onCommandPreprocess?
    It's been a long time since I've been here. Why is bukkit putting [/i][/i][/i] after my last line?

    I'm working on rewriting an old admin plugin that used to work. Right now, I'm stuck at a "coder's block" where I can't think of any reason why this custom getPlayer() function isn't working.
    Code:JAVA
    1. /**
    2. * Get's a player based on a string.
    3. * @param s | Part or a whole player name.
    4. * @return The player specified.
    5. */
    6. public Player getPlayer(String s){
    7. String dbug;
    8. Player[] plyr = root.getServer().getOnlinePlayers();
    9. int plyrnum = plyr.length;
    10. for(int i=1; i < plyrnum; i++){
    11. dbug = plyr[I].getName().toLowerCase();[/I]
    12. [I] System.out.print(dbug);[/I]
    13. [I] if(plyr[I].getName().toLowerCase().startsWith(s.toLowerCase())){[/I][/I]
    14.  
    15. [I] return plyr[I];[/I][/I]
    16. [I] } else continue;[/I]
    17. [I] }[/I]
    18.  
    19. [I] return null;[/I]
    20. [I] }[/I]

    Since my in game name is Evangon, I do /kill e (it calls for getPlayer() and then sets the health to zero). However, it is not finding my player and it spits out the error message ("Player not found!"). What mistakes am I making? Note that:
    • There is no console errors. The "dbug" string is there so I can, well, debug.
    • I am using build 2222.
    • Compiling under Java 7. (1.7.0_03-b05 to be exact)
    • I am testing on a plugin testing server, so there are no other plugins at all.
    Ignore the Flandre thing; I just did it there to refresh my plugin-programming skills.

    Main/Core class:
    Code:JAVA
    1. // Complete overhaul of old plugin.
    Code:JAVA
    1.  
    2.  
    3. [I]package core;[/I]
    4.  
    5. [I]import java.util.HashMap;[/I]
    6. [I]import java.util.Map;[/I]
    7. [I]import org.bukkit.ChatColor;[/I]
    8. [I]import org.bukkit.Server;[/I]
    9. [I]import org.bukkit.configuration.file.FileConfiguration;[/I]
    10. [I]import org.bukkit.entity.Player;[/I]
    11. [I]import org.bukkit.plugin.java.JavaPlugin;[/I]
    12.  
    13. [I]public class root extends JavaPlugin {[/I]
    14.  
    15. [I]//cmd as in like DOS prompt. Gotta love commandline.[/I]
    16. [I]private commandHandeler cmd = new commandHandeler(this);[/I]
    17.  
    18. [I]//Me and my weird formatting :3[/I]
    19. [I]public Map<Player, Boolean>[/I]
    20. [I]GodModer = new HashMap<Player, Boolean>();[/I]
    21.  
    22. [I]public static Map<Player, Boolean>[/I]
    23. [I]Flandre = new HashMap<Player, Boolean>(),[/I]
    24. [I]Muted = new HashMap<Player, Boolean>();[/I]
    25.  
    26. [I]//Cuz I'm so darn lazy.[/I]
    27. [I]public void print(String s){[/I]
    28. [I]System.out.print(s);[/I]
    29. [I]}[/I]
    30.  
    31. [I]void sendmsg(String msg, String type, String player) {[/I]
    32. [I]Server server = getServer();[/I]
    33. [I]if (server.getPlayer(player) == null) {[/I]
    34. [I]if (type.equalsIgnoreCase("alert")) server.broadcastMessage(ChatColor.RED + "[9Admin] " + msg);[/I]
    35. [I]if (type.equalsIgnoreCase("warn")) server.broadcastMessage(ChatColor.YELLOW + "[9Admin] " + msg);[/I]
    36. [I]if (type.equalsIgnoreCase("ok")) server.broadcastMessage(ChatColor.GREEN + "[9Admin] " + msg);[/I]
    37. [I]} else if (server.getPlayer(player) instanceof Player) {[/I]
    38. [I]if (type.equalsIgnoreCase("alert")) server.getPlayer(player).sendMessage(ChatColor.RED + "[9Admin] " + msg);[/I]
    39. [I]if (type.equalsIgnoreCase("warn")) server.getPlayer(player).sendMessage(ChatColor.YELLOW + "[9Admin] " + msg);[/I]
    40. [I]if (type.equalsIgnoreCase("ok")) server.getPlayer(player).sendMessage(ChatColor.GREEN + "[9Admin] " + msg);[/I]
    41. [I]} else if (player.equalsIgnoreCase("global")) {[/I]
    42. [I]server.broadcastMessage(msg);[/I]
    43. [I]}[/I]
    44. [I]}[/I]
    45.  
    46. [I]public void Config(){[/I]
    47. [I]FileConfiguration config = this.getConfig();[/I]
    48. [I]config.addDefault("JoinMessage", "Welcome!");[/I]
    49. [I]config.addDefault("useWelcomeMessage", true);[/I]
    50. [I]config.options().copyDefaults(true);[/I]
    51. [I]saveConfig();[/I]
    52. [I]}[/I]
    53.  
    54. [I]public void onEnable(){[/I]
    55. [I]print(getName() + ": Loaded config!");[/I]
    56. [I]getServer().getPluginManager().registerEvents(new Listeners.plyrlisten(this), this);[/I]
    57. [I]getCommand("play").setExecutor(cmd);[/I]
    58. [I]getCommand("kill").setExecutor(cmd);[/I]
    59. [I]}[/I]
    60.  
    61. [I]public void onDisable(){[/I]
    62. [I]Flandre.clear();[/I]
    63. [I]Muted.clear();[/I]
    64. [I]}[/I]
    65.  
    66. [I]}[/I]


    Player listener:
    Code:JAVA
    1. package Listeners;
    Code:JAVA
    1.  
    2.  
    3. [I]import org.bukkit.ChatColor;[/I]
    4. [I]import org.bukkit.entity.Player;[/I]
    5. [I]import org.bukkit.event.EventHandler;[/I]
    6. [I]import org.bukkit.event.Listener;[/I]
    7. [I]import org.bukkit.event.player.PlayerChatEvent;[/I]
    8. [I]import org.bukkit.event.player.PlayerJoinEvent;[/I]
    9.  
    10. [I]import core.root;[/I]
    11.  
    12.  
    13. [I]public class plyrlisten implements Listener {[/I]
    14.  
    15. [I]public root plugin;[/I]
    16.  
    17. [I]public plyrlisten(root c){[/I]
    18. [I]plugin = c;[/I]
    19. [I]}[/I]
    20.  
    21. [I]public void Flandre(PlayerChatEvent event){[/I]
    22. [I]Player victim = event.getPlayer();[/I]
    23. [I]String msg = event.getMessage();[/I]
    24. [I]victim.getServer().broadcastMessage("<" + ChatColor.RED + "Flandre Scarlet" + ChatColor.WHITE + "> "[/I]
    25. [I]+ (msg.startsWith("Yes") || msg.startsWith("yes") ? "I broke " + victim.getName() : "Why did you ignore me " + victim.getName() + "!?"));[/I]
    26. [I]victim.setHealth(0);[/I]
    27. [I]}[/I]
    28.  
    29. [I]@EventHandler[/I]
    30. [I]public void onPlayerChat(PlayerChatEvent event) {[/I]
    31. [I]String msg = event.getMessage();[/I]
    32. [I]if (msg.startsWith("Yes") || msg.startsWith("No") || msg.startsWith("yes") || msg.startsWith("no")) {[/I]
    33. [I]Flandre(event);[/I]
    34. [I]}[/I]
    35. [I]}[/I]
    36.  
    37. [I]public void onPlayerJoin(PlayerJoinEvent event){[/I]
    38. [I]if(!root.Muted.containsKey(event.getPlayer())){[/I]
    39. [I]root.Muted.put(event.getPlayer(), false);[/I]
    40. [I]}[/I]
    41. [I]if(!root.Flandre.containsKey(event.getPlayer())){[/I]
    42. [I]root.Flandre.put(event.getPlayer(), false);[/I]
    43. [I]}[/I]
    44. [I]}[/I]
    45. [I]}[/I]


    Command Handeler:
    Code:JAVA
    1. package core;
    Code:JAVA
    1.  
    2.  
    3. [I]import org.bukkit.ChatColor;[/I]
    4. [I]import org.bukkit.command.Command;[/I]
    5. [I]import org.bukkit.command.CommandExecutor;[/I]
    6. [I]import org.bukkit.command.CommandSender;[/I]
    7. [I]import org.bukkit.entity.Player;[/I]
    8.  
    9. [I]public class commandHandeler implements CommandExecutor {[/I]
    10.  
    11. [I]root root;[/I]
    12.  
    13. [I]public commandHandeler(root root){[/I]
    14. [I]this.root = root;[/I]
    15. [I]}[/I]
    16.  
    17. [I]/**[/I]
    18. [I]* Get's a player based on a string.[/I]
    19. [I]* @param s | Part or a whole player name.[/I]
    20. [I]* @return The player specified.[/I]
    21. [I]*/[/I]
    22. [I]public Player getPlayer(String s){[/I]
    23. [I]String dbug;[/I]
    24. [I]Player[] plyr = root.getServer().getOnlinePlayers();[/I]
    25. [I]int plyrnum = plyr.length;[/I]
    26. [I]for(int i=1; i < plyrnum; i++){[/I]
    27. [I]dbug = plyr.getName().toLowerCase();[/I]
    28. [I]System.out.print(dbug);[/I]
    29. [I]if(plyr.getName().toLowerCase().startsWith(s.toLowerCase())){[/I]
    30.  
    31. [I]return plyr;[/I]
    32. [I]} else continue;[/I]
    33. [I]}[/I]
    34.  
    35. [I]return null;[/I]
    36. [I]}[/I]
    37.  
    38. [I]@SuppressWarnings("static-access")[/I]
    39. [I]public boolean onCommand(CommandSender sender, Command command, String label, String[] args0) {[/I]
    40. [I]String cmd = command.getName();[/I]
    41. [I]if(cmd.equalsIgnoreCase("play")){[/I]
    42. [I]root.Flandre.put(sender.getServer().getPlayer(sender.getName()), true);[/I]
    43. [I]root.getServer().getPlayer(sender.getName()).sendMessage("<" + ChatColor.RED + "Flandre Scarlet" + ChatColor.WHITE + "> Hi!");[/I]
    44. [I]return true;[/I]
    45. [I]} else if(cmd.startsWith("kill")){[/I]
    46. [I]Player target = getPlayer(cmd.replace("kill ", ""));[/I]
    47. [I]if(target != null){[/I]
    48. [I]target.setHealth(0);[/I]
    49. [I]} else {[/I]
    50. [I]root.sendmsg("Player not found!", "alert", sender.getName());[/I]
    51. [I]}[/I]
    52. [I]return true;[/I]
    53. [I]}[/I]
    54. [I]return false;[/I]
    55. [I]}[/I]
    56. [I]}[/I]


    This post has been edited 4 times. It was last edited by Cirno Jul 1, 2012.
  2. Offline

    Chlorek

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Maybe I do not know Java too much or I do not understand you but why the hell are you doing all this code? Bukkit gives you methods well you should use them. Isn't that simpler to get player by:
    Code:
    Bukkit.getPlayer(String name);
    If no player found returns null. If I do not understand you please explain.
  3. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I'm trying to do a more "advanced" way of retrieving a player. With Bukkit (as far as I remember), the getPlayer() function requires the entire name, not part of it. I'm trying to do both part and full.
  4. Offline

    Chlorek

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I did not test this method now, but if I member that right it finds player even if specifed name is a part of whole player name. Well, you can check it.
    Oh, and according to your question - isn't that better to use "contains" method instead of "startswith"? It would be more useful.
  5. Offline

    kumpelblase2

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Nope, that's what getPlayerExact is doing. getPlayer() does work like you need it.
  6. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If I do "contains" then if I do /kill a, everyone with the letter "a" in their name would die.
    Forwhatever reason, I did something to the code (can't remeber what), but now only Evangon dies. If I do a cracked client and login as "asdfasdf" and do /kill a, Evangon is killed instead.

    EDIT:
    Thank you; I found out that getPlayer does work like the way I was trying to do. Now, forwhatever reason, I can't override the /kill command.

    This post has been edited 1 time. It was last edited by Cirno Jul 1, 2012.
  7. Offline

    Sushi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    -snip-

    getPlayer should get a player, even if the name is not the full username.

    This post has been edited 1 time. It was last edited by Sushi Jul 1, 2012.
  8. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Sorry, I just figured that out with the two other posts. Do you happen to know how to override the /kill command?
  9. Offline

    Sushi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I think that commands in plugins take precedence over the commands that come with bukkit (not completely sure).

    Just write a kill command as normal and it would probably work. If it doesn't I can help you out. o3o
  10. Offline

    Cirno

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I just got it to work. Thanks all :3
    Sushi likes this.
  11. Offline

    Sushi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    No problem :D

Share This Page