什么是@SuppressWarnings(“弃用”),我该如何解决? [英] What is @SuppressWarnings("deprecation") and how do I fix it?

查看:152
本文介绍了什么是@SuppressWarnings(“弃用”),我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注YouTube教程,却遇到此错误。

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

在这一行, @SuppressWarnings( deprecation)出现。

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;
    }
}


推荐答案

不是错误。
您应该阅读弃用的定义


使用@Deprecated注释的程序元素是程序员不鼓励使用的元素,通常是因为这样做很危险,或者是因为存在更好的选择。

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.

不赞成使用该特定方法的原因是,编译器会在不赞成使用的代码中使用或覆盖不赞成使用的程序元素时发出警告。 Bukkit现在正在转移到新的UUID系统,因此使用名称并不是获取 Player 对象的最佳方法。

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.

这篇关于什么是@SuppressWarnings(“弃用”),我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆