Execute Commands from Plugin?

Discussion in 'Plugin Development' started by TheDuceCat, Oct 29, 2011.

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

    TheDuceCat

    Is there a way to execute commands from a plugin? As a simple example, the player types in a command for my plugin, and the plugin will, say, use the server's default /kick. So, how can I, say do /kick from the plugin. (I'm not looking for a way to kick someone, but to run a command that is already in place.)
     
  2. Offline

    arnie231

  3. Offline

    emericask8ur

  4. Offline

    coldandtired

    Here's the first thing I wrote and it does exactly what you want:

    Show Spoiler
    Code:Java
    1. package me.coldandtired.bookmarker;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.plugin.PluginDescriptionFile;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.util.config.Configuration;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12.  
    13. public class bookmarker extends JavaPlugin
    14. {
    15. Logger log = Logger.getLogger("Minecraft");
    16. Configuration config;
    17.  
    18. public void onEnable()
    19. {
    20. PluginDescriptionFile pdf = this.getDescription();
    21. String version = pdf.getVersion();
    22. log.info("Bookmarker " + version + " active!");
    23. config = getConfiguration();
    24. }
    25.  
    26. public void onDisable()
    27. {
    28. log.info("Bookmarker inactive.");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    32. {
    33. String s = "";
    34. Player player = null;
    35. if (sender instanceof Player)
    36. {
    37. player = (Player) sender;
    38. }
    39. if (cmd.getName().toLowerCase().equals("bm"))
    40. {
    41. switch (args.length)
    42. {
    43. case 0: // /bm - show a list of bookmarks
    44. {
    45. player.sendMessage("");
    46. player.sendMessage(ChatColor.DARK_AQUA + "----- Bookmarks -----");
    47. for (int i = 1; i < 10; i++)
    48. {
    49. String j = Integer.toString(i);
    50. s = config.getString(player.getName() + ".bm" + j);
    51. if (s == null) s = "";
    52. if (s.length() > 0) player.sendMessage(" " + ChatColor.YELLOW + j + " - " + s);
    53. else player.sendMessage(" " + ChatColor.GRAY + j + " -");
    54. }
    55. break;
    56. }
    57. case 1: // /bm x - run a bookmark
    58. {
    59. s = config .getString(player.getName() +".bm" + args[0]);
    60. if (s != null && s.length() > 0) player.chat(s);
    61. else
    62. if (args[0].matches("^[1-9]"))
    63. {
    64. player.sendMessage("");
    65. player.sendMessage(ChatColor.DARK_RED + "No bookmark assigned to " + args[0] + "!");
    66. player.sendMessage("");
    67. }
    68. else if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("?"))
    69. {
    70. player.sendMessage("");
    71. player.sendMessage(ChatColor.DARK_AQUA + "----- Usage -----");
    72. player.sendMessage(ChatColor.GOLD + " /bm " + ChatColor.WHITE + "-" + ChatColor.GREEN + " lists all saved bookmarks.");
    73. player.sendMessage(ChatColor.GOLD + " /bm [1-9] " + ChatColor.WHITE + "-" + ChatColor.GREEN + " uses the selected bookmark.");
    74. player.sendMessage(ChatColor.GOLD + " /bm [1-9] assign [/command or chat message] " + ChatColor.WHITE + "-" + ChatColor.GREEN + " assigns a command to the selected bookmark.");
    75. player.sendMessage(ChatColor.GOLD + " /bm [1-9] clear " + ChatColor.WHITE + "-" + ChatColor.GREEN + " clears the selected bookmark.");
    76. player.sendMessage("");
    77. player.sendMessage(ChatColor.DARK_RED + " Add a / to the beginning of a command, otherwise it will be treated as a chat message!");
    78. player.sendMessage("");
    79. }
    80. else
    81. {
    82. player.sendMessage("");
    83. player.sendMessage(ChatColor.DARK_RED + "A bookmark needs to be a number from 1 to 9!");
    84. player.sendMessage("");
    85. }
    86. break;
    87. }
    88. case 2: // /bm x delete
    89. {
    90. if (args[1].equalsIgnoreCase("clear"))
    91. {
    92. s = args[0];
    93. if (s.matches("^[1-9]"))
    94. {
    95. config.setProperty(player.getName() + ".bm" + s, null);
    96. config.save();
    97. player.sendMessage("");
    98. player.sendMessage(ChatColor.DARK_AQUA + "Bookmark " + s + " cleared!");
    99. player.sendMessage("");
    100. }
    101. }
    102. }
    103. default: // /bm x assign command or chat
    104. message
    105. {
    106. if (args[1].equalsIgnoreCase("assign"))
    107. {
    108. s = args[0];
    109. if (s.matches("^[1-9]"))
    110. {
    111. String new_command = "";
    112. for (int k = 2; k < args.length; k++)
    113. {
    114. new_command = new_command + args[k];
    115. if (k != args.length - 1) new_command = new_command + " ";
    116. }
    117. config.setProperty(player.getName() + ".bm" + s, new_command);
    118. config.save();
    119. player.sendMessage("");
    120. player.sendMessage(ChatColor.DARK_AQUA + "'" + new_command + "' assigned to bookmark " + s + "!");
    121. player.sendMessage("");
    122. }
    123. }
    124. }
    125. }
    126. return true;
    127. }
    128. return false;
    129. }
    130. }


    It also saves each player's favourites. It uses the old config but it's an easy fix.

    Feel free to look through it and examine how it works.
     
  5. Shameless self-promotion, lol.
     
Thread Status:
Not open for further replies.

Share This Page