Resource [Guide] How to REALLY program

Discussion in 'Spigot Plugin Development' started by El_Chupe, May 31, 2017.

  1. Average newbie:
    How can I does this?
    ...
    Can u give me teh code?
    ...
    *Copies and pastes into Eclipse*.
    *Tries to run it*
    ...
    WUT!? y u no work!?
    *Complains on the forums without trying to figure it out his/herself*
    ...
    *Eventually gets it working but he/she has no idea how*

    Most people who are learning how to make Bukkit/Spigot plugins are not learning how to program. They are learning how to copy/paste. Programming is about more than "getting it to work," believe or or not. There are some key principles that every programmer should know, but are not being taught by YouTube tutorials.

    Some of the most common and best principles are Object Oriented Principles. They are basically concepts that are considered best practice in Object Oriented Programming (OOP). Object Oriented Programming is a programming pattern that involves Objects (an instance of a class) that contain data (fields) and code (methods).

    Without further ado, here are the Object Oriented Principles most relevant to Bukkit plugins:

    1. The DRY Principle
    This one is probably the most important. The DRY principle stands for Don't Repeat Yourself! This principle can be implemented in a number of ways. One common way is code repetition. Have you ever made a plugin that gave a player a custom item? It usually looks something like this:
    Code (Java):
    ItemStack item = new ItemStack(Material.DIAMOND, 1);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName("DIAMOND!!!");
    item.setItemMeta(meta);
    player.getInventory().addItem(item);
    What a lot of people do is copy/paste this and change a few things everywhere in their code that they want to use it. Solution: Put it in a method that can take arguments, such as the player, the material, and the amount, and any custom data, and then run the method anywhere you need to use the code. Like so:
    Code (Java):
    private static void giveItem(Player player, Material type, int amount, String name, List<String> lore) {
      ItemStack item = new ItemStack(type, amount);
      ItemMeta meta = item.getItemMeta();
      meta.setDisplayName(name);
      meta.setLore(lore);
      item.setItemMeta(meta);
      player.getInventory().addItem(item);
    }
    The same can be said for Strings and other values. Instead of typing out "Insufficient Permissions" in several String literals, store it in a field and reference it from there.

    2. The Single Responsibility Principle
    The Single Responsibility principle basically means that each class/method/field should have one purpose. Unless it's a small plugin, your main class should not me your event listener, and you shouldn't put EVERYTHING that happens when a player runs a command in your onCommand method. Instead, have your onCommand check and analyze the arguments and then run a method designated for those arguments. Better yet, put your onCommand in another class.

    3. The Delegation Principle
    The Delegation Principle means: Don't do everything yourself. Don't try to do everything manually, that's why the Bukkit/Spigot API is there. Don't dabble in NMS code just to give a player an item, or spawn an entity. There are methods for that. All you do is call the method, and everything else runs behind the scenes.

    Again, I know that there are more good practices in Object Oriented Programming. I just picked the ones I thought were most relevant to Bukkit plugin development.

    If you liked this please leave a positive rating, and if you have anything to add, post it and I'll add it. :D
     
    #1 El_Chupe, May 31, 2017
    Last edited: May 31, 2017
    • Agree x 7
    • Winner x 2
    • Like x 1
    • Funny x 1
    • Informative x 1
    • Optimistic x 1
    • Creative x 1
  2. Thank you for sharing, this approaches a few things to help newcomers take their coding to the next level. I am sure over time this could grow into a fun resource to have as a bookmark to link others to.

    For example, there are enough existing articles that point out some design and development principles that (java) coders should keep in mind while architecting their projects;
    http://javarevisited.blogspot.nl/2012/03/10-object-oriented-design-principles.html
     
  3. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    This really applies to all programming languages, and should be something every new programmer reads. "Proper" (pragmatic) coding is a big deal, and that isn't something you really learn when you first start.
     
  4. Atleast when making a guide, don't use Eclipse in it. Please for mankind. Move to Intellij. I used eclipse myself in the begin. Moved to Intellij. Eclipse is buggy, outdated and poor.
     
    • Optimistic Optimistic x 3
    • Agree Agree x 1
  5. Mas

    Mas

    Mark this with resource so other's will see it ;)

    It's personal preference and makes no difference on how to "really program". Althought you're right, IntelliJ is better
     
    • Agree Agree x 2
    • Optimistic Optimistic x 2
  6. It does. Eclipse is outdated, buggy and very poor. Intellij is up to date, nice UI, compile's good, many options to choose for programming, inbuild maven. Tell me the Pro's and Con's of Eclipse and Intellij and lets see ;).
     
    • Funny Funny x 1
  7. Mas

    Mas

    Outdated? Is it? It gets frequent updates.
    Buggy? Yeah, I agree, although with my experience most bugs get fixed. Eclipse compile time is also brilliant.
    Poor? What are you trying to mean by saying this? It's completely subjective.
    Once again, it's a matter of personal preference, and although I personally do use IntelliJ, there are reasons why others may choose not to. You can't decide on whether someone can "really program" solely through the IDE they use.
     
  8. Intellij compiles for me in less than a second, (345kb+). While eclipse takes a couple seconds for it.. And Eclipse changes every single time of name. And with poor I mean the way its build, the UI, options etc.
     
    • Funny Funny x 1
  9. Mas

    Mas

    Once again, all this depends on your personal preference. It's possible to like other things to what you like. Some people may like the UI, options, build, etc.
     
    • Agree Agree x 2
  10. Agree but if looking at the performance, Intellij is way better than Eclipse.
     
  11. Oh, yeah, those 2.4 seconds you miss due to Eclipse's compilation time... I use that time to open the workspace where the jar's gonna appear or to start the test server.
     
    • Agree Agree x 2
    • Winner Winner x 1
  12. I just bought IntelliJ and have absolutely fallen in love with it. If you can afford it or even get the student license, its worth it. I believe there is also a community edition? Never used it but it might also be good.

    Good job OP. Learn Java before you learn the API :p
     
    • Like Like x 1
  13. I completely agree with this thread, and I encourage other newbie developers to follow this path. Infact I use that item method in a lot of my programming ^o^
    I use the community edition, it's awesome.

    I would have used the class ItemStack instead of void, and then just return the itemstack
     
    • Winner Winner x 1
  14. IntelliJ has one cute UI.... *slaps myself on the face*

    The principles stated in the Original Post are applicable to every language, regardless of syntax or framework. And that's what I like about it. For me though, it's kind of something that's common sense, but to see it drawn out like this makes me happy, honestly.

    (Psst, don't look at my plugins' source code from last year ;-;)
     
  15. Also if you want to REALLY program, use Kotlin.

    Obligatory kotlin post is obligatory... I'm not sorry...
     
    • Optimistic Optimistic x 4
  16. (yes i use intellij ultimate myself, so this is not to protect my fav ide as i dont like eclipse myself, but in this case, I'm siding with @ItsMas_ )
    Eclipse is functional, works like it should, yes it has a couple bugs, but there's still big reason to use it.
    E.g. @kennethbgoodin (hope you dont kill me for tag) uses it simply because of his familarity with it. He's an amazing developer, who's most likely much further within java and other languages than both me and you combined, and he has tried IntelliJ, yes, but it wasn't familiar, thus Eclipse. Eclipse with gradle/maven works for dependencies and build, and their editor has actual highlighting, simple errors and nothing too big, which is just enough for most people.
    luv ur posts
    kotlin is bae
    (Not meant to be rude)
    That is something you should know yourself, and not IntelliJ teach you.
    You should add dependency injection and teach that instead of static, in such a way we can just redirect stattik abooz over here instead.
     
  17. The primary reason to use Eclipse is, at least for me, the fact that it's easier for a begginer. When I was a begginer, and I mean, a BEGGINER, I couldn't understand how IntelliJ worked when I tried it; but, when you get better, you'll switch to IntelliJ by common sense then use and love it as everyone should (although that there are some weird people that don't like IntelliBae for some reason which I will never know, they might be that kind of person that don't like the changes /s).
     
    #17 F8te, Jun 1, 2017
    Last edited: Jun 1, 2017
  18. How to really program: Talk shit to anyone that doesn't use the same IDE as you. That's how you become a professional programmer... /s

    Edit: can't tell if people are taking this seriously or not, so I added /s to signify I was being sarcastic while pointing out how this lovely resource turned into a flaming pile of shit because people want to argue over IDEs..
     
    #18 BillyGalbreath, Jun 1, 2017
    Last edited: Jun 1, 2017
    • Like x 1
    • Agree x 1
    • Informative x 1
    • Useful x 1
    • Optimistic x 1
  19. Will definitely follow this tutorial for the rest of my days Cx
     
  20. I agree 100%, especially if you use an IDE that is considered superior. IntelliJ -> Eclipse -> Notepad++ -> Notepad
     
    • Optimistic Optimistic x 1