Common Mistakes

Discussion in 'Plugin Development' started by mbaxter, Sep 12, 2012.

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

    Saposhiente

    stuntguy3000 False. If you are reacting an event without changing it, you must use EventPriority.MONITOR and ignoreCancelled=true. Otherwise, other plugins that do change the event will conflict with your plugin.
     
  2. Everything you said is just plain wrong, honestly.

    This is from my understanding of the Bukkit API, if you can prove me wrong, go for it.
     
  3. Offline

    Saposhiente

    stuntguy3000 Fine, whatever, "If you are reacting an event without changing it, not using EventPriority.MONITOR and ignoreCancelled=true will cause plugin conflicts." I mean, I guess you don't have to, but it's really quite important and you should.
     
  4. How is it important? Does it actually matter? No.

    Also, can you stop tagging me as it is giving me double notifications..
     
  5. Offline

    Saposhiente

    stuntguy3000 Example plugin conflict:
    Protection Plugin (correct):
    Code:java
    1. @EventHandler(priority=EventPriority.HIGH, ignoreCancelled=true) //correct (though a lower priority may be better depending on what sort of protection you are doing)
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. if (isProtected(...)) event.setCancelled(true);
    4. }

    Conflicting Plugin (incorrect):
    Code:java
    1. @EventHandler //incorrect (should be EventPriority.MONITOR, ignoreCancelled=true)
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. if (isASpecialBlock(...)) event.getPlayer().giveExp(99); //Reward players who find and break certain blocks, something of the sort
    4. }

    If you install both of these plugins, and a block both isProtected(...) and isASpecialBlock(...), a player who tries to break this block repeatedly will collect unlimited amounts of reward. If Conflicting Plugin had properly set its EventPriority, there would be no conflict. This sort of thing can happen between any two plugins, one of which is cancelling events and one of which is reacting to events, if the reacting plugin does not use EventPriority.MONITOR.
     
  6. stuntguy3000 - Saposhiente is actually making a good point here, which you seem to be missing.

    Putting it another way: when you receive any cancellable event, it's a notification that the event is about to happen, not that it has happened. I've seen people miss this distinction in the past, and wonder why their BlockBreakEvent handler isn't doing what it's supposed to. I was even guilty of it myself in an earlier version of one of my plugins (which worked fine until I started using it with a different plugin, and then it started failing mysteriously).

    The only way to be sure such an event really will happen is to use priority=EventPriority.MONITOR and ignoreCancelled=true.
     
    ferrybig, Sleaker and Saposhiente like this.
  7. don't know if anyone mentioned this already but using
    Code:java
    1. Logger log = Logger.getLogger("Minecraft");

    which you see do instead of using the logger from bukkit which includes the plugin prefix already
    Code:java
    1. Bukkit.getLogger()
     
  8. Offline

    socram8888

    Actually Bukkit.getLogger returns Minecraft's logger (see https://github.com/Bukkit/CraftBukk.../org/bukkit/craftbukkit/CraftServer.java#L846). To get the logger with your plugin's prefix, you have to call getLogger from your plugin's instance, which does returns a PluginLogger (https://github.com/Bukkit/Bukkit/bl...a/org/bukkit/plugin/java/JavaPlugin.java#L246)
     
    Ultimate_n00b and Janmm14 like this.
  9. Offline

    egetheking


  10. Some plugins may want to log ignored events ( attempted to break block, checking for malicious plugins that use the AsyncPlayerChatEvent, etc). I agree, most of the times, using ignoreCancelled is a good idea.
    I wouldn't want to get xp from that plugin if I broke a block in a protected area. ;)
     
  11. *converts project to Java 1.6*
    ERROR: Cannot use String in switch statement.
    ERROR: Cannot use String in switch statement.
    ERROR: Cannot use String in switch statement.
    (20 more)

    *fixes String-switches*
    ERROR: Cannot use try-with-resource for source level below 1.7.
    ERROR: Cannot use try-with-resource for source level below 1.7.
    ERROR: Cannot use try-with-resource for source level below 1.7.
    ERROR: Cannot use try-with-resource for source level below 1.7.
    (30 more)

    Fun!
    -----
    Couldn't you do something like
    Code:
    Bukkit.getPluginManager().getPlugin("ExLogPlugin").getConfig();
    -----
    Just in case anyone's still in doubt, here's a simple test I ran:
    Show Spoiler
    Code:java
    1. public class TestConcat {
    2.  
    3. public static void main(String[] args) {
    4. //Test1
    5. long time1_1 = System.nanoTime();
    6. String test1 = "";
    7. for (int i = 0; i < 50000; i++) {
    8. test1 += "hi";
    9. }
    10. long time1_2 = System.nanoTime();
    11.  
    12. //Test2
    13. long time2_1 = System.nanoTime();
    14. StringBuilder test2 = new StringBuilder();
    15. for (int i = 0; i < 50000; i++) {
    16. test2.append("hi");
    17. }
    18. long time2_2 = System.nanoTime();
    19.  
    20. System.out.println(time1_2 - time1_1);
    21. System.out.println(time2_2 - time2_1);
    22. }
    23.  
    24. }
    25.  

    The results?
    2.6361 seconds using concat and 0.0057 seconds using StringBuilder.
    That's an overhead of 46000% (count the zeroes!) when using straight String concatenation!
    Even I'm surprised!
     
    1mpre55 likes this.
  12. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

    Added two entries: IO on the main thread and STATIC ALL THE THINGS
     
  13. mbaxter Please add Check wether your plugin.yml is there. Many beginners tend to forget about it , thinking that its not important.
     
  14. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

    That mistake is one you notice rather instantly, when your plugin doesn't load ;)
     
  15. mbaxter Is that sacarsm. .-. xD
     
  16. Offline

    Jaker232

    Probably not a mistake, but you could add it to the lost. I hate it and want to punch a ceiling when someone uses a deprecated method. In a future update, the method's going to be removed and the plugin will be broke. Do yourself a favor and save yourself from the frustrations in the future.
     
  17. You should update the last line, store the player's UUID instead of user name.
     
  18. Only if you plan on storing the player for long amounts of time. But if your only storing it while their online then its fine
     
  19. Sure, but one thing leads to another. There is no harm in storing the UUID instead of the name, so you might as well start right in case you decide later you want to write it to file.
     
  20. Fair enough, it just means you need to write a bit more to get and add values.
     
  21. You can get and store UUID just as easy as player name. The only bit more you do have to is retrieving a player from UUID, which you can do by iteration, but there is a pull request to include that in Bukkit so that won't be for long. I would venture in most cases your comparing an event player to a stored player in which case it's just as easy with a UUID as with a name.

    It just seems easier for the target audience of this guide to tell them always store UUID, than to try an explain store player names except for when storing for a long time.
     
  22. For some reason I was thinking of storing the actual player instance and not the name so yeah it is just as easy. For anyone wanting the code to get a player by their UUID, here it is

    Code:java
    1. public static Player getByUUID(UUID uuid){
    2. for (Player player : Bukkit.getOnlinePlayers()){
    3. if (player.getUniqueId().equals(uuid)){
    4. return player;
    5. }
    6. }
    7.  
    8. return null;
    9. }
     
  23. Offline

    vimpvamp

    Can anyone help me i cant find java 6 and it said i need it for the plugins to work
     
  24. Online

    timtower Ninja on the waves Moderator

    You can run java 6 with java 7...
     
  25. Offline

    vimpvamp

    How do i run java 6 with java 7 :)?
     
  26. Online

    timtower Ninja on the waves Moderator

    It is backwards compatible. Install java 7 / 8 and everything below that will work.
     
  27. Offline

    vimpvamp

    How does this help me in Eclipse, is there anything i need to do after i installed java 8?
     
  28. Online

    timtower Ninja on the waves Moderator

    What are you exactly trying to do?
     
  29. Offline

    vimpvamp

    I am trying to create a minecraft plugin but in the console i get an error message it has something to do with minor.major version or something and i thought it may have been the version of java
     
  30. Online

    timtower Ninja on the waves Moderator

    It does. But you shouldn't get that if you are developing in a lower or equal version as your runtime
     
Thread Status:
Not open for further replies.

Share This Page