PlayerCommandPreproccessEvent not letting me run commands not in plugin.yml

Discussion in 'Plugin Development' started by dingus007, Aug 20, 2013.

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

    dingus007

    I am using the PlayerCommandPreproccessEvent to let commands that aren't in the plugin.yml run. It isn't going too well. I don't know if I should register the command or do what. The code is pretty short and should be able to be fixed pretty easy. I'll just put the part that handles the commands.
    Code:java
    1. public boolean PlayerCommandPreprocessEvent(Player player, String msg, org.bukkit.event.player.PlayerCommandPreprocessEvent event){
    2. ConfigurationSection cs = getConfig().getConfigurationSection("warpcmds");
    3. for (String s : cs.getKeys(false)) {
    4. ConfigurationSection cs2 = cs.getConfigurationSection(s);
    5. if (msg == cs2.getString("command")){
    6. if (player.hasPermission("WarpCommands." + cs2.getString("command"))) {
    7. player.performCommand("warp " + cs2.getString("warpname"));
    8. event.setCancelled(false);
    9. } else return false;
    10. }
    11. }
    12. return true;
    13. }
     
  2. Offline

    Rockon999

    dingus007
    ... hope this helps :)
    Code:java
    1.  
    2. HashMap<String, String> map = new HashMap<String, String>(); // map.put(warpname, command)
    3. HashMap<String, Location> warps = new HashMap<String, Location>(); //map.put(warpname, location)
    4. public boolean warp(Player p, String warpname){
    5. if(warps.contains(warpname)){
    6. e.getPlayer().teleport(warps.get(warpname);
    7. return true;
    8. }else{
    9. return false;
    10. }
    11. }
    12. @EventHandler
    13. public void command(PlayerCommandPreprocessEvent e) {
    14. for (Entry<String, String> en : map) {
    15. if (e.getMessage().equalsIgnoreCase(en.getValue())) {
    16. if(warp(e.getPlayer(), en.getKey()))
    17. e.getPlayer().sendMessage("Warped To " + en.getKey());
    18. else
    19. e.getPlayer().sendMessage("Warp Doesn't Exist.. problem!");
    20. } else {
    21. }
    22. }
    23. }
     
  3. Offline

    AmShaegar

    if(msg == cs2.getString("command")) {

    Never do this. Always compare Strings with equals or equalsIgnoreCase.

    if(msg.equalsIgnoreCase(cs2.getString("command")) {
     
  4. Offline

    dingus007

    There were some errors I couldn't fix on my own, I put them in comments

    Code:java
    1. if(warps.contains(warpname)){ //The method contains(String) is undefined for the type HashMap<String,Location>
    2. e.getPlayer().teleport(warps.get(warpname)); //e cannot be resolved
    3. return true;
    4. }else{
    5. return false;}
    6. }
    7. @EventHandler
    8. public void command(PlayerCommandPreprocessEvent e) {
    9. for (Entry<String, String> en : map) { //Can only iterate over an array or an instance of java.lang.Iterable


    Also, here is my imports:
    Code:java
    1. import java.util.HashMap;
    2. import java.util.Map.Entry;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.configuration.ConfigurationSection;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Event;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
     
  5. Offline

    Rockon999

    Code:java
    1. if(warps.containsKey(warpname)){ //The method contains(String) is undefined for the type HashMap<String,Location> -- oops didn't do the write method
    2. p.teleport(warps.get(warpname)); //e cannot be resolved - oops... forgot to change the variable
    3. return true;
    4. }else{
    5. return false;}
    6. }
    7. @EventHandler
    8. public void command(PlayerCommandPreprocessEvent e) {
    9. for (Entry<String, String> en : map.entrySet()) { //Can only iterate over an array or an instance of java.lang.Iterable -- oops didn't use .entrySet()
    10.  

     
  6. Offline

    dingus007

    Still doesn't work, I can't figure out why...
    Code:java
    1. package com.elements.warpcommands;
    2.  
    3.  
    4. import java.util.HashMap;
    5. import java.util.Map.Entry;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13.  
    14. public class WarpCommands extends JavaPlugin {
    15.  
    16. public void sendMessage(Player player, String msg){
    17. player.sendMessage(ChatColor.GOLD + "[WarpCommands] " + ChatColor.WHITE + "You must be a player to execute this command");
    18. }
    19.  
    20. public void onEnable() {
    21. System.out.println("Enabling WarpCommands");
    22. this.saveDefaultConfig();
    23. }
    24.  
    25. public void onDisable() {
    26. System.out.println("Disabling WarpCommands");
    27. }
    28.  
    29.  
    30.  
    31. HashMap<String, String> map = new HashMap<String, String>(); // map.put(warpname, command)
    32. HashMap<String, Location> warps = new HashMap<String, Location>(); //map.put(warpname, location)
    33. public boolean warp(Player p, String warpname){
    34. if(warps.containsKey(warpname)){ //The method contains(String) is undefined for the type HashMap<String,Location> -- oops didn't do the write method
    35. p.teleport(warps.get(warpname)); //e cannot be resolved - oops... forgot to change the variable
    36. return true;
    37. }else{
    38. return false;}
    39. }
    40. @EventHandler
    41. public void command(PlayerCommandPreprocessEvent e) {
    42. for (Entry<String, String> en : map.entrySet()) { //Can only iterate over an array or an instance of java.lang.Iterable -- oops didn't use .entrySet()
    43. if (e.getMessage().equalsIgnoreCase(en.getValue())) {
    44. if(warp(e.getPlayer(), en.getKey()))
    45. e.getPlayer().sendMessage(ChatColor.GOLD + "Warped To " + ChatColor.WHITE + en.getKey());
    46. else
    47. e.getPlayer().sendMessage(ChatColor.RED + "Warp doesn't exist!");
    48. } else {
    49. }
    50. }
    51. }
    52. }
    53.  
     
  7. Offline

    Saposhiente

    getCommand("commandName").register(((CraftServer)Bukkit.getServer()).getCommandMap())
    This will require importing the craftbukkit library
     
  8. Offline

    dingus007

    Am I doing it right?
    Code:java
    1. for (Entry<String, String> en : map.entrySet()) {
    2. getCommand(en.getValue()).register(((CraftServer)Bukkit.getServer()).getCommandMap());
    3. if (e.getMessage().equalsIgnoreCase(en.getValue())) {
    4. if(warp(e.getPlayer(), en.getKey()))
    5. e.getPlayer().sendMessage(ChatColor.GOLD + "Warped To " + ChatColor.WHITE + en.getKey());
    6. else
    7. e.getPlayer().sendMessage(ChatColor.RED + "Warp doesn't exist!");
    8. } else {
    9. }
    10. }
     
  9. Offline

    Saposhiente

    You have to register the command in advance, not during the CommandPreprocessEvent. If the command isn't registered, the CommandPreprocessEvent is never called in the first place.
     
  10. Offline

    dingus007

    So I made this function outside of the PlayerCommandPreprocessEvent to register all commands in config, but it still doesn't work...
    Code:java
    1. public void registerCommands() {
    2. ConfigurationSection cs = getConfig().getConfigurationSection("warpcmds");
    3. for (String s : cs.getKeys(false)) {
    4. ConfigurationSection cs2 = cs.getConfigurationSection(s);
    5. getCommand(cs2.getString("command")).register(((CraftServer)Bukkit.getServer()).getCommandMap());
    6. }
    7. }
     
  11. Offline

    Tarestudio

    Thats not true, my plugin use this event to execute dynamic defined commands and aliases and is working well. CommandPreprocessEvent triggers on any chat of players starting with /

    dingus007
    warp is a command that really exists and manages the warpnames, right? Then try to replace player.performCommand("warp " + cs2.getString("warpname")); with player.chat("/warp " + cs2.getString("warpname"));
     
  12. Offline

    dingus007

    That's not the problem I'm having. It teleports them fine if the warp exists, I just need to get some way of running commands without them being registered. I have already tried the above methods, so either I'm not doing something right or the code is non functional (which I doubt)
     
  13. Offline

    Saposhiente

    Oh, yes, PlayerCommandPreprocess does grab any command, even unregistered. It just doesn't hear commands that are performCommand(x)ed. Using player.chat(x) will work, as will simply calling your command function with a PlayerCommandPreprocessEvent that you create.
     
  14. Offline

    Rockon999

  15. Offline

    dingus007

Thread Status:
Not open for further replies.

Share This Page