When, and When Not to use Deprecated methods.

Discussion in 'Spigot Plugin Development' started by FlailoftheLord, Sep 14, 2018.

  1. Well, this has kinda been a sketchy topic for me, as im a newer developer, still learning the Bukkit api and getting better with java at the same time, but one thing ive noticed as i only started coding with Bukkit during the 1.13 release, is that theres some methods that either have just been put into "deprecation" or have already been in deprecation for the past few Spigot versions, and for me, being super ocd or whatever, ive aways tried to use methods that arent deprecated, even if it costs more code.

    But ive seen several big devs on here saying that deprecated doesnt mean "dont use this anymore" just a warning, "hey! theres a new method out which does the same thing, use that instead."

    Especially with the huge UUID overhaul in 1.8 i dont feel safe using the getPlayer(String) anymore, mainly because its deprecated, cause i was wondering when i put that into a plugin i was making and my IDE told me it was deprecated...( my first thought was whats deprecated... jk.)

    Is it always fine to use Deprecated methods, when would it be alright or, maybe not safe to use a deprecated method that you know and is easier to use than the newer method that "does the same thing" and was implemented in a newer version of Bukkit?
  2. Deprecated methods should be avoided if possible. Deprecated methods etc are basically marked 'unimportant'. They will no longer get updated or changed and may at some point get removed completely. Some of them may still work but they are not supported anymore.
  3. Essentially, don't use deperacated methods unless you must. Such methods are discouraged (hence why they are deprecated) from use and there is no guarantee they will still be there in the future.
    • Agree Agree x 1
  4. Methods are in general to be marked as deprecated when they
    • are not named properly and / or consistently, e.g. BukkitScheduler#scheduleAsyncDelayTask is ambiguous because it can be read as "schedule a sync delayed task" instead of "schedule asnyc delayed task" and PlayerInventory#getItemInHand() doesn't clearly say if the item in the on or off hand slot is returned
    • are made redundant by an update, be it an update to linked software or to the the software itself, e.g. the Horse.Variant class
    • contain a design flaw, e.g. the Server#getOnlinePlayers() array that has since then been replaced with the Server#getOnlinePlayers() Collection
    • upcoming updates are known to break the feature, e.g. numeric Material and EntityType IDs (however, that is also a design flaw)
    Deprecated methods are always supposed to either have or get replacements later, but are kept for compatibility reasons. There are unfortunately deprecated methods in Bukkit that are deprecated for none of these reasons and not going to be removed either, like Server#getOfflinePlayer(String name). This method has been deprecated by the Bukkit team in 1.7.x to "raise developer awareness of the switch to UUIDs and the impact it would have" and supposed to "be removed as time for preparation has passed, if they haven't been removed already" in 1.8. However, the Bukkit team disbanded and the SpigotMC team never did this :C

    Except these few methods, all deprecated methods should either be avoided or used after an environment check that makes sure that the method does what it should.
    • Agree Agree x 1
  5. Yeah, ive noticed that they have just stayed there, and havent been removed, and seeing as that was an easier way to grab a player instead of going throught the minecraft.net api, maybe Spigot can re-implement this? or re create it, by making or adding a method where it converts the Username to the UUID inside of spigot like a method getUuid(playerNameString)
  6. Moderator. please move to Discussion thread.
  7. You should never try to get a player by their name unless they are in the server because their name can change. The only thing that is attached to a player is their unique ID. That said, if a player is in your server you can get their name via Bukkit#getPlayer("Ugleh"), and that is not deprecated.
    You can than do player#getUniqueID().
    #7 Ugleh, Sep 14, 2018
    Last edited: Sep 15, 2018
  8. Well, you cannot exactly 'convert' from a username to a UUID. What you can do, however, is utilize OfflinePlayer or if you want to get an online player by uuid that is as simple as Bukkit#getPlayer(UUID).
  9. Yes, you should. For example, when a command should target a player that is offline, like a stats or ban command. That the name can change is no issue in that case.
  10. 2008Choco

    Junior Mod

    Just for the record, neither of these are the reasons the methods are deprecated.
    • #scheduleAsyncDelayedTask(Plugin, BukkitRunnable, long) is deprecated because BukkitRunnable has more convenient methods. The overloaded method that takes Runnable is not deprecated.
    • Player#getItemInHand() was deprecated because it should not be used as a replacement for the more proper PlayerInventory#getItemInMainHand() or PlayerInventory#getItemInOffHand() methods. As per the deprecation notice on #getItemInHand() it returns the main hand.
    The deprecation notice should always be referred to when using a deprecated method. Most deprecated methods in Bukkit will not be removed but are deprecated for reasons such as draft APIs (deprecation is removed at a later time when more thoroughly tested by the public) or general warnings (such is the case with #getOfflinePlayer(String) indicating that the server may temporarily halt while making a request to Mojang's servers)
  11. The deprecation notice however says "This name is misleading, as it does not schedule "a sync" task, but rather, "an async" task"

    It's true though that there are more convenient methods that should instead be used, and that the methods for synchronous tasks are deprecated with a link to these methods.

    That's what I meant by "'item in hand' doesn't clearly specify a hand", I'm sorry if this could somehow be misunderstood.
    • Like Like x 1
  12. well as well as you see a lot of people just not carrying about deprecation, you also see a lot of people wining about the removal of all numeric ids ;)
  13. I meant to append "For offline players", but forgot ._.

Share This Page