How to make custom commands in config file?

Discussion in 'Plugin Development' started by The Gaming Grunts, Sep 29, 2013.

Thread Status:
Not open for further replies.
  1. Hey everyone! I was wondering how I would go about letting someone to set custom commands in the config file. Basically what the plugin will do is when someone types "/setwarp example" the player can type "/example" and be teleported to the specific location. I know how to do the teleportation, but don't know how to make it so the warp name is set as a command. How can I go about doing this? Thanks in advance for the help :)
     
  2. Offline

    xize

    you could do this by using a alias, if Im correct there is a way to set the alias from inside the main class but I'm not 100% sure I thought it whas in getPluginDescription() maybe it helps a bit.
     
  3. An alias wouldn't work for this, I don't think.
     
  4. Offline

    bloodless2010

    The Gaming Grunts Funny enough, I recently made a plugin like this, because commands have to be defined in plugin.yml for bukkit to even use it, you have to kinda make a workaround, I'm not even sure if it is allowed by BukkitDev plugin policy so I hope your plugin is Private; I'll just dump my source here I'm too lazy to modify it, I assume you know what to do after this

    Main Class : http://www.hastebin.com/vutudiqihi.coffee
    EventListener.java : http://www.hastebin.com/ticuhugisu.avrasm
    (This is only the inside of the class {}, so you need to import your package and import the different stuff too ;D)

    Here is an example config:
    Code:yml
    1. commands:
    2. /testcommand:
    3. permission: hahaha.lol
    4. chat:
    5. - '&ctest'
    6. - '&btest'
    7. - moar
    8. teleport:
    9. x: 60
    10. y: 100
    11. z: 40
    12. /command:
    13. permission: hahaha.lol
    14. chat:
    15. - '&bYou ran the command /command'
    16. - '&cTeleporting..'
    17. teleport:
    18. x: 181
    19. y: 64
    20. z: 271


    As you can see, you can put multiple lines of chat, you can use colour/format codes, you can use teleport and you can even set a permission for it!

    ~Enjoy
     
  5. Offline

    xize

  6. Awesome! I'm just a little confused on how to exactly incorporate it :(

    bloodless2010
    Ignore what my last message said. Kinda derped a bit. Thanks for the help :) I'll let you know if I have any problems :)

    bloodless2010
    I tried implementing my features, but I get 2 different errors. One is in the onEnable() and the other is when the player types /setwarp.

    onEnable() error:

    Code:
    at com.gmail.thegurugamers.Main.onEnable(Main.java:27)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.reload(CraftServer.java:605)
        at org.bukkit.Bukkit.reload(Bukkit.java:275)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchServerCommand(CraftServer.java:512)
        at net.minecraft.server.v1_6_R2.DedicatedServer.ar(DedicatedServer.java:262)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    /setwarp error:

    Code:
     giantsfan99999 issued server command: /setwarp Hi
    2013-09-29 18:29:06 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setwarp' in plugin CommandTP v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerConnection.java:964)
        at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.java:882)
        at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java:839)
        at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
        at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java:118)
        at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
        at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
        at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path
        at org.apache.commons.lang.Validate.notEmpty(Validate.java:363)
        at org.bukkit.configuration.MemorySection.set(MemorySection.java:159)
        at org.bukkit.configuration.MemorySection.set(MemorySection.java:189)
        at com.gmail.thegurugamers.Commands.onCommand(Commands.java:27)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    Here is the current code I have for all 3 of my classes. Not sure what I did wrong.

    Main.java:

    Code:java
    1. package com.gmail.thegurugamers;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public final class Main extends JavaPlugin {
    13. boolean debug = false;
    14.  
    15. @Override
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(new EventListener(this), this);
    18. getLogger().info("Enabled!");
    19. if (debug) {
    20. getLogger().info("Debug is ON");
    21. }
    22.  
    23. File config = new File(this.getDataFolder(), "config.yml");
    24. if (!config.exists()) {
    25. this.saveDefaultConfig();
    26. }
    27. getCommand("setwarp").setExecutor(new Commands(this));
    28. }
    29.  
    30. @Override
    31. public void onDisable() {
    32. getLogger().info("Disabled!");
    33. saveDefaultConfig();
    34. }
    35.  
    36. public void MSG(String message, Player p) {
    37. String msg = message;
    38. msg = ChatColor.translateAlternateColorCodes('&', msg);
    39. p.sendMessage(ChatColor.DARK_AQUA+"[CustomCommands] " + ChatColor.AQUA + msg);
    40. }
    41.  
    42. @Override
    43. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    44.  
    45. if(cmd.getName().equalsIgnoreCase("customcommands") || cmd.getName().equalsIgnoreCase("cc")){
    46. if(sender instanceof Player) {
    47. Player p = (Player) sender;
    48. if(args.length == 1) {
    49. if(args[0].equalsIgnoreCase("reload")) {
    50. if(sender.hasPermission("customcommands.reload")) {
    51. reloadConfig();
    52. MSG("Reloaded Config!", p);
    53. return true;
    54. } else {
    55. MSG("&cYou don't have permission for this!", p);
    56. }
    57. }
    58. } else {
    59. MSG("You are currently running version: v" + getDescription().getVersion(), p);
    60. }
    61. } else {
    62. getLogger().info("Only players can run this command!");
    63. }
    64. }
    65.  
    66. return false;
    67. }
    68. }


    EventListener.java:

    Code:java
    1. package com.gmail.thegurugamers;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    12.  
    13. public class EventListener implements Listener {
    14. private Main plugin;
    15. boolean debug = false;
    16.  
    17. public EventListener(Main plugin) {
    18. this.plugin = plugin;
    19. }
    20.  
    21. public void debugLog(String msg) {
    22. if (debug) {
    23. plugin.getLogger().info(msg.toString());
    24. }
    25. }
    26.  
    27. @EventHandler
    28. public void playerCommandChat(PlayerCommandPreprocessEvent event) {
    29. debugLog("playerCommandChat event initiated");
    30. debugLog(String.format("Event is cancelled: %s Message sent: %s", String.valueOf(event.isCancelled()), event.getMessage()));
    31. FileConfiguration config = plugin.getConfig();
    32. if (config.contains("commands."+event.getMessage())) {
    33. if(event.getPlayer() instanceof Player) {
    34. if(event.getPlayer().hasPermission(config.getString("commands."+event.getMessage()+".permission"))) {
    35. String command = event.getMessage();
    36. event.setCancelled(true);
    37. debugLog("playerCommandChat event command "+ command +" was ran");
    38.  
    39. /* Start Player chat sendMessage */
    40. if (config.contains("commands."+command+".chat")) {
    41. List<String> chatLines = plugin.getConfig().getStringList("commands."+ command +".chat");
    42.  
    43. for (String s : chatLines) {
    44. event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', s));
    45. }
    46. }
    47. /* End Player chat sendMessage */
    48.  
    49. /* Start Player teleport */
    50. if (config.contains("commands."+command+".teleport")) {
    51. int x;
    52. int y;
    53. int z;
    54. float yaw;
    55. float pitch;
    56.  
    57. Location playerLoc = event.getPlayer().getLocation();
    58. if (config.contains("commands."+command+".teleport.x")) {
    59. x = config.getInt("commands."+command+".teleport.x");
    60. } else {
    61. x = playerLoc.getBlockX();
    62. }
    63.  
    64. if (config.contains("commands."+command+".teleport.y")) {
    65. y = config.getInt("commands."+command+".teleport.y");
    66. } else {
    67. y = playerLoc.getBlockY();
    68. }
    69.  
    70. if (config.contains("commands."+command+".teleport.z")) {
    71. z = config.getInt("commands."+command+".teleport.z");
    72. } else {
    73. z = playerLoc.getBlockZ();
    74. }
    75. //added yaw and pitch
    76. if (config.contains("commands." + command + ".teleport.yaw")){
    77. yaw = config.getInt("commands." + command + ".teleport.yaw");
    78. }else{
    79. yaw = playerLoc.getYaw();
    80. }
    81.  
    82. if (config.contains("commands." + command + ".teleport.pitch")){
    83. pitch = config.getInt("commands." + command + ".teleport.pitch");
    84. }else{
    85. pitch = playerLoc.getPitch();
    86. }
    87.  
    88. Location location = new Location(event.getPlayer().getWorld(), x, y, z, yaw, pitch);
    89. event.getPlayer().teleport(location);
    90. }
    91. /* End Player teleport*/
    92.  
    93. } else {
    94. event.setCancelled(true);
    95. //No permisison to run the command
    96. if (config.contains("commands."+event.getMessage()+".noperms")) {
    97. String msg = config.getString("commands."+event.getMessage()+".noperms");
    98. event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
    99. } else {
    100. event.getPlayer().sendMessage(ChatColor.RED + "You do not have permission to run this command!");
    101. }
    102. }
    103. } else {
    104. event.getPlayer().sendMessage(ChatColor.RED+"This command is for players only!");
    105. }
    106. }
    107. }
    108.  
    109. }


    Commands.java:

    Code:java
    1. package com.gmail.thegurugamers;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.file.FileConfiguration;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class Commands implements CommandExecutor {
    12.  
    13. Main plugin;
    14.  
    15. public Commands(Main instance){
    16. plugin = instance;
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args){
    20. FileConfiguration c = plugin.getConfig();
    21. Player player = (Player) sender;
    22. Location loc = player.getLocation();
    23. if (sender instanceof Player){
    24. if (commandLabel.equalsIgnoreCase("setwarp")){
    25. if (sender.hasPermission("commandtp.setwarp")){
    26. if (args.length > 0){
    27. c.set("commands.", "/" + args[0]);
    28. c.set("commands." + "/" + args[0] + ".permission", null);
    29. c.set("commands." + "/" + args[0] + ".teleport" + ".x", loc.getBlockX());
    30. c.set("commands." + "/" + args[0] + ".teleport" + ".y", loc.getBlockY());
    31. c.set("commands." + "/" + args[0] + ".teleport" + ".z", loc.getBlockZ());
    32. c.set("commands." + "/" + args[0] + ".teleport" + ".yaw", loc.getYaw());
    33. c.set("commands." + "/" + args[0] + ".teleport" + ".pitch", loc.getPitch());
    34. c.set("commands." + "/" + args[0] + ".teleport" + ".world", loc.getWorld());
    35. }else{
    36. sendArgsMessage(player);
    37. }
    38. }
    39. }
    40. }
    41. return false;
    42.  
    43. }
    44.  
    45. public void sendArgsMessage(Player p){
    46. p.sendMessage(ChatColor.DARK_RED + "Not enough arguments! Check your command and try again!");
    47. }
    48.  
    49. }
    50.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    bloodless2010

    The Gaming Grunts I'm currently on my phone and am unable to help you sorry, if you still cant get it to work I suggest using the code I posted and just manually put the coords and command into the config.

    The error seems to be coming from your code and the problem is IllegalArgumentException:Cannot setto anemptypath

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Ik that part :p I just don't know what's causing it.
     
  9. Plugin is still broken and I've yet to figure out a solution :(
     
Thread Status:
Not open for further replies.

Share This Page