Flying code?

Discussion in 'Plugin Development' started by dsmyth1915, Mar 15, 2012.

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


    I formatted this code in class today, I'm wondering if it would actually work, or would it give me a ton of errors as soon as I drop it into a plugin.

    public boolean onCommand(CommandSender senderCommand cmdString CommandLabelString[] args){
        if (
    sender isntanceOf Player)){
    sender.sendMessage("Only players may use this command!")}
            else if (
    sender instanceOf Player){
    Player sender sender.getName();
            else return 
    Being on my phone, this may have a few cap mistakes or spacing mistakes. Other than that, will appreciate any help or critism that you may have. Thank you for your time.
  2. Offline


    i don't know much about java but i think you should make from this:
    if getAllowFlight(false){
    if getAllowFlight(true){
    to this:
    if (getAllowFlight.equals(true)){
    if (getAllowFlight.equals(true)){

    and you placed your return wrong

    i a'm not sure so correct me if i'm wrong!
  3. Offline


    Equals true is probably correct, and the returns I can fix when I get home. Anything else you see wrong?
  4. Offline


    Player sender = sender.getName();

    Should be:

    Player player = (Player) sender;

    as you've already got a variable called sender and you're declaring it as Player but setting it as String.

    Also, this would be easier:


    You've also misplaced a lot if brackets {} and it's unnecessary to check if the sender instanceof Player twice.

    So, this is our result:

    public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
            Player player;
            if(!(sender instanceof Player)){
                sender.sendMessage("Only players may use this command!")
                return false;
            player = (Player) sender;
            return true;
    Edit: And of cource, you have to return something even if the command isn't "fly".
  5. Offline


    Else return false; should e in there. Thank you very much :)
  6. Code:java
    2. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    3. if (cmd.getName().equalsIgnoreCase("fly"){
    4. if(!(sender isntanceOf Player)){
    5. /* ^^ unknown keyword: isntanceOf */
    6. sender.sendMessage("Only players may use this command!")}
    7. /* ^^ Missing ";" */
    8. return false;
    9. /* ^^ Code stops here, at this false, thats causing an
    10.   unreachable statement at the line below */
    11. else if (sender instanceOf Player){
    12. /* ^^Unknown keyword: "instanceOf" */
    13. /* ^^ unknown "else", you need to place it direct after an "if" statement */
    14. Player sender = sender.getName();
    15. /* ^^ String cant be cast to an player */
    16. /* ^^ You already defined "sender" */
    17. sender.getAllowFlight(){
    18. /* ^^ Missing if statement or missing ;*/
    19. if getAllowFlight.equals(false){
    20. /* ^^ Never defined "getAllowFlight" */
    21. /* ^^ missing () at the if statement */
    22. setAllowFlight.(True)}
    23. /* ^^ You never defined variable " setAllowFlight" */
    24. /* ^^ You forgot the methode name after " setAllowFlight .(True)" */
    25. /* ^^ There is no class named "True" */
    26. return true;
    27. /* ^^ code stops here, and causing unreachable stament errors on lines after */
    28. if getAllowFlight.equals(true){
    29. /* ^^ missing () at if statement */
    30. /* ^^ never defined " getAllowFlight " */
    31. setAllowFlight(false)}
    32. /* ^^ Unknown methode: setAllowFlight(boolean) */
    33. /* ^^ missing ';' */
    34. return true;
    35. }
    36. return true;
    37. else return false;
    38. }}
    39. /* ^^ Missing return statement */
  7. Offline


    Thank you ferrybig , vilda was able to simplify shorten and correct most of it,

    public boolean onCommand(CommandSender senderCommand cmdString CommandLabelString[] args){
    Player player;
    sender instanceof Player)){
    sender.sendMessage("Only players may use this command!")
    player = (Playersender;

  8. Offline


    So, I have finally been able to get to a computer with eclipse. I plugged the code. I corrected a few bracket errors, and tested it. But nothing is working. here is what I have so far;

    1. import java.util.logging.Logger;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.entity.Player;
    5. import;
    7. public class Main extends JavaPlugin{
    9. Logger log;
    10. public void onEnable(){
    11. log = this.getLogger();
    12."Plugin has been enabled.");
    13. }
    14. public void onDisable(){
    15."Plugin has been disabled.");
    16. }
    17. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    18. if(cmd.getName().equalsIgnoreCase("fly")){
    19. Player player;
    21. if(!(sender instanceof Player)){
    22. sender.sendMessage("Only players may use this command!");
    23. return false;
    24. }
    25. else {
    26. player = (Player) sender;
    27. player.setAllowFlight(!player.getAllowFlight());
    28. return true;
    29. }
    30. }
    31. return true;
    32. }
    33. }

    Any suggestions?
    EDIT: And by "Nothing is working" I mean when I type in the code, I get nothing. No errors, no altered mechanics. Doesn't make me "fly" Like I had hoped.
  9. Offline


    If you are expecting it will make you fly, I don't think this will. I believe this just gives you the ability to fly. Try double jumping and see if you can fly.
  10. Offline


    Well, when I type in the command, it says "Unkown command" there's no errors in console, and from I understand it /should/ work.
  11. Offline


    Check your plugin.yml and make sure the command is set up properly and that you follow the format. Also make sure you are registering the command onEnable().
  12. Offline


    That could be it. I don't have it set it onEnable(). Time to go and look back in wiki.bukkit plugintut. I'll repaste when I get it right. Thanks for your help :)
  13. Offline


    Your welcome :) By the way the problem is the fact that your not registering anything for onEnable(). onEnable() is pretty essential to a plugin (my opinon). Then within that add the register code that should be on the wiki or just look at someones code about simple commands through youtube videos or github links off that.
    Hope it works!
    (By the way I think this only lets you have thw ability to fly through mods/Clients. I recommend checking out a youtube video that shows you how to make a fly command.)
  14. Offline


    Well I think I found the problem. I got it to work and display the message "Only users are able to use this command!" to the console. but as a player it sais I do not5 have permission to use it. I looked back into the jd.bukkit and found that there are two getAllowFlight and setAllowFlight methods. One for letting people in the server fly(Like in creative) and one in org.bukkit.plugin.messaging.TestPlayer node where It sais "Ability to set if a player is able to fly using double space". Is there anyway to distinguish in the code which should be used?
  15. Offline


    no you werent oped when you tried it. op yourself.

    give the command a permission and use permissionsex

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


    Had already been done. I'm going to keep fiddling

    No more permission problem, but I think my problem now is it's not saving the players ability to fly. I've also added a player.sendMessage to say that they can fly. it displays that but When I try to doubletap fly it doesn't let me. which brought me to the conclusion of it's not saving the fly variable anywhere. here's the oncommand method.

    1. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("fly")){
    3. Player player;
    5. if (!(sender instanceof Player)){
    6. sender.sendMessage("Only players may use this command!");
    7. return false;
    8. }
    9. player = (Player) sender;
    10. player.setAllowFlight(!(player.getAllowFlight()));
    11. sender.sendMessage("You can now fly!");
    12. return true;
    13. }return true;
    14. }
    15. }
    17. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 24, 2016
  17. Offline


                    if ((commandlabel.equalsIgnoreCase("fly")))
                      if(args.length == 0){
                          if (player.getAllowFlight() == (true)){
                              player.sendMessage(" Flying Disabled");
                          } else {
                                player.sendMessage(" Flying Enabled");
    This always works.
Thread Status:
Not open for further replies.

Share This Page