Solved Help with Bukkit Java Programming!

Discussion in 'Plugin Development' started by ThatOtherGuyLax, May 3, 2014.

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

    ThatOtherGuyLax

    Hello all, tonight I am in need of some assistance with a simple plugin that I am creating... as we speak. At the moment, I have only had about 2 days of actual Java training (online tutorials :p). I continuously run into problems. One problem is that I cannot figure out how to have more that two commands in one class (sorry being such a noob :oops:). Here is the code below that I have been messing around with for my new network of servers:

    Code:java
    1. package me.laxerbrohan.Relicinfo;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Relicinfo extends JavaPlugin{
    13. public Logger logger = Logger.getLogger("Minecraft");
    14. public static Relicinfo plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. PluginDescriptionFile pdfFile = this.getDescription();
    25. this.logger.info(pdfFile.getName() + " Version " +pdfFile.getVersion() + " Has Been Enabled!");
    26.  
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    30. Player player = (Player) sender;
    31. if(commandLabel.equalsIgnoreCase("website")){
    32. player.sendMessage(ChatColor.UNDERLINE + "Our Website:" + ChatColor.GOLD + " [url]http://www[/url]." + ChatColor.AQUA + "RelicNetworks" + ChatColor.GOLD + ".cf");
    33.  
    34. if(commandLabel.equalsIgnoreCase("Relic")){
    35. player.sendMessage(ChatColor.AQUA + "====================================================" + ChatColor.GOLD + "Relic is COMING SOON!" + ChatColor.AQUA + "");
    36. }
    37. return false;
    38. }
    39. return false;
    40. }
    41. }
    42.  


    If you could either explain to me what I am doing wrong and possibly fix it, that would be great. Or, better yet, you could do that and post a "template" with if(commandLabel.equalsIgnoreCase("<cmdname>")){



    Thanks a million! I hope to be as good as some of you one day ;)
     
  2. Offline

    Wizehh

  3. Offline

    BungeeTheCookie

    I was about to say, "wrong section"
     
  4. Offline

    Iroh

    Moved to plugin dev.
     
  5. Offline

    mibac138

    Code:java
    1. if (commandLabel.equalsIgnoreCase(command 1 name) {
    2. //CODE
    3. } else if (commandLabel.equalsIgnoreCase(command 2 name) {
    4. //CODE
    5. } else if (commandLabel.equalsIgnoreCase(command 3 name) {
    6. //CODE
    7. }
    8.  
    9. // And so on
     
  6. Offline

    Jaaakee224

    ThatOtherGuyLax
    Are you watching TheBCBroz?..
    Code:java
    1. package me.laxerbrohan.Relicinfo;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Relicinfo extends JavaPlugin{
    13. public Logger logger = Logger.getLogger("Minecraft");
    14. public static Relicinfo plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. PluginDescriptionFile pdfFile = this.getDescription();
    25. this.logger.info(pdfFile.getName() + " Version " +pdfFile.getVersion() + " Has Been Enabled!");
    26.  
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    30. Player player = (Player) sender;
    31. if(cmd.getName().equalsIgnoreCase("website")){
    32. player.sendMessage(ChatColor.UNDERLINE + "Our Website:" + ChatColor.GOLD + " [url]http://www[/url]." + ChatColor.AQUA + "RelicNetworks" + ChatColor.GOLD + ".cf");
    33.  
    34. } else if(cmd.getName().equalsIgnoreCase("Relic")){
    35. player.sendMessage(ChatColor.AQUA + "====================================================" + ChatColor.GOLD + "Relic is COMING SOON!" + ChatColor.AQUA + "");
    36. }
    37. return false;
    38. }
    39. }
     
  7. Offline

    ThatOtherGuyLax

    Jaaakee224 Yes, yes I am ;), and thanks a million for the help. In one of his videos I watched a couple minutes ago, he explained this.

    ~Lax
     
  8. Offline

    GreatBlitz

    ThatOtherGuyLax
    For your own good, stop watching BCBroz right now. You won't learn half as much if you watch videos (unless they are REALLY good), let alone BCBroz. If I were you, I would first read up the link kind sir Wizehh had provided. Next, I would explore the Bukkit plain javadocs/doxygen and gasp at the possibilities of Bukkit. Next, I would try making an example project.
     
  9. Offline

    Konkz

    No. Stop right there.

    Go learn Java itself then come back to Bukkit API. You are basically trying to write a French
    essay without any lessons, but only a sheet of paper with French words as hints.
     
    ZodiacTheories likes this.
  10. Offline

    Jaaakee224

    ThatOtherGuyLax
    Both GreatBlitz and Konkz said some REALLY important stuff, which can help you fix these problems with ease. On the other hand, TheBCBroz isn't the BEST choice out there, maybe try TheNewBoston or PogoStick29Dev. Also, as GreatBlitz said, the javadocs/doxygen are very helpful, and as Konkz said, learn Java before the Bukkit API. I don't suggest online training for Java though (video tutorials), get a book and read, also if you really want to learn Java, maybe take a class on it. I am going to take a class on Java this summer taught by iDTech (Coding Camp). I hope you learn something out of this, if you have ANY questions about learning, do not hesitate and ask me any questions. Good luck coding!
     
  11. Offline

    ThatOtherGuyLax

    Yes, I have been taking apart plugins, exploring the possibilities of Bukkit, etc. Thanks Jaaakee224, GreatBlitz!. Konkz, I meant to say java training from TheBCBroz. I have been practicing with a friend of mine for quite some time whom I believe is a prodigy (13 years old, can create literally anything relating to C++, HTML, PHP, Java, jQ, JS, ect). Thanks though!
     
  12. Offline

    Jaaakee224

    ThatOtherGuyLax
    13 years old and knows C++? That's awesome :D, having someone like that can help you a lot, but don't rely on them, because then you can learn more.
     
  13. Offline

    ThatOtherGuyLax

    Jaaakee224
    Exactly! Well, he is sort of a "big shot" and is now working on a fairly large server that I shall not name for the time being :p. I am currently starting a server, and this is why I am getting back into java developing!
     
  14. Offline

    Opacification


    Age doesn't fine experience.

    I'm 13 and I know more than a few languages myself ;D
     
  15. Offline

    Jaaakee224

    ThatOtherGuyLax
    Well that is really awesome, hopefully you get a little inspiration from him and then you can create something big, also set this thread to Solved please. PM me instead of reply because I solved your problem and there is no point on bumping a solved thread.

    Opacification You're correct, but even the fact that you are interested in coding blows my mind at this young of an age. (I'm 14, and my family/friends are shocked I love doing this stuff)

    As for you knowing more than a few languages yourself, you should be VERY proud of yourself. Most 13 year olds barely know how to work with computers. (Especially my friends)

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

    Opacification


    Nice, well, I sort of like to push my own limits to my extent, from Java to JavaScript, to other stuff like Python and Ruby, and many other languages. I just find it amazing how pieces come together to form an object.
     
  17. Offline

    ThatOtherGuyLax

    Jaaakee224 & Opacification
    Well, thanks to all that have helped, but I have one last question: If I were to add another command (such a noob :p), how could I do that. I know the code in which I have to use, but just not where to actually put it. Here's my code, atm:

    Code:java
    1. package me.laxerbrohan.Relicinfo;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Relicinfo extends JavaPlugin{
    13. public Logger logger = Logger.getLogger("Minecraft");
    14. public static Relicinfo plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. PluginDescriptionFile pdfFile = this.getDescription();
    25. this.logger.info(pdfFile.getName() + " Version " +pdfFile.getVersion() + " Has Been Enabled!");
    26.  
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    30. Player player = (Player) sender;
    31. if(cmd.getName().equalsIgnoreCase("website")){
    32. player.sendMessage(ChatColor.UNDERLINE + "Our Website:" + ChatColor.GOLD + " [url]http://www[/url]." + ChatColor.AQUA + "RelicNetworks" + ChatColor.GOLD + ".cf");
    33.  
    34. } else if(cmd.getName().equalsIgnoreCase("ultra")){
    35. player.sendMessage(ChatColor.AQUA + "+====================================================+" + ChatColor.GOLD + "ULTRA is COMING SOON!" + ChatColor.AQUA + " +====================================================+");
    36. }
    37. return false;
    38.  
    39. } else if(cmd.getName().equalsIgnoreCase("mega")){
    40. player.sendMessage(ChatColor.AQUA + "+====================================================+" + ChatColor.GOLD + "MEGA is COMING SOON!" + ChatColor.AQUA + " +====================================================+");
    41. }
    42. }
    43. }


    There is obviously something terribly wrong with my code (I don't really understand the "}" but do understand the "{"). Extremely sorry for asking again :p

    ~ Lax
     
  18. Offline

    Esophose

    ThatOtherGuyLax
    This should work, you had some unbalanced brackets.
    You also want to have a return statement if the command was run, so I put those in too.
    Code:java
    1. package me.laxerbrohan.Relicinfo;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Relicinfo extends JavaPlugin{
    13. public Logger logger = Logger.getLogger("Minecraft");
    14. public static Relicinfo plugin;
    15.  
    16. @Override
    17. public void onDisable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. PluginDescriptionFile pdfFile = this.getDescription();
    25. this.logger.info(pdfFile.getName() + " Version " +pdfFile.getVersion() + " Has Been Enabled!");
    26.  
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    30. Player player = (Player) sender;
    31. if(cmd.getName().equalsIgnoreCase("website")){
    32. player.sendMessage(ChatColor.UNDERLINE + "Our Website:" + ChatColor.GOLD + " [url]http://www[/url]." + ChatColor.AQUA + "RelicNetworks" + ChatColor.GOLD + ".cf");
    33. return true;
    34. } else if(cmd.getName().equalsIgnoreCase("ultra")){
    35. player.sendMessage(ChatColor.AQUA + "+====================================================+" + ChatColor.GOLD + "ULTRA is COMING SOON!" + ChatColor.AQUA + " +====================================================+");
    36. return true;
    37. }
    38. else if(cmd.getName().equalsIgnoreCase("mega")){
    39. player.sendMessage(ChatColor.AQUA + "+====================================================+" + ChatColor.GOLD + "MEGA is COMING SOON!" + ChatColor.AQUA + " +====================================================+");
    40. return true;
    41. }
    42. return false;
    43. }
    44. }
     
  19. Offline

    Wizehh

    Put each command in its own class, and have that class implement the CommandExecutor interface. Register the command in your main class's enable method.
    http://wiki.bukkit.org/Plugin_Tutorial#Using_a_separate_CommandExecutor_class
     
  20. Offline

    ThatOtherGuyLax


    Thanks a million!


    Thanks again. Would you happen to know how to edit class files? I had a plugin made a while back and I would like to see how it works :p. Thanks!

    ~Lax

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

    Esophose

    ThatOtherGuyLax
    There isn't really a way to get the class perfectly without obfuscating the source code a bit. You can try Jd-GUI, that's about the closest you are going to be able to get. http://jd.benow.ca/
     
  22. Offline

    ThatOtherGuyLax

  23. Offline

    Esophose

    ThatOtherGuyLax
    Using getPlayer("PlayerName") is now depricated, although it gives you an error, the plugin will still run fine with it. In the future that might be removed. Getting players is being shifted over into UUIDs instead.
     
  24. Offline

    ThatOtherGuyLax

    Ahhh, thanks! I was scared for a second :p
     
  25. Offline

    GreatBlitz

    Jaaakee224
    Oh don't get me started, haha. My friends too wonder, "Why is a 13 year old interested more in developing than in playing?" I learnt Java when I was 10, before Java I learnt HTML, CSS, JavaScript and Python. Python was the base of all my fundamental programming. I've forgotten Python, CSS, and JS for the most part cause I'm more interested in Java.

    ThatOtherGuyLax
    May I recommend a book for java? Its called "Head First Java."
    http://www.headfirstlabs.com/books/hfjava/
     
  26. Offline

    mazentheamazin

    Wizehh
    For a plugin that small, it doesn't really matter.
     
  27. Offline

    ZodiacTheories

  28. Offline

    ThatOtherGuyLax

  29. Offline

    xTrollxDudex

    maxben34 likes this.
  30. Offline

    maxben34

    ZodiacTheories xTrollxDudex There are actually very few out there that "Know Java" I mean in all seriousness, programming and coding is and can be an art to many people. Perfect code is something that a good programmer always aims for. While one can understand the concepts of programming with Java, it is difficult to "know" it.
     
Thread Status:
Not open for further replies.

Share This Page