gpt4 book ai didi

java - Bukkit 传送 - nullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:39:15 26 4
gpt4 key购买 nike

我正在尝试使用以下代码将用户传送到他们自己的领域:

@EventHandler
public static void onPortalTravel(PlayerPortalEvent event) throws Exception {
if(event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
int x = event.getPlayer().getLocation().getBlockX();
int y = event.getPlayer().getLocation().getBlockY();
int z = event.getPlayer().getLocation().getBlockZ();

String[] data = getPageData("http://example.com/game.php?type=getRealm&location="+x+":"+y+":"+z ).split(":"); // THIS RETURNS <username>:<oldblockid>

String realm = data[0];
int oldID = Integer.parseInt(data[1].trim());

Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setTypeId(oldID);
*err* event.getPlayer().teleport(new Location(Bukkit.getWorld("realms/" + realm), 1, 65, 16.5));
}

}

错误是:

Caused by: java.lang.NullPointerException
at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.teleport(CraftPlayer.java:395)
at org.bukkit.craftbukkit.v1_6_R2_entity.CraftEntity.teleport(CraftEntity.java:199)
at com.mysite.plugin.Start.onPortalTravel(Start.java:202)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
... 26 more

最佳答案

这个世界似乎还没有加载。您必须先加载它。

此片段检查世界是否为空。如果为空,则加载(如果世界已经存在)或创建(如果世界尚不存在)世界。

@EventHandler
public static void onPortalTravel(PlayerPortalEvent event) throws Exception {
if (event.getCause() == PlayerPortalEvent.TeleportCause.END_PORTAL) {
int x = event.getPlayer().getLocation().getBlockX();
int y = event.getPlayer().getLocation().getBlockY();
int z = event.getPlayer().getLocation().getBlockZ();

String[] data = getPageData("http://example.com/game.php?type=getRealm&location="+x+":"+y+":"+z).split(":"); // THIS RETURNS <username>:<oldblockid>
String realm = data[0];
int oldID = Integer.parseInt(data[1].trim());

Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setTypeId(oldID);
World world = Bukkit.getWorld("realms/" + realm);
if(world == null){
//Loads a world with the name given in the constructor
WorldCreator wc = new WorldCreator("realms/" + realm);
world = Bukkit.createWorld(wc);

}
event.getPlayer().teleport(new Location(world, 1, 65,16.5));
}

}

关于java - Bukkit 传送 - nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18212244/

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