Convert player-only commands to console commands?

Discussion in 'Plugin Development' started by BlackBeltPanda, Jun 10, 2014.

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

    BlackBeltPanda

    I'm not a plugin developer, but a plugin I use hasn't been updated in quite a while and one of the things I need changed is the ability to run some of the commands from the console. How would I go about doing that? Here's some example code for a command (not from the plugin) that I would have to change:

    Code:java
    1.  
    2. public class TestCommand implements CommandExecutor {
    3.  
    4. public TestCommand() {
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command command, String label, String split[]) {
    8. if (!(sender instanceof Player))
    9. return false;
    10. Player player = (Player) sender;
    11. if (split.length == 0) {
    12. if (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp()) {
    13. player.sendMessage("[usage]");
    14. if (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp())
    15. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append("/do test <playername>:").append(ChatColor.WHITE).append("test").toString());
    16. }
    17. else {
    18. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You don't have permission to use this command.").toString());
    19. }
    20. }
    21. else
    22. if (split.length == 2) {
    23. if (split[0].equals("test") && (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp())) {
    24. if (!testPlugin.getInstance().getActivePlayers().containsKey(split[1])) {
    25. PlayerInfo pi = new PlayerInfo(split[1]);
    26. if (!pi.getHasStuff()) {
    27. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("Error: Invalid Player (check spelling)").toString());
    28. return true;
    29. }
    30. pi.test();
    31. pi.savePlayerConfig(split[1]);
    32. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append(split[1]).append(" tested.").toString());
    33. }
    34. else {
    35. ((PlayerInfo) testPlugin.getInstance().getActivePlayers().get(split[1])).test();
    36. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append(split[1]).append(" tested.").toString());
    37. }
    38. }
    39. else
    40. return true;
    41. }
    42. }
    43. }
    44.  
     
  2. Offline

    Zwander

    If you can get the code to display in a properly formatted manner (IE: with indentation), you are much more likely to get help in future. I have reformatted the code for you here:

    Code:java
    1. public class TestCommand implements CommandExecutor {
    2.  
    3. public TestCommand() {
    4. }
    5.  
    6. public boolean onCommand(CommandSender sender, Command command, String label, String split[]) {
    7. if (!(sender instanceof Player))
    8. return false;
    9. Player player = (Player) sender;
    10. if (split.length == 0) {
    11. if (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp()) {
    12. player.sendMessage("[usage]");
    13. if (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp())
    14. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append("/do test <playername>:").append(ChatColor.WHITE).append("test").toString());
    15. }
    16. else {
    17. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You don't have permission to use this command.").toString());
    18. }
    19. }
    20. else
    21. if (split.length == 2) {
    22. if (split[0].equals("test") && (VaultHandler.checkPerk(player.getName(), "mod.test", player.getWorld()) || player.isOp())) {
    23. if (!testPlugin.getInstance().getActivePlayers().containsKey(split[1])) {
    24. PlayerInfo pi = new PlayerInfo(split[1]);
    25. if (!pi.getHasStuff()) {
    26. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("Error: Invalid Player (check spelling)").toString());
    27. return true;
    28. }
    29. pi.test();
    30. pi.savePlayerConfig(split[1]);
    31. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append(split[1]).append(" tested.").toString());
    32. }
    33. else {
    34. ((PlayerInfo) testPlugin.getInstance().getActivePlayers().get(split[1])).test();
    35. player.sendMessage((new StringBuilder()).append(ChatColor.YELLOW).append(split[1]).append(" tested.").toString());
    36. }
    37. }
    38. else
    39. return true;
    40. }
    41. }
    42. }


    After a quick skim it appears to me that the command are for players only. As in, it would not make sense to run them from the console.
     
  3. Offline

    BlackBeltPanda


    Sorry, didn't realize it wasn't formatted properly.

    The command I posted is coded the same way as the command I need to change. The original command basically changes data related to the player-argument. It doesn't have any affect on the command-sender. At least, it's not supposed to.
     
  4. Offline

    Zwander

    All good
    So the one you posted isn't the one you are changing? We really need the one you are changing because this is quite subjective.

    EDIT: Basically what you need to do is remove the if (sender instanceof Player) check, and the Player player=(Player)sender then replace all the player.whatevers with sender.whatever

    Cant guarantee anything here. It really depends on whats being done.

    EDIT 2: If a player is required you could do something like

    Player player=Bukkit.getPlayer(arg[whatever]);
     
  5. Offline

    BlackBeltPanda


    I just don't want to publicly post part of someone else's plugin. If you're willing to take a look, could I PM it to you?
    If not, I'll keep trying. =)
     
Thread Status:
Not open for further replies.

Share This Page