[Newbie Tutorial] The ultimate guide to code a Bukkit plugin!

Discussion in 'Resources' started by TigerHix, Apr 30, 2014.

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

    TigerHix

    CHAPTER 0. INTRODUCTION

    You arrived Bukkit's forums. And you clicked accidentally or purposely click into this thread. Well, if you are fresh to Java programming or Bukkit API or both, you've came to the right place.

    So what is Java? A programming language. What a programming language did? Put it in a nutshell, it's used to make a program that can be read by your adorable computer, and your computer will be able to do something as you wish. Like creating a program that keep printing "your mom" jokes, or making a game that runs on your smartphone, they all requires you to code using a programming language.

    In Bukkit's case, we create programs using Minecraft and Bukkit's programming language: Java, which are usually named "plugins", that the Bukkit server will load and execute the functions you set in the program.

    So how does Java codes in Bukkit look like? For example, if we want to write some code that will send a message to the console, telling your user your plugin is the best, it should be like this:

    Code:java
    1. Bukkit.getConsoleSender().sendMessage("My plugin is awesome");


    Fair easy to you, isn't it? "Though I don't understand what do those brackets and full stops for, I could still understand the most of it," you might think.

    ..or write a code that will set a player's health to 20.0, the maximum player health.

    Code:java
    1. player.setHealth(20.0);


    Even easier. How about a code that will auto respawn players when they are died? Definitely a cool feature to my server. And it doesn't seem to be very hard.

    Here you go:

    Code:java
    1. try {
    2. Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player);
    3. Object con = nmsPlayer.getClass().getDeclaredField("playerConnection").get(nmsPlayer);
    4. Class<?> EntityPlayer = Class.forName(nmsPlayer.getClass().getPackage().getName() + ".EntityPlayer");
    5. Field minecraftServer = con.getClass().getDeclaredField("minecraftServer");
    6. minecraftServer.setAccessible(true);
    7. Object server = minecraftServer.get(con);
    8. Object list = server.getClass().getDeclaredMethod("getPlayerList").invoke(server);
    9. Method moveToWorld = list.getClass().getMethod("moveToWorld", EntityPlayer, int.class, boolean.class);
    10. moveToWorld.invoke(list, nmsPlayer, 0, false);
    11. } catch (Exception ex) {
    12. ex.printStackTrace();
    13. } // From a thread on Bukkit forum.


    "Wait, how come it become suddenly complicated and unreadable!? What's all of this!?!?"

    The reason is, Bukkit's API has it's own limits.

    An API can be viewed as an extension to a program, making this program interact-able with other programs. In the first 2 examples above, we used Bukkit's API to done our objectives. It's API shares it's built-in features for us, like sending message to console and set health for a player as what we've done in the examples.

    Of course, Bukkit's API is not only limited to sending message and setting health, it can done many many things, setting weather, changing item's name, creating custom mobs, etc.

    But as I said, Bukkit's API also has limitations. Like, you can cancel the death of a player, you can cancel the movement of a player, but you cannot cancel players from opening a book. Isn't that strange?

    Bukkit's API isn't perfect, but if we still want to achieve a feature, what should we do? Unfortunately, by 2014/5/1, I can't found any way to cancel players from opening a book. But we can do the auto respawn one, through some "hack" that we called NMS code, to the Minecraft.

    But let's talk about that later. I'm sure you are already confused, so let's end this clumsy introduction by a simple conclusion:

    1. Programming language is the way you create a program, and let it communicates with your computer to tell the computer do something.

    2. Bukkit's programming language is Java.

    3. A plugin is a program that hook into the Bukkit's API and eventually achieve something on a Bukkit server.

    Let's move into the next chapter.

    (Still editing :p)

    RESERVED.

    Chapter 1~3 is W.I.P. Mainly showing how to setup IDE and have your first plugin made. It shall introduces basics to Event API, CommandExecutor and permissions.

    Meanwhile, you can refer to Bukkit's official Wiki:

    http://wiki.bukkit.org/Setting_Up_Your_Workspace
    http://wiki.bukkit.org/Plugin_Tutorial
    http://wiki.bukkit.org/Event_API_Reference

    And an interactive Java tutorial, though it has left lots of things unexplained.

    http://www.learnjavaonline.org/en/Welcome

    CHAPTER 1. MAKING YOUR FIRST PLUGIN

    How do we code a plugin using Java then? Well first make sure you have JDK installed, and just choose an 3rd party IDE that suits you. You can also use notepad and JDK to make a plugin, but unfortunately it won't be covered in this tutorial.

    An IDE - Integrated Development Environment is a software that provides must-have things, like editing source code (otherwise how can we code!?), and also useful awesome features like:
    • debugging (attempt to remove bugs from your plugin through testing)
    • auto-completion (for example you have something in your plugin called "getPlayerInventoryAutomatically", you can just type "ge" and the IDE will popup a window contains all things that starts with "ge" so you won't need to type the full-name every time),
    • better user interface than notepad
    Many tutorials recommend Eclipse; it's cool, but I suggest IntelliJ instead. Better-looking interface, smarter auto-completion, find usages in the whole project, basically it's just awesome. Install the community version here, and let's write our first Bukkit plugin.

    RESERVED 2.

    RESERVED 3.

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

    Tehmaker

    There are so many guides out there-- do something unique. Don't just do the "basics," because people can find that all over. Do interesting things like events, etc.
     
  3. Offline

    Garris0n

    http://wiki.bukkit.org/Event_API_Reference
     
  4. Offline

    Onlineids

    Do the conversation API ;)
     
  5. Offline

    BungeeTheCookie

    TigerHix
    There are probably dozens, if not hundreds of tutorials on creating basic Bukkit plugins. Half of them are on YouTube, about one-third are on the Bukkit Resources (no one looks at them unless they are by TnT or something,) and there is even one created by Bukkit itself on the Bukkit Wiki. I do not think anyone will find use in looking at this thread. I advise that you do not waste your time writing this up and create a resource that people will actually use.

    Wow. Bukkit Wiki is so creative.

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

    xTrollxDudex

    Not bukkit. Sagacious_Zed ;)
     
    TigerHix likes this.
  7. Offline

    BungeeTheCookie

    Have a nice day. :p
     
    xTrollxDudex likes this.
  8. Offline

    TigerHix


    Well, look at Plugin Development forum. Many questions aren't even Bukkit-related, mostly using a for loop wrongly. The purpose to write this tutorial is basically teaching Java, I guess :p

    Anyway, this guide is not limited to "creating a plugin" and "java basics", but also includes things that one should know, for example NMS, Conversation API, and I will attempt to teach creating a minigame in the last chapter of the tutorial.
     
  9. Offline

    Onlineids

    You should probably rename this to java tutorial and remove ultimate. Instead of:
    the ultimate guide to code a bukkit plugin
     
  10. Offline

    Slikey

    Newbies shouldn't learn about NMS and OBC classes :/ What makes things worse :/
     
  11. Offline

    BungeeTheCookie

    You should just create separate threads for those. I don't see why you should cram it all into one resource.
     
  12. Offline

    TeamJesus

    actually this is quote useful thank you for taking the time to do this despite what others on here say!
     
  13. Offline

    BungeeTheCookie

    We are not trying to offend him. It's just, more experienced people at coding take a look at the resource section. And this resource can simply be found on the bukkit wiki. It is constructive criticism, not insults.
     
  14. Offline

    garbagemule

    +1

    Stuff like this is going to give the impression that casually using nms is okay. Furthermore, I want to +1 the other people saying "don't do this" because you are just creating redundant, duplicate information in an already massive pool of "getting started".

    I think the biggest issue with the majority of the newbie guides is that they throw a metric shit tonne of information at the newbies and say "just compile it, it's fine, here's how your IDE works", which teaches them nothing about Java. A lot of newbies are impatient, so they don't want to listen to "you should probably learn the basics first", because it's boring and has nothing to do with plugins and Minecraft. The missing resource is a "Java basics from a Minecraft/Bukkit perspective" that teaches Java syntax, object-oriented programming, and event-driven programming, but without blobs of source code. If the newbies were taught the basics properly through visualization and diagrams (with a Minecraft-flair to it), they wouldn't be posting stuff like "tell me what code to write to make a no-tnt plugin", but rather "is there an event for when TNT is ignited?". I haven't seen any such resources yet, probably because it is a lot more work than just writing "write this code, click this button in the IDE" or recording the writing of a simple plugin with "so now we call this method and the plugin works" commentary.
     
  15. Offline

    xTrollxDudex

    garbagemule
    "Metric shit tonne"

    Dang. We need more people like you in the language division.
     
  16. Offline

    TeamJesus


    sometimes the wiki does not give stuff like this, it can and it can not, its good to see from someone's prospective about these things, weather your a newbie or an expert we all still learn.. on these forums its rather bad, since " your all an expert " giving each other " SHIT " sorry but its true :p
     
  17. Offline

    BungeeTheCookie

    Why do people do not want to learn the basics of Java? There is no code without Java. We do not need any resources about basics of Java because there is the Oracle Documentation right there for you! If you do not need to read that, why should we waste our time teaching you how to code? Oracle already does that for you.

    EDIT: We can teach you how to do some Bukkit basics, but not too actually code.

    For a person who was TeamJESUS as their username, you use naughty language :p

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

    garbagemule

    Perspective, environment, and medium are everything when it comes to how well people learn and absorb the material they are presented. Don't utter such absolute nonsense...

    As a teaching assistant, I take offense in the ignorance of your statement. Who are you to judge anyone based on their ability to digest one specific source of knowledge? Your uneducated twaddle insinuates a world with no more than one textbook on any given subject taught in schools, and students who never have to ask for elaboration, because everyone understands everything in the first go. That's not quite how things work in real life, is it? I've taught quite a few different types of computer science first years; from the nerds who thought they knew everything there was to know before they even got started, to insecure "never programmed in my life" newbies who thought they would never make it and ended up acing the exams by the end of the semester. If there's one thing I've learned from my experience, it's that people learn very differently, and being able to accomodate that is part of what makes a great teacher. Do you know what teachers make?

    As a student, I take offense in the arrogance of your statement. I happen to be a visual learner, and I struggled through most of the heavy math courses (calculus, probability and statitics, linear algebra), because even though all of the subjects lend themselves very well to drawings, figures, and graphs, there were always very, very few of them in our textbooks (in linear algebra, the only mandatory textbook was an 80-page PDF of theorems and proofs, text-only and with absolutely no context). Salman Khan's brilliant videos are probably one of the reasons I got through those courses anyway, and actually got something out of them too. So just because I don't learn as well as you from dry, abstract walls of text (I guess this post is very easy for you to digest ;)), I shouldn't be allowed to be part of this community, even though I could very well be miles ahead of you in every aspect of the Java language and the Bukkit API? We were all newbies once, and most of us still have a lot to learn. Get off your high horse before you fall down and hurt yourself.

    Onwards...

    Yes, we do need resources about Java basics for the very reasons presented in my previous post (if I had more time, I'd be all over it). We need resources that teach the "boring" stuff (which isn't boring at all when you know it, but it is when it's just blobs of text that you can't abstract away from or see any meaning with) in a Minecraft/Bukkit-related way, because it would attract a lot of the people who wouldn't read up on the basics otherwise. And I know how effective familiar examples can be, because I've used Minecraft and MobArena in my classroom with great success before.

    Fire up Google and type in "event-driven programming" and tell me how many of the results would be easily digestible by newbies. This is probably the best piece, and it's hidden away in image search! Most real-time games are event-driven, and the entire Bukkit API is littered with events, yet there is no source of easily digestible information on the topic. Oh, and Oracle's documentation, which you seem to be so fond of... How many clicks are required to get from the tutorial start page to anything that even begins to teach concepts? Three! And that's after the scary hurdle of the big fat blob about all the new, advanced things in Java 8. Besides, that, how boring is that actual page about objects? Quite.

    One of my favorite "online teachers" of all time (aside from Salman Khan) is Jason "Buzz" Busby of 3DBuzz. Way back in 2008 or something, I followed most their XNA series (and before that, all of their Maya Basics and Maya Advanced series), and I learned so much more from their video training material than I did from the random scribbles with a few images that people would post on various sites. They taught object-oriented programming concepts in C# with game programming in mind, and the basics were never boring that way, because they felt like they were important. I'm sure something similar can be done with Java and Bukkit.

    As a final note, when my little brother started taking guitar lessons, he told his teacher he didn't want to play Wonderwall and all those other cliché, boring songs. His teacher started him off with "Fuck Her Gently" by Tenacious D and "Nothing Else Matters" by Metallica, and he's pretty damn good nowadays. "Tried and true" doesn't mean "one size fits all", it means "one size fits many", and that doesn't mean there isn't a size for the rest.
     
    Ultimate_n00b and Wizehh like this.
  19. Offline

    Wizehh

    I always enjoy reading your eloquent and well put posts. As an addendum to your post, I'd like to add that I absolutely abhor the "You haven't mastered Java? Get out11!!!" posts; I don't deny that you do need some understanding of Java, per se - but the assertion that you cannot or should not attempt to make Bukkit plugins without having read the entire Oracle Java tutorial is absurd. In my opinion, the Oracle tutorials should only be used as an adjunct to actual development; if you need a refresher on the HashMap class, then I'd probably refer you to the official tutorials. But to learn the language from that? That's an almost unfeasible task; while they are great resources, they're as monotous as could be to someone new to Java or plugin development.
     
    garbagemule likes this.
  20. Offline

    BungeeTheCookie

    That was beautiful. Anyways, I am not uttering such "absolute nonsense." There are just thousands and tens of thousands of resources out there. All people need to do is just do a simple search for Google and do site:forums.bukkit.org at the end. And when people are just that arrogant about not learning the basics of Java, it really pisses me off. "Just teach me how to code this thing." If you do not know Java, you are not going to even understand it. When you ask someone to tell you how to code something, you are basically asking for your own personal coder and taking credit for their code. I used to be that kind of person, not wanting to learn Java and take shortcuts, now I know what a fool I was. You will ALWAYS learn Java one way or the other, so you might as well just learn it before you code Bukkit.

    I am not saying that you should read the entire Oracle Documentation. I am just saying there are resources as good or even better than the Oracle Documentation provided by people in a more friendly manner. I was just using the Oracle Documentation as an example. You can learn Java by reading threads, watching videos, or even taking coding lessons from a friend. I am not saying MASTER java. No one will ever master Java, except for the coders who make Java. I am saying at least know the basics. My audience was being directed towards the people who know very little to nothing about Java and want to make minigame plugins or NMS and OBC packets and junk.

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

    garbagemule

    You make it sound like we're on completely different pages, but I think we are just on opposite sides of the same page, really. You hate it when people ask questions that are answered in the vast number of tutorials and learning material available, and I agree. I'm proposing a solution to that problem.

    But why not learn Java in an environment that is relevant to your goal? Would you be thrilled if you were told that the only way you could learn how to write Bukkit plugins would be to first learn how to write a Swing GUI, an Android app, or a web server? None of these have anything to do with Bukkit and Minecraft. An agreed-upon notion in teaching is that people learn more efficiently when they can draw perspectives from the presented material to what they are interested in. That's why analogies are so freaking effective - my car-fanatical "jock" brother-in-law once asked me about my education and didn't understand anything at first, but then I used cars and their components as an example of object-oriented design, at which point he laughed at how little I actually seem to know about cars, but he understood what I was telling him, which is the point. How fast do you think he would have interrupted me and said "forget it, I'll never understand it" if I had started drawing abstract diagrams or used web server material for examples?

    Software development is no different than any other craft. You can build a house in a variety of ways, and understanding your tools is important, but it can be difficult to find motivation to keep trying if all you're ever presented with is learning material that teaches you how to hammer in nails, or how to make key cabinets or bird boxes, and you never seem to get anywhere near laying bricks, installing wall insulation, and building roofs. In the same way, without any material that teaches Java with Minecraft/Bukkit in mind, it can be hard to stay motivated. This has nothing to do with being arrogant, but being impatient or afraid of wasting time on something that may never come of anything. Quite a good deal of the people I know from university had on/off experiences with programming when they were younger, where they would start learning and then kind of give up, thinking they'd never reach their goal (often making video games or advanced utility programs) because it was going too slowly. And now they hold BSc and MSc degrees in computer science - they must have had some potential, even if they didn't enjoy the boring stuff.
     
  22. Offline

    BungeeTheCookie

    Your responses are Ivy League college material.
     
    Cryices likes this.
  23. Offline

    garbagemule

    Haha, I guess four years (and still going) of academia and a passion for teaching programming have taken their toll :p
     
Thread Status:
Not open for further replies.

Share This Page