What is @SuppressWarnings(“deprecation”) and how do I fix it?

帅比萌擦擦* 提交于 2020-01-07 08:54:07

问题


I am following a YouTube tutorial and I came across this error.

At this line, @SuppressWarnings("deprecation") comes up.

Player targerPlayer = Bukkit.getServer().getPlayer(args[0]);

Here is my simple healing plugin.

package me.roofer.youtube;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

public class youtube extends JavaPlugin {
    public static Logger logger = Logger.getLogger("Minecraft");   
    public static youtube plugin;

    @Override
    public void onDisable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        youtube.logger.info(pdfFile.getName() + " has been disabled!");
    }
    @Override
    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        youtube.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " has been Enabled!");

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        @SuppressWarnings("unused")
        Player player = (Player) sender;
        if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")) {
            // heal >> 0 args | heal roofer777 >> 1 arg
            if (args.length == 0){
                player.setHealth(20);
                player.sendMessage(ChatColor.RED + "Healed!");
            }else if(args.length == 1){
                @SuppressWarnings({"unused", "deprecation"})
                Player targerPlayer = Bukkit.getServer().getPlayer(args[0]);
                targetPlayer.setHealth(20);
            }
        }
        return false;
    }
}

回答1:


That is not an error. You should read up on the definition of deprecation.

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

The reason why that specific method is deprecated is because Bukkit is now moving over to the new UUID system, so using names is not the best way to get a Player object.



来源:https://stackoverflow.com/questions/27325391/what-is-suppresswarningsdeprecation-and-how-do-i-fix-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!