Good practice/optimizing your plugins ?

Discussion in 'Plugin Development' started by arnie231, Nov 26, 2011.

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

    arnie231

    hey ive not really see a thread about this but what ways could i make a plugin better and to optimize it without having it lag a server or just make it more simple

    Any thoughts ?
     
    Lex Talionis likes this.
  2. Offline

    Lex Talionis

    This is actually a really good question IMO. I'd love to see a well thought out best practices thread here, possibly stickied. Unfortunately I haven't been doing this long enough to make such a thread myself, so if any plugin gurus out there would like to take a crack at it, I'm sure it would be much appreciated by the whole community. :D
     
  3. Offline

    h4344

    It might not help all that much but when i code i try to make as few lines as possible. But i still leave the few separating lines so everything is still easy to find and read.
     
  4. Offline

    Acrobot

    - Store your objects locally (instead of calling functions like event.getPlayer() every time)
    - Don't use too many ifs inside ifs
    - Generally, try to make your code efficent.
     
    Father Of Time and WalkerCrouse like this.
  5. Try to avoid duplicate snippets of code.
    If you need to do the same thing pretty often, make a new private method and put the repeating code inside there.
     
  6. Offline

    bergerkiller

    Be careful with collections. No seriously, I was generating entity lists in a function and it made the RAM usage go up 5 mb/s faster. When storing the lists locally and using clear on them, it even brought down the time it took to perform everything. (talking of NoLagg's item/orb stacking and entity spawn limit handling)

    Also, be very careful with while loops. For example, when performing an operation until it succeeds. If you don't specify a counter-like thing it could loop for hours taking your thread/server down with it.

    Ow and if possible, re-use your variables.
    Code:
    int value1 = 5;
    int value2 = 7;
    int result = 5 + 7;
    //can be
    int value1 = 5;
    int value2 = 7;
    value1 = value1 + value2;
    Taking into account that you need value1 and value2 as variables. And, as stated above, try to prevent the creation of arrays/lists in functions. It can cause RAM usage to increase rapidly, making the Garbage Collector go into overdrive.
     
  7. Offline

    Jogy34

    If you make a loop to try to find something use a break statement once you have found it.
     
  8. if you don't need to know exactly when a player moves 0.005 blocks, don't just use onMove, add a if call to exit out if they are in the same "block location". Only run the expensive check when they've actually moved far enough.
     
    CptSausage likes this.
  9. Offline

    xpansive

    Don't make a bunch of objects in loops.

    NEVER use boxed primitives (except with lists, and the previous rule applies for those)

    Use NMS code to squeeze out a little bit more speed, for example world.getHandle().setRawTypeId is a lot faster than world.getBlockAt(...).setTypeId because the bukkit method is creating a Block object just for you.

    Generally try to make the code look as nice as possible, and if you think something could be done better, change it.
     
  10. Offline

    desht

    Think about the problem you're trying to solve carefully, and choose your data structures to match your problem.

    Never use string concatenation inside a loop. Use StringBuilder or Joiner.

    Cache the return value of expensive function calls wherever possible.
     
  11. Offline

    arnie231

    Have to say thank you for all these great comments
     
  12. Offline

    Windwaker

    • Think about what the program should do and leave it at that
    • Use polymorphism
     
  13. Now your just throwing random words out of a dictionary in this topic! ;)
    OT: Polymorphism
     
  14. Offline

    Don Redhorse

    ok, my input to this... REPOST all of this in NOOB speak.. ;)

    It would be really cool if the more knowledgeable programmers would come up with a best practice wiki page / pages about how to code perhaps containing some snippet of code or even a reference to the source code etc...

    I'm learning from books and good plugins... (codewise)... which I can notice because I have a little knowledge..

    but a lot of the stuff you people throw around is common sense for real java programmers but gives you a Häääh????? :confused: from most of the programmers here, oh yeah and a lot of the people here neither speak english or java as a native language :D
     
  15. Offline

    Windwaker

    Am not, polymorphism is crucial to any good structured Java program which is an Object Oriented platform. By this I mean heirarchy trees and actual objects such as (Player, Entity, etc) not (PlayerManager or EntityManager) some times this is difficult to dodge, but I try to use it whenever possible.

    An example of poly morphism...

    Dog extends Canine which extends Animal
     
    dadaemon likes this.
  16. That's what the wink smiley is for right?!

    So what you mean is.

    Creeper extends Monster which extends Creature (yay for javadocs!)

    But how about a plugin example? How would you use it in a plugin? Got any real life examples?
     
    WalkerCrouse likes this.
  17. Offline

    bleachisback

    TemplarNPC extends BasicHumanNPC, which extends BasicNPC (taken from NPC spawner lib =P)
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    use the final keyword for any variable you don't plan on reassigning. You are of course free to mutate said variable.
     
  19. Yeah... But that's just the same as what I said about the Creeper... Other examples?

    Edit: Sorry, maybe I'm now hijacking this topic. Need to make a new one?
     
  20. Offline

    ItsHarry

    Well for example:

    PlayerInteractEvent extends Event
     
  21. Offline

    xpansive

    I definitely think it would be great to have a wiki page for this. I don't know how to make one, would someone else care to?
     
  22. Offline

    bleachisback

  23. Offline

    Sagacious_Zed Bukkit Docs

  24. Offline

    halley

    All of the Minecraft and Bukkit code are shockingly poor at memory management, compared to the kinds of lean optimizations you are expected to do in small systems and embedded software, like say Java-based Android applications.

    Java's collection classes soak up memory like a sponge. You're relying on the garbage collector to do a good job of returning megabytes of memory to the pool every few seconds, and doing it without stuttering the CPU load on those opportunities. Tuning the server's garbage collector settings can only get you so far.

    In addition to the above tips, when possible, use plain arrays of a fixed size, instead of the genericized Java collections. Recycle lightweight objects instead of just discarding them and making new ones. Understand an algorithm's "cyclometric complexity," which is fancy talk for how bad the worst case looping might be. Don't loop through a List to see if an item is in it; use a Set if you're interested in membership more often. Break up larger work loops into slices or strips, and do a little bit of work every few ticks, rather than all of the job in one tick.
     
  25. Offline

    Jogy34

    That's actually java in general not just Minecraft and Bukkit.
     
  26. Offline

    xpansive

    Minecraft and, especially Bukkit are massive memory hogs in my expedience compared to all the other java libraries I've worked with.
     
  27. Offline

    Lex Talionis

    Wow! A definite thanks for the all the input here so far, and a permanent wiki page sounds nice. I think calling it coding etiquette may be a little bit of a misrepresentation though... Etiquette sounds more like the 'polite' way of coding so that others can easily understand what your code does after a quick skim through, where as best practices tend to be ways to actually make your code more efficient. Just my two cents. :3
     
  28. Offline

    IDragonfire

    Ok,
    I am new to Bukkit, but not new to JAVA,
    so maybe we can discuss about some examples:

    storage:
    store are Object on a locale field or not?
    If you need the object huge times, yes (dont forget to update the object)

    How?
    example: We want to change Blocks.
    Mostly bad idea:
    Code:
    ArrayList<Blocks> myList;
    [...]
    for (...)
    myList.add(block)
    
    If you know the size of the Array, use:
    Code:
    Block[] betterBlockStorage = new Block[mysize];
    for(...)
    betterBlockStorage[i] = block;
    
    Otherwise if you read >= add maybe it is a good idea to use Collections.toArray.
    Did you need the whole Block-Object?
    If you need only the int Coordinates of the Block use an int[][] array (or your own PlaceHolderObject).

    Another point:
    Code:
    private Block[] betterBlockStorage;
    [...]
    if(betterBlockStorage  == null || betterBlockStorage .length != mysize) {
        betterBlockStorage = new Block[mysize];
    for(...)
    betterBlockStorage[i] = block;
    
    PLEASE CORRECT ME !!!

    btw,
    PlayerManager, EntityManager, ... are not ObjectOriented, but necessary, is that your statement?
     
  29. Offline

    Windwaker

    PlayerManager and EntityManager don't exist in Bukkit, I was explaining why.
     
  30. Offline

    IDragonfire

    Yes right,
    but e.g. the NPCLibaries have a NPCManager and sometimes it is usefull ;)
    I would write some more stuff later^^

    btw. we can open a thread that shows "good" coded plugins...
     
Thread Status:
Not open for further replies.

Share This Page