General Plugin Development Questions

Discussion in 'Spigot Plugin Development' started by TheDiamondWorm, May 9, 2015.

  1. I had a few questions about plugin development i'd like to ask and get cleared up.

    1. Is there any difference between these three? Is one better than the other?
    Code (Java):
    Code (Java):
    Code (Java):
    2. Is there a difference between these two? Is one better than the other?
    Code (Java):
    public String prefix = getConfig().getString("prefix");
    Code (Java):
    public String prefix;
    public void onEnable() {
    prefix = getConfig().getString("prefix");
    3. Does it matter which one of these I use?
    Code (Java):
    if (player.getLocation().getX() != 69.0D) {
    Code (Java):
    if (!(player.getLocation().getX() == 69.0D)) {
    4. Does it matter which one of these I use?
    Code (Java):
    if (player.getItemInHand().getType().equals(Material.DIAMOND_SWORD)) {
    Code (Java):
    if (player.getItemInHand().getType() == Material.DIAMOND_SWORD) {
    Thanks, I wanted to ask these because I was curious :3
  2. 1. 12 characters.
    2. Initializing a field in the constructor is the same as initializing it outside, although the field should be private unless there is a valid reason not to.
    3. The first one looks better IMO
    4. If you look at the source in Enum class, 'equals' simply refers to '=='
    Code (Text):
    public final boolean equals(Object other) {
        return this==other;
    This essentially means that Enum#equals(Enum) is the same as Enum == Enum; however, this is not the same for other classes such as 'String' which actually overrides the 'equals' methods to check whether the characters in the String are equal instead of if the references are equal which they are probably not because Strings are immutable.
    #2 Skionz, May 9, 2015
    Last edited: May 9, 2015
    • Like Like x 1
    • Agree Agree x 1
  3. gigosaurus


    1. There is no functional difference, they all end up returning the same object, just use whichever is easiest to use (so option 3 if you can, otherwise option 1).
    2. Use option 2 - it's better to load everything to do with the plugin when Bukkit actually gets around to enabling the plugin. The class is loaded long before that happens.
    3. Option 1 because it's easier to understand.
    4. What Skionz said.
    • Like Like x 1
    • Agree Agree x 1
  4. EAX


    4) Some objects have special implementation of equals method, string is one of that objects.
    • Like Like x 1
  5. The first two are identically, BUT the last one will only work in your main class (that extends JavaPlugin) as getServer() is a method defined in the JavaPlugin class. So I'd recommend you use the first one, as it can be used everywhere and is short.

    First of all, don't make this field public. If it has to be used from other classes, add a getter for it.
    The first code is executed when an instance of your class is created. IIRC JavaPlugin#getConfig() only works when the plugin is enabled / loaded, so better use the second one.

    Thats simple java: No. However first one is recommended, as it's easier to read and understand.

    As stated, they both work the same, but conventions say you should use the second one (as there's only one instance of each enum it's pointless to use #equals).

    Finally, I'd recommend you to learn a bit more about the java language. Some of your questions aren't in a relation to bukkit / spigot, only to java.
    The biggest mistake one can make is to assume, you could simple learn java by using it.
    • Like Like x 1
  6. for 1. After compiler and runtime optimisation, you won't see much difference. Pick the option with least key typing.

    for 2. if you do the first option, you would be writing that statement in a method and the variable "prefix" can only exist during the invocation of that method. Hence, if you want to use that variable elsewhere, you would want to do the 2nd option.

    for 3. you should do the first option (although i would recommend not to have a magic number in the statement.) The main reason is not because of its readability but that you should avoid "==" to compare floating point numbers due to the rounding errors. (if you want to know why... you should learn Computer Science.)

    for 4. both would end up the same. use use '==' , however, if you are implementing your own "equals" method, make sure that objects' hash code are actually unique. if you have crappy hash code generation for objects, you might end up with memory leak because a Garbage Collector might not be able to clean up the unreferenced objects.
    #6 vk2gpz, May 9, 2015
    Last edited: May 9, 2015
    • Like Like x 1
    • Agree Agree x 1