Help with plugin (plugin works, command doesn't)

Discussion in 'Plugin Development' started by sparkhead95, Mar 13, 2014.

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

    sparkhead95

    Hi,
    My plugin is somewhat simple, however it is not working.
    The plugin loads fine, no errors, however when i type the command that should change the value of my config.yml, nothing happens. It simply comes up with the usage of the command.
    Here is the code:
    Code:java
    1. package me.sparkhead95;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.GameMode;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.event.player.PlayerToggleFlightEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. //import org.bukkit.command.Command;
    15. //import org.bukkit.command.CommandSender;
    16.  
    17. import java.util.logging.Logger;
    18.  
    19. public class DoubleJumpMain extends JavaPlugin implements Listener {
    20.  
    21. Logger logger = Logger.getLogger("Minecraft");
    22.  
    23. double jheightdefault = 1;
    24.  
    25. public void onEnable() {
    26. getServer().getPluginManager().registerEvents(this, this);
    27. getConfig().options().copyDefaults(true);
    28.  
    29. getConfig().set(".Jump Height = ", jheightdefault);
    30. saveConfig();
    31. logger.info("Jumpaholic Enabled!");
    32. }
    33.  
    34. public void onDisable() {
    35. logger.info("Jumpaholic Disabled!");
    36. saveConfig();
    37. }
    38.  
    39. @EventHandler
    40. public void onPlayerToggleFlight(PlayerToggleFlightEvent event){
    41. Player player = event.getPlayer();
    42. if (player.getGameMode() == GameMode.CREATIVE)
    43. return;
    44. double jheight = getConfig().getDouble(".Jump Height = ");
    45. event.setCancelled(true);
    46. player.setAllowFlight(false);
    47. player.setFlying(false);
    48. player.setVelocity(player.getLocation().getDirection().multiply(0.5).setY(jheight));
    49. player.getLocation().getWorld().playEffect(player.getLocation(), Effect.SMOKE, 3);
    50.  
    51. }
    52.  
    53. @EventHandler
    54. public void onPlayerMove(PlayerMoveEvent event){
    55. Player player = event.getPlayer();
    56. if ((player.getGameMode()!= GameMode.CREATIVE) && (player.getLocation().subtract(0,1,0).getBlock().getType()!= Material.AIR)&&(!player.isFlying())) {
    57. player.setAllowFlight(true);
    58. }
    59. }
    60.  
    61.  
    62. public boolean onCommand(CommandSender sender, Command command,
    63. String commandlabel, double[] args) {
    64. if(sender instanceof Player){
    65. Player player = (Player) sender;
    66. if(commandlabel.equalsIgnoreCase("setjheight")){
    67.  
    68. //arg 0 = first word entered after command, arg 1 = second etc.
    69. int length = args.length;
    70. if (length == 1) {
    71. double newjheight = args[0];
    72. getConfig().set(".Jump Height = ", newjheight);
    73. saveConfig();
    74. player.sendMessage("Jump height has been written. New value is: " + newjheight + ".");
    75. }
    76.  
    77. }
    78. }
    79. return false;
    80. }
    81. }
    82.  



    As you can see, if i were to type "/setjheight 0.5", according to my code this should work.
    The config.yml output once the plugin is running is as follows:
    This is the default value assigned at the top.

    Here is a screenshot of the result of the input of this code once the game is running: 48f3114d6e6a0a4ec7381eb6d5de4207.png

    And this is when i have typed:
    Any help will be greatly appreciated. If you need more information or i have done something wrong, please be patient as i'm new here.

    Thanks in advance,
    Sparkhead95
     
  2. Offline

    tamajpm

    Do you have all commands registred in the plugin.yml?
     
  3. Offline

    sparkhead95

    Ah, sorry, forgot the YML. Here it is:

     
  4. Offline

    Arcoz

    Your plugin.yml is setup invalid.

    This is an example of a proper YAML(LINK)
    Code:
    name: YourName
    main: your.main.Class
    version: 2.3
    description: This is the description of this plugin
    commands:
        yourbasecommand:
          description: What It Does
          usage: How to use it message, if you're returning false
     
    tamajpm likes this.
  5. Offline

    tamajpm

    Maybe this helps:

    Code:
    name: Jumpaholic
    main: me.sparkhead95.DoubleJumpMain
    version: 2.3
    authors: [Sparkhead95]
    commands:
        setjheight:
              description: Set how high you want the player to jump!
              usage: /<setjheight> [double value]
     
  6. Offline

    sparkhead95



    Appreciate the help guys, however my YAML was valid, i just put it into the 'Quote' function on here and it took away the indents.
    But i tested both of your YML's just to be safe, and the same outcome happened.

    Arcoz, you said that "How to use it message, if you're returning false", I can confirm that i must be returning false immediately because my 'onCommand' method is not affecting my config.yml.
    E.g if i type "/setjheight 0.5", if i reload the config.yml, nothing has been effected, and the "usage" message comes up.

    Sorry to be a nuisance guys, really appreciate your help though.
     
  7. Offline

    Arcoz

    Can you show your full config.yml? sparkhead95

    EDIT: Also try to return true; after the command has been executed:
    EXAMPLE:

    if(commandLabel.equalsIgnoreCase("test"){
    //do something
    return true;
    }
     
    tamajpm likes this.
  8. Offline

    sparkhead95


    aa58f42e7d1d4ad5049d536d30d464e0.png



    It's not very big.
     
  9. Offline

    tamajpm

    Arcoz +1

    Why do you use ' and = ? Why not only "JumpHeight".

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

    sparkhead95

    I don't really know why it's putting those ' in. But i thought the = was a fair value to put in? i'll take it out to see if it fixes it.
    Thanks. Implemented it, now testing
     
  11. Offline

    sgavster

    Its not working because its
    String[] args not double[] args.
     
    Arcoz likes this.
  12. Offline

    Arcoz

    Why do you also have a (dot) before your config string finder?

    I would change it to this onEnable:
    getConfig().set("JumpHeight", jheightdefault);

    And when you're setting the jumpheight again use
    getConfig().set("JumpHeight", args[0]);

    And then to get from it is pretty straight foward
    getConfig().getInt("JumpHeight", args[0]);

    I don't think YAMLs allow spaces like that, also because you're using '' to case-in the string finder.

    EDIT: as said above it's String[] args not double[] args, at your onCommand method
     
  13. Offline

    sparkhead95

    I need it to be a double value though. Thus the arguments must be double.


    I'll try and implement what you've said, but i need the args to be double values as the ideal value for the jumpheight should be about 0.5/0.4
     
  14. Offline

    Arcoz


    Just use getConfig().getDouble(string) ?

    I don't see why you have to change the method.
     
  15. Offline

    tamajpm

    Maybe you need to parse it?

    Double.parseDouble(args[0]);

    ???

    I hate double posts but you need to check if the args[0] is a double and not a string or something.

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

    sgavster

    ..........

    You make it Sring[] args.
    You then do this:
    Code:java
    1. try {
    2. double dbl = Double.parseDouble(args[0]);
    3.  
    4. } catch (Exception ex) { ex.printStackTrace(); }
    5.  
    6. //do something with dbl.


    You can't just change the method .-.
     
  17. Offline

    tamajpm

    Code:java
    1. int number = null;
    2.  
    3. try {
    4. number = Double.parseDouble(args[0]);
    5. } catch(Exception e) {
    6. cs.sendMessage("You need to put a double as argument.");
    7. }
    8.  
    9. //Do something with the number.
     
  18. Offline

    sparkhead95


    Thanks for your help.

    Thanks for your help too, however why would i make "number" an integer? It must be a double




    THANKYOU! This is the fix. This works.

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

    tamajpm

    Sorry wrong xD, it needs to be a double :p

    sparkhead95

    You're welcome ;)

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

    sparkhead95



    Sorry to bring this thread back, but now I have somewhat a minor problem.
    The plugin works; the jump height is being set and read efficiently, however my old attempts to make the plugin work are still in the config file! if you look here:
    ce375a32f1bd3a8401ec16851dae2af0.png

    The bottom "JumpHeight" is the value being read and set as it should be.
    But these other two values, the other two jump heights, are staying in my config every time i reload the plugin/server and they're not even in my code any more.
    My current (Functional) code:
    Code:java
    1. package me.sparkhead95;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.GameMode;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.bukkit.event.player.PlayerToggleFlightEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. //import org.bukkit.command.Command;
    15. //import org.bukkit.command.CommandSender;
    16.  
    17. import java.util.logging.Logger;
    18.  
    19. public class DoubleJumpMain extends JavaPlugin implements Listener {
    20.  
    21. Logger logger = Logger.getLogger("Minecraft");
    22.  
    23. double jheightdefault = 0.3;
    24.  
    25. public void onEnable() {
    26. getServer().getPluginManager().registerEvents(this, this);
    27. getConfig().options().copyDefaults(false);
    28.  
    29. getConfig().set("JumpHeight", jheightdefault);
    30. saveConfig();
    31. logger.info("Jumpaholic Enabled!");
    32. }
    33.  
    34. public void onDisable() {
    35. saveConfig();
    36. logger.info("Jumpaholic Disabled!");
    37. }
    38.  
    39. @EventHandler
    40. public void onPlayerToggleFlight(PlayerToggleFlightEvent event){
    41. Player player = event.getPlayer();
    42. if (player.getGameMode() == GameMode.CREATIVE)
    43. return;
    44. double jheight= getConfig().getDouble("JumpHeight");
    45. event.setCancelled(true);
    46. player.setAllowFlight(false);
    47. player.setFlying(false);
    48. player.setVelocity(player.getLocation().getDirection().multiply(0.5).setY(jheight));
    49. player.getLocation().getWorld().playEffect(player.getLocation(), Effect.SMOKE, 3);
    50.  
    51. }
    52.  
    53. @EventHandler
    54. public void onPlayerMove(PlayerMoveEvent event){
    55. Player player = event.getPlayer();
    56. if ((player.getGameMode()!= GameMode.CREATIVE) && (player.getLocation().subtract(0,1,0).getBlock().getType()!= Material.AIR)&&(!player.isFlying())) {
    57. player.setAllowFlight(true);
    58. }
    59. }
    60.  
    61.  
    62. public boolean onCommand(CommandSender sender, Command command,
    63. String commandlabel, String[] args) {
    64. if(sender instanceof Player){
    65. Player player = (Player) sender;
    66. if(commandlabel.equalsIgnoreCase("setjheight")){
    67.  
    68. //arg 0 = first word entered after command, arg 1 = second etc.
    69. int length = args.length;
    70. if (length == 1) {
    71. double newjheight = Double.parseDouble(args[0]);
    72. getConfig().set("JumpHeight", newjheight);
    73. saveConfig();
    74. player.sendMessage("Jump height has been written. New value is: " + getConfig().getDouble("JumpHeight") + ".");
    75.  
    76. }
    77. return true;
    78. }
    79. }
    80. return false;
    81. }
    82. }



    Really appreciate the feedback and support i get on this site.
     
  21. Offline

    Arcoz

    Delete the config.yml you have in your plugin folder: /plugins/Pluginname/config.yml and reload the server?
     
    tamajpm likes this.
  22. Offline

    sparkhead95



    Thank you, and thank everyone else. This has been very useful.
     
Thread Status:
Not open for further replies.

Share This Page