gpt4 book ai didi

java - craftbukkit.v1_15_R1.entity.CraftPlayer 无法转换

转载 作者:行者123 更新时间:2023-12-01 17:55:05 25 4
gpt4 key购买 nike

我英语不好,但我必须询问这个错误,因为我无法在任何地方修复它。

我正在制作我的世界插件来返回到玩家选择的位置。

但我无法修复这个错误...

这是整个错误日志

[10:57:29] [Server thread/INFO]: Player issued server command: /setposition
[10:57:29] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'setposition' in plugin Main v0.1
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:723) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1658) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1498) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:918) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:911) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:895) [Server.jar:git-Spigot-6de3d4b-fc24934]
at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:828) [Server.jar:git-Spigot-6de3d4b-fc24934]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer cannot be cast to package1.PlayerPosition
at package1.SetPosition.onCommand(SetPosition.java:16) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[Server.jar:git-Spigot-6de3d4b-fc24934]
... 17 more

返回命令

玩家可以通过setposition命令到达选定的位置

package package1;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.ChatColor;

public class Back implements CommandExecutor {

public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player) {
Player p = (Player) sender;
PlayerPosition pp = (PlayerPosition) p;
if(pp.location.equals(null)) {
p.sendMessage(ChatColor.RED + "Set Your Position To Back First By Using SetPosition.");
return false;

}
else
{

p.teleport(pp.location);
return true;

}

}
return false;
}

}

这是 PlayerPosition 类

此类扩展了 Player

和添加位置

package package1;

import org.bukkit.Location;
import org.bukkit.entity.Player;

public abstract class PlayerPosition implements Player {
public Location location;

PlayerPosition(Location l){
this.location = l;
}



}

这是 setposition 命令。

package package1;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.md_5.bungee.api.ChatColor;

public class SetPosition implements CommandExecutor {

public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(sender instanceof Player) {
Player p = (Player) sender;
PlayerPosition pp = (PlayerPosition) p;

pp.location = p.getLocation();

p.sendMessage(ChatColor.BLUE + "Position Setting Complete.");
return true;



}
return false;

}

}

如何修复它?

最佳答案

Player是一个接口(interface),而不是一个类,因此任何实现Player接口(interface)的类也必须实现Player中的所有方法。

示例:

public interface MyInterface {
public void aMethod();
}

如果您有一个实现 myIntefrace 的类,那么您的类必须实现 aMethod:

public class MyClass implements MyInterface {
public void aMethod() {
//Do something
}
}

有关更多信息,请参阅此处:Java Interfaces

因此,如果您希望 PlayerPosition 实现 Player,您必须自己实现 Player 接口(interface)中包含的大量方法。因此根本不推荐。

正因为如此,您无法从 CraftPlayer 转换到 PlayerPosition

解决方法是将玩家位置保存在 /setposition 上的 HashMap 中,并在 back 上从中获取它。

您的问题的解决方案可能如下:

SetPosition.java:

public class SetPosition implements CommandExecutor {
@Override
public boolean onCommand(CommandSender theSender, Command command, String s, String[] strings) {
if (theSender instanceof Player) {
Player p = (Player) theSender;
PluginCore.getInstance().setLocation(p, p.getLocation());
p.sendMessage(ChatColor.BLUE + "Position Setting Complete.");
}
return false;
}
}

返回.java:

public class Back implements CommandExecutor {
@Override
public boolean onCommand(CommandSender theSender, Command command, String s, String[] strings) {
if (theSender instanceof Player) {
Player p = (Player) theSender;
Location location = PluginCore.getInstance().getLocation(p);
if (location == null) {
p.sendMessage(ChatColor.RED + "Set Your Position To Back First By Using SetPosition.");
} else {
p.teleport(location);
}
}
return false;
}
}

PluginCore.java:(主类)

public class PluginCore extends JavaPlugin {

private static PluginCore instance;

private HashMap<Player, Location> locations = new HashMap<Player, Location>();

@Override
public void onEnable() {
instance = this;
this.getCommand("back").setExecutor(new Back());
this.getCommand("setposition").setExecutor(new SetPosition());
}

public static PluginCore getInstance() {
return instance;
}

public Location getLocations(Player p) {
return locations.get(p);
}

public void setLocation(Player p, Location location) {
this.locations.put(p, location);
}
}

希望对您有帮助!

关于java - craftbukkit.v1_15_R1.entity.CraftPlayer 无法转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60732981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com