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 /*** Get's a player based on a string.* @param s | Part or a whole player name.* @return The player specified.*/public Player getPlayer(String s){String dbug;Player[] plyr = root.getServer().getOnlinePlayers();int plyrnum = plyr.length;for(int i=1; i < plyrnum; i++){dbug = plyr[I].getName().toLowerCase();[/I][I] System.out.print(dbug);[/I][I] if(plyr[I].getName().toLowerCase().startsWith(s.toLowerCase())){[/I][/I] [I] return plyr[I];[/I][/I][I] } else continue;[/I][I] }[/I] [I] return null;[/I][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 // Complete overhaul of old plugin. Code:JAVA [I]package core;[/I] [I]import java.util.HashMap;[/I][I]import java.util.Map;[/I][I]import org.bukkit.ChatColor;[/I][I]import org.bukkit.Server;[/I][I]import org.bukkit.configuration.file.FileConfiguration;[/I][I]import org.bukkit.entity.Player;[/I][I]import org.bukkit.plugin.java.JavaPlugin;[/I] [I]public class root extends JavaPlugin {[/I] [I]//cmd as in like DOS prompt. Gotta love commandline.[/I][I]private commandHandeler cmd = new commandHandeler(this);[/I] [I]//Me and my weird formatting :3[/I][I]public Map<Player, Boolean>[/I][I]GodModer = new HashMap<Player, Boolean>();[/I] [I]public static Map<Player, Boolean>[/I][I]Flandre = new HashMap<Player, Boolean>(),[/I][I]Muted = new HashMap<Player, Boolean>();[/I] [I]//Cuz I'm so darn lazy.[/I][I]public void print(String s){[/I][I]System.out.print(s);[/I][I]}[/I] [I]void sendmsg(String msg, String type, String player) {[/I][I]Server server = getServer();[/I][I]if (server.getPlayer(player) == null) {[/I][I]if (type.equalsIgnoreCase("alert")) server.broadcastMessage(ChatColor.RED + "[9Admin] " + msg);[/I][I]if (type.equalsIgnoreCase("warn")) server.broadcastMessage(ChatColor.YELLOW + "[9Admin] " + msg);[/I][I]if (type.equalsIgnoreCase("ok")) server.broadcastMessage(ChatColor.GREEN + "[9Admin] " + msg);[/I][I]} else if (server.getPlayer(player) instanceof Player) {[/I][I]if (type.equalsIgnoreCase("alert")) server.getPlayer(player).sendMessage(ChatColor.RED + "[9Admin] " + msg);[/I][I]if (type.equalsIgnoreCase("warn")) server.getPlayer(player).sendMessage(ChatColor.YELLOW + "[9Admin] " + msg);[/I][I]if (type.equalsIgnoreCase("ok")) server.getPlayer(player).sendMessage(ChatColor.GREEN + "[9Admin] " + msg);[/I][I]} else if (player.equalsIgnoreCase("global")) {[/I][I]server.broadcastMessage(msg);[/I][I]}[/I][I]}[/I] [I]public void Config(){[/I][I]FileConfiguration config = this.getConfig();[/I][I]config.addDefault("JoinMessage", "Welcome!");[/I][I]config.addDefault("useWelcomeMessage", true);[/I][I]config.options().copyDefaults(true);[/I][I]saveConfig();[/I][I]}[/I] [I]public void onEnable(){[/I][I]print(getName() + ": Loaded config!");[/I][I]getServer().getPluginManager().registerEvents(new Listeners.plyrlisten(this), this);[/I][I]getCommand("play").setExecutor(cmd);[/I][I]getCommand("kill").setExecutor(cmd);[/I][I]}[/I] [I]public void onDisable(){[/I][I]Flandre.clear();[/I][I]Muted.clear();[/I][I]}[/I] [I]}[/I] Player listener: Code:JAVA package Listeners; Code:JAVA [I]import org.bukkit.ChatColor;[/I][I]import org.bukkit.entity.Player;[/I][I]import org.bukkit.event.EventHandler;[/I][I]import org.bukkit.event.Listener;[/I][I]import org.bukkit.event.player.PlayerChatEvent;[/I][I]import org.bukkit.event.player.PlayerJoinEvent;[/I] [I]import core.root;[/I] [I]public class plyrlisten implements Listener {[/I] [I]public root plugin;[/I] [I]public plyrlisten(root c){[/I][I]plugin = c;[/I][I]}[/I] [I]public void Flandre(PlayerChatEvent event){[/I][I]Player victim = event.getPlayer();[/I][I]String msg = event.getMessage();[/I][I]victim.getServer().broadcastMessage("<" + ChatColor.RED + "Flandre Scarlet" + ChatColor.WHITE + "> "[/I][I]+ (msg.startsWith("Yes") || msg.startsWith("yes") ? "I broke " + victim.getName() : "Why did you ignore me " + victim.getName() + "!?"));[/I][I]victim.setHealth(0);[/I][I]}[/I] [I]@EventHandler[/I][I]public void onPlayerChat(PlayerChatEvent event) {[/I][I]String msg = event.getMessage();[/I][I]if (msg.startsWith("Yes") || msg.startsWith("No") || msg.startsWith("yes") || msg.startsWith("no")) {[/I][I]Flandre(event);[/I][I]}[/I][I]}[/I] [I]public void onPlayerJoin(PlayerJoinEvent event){[/I][I]if(!root.Muted.containsKey(event.getPlayer())){[/I][I]root.Muted.put(event.getPlayer(), false);[/I][I]}[/I][I]if(!root.Flandre.containsKey(event.getPlayer())){[/I][I]root.Flandre.put(event.getPlayer(), false);[/I][I]}[/I][I]}[/I][I]}[/I] Command Handeler: Code:JAVA package core; Code:JAVA [I]import org.bukkit.ChatColor;[/I][I]import org.bukkit.command.Command;[/I][I]import org.bukkit.command.CommandExecutor;[/I][I]import org.bukkit.command.CommandSender;[/I][I]import org.bukkit.entity.Player;[/I] [I]public class commandHandeler implements CommandExecutor {[/I] [I]root root;[/I] [I]public commandHandeler(root root){[/I][I]this.root = root;[/I][I]}[/I] [I]/**[/I][I]* Get's a player based on a string.[/I][I]* @param s | Part or a whole player name.[/I][I]* @return The player specified.[/I][I]*/[/I][I]public Player getPlayer(String s){[/I][I]String dbug;[/I][I]Player[] plyr = root.getServer().getOnlinePlayers();[/I][I]int plyrnum = plyr.length;[/I][I]for(int i=1; i < plyrnum; i++){[/I][I]dbug = plyr.getName().toLowerCase();[/I][I]System.out.print(dbug);[/I][I]if(plyr.getName().toLowerCase().startsWith(s.toLowerCase())){[/I] [I]return plyr;[/I][I]} else continue;[/I][I]}[/I] [I]return null;[/I][I]}[/I] [I]@SuppressWarnings("static-access")[/I][I]public boolean onCommand(CommandSender sender, Command command, String label, String[] args0) {[/I][I]String cmd = command.getName();[/I][I]if(cmd.equalsIgnoreCase("play")){[/I][I]root.Flandre.put(sender.getServer().getPlayer(sender.getName()), true);[/I][I]root.getServer().getPlayer(sender.getName()).sendMessage("<" + ChatColor.RED + "Flandre Scarlet" + ChatColor.WHITE + "> Hi!");[/I][I]return true;[/I][I]} else if(cmd.startsWith("kill")){[/I][I]Player target = getPlayer(cmd.replace("kill ", ""));[/I][I]if(target != null){[/I][I]target.setHealth(0);[/I][I]} else {[/I][I]root.sendmsg("Player not found!", "alert", sender.getName());[/I][I]}[/I][I]return true;[/I][I]}[/I][I]return false;[/I][I]}[/I][I]}[/I]
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.
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.
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.
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.
Sorry, I just figured that out with the two other posts. Do you happen to know how to override the /kill command?
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