首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java类型不匹配:无法从CommandSender转换为播放器

Java类型不匹配:无法从CommandSender转换为播放器
EN

Stack Overflow用户
提问于 2018-07-28 05:56:59
回答 1查看 291关注 0票数 0

我有这个错误:类型不匹配:无法从CommandSender转换到播放器这是错误的代码:

在"final Player senderPlayer = (sender instanceof Player)?sender:null;“行中

代码语言:javascript
复制
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
    if (args.length < 2) {
        sender.sendMessage(ConfigurationService.RED + "Usage: /" + label + " <playerName> <amount>");
    } else {
        final Integer amount = Ints.tryParse(args[1]);

        if (amount == null) {
            sender.sendMessage(ConfigurationService.RED + "'" + args[1] + "' is not a valid number.");
        } else if (amount <= 0) {
            sender.sendMessage(ConfigurationService.RED + "You must send money in positive quantities.");
        } else if (amount >= 50000) {
            sender.sendMessage(ConfigurationService.RED + "You cannot send more than " + 50000 + ".");
        } else {
            final Player senderPlayer = (sender instanceof Player) ? sender : null;
            final FactionUser playerUser = (senderPlayer != null) ? this.plugin.getUserManager().getUser(senderPlayer.getUniqueId()) : null;
            final int senderBalance = (playerUser != null) ? playerUser.getBalance() : 5000;

            if (senderBalance < amount) {
                sender.sendMessage(ConfigurationService.RED + "You tried to pay " + '$' + amount + ", but you only have " + '$' + senderBalance + " in your bank account.");
            } else {
                final UUID target = this.plugin.getUserManager().fetchUUID(args[0]);
                final FactionUser targetUser;

                if (target == null || (targetUser = this.plugin.getUserManager().getUser(target)).getName() == null) {
                    sender.sendMessage(ConfigurationService.RED + "Player not found");
                } else if (senderPlayer != null && senderPlayer.getUniqueId() == target) {
                    sender.sendMessage(ConfigurationService.RED + "You cannot send money to yourself.");
                } else {
                    if (playerUser != null) {
                        playerUser.setBalance(playerUser.getBalance() - amount);
                    }

                    targetUser.setBalance(targetUser.getBalance() + amount);

                    if (targetUser.isOnline()) {
                        targetUser.getPlayer().sendMessage(ConfigurationService.YELLOW + sender.getName() + " has sent you " + ConfigurationService.GOLD + '$' + amount + ConfigurationService.YELLOW + '.');
                    }

                    sender.sendMessage(ConfigurationService.YELLOW + "You have sent " + ChatColor.GREEN + '$' + amount + ConfigurationService.YELLOW + " to " + targetUser.getName() + '.');
                }
            }
        }
    }
    return true;
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-28 06:19:09

我猜Player继承自CommandSender,在这种情况下,您需要将CommandSender对象强制转换为Player对象。的实例只是检查它是否是一个播放器,从技术上讲它是一个播放器。这条线应该是。

代码语言:javascript
复制
final Player senderPlayer = (sender instanceof Player) ? (Player)sender : null;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51566019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档