[NOOB] Best way to code commands?

Discussion in 'Plugin Development' started by Artaelz, Feb 27, 2014.

?

Do most people use a specific way or different than other people

  1. Specific

    83.3%
  2. Different

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

    Artaelz

    Hello bukkiters, in the last few days I've been trying to learn java and bukkit coding. Though the videos i watched on youtube were kinda old so maybe things have changed and there is a new better way to make commands.

    How would a professional code a command like /heal <PLAYER> <AMOUNT> but if it's written like /heal 10 then it heals the command sender.

    Thank you.
     
  2. Offline

    CubieX

    I don't think this has changed.
    Check the length of the "args[]" array.
    If the length of args[] is 1, then heal the issuing player (-> the "sender") by the value of the 1st argument.
    If the length of args[] is 2, then try to find a player with the name of the 1st argument and heal him by the value of the 2nd argument (if it's a positive number).
     
  3. Offline

    Artaelz

    CubieX
    Thanks a lot, this cleared things in my mind but that didn't explain my whole question. I also want to know how to actually code a command which can be used. Can you make an example if it doesn't bother you?
     
  4. Offline

    Booshayy

    I'll do it.
    I'm assuming you already know how to use public boolean onCommand already, so I'll just make the command "if" statement.

    Code:java
    1. Player player = (Player) sender;
    2. if (cmd.getName().equalsIgnoreCase("heal") {
    3. if (!(player.hasPermission("heal.heal")) || (player.isOp()) { //making sure the player has permission or is OP
    4. player.sendMessage(ChatColor.RED + "You do not have permission to perform this command!");
    5. } else if (args.length == 0) { // will heal the command sender if they type /heal
    6. player.setHealth(20.0F); //health
    7. player.setFoodLevel(20.D); //food
    8. player.setFireTicks(0); //fire
    9. player.sendMessage(ChatColor.AQUA + "Healed!");
    10. } else if (args.length == 1) { //will heal the player specified after /heal
    11. targetPlayer.setHealth(20.0F); // targetPlayer refers to the player specified after /heal
    12. targetPlayer.setFoodLevel(20.0D);
    13. targetPlayer.setFireTicks(0);
    14. player.sendMessage(ChatColor.AQUA + "You have healed " + targetPlayer.getName() + ".");
    15. targetPlayer.sendMessage(ChatColor.AQUA + "You have been healed by " + player.getName() + "! Remember to thank them!");
    16. } else {
    17. } else {
    18. }
     
  5. Offline

    Niknea

    Artaelz I hope your not watching TheBCBros
     
    DogeDev, FadedMystery and Blah1 like this.
  6. Offline

    PatoTheBest

    I learned from TheBcBroz, but later I discovered PogoStick29Dev
     
  7. Offline

    badboysteee98

    Quick Example Code :p Messing around to show what I do for Commands/Permission​
    Code:java
    1.  
    2. public void onEnable() {
    3. Bukkit.getServer().getLogger().info("Help Enabled!");
    4. }
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    7.  
    8. Player p = (Player) sender;
    9.  
    10. if(!(sender instanceof Player)) {
    11. sender.sendMessage(ChatColor.RED + "Only Player's Can Run That Command");
    12. }
    13.  
    14. if(cmd.getName().equalsIgnoreCase("Help")) { //The Command Is Help
    15. if(!p.hasPermission("helping.help")) { //If The Player Has That Permission It Will Do The Part Inside Here!
    16. p.sendMessage(ChatColor.GREEN + "Hope This Helped"); //Send Them A Nice Message :)
    17. p.setHealth(20); //Be Kind And Give Them Full Help :P
    18. p.setFoodLevel(20); //They Mite Be Hungry
    19. }
    20. } else { //If They Don't Have The Command Kick Them And Tell Them ;)
    21. p.kickPlayer("Kicked Because It Didn't Help :P");
    22. }
    23. return true; //Always return true; (For Most Thing) So Bukkit know's the Command was successful :)
    24. }
    25.  
    26. }


    I learned from Pogo now I read Books as what Pogo tough me wasn't everything I needed to know

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

    calebbfmv

    PatoTheBest
    If that was your past, I feel for you.
     
    MrInspector likes this.
  9. Offline

    PatoTheBest

  10. Offline

    calebbfmv

    PatoTheBest
    I think you need to see a doctor. I don't do tuts, yet!
     
  11. Offline

    PatoTheBest

  12. Offline

    Booshayy

    SO SORRY, I FORGOT TO DEFINE TARGET PLAYER.
    Here's the code again with targetPlayer defined

    Code:java
    1. Player player = (Player) sender;
    2. Player t = this.getServer().getPlayer(args[0]);
    3. if (cmd.getName().equalsIgnoreCase("heal") {
    4. if (!(player.hasPermission("heal.heal")) || (player.isOp()) { //making sure the player has permission or is OP
    5. player.sendMessage(ChatColor.RED + "You do not have permission to perform this command!");
    6. } else if (t == null) {
    7. player.sendMessage(ChatColor.DARK_RED + "Player not found!");
    8. } else if (args.length == 0) { // will heal the command sender if they type /heal
    9. player.setHealth(20.0F); //health
    10. player.setFoodLevel(20.D); //food
    11. player.setFireTicks(0); //fire
    12. player.sendMessage(ChatColor.AQUA + "Healed!");
    13. } else if (args.length == 1) { //will heal the player specified after /heal
    14. t.setHealth(20.0F); // targetPlayer refers to the player specified after /heal
    15. t.setFoodLevel(20.0D);
    16. t.setFireTicks(0);
    17. player.sendMessage(ChatColor.AQUA + "You have healed " + t.getName() + ".");
    18. t.sendMessage(ChatColor.AQUA + "You have been healed by " + player.getName() + "! Remember to thank them!");
    19. } else {
    20. player.sendMessage(ChatColor.DARK_RED + "You don't have permission!");
    21. } else {
    22. }


    My bad for weird spacing.... some glitch.
     
    Artaelz likes this.
  13. Offline

    calebbfmv

  14. Offline

    Artaelz

    Booshayy
    Waow thanks! Your code made me understand much of how things work and the comments (//) are well explained.
    badboysteee98
    Yea, that seems the best way to make the permissions from what i have seen so far.

    Niknea
    I'm watching and learning from ApplJuze about bukkit and for the java basics i got it from thenewboston. Is there anyone you might recommend?

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

    xTrollxDudex

    Artaelz
    I never watch videos. A person who makes a codeo on youtube is obviously too poor to host his own videos, get a book from the library. I reccommend Head First Java and Java for Dummies. Once you get down the basiics, read Essential Java, discusses intermediate topics to inprove your code, such as security, field shading, performance, serialization, hashing, things like so.
     
    Artaelz likes this.
  16. Offline

    Artaelz

    xTrollxDudex
    I have considered to borrow a book from my local library but the thing is im in Denmark and i am afraid that my english is not good enough to understund what a book of such kind would say.
     
  17. Offline

    Rocoty

    Artaelz no harm done in trying :)
     
  18. Offline

    MordorKing78

    Everyone answerd this so... Just use arguments :)
     
  19. Offline

    xTrollxDudex

    Artaelz
    Its a basic book, with the english you have now, you shoild be able to understand about 90% of it.
     
  20. Offline

    Booshayy

    Youre welcome :D
     
Thread Status:
Not open for further replies.

Share This Page