Java (Bukkit) 没有这个如何访问配置?
Java (Bukkit) how to acces to the Config with out this?
我有两个 classes "main" 用于命令等,"events" 用于事件,现在
我想让事件 "PlayerdeathEvent" 将我传送到一个点,并且该点的坐标保存在配置中,现在我测试这个:
我参加了活动class
public void lbbkampfrespawn()
{
FileConfiguration cfg = this.getConfig();
{
String world = cfg.getString("LBBsetkampfrespawn1.world");
double x = cfg.getDouble("LBBsetkampfrespawn1.x");
double y = cfg.getDouble("LBBsetkampfrespawn1.y");
double z = cfg.getDouble("LBBsetkampfrespawn1.z");
double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw");
double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch");
Location loc = new Location(Bukkit.getWorld(world), x, y, z);
loc.setYaw((float) yaw);
loc.setPitch((float) pitch);
p1.teleport(loc);
}
{
String world = cfg.getString("LBBsetkampfrespawn2.world");
double x = cfg.getDouble("LBBsetkampfrespawn2.x");
double y = cfg.getDouble("LBBsetkampfrespawn2.y");
double z = cfg.getDouble("LBBsetkampfrespawn2.z");
double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw");
double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch");
Location loc = new Location(Bukkit.getWorld(world), x, y, z);
loc.setYaw((float) yaw);
loc.setPitch((float) pitch);
p2.teleport(loc);
}
}
}
问题:
不知道 "getConfig()"
eclipse 告诉我要用这个方法 getConfig()
我用静态
在主要class中做到了
public static void lbbkampfrespawn()
{
FileConfiguration cfg = this.getConfig();
{
String world = cfg.getString("LBBsetkampfrespawn1.world");
double x = cfg.getDouble("LBBsetkampfrespawn1.x");
double y = cfg.getDouble("LBBsetkampfrespawn1.y");
double z = cfg.getDouble("LBBsetkampfrespawn1.z");
double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw");
double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch");
Location loc = new Location(Bukkit.getWorld(world), x, y, z);
loc.setYaw((float) yaw);
loc.setPitch((float) pitch);
p1.teleport(loc);
}
{
String world = cfg.getString("LBBsetkampfrespawn2.world");
double x = cfg.getDouble("LBBsetkampfrespawn2.x");
double y = cfg.getDouble("LBBsetkampfrespawn2.y");
double z = cfg.getDouble("LBBsetkampfrespawn2.z");
double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw");
double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch");
Location loc = new Location(Bukkit.getWorld(world), x, y, z);
loc.setYaw((float) yaw);
loc.setPitch((float) pitch);
p2.teleport(loc);
}
}
问题:我不能用这个。在静态
我要做的感谢您的帮助!
当你需要更多的鳕鱼时请发送什么。
谢谢。对不起拼写
Editor's Note:
A Deutsche version of the answer was included previously, but removed because posts should be in English. If you would like to see it, use the revision history.
getConfig()
方法继承自 JavaPlugin
class,您的 classes 中只有一个应该扩展。不过,您可以通过多种方式从另一个 class 访问配置文件。例如,可以保留对 "main" class 或插件(扩展 JavaPlugin
的那个)的引用,然后使用 object/reference 调用 getConfig()
.您还可以创建一个静态 getter 方法,该方法 returns 您的插件实例(不确定这是否是好的做法)。 Eclipse 可能会告诉您创建 getConfig()
方法,因为您在不扩展 JavaPlugin
.
的 class 中调用它
例如,假设扩展 JavaPlugin
的 class 称为 "Main"。例如,如果您有一个监听器 class 想要访问您的 Main class,您可以在监听器 class 的构造函数中请求此信息。在您的听众 class 中,您将拥有:
public class MainListener implements Listener {
private Main plugin;
public MainListener(Main plugin) {
this.plugin = plugin;
}
@EventHandler
public void onRespawn(PlayerRespawnEvent event) {
FileConfiguration config = plugin.getConfig();
}
如您所见,这使您可以在 MainListener
class 中的任何位置使用 plugin.getConfig()
。您将使用 new MainListener(this);
实例化侦听器(例如在 Main class 的 onEnable()
方法中)。当然不要忘记注册监听器。
我明白你想在这里做什么,但我相信有更好的方法来做到这一点(在玩家死后传送玩家是个小问题,或者根本行不通)。我不确定你为什么把你的方法设为静态,你是在事件方法中调用它吗? P1
和 p2
没有作为参数传递到方法中。这就是我会做的(尝试过并且成功):不是在玩家死亡时传送玩家,而是在玩家重生时更改重生位置,如下所示:
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
FileConfiguration config = getConfig(); //Or whichever method you are using
World world = Bukkit.getWorld(config.getString("world"));
double x = config.getDouble("x");
double y = config.getDouble("y");
double z = config.getDouble("z");
float yaw = (float) config.getDouble("yaw");
float pitch = (float) config.getDouble("pitch");
event.setRespawnLocation(new Location(world, x, y, z, yaw, pitch));
}
这是我的测试config.yml文件:
world: world
x: 0
y: 80
z: 0
yaw: 0
pitch: 0
注意:我没有检查具有该名称的世界是否真的存在以及玩家传送到的位置是否安全。那将是您需要实施的事情。
我有两个 classes "main" 用于命令等,"events" 用于事件,现在 我想让事件 "PlayerdeathEvent" 将我传送到一个点,并且该点的坐标保存在配置中,现在我测试这个:
我参加了活动class
public void lbbkampfrespawn() { FileConfiguration cfg = this.getConfig(); { String world = cfg.getString("LBBsetkampfrespawn1.world"); double x = cfg.getDouble("LBBsetkampfrespawn1.x"); double y = cfg.getDouble("LBBsetkampfrespawn1.y"); double z = cfg.getDouble("LBBsetkampfrespawn1.z"); double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw"); double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch"); Location loc = new Location(Bukkit.getWorld(world), x, y, z); loc.setYaw((float) yaw); loc.setPitch((float) pitch); p1.teleport(loc); } { String world = cfg.getString("LBBsetkampfrespawn2.world"); double x = cfg.getDouble("LBBsetkampfrespawn2.x"); double y = cfg.getDouble("LBBsetkampfrespawn2.y"); double z = cfg.getDouble("LBBsetkampfrespawn2.z"); double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw"); double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch"); Location loc = new Location(Bukkit.getWorld(world), x, y, z); loc.setYaw((float) yaw); loc.setPitch((float) pitch); p2.teleport(loc); } } }
问题:
不知道 "getConfig()"
eclipse 告诉我要用这个方法 getConfig()
我用静态
在主要class中做到了public static void lbbkampfrespawn() { FileConfiguration cfg = this.getConfig(); { String world = cfg.getString("LBBsetkampfrespawn1.world"); double x = cfg.getDouble("LBBsetkampfrespawn1.x"); double y = cfg.getDouble("LBBsetkampfrespawn1.y"); double z = cfg.getDouble("LBBsetkampfrespawn1.z"); double yaw = cfg.getDouble("LBBsetkampfrespawn1.yaw"); double pitch = cfg.getDouble("LBBsetkampfrespawn1.pitch"); Location loc = new Location(Bukkit.getWorld(world), x, y, z); loc.setYaw((float) yaw); loc.setPitch((float) pitch); p1.teleport(loc); } { String world = cfg.getString("LBBsetkampfrespawn2.world"); double x = cfg.getDouble("LBBsetkampfrespawn2.x"); double y = cfg.getDouble("LBBsetkampfrespawn2.y"); double z = cfg.getDouble("LBBsetkampfrespawn2.z"); double yaw = cfg.getDouble("LBBsetkampfrespawn2.yaw"); double pitch = cfg.getDouble("LBBsetkampfrespawn2.pitch"); Location loc = new Location(Bukkit.getWorld(world), x, y, z); loc.setYaw((float) yaw); loc.setPitch((float) pitch); p2.teleport(loc); } }
问题:我不能用这个。在静态
我要做的感谢您的帮助!
当你需要更多的鳕鱼时请发送什么。
谢谢。对不起拼写
Editor's Note:
A Deutsche version of the answer was included previously, but removed because posts should be in English. If you would like to see it, use the revision history.
getConfig()
方法继承自 JavaPlugin
class,您的 classes 中只有一个应该扩展。不过,您可以通过多种方式从另一个 class 访问配置文件。例如,可以保留对 "main" class 或插件(扩展 JavaPlugin
的那个)的引用,然后使用 object/reference 调用 getConfig()
.您还可以创建一个静态 getter 方法,该方法 returns 您的插件实例(不确定这是否是好的做法)。 Eclipse 可能会告诉您创建 getConfig()
方法,因为您在不扩展 JavaPlugin
.
例如,假设扩展 JavaPlugin
的 class 称为 "Main"。例如,如果您有一个监听器 class 想要访问您的 Main class,您可以在监听器 class 的构造函数中请求此信息。在您的听众 class 中,您将拥有:
public class MainListener implements Listener {
private Main plugin;
public MainListener(Main plugin) {
this.plugin = plugin;
}
@EventHandler
public void onRespawn(PlayerRespawnEvent event) {
FileConfiguration config = plugin.getConfig();
}
如您所见,这使您可以在 MainListener
class 中的任何位置使用 plugin.getConfig()
。您将使用 new MainListener(this);
实例化侦听器(例如在 Main class 的 onEnable()
方法中)。当然不要忘记注册监听器。
我明白你想在这里做什么,但我相信有更好的方法来做到这一点(在玩家死后传送玩家是个小问题,或者根本行不通)。我不确定你为什么把你的方法设为静态,你是在事件方法中调用它吗? P1
和 p2
没有作为参数传递到方法中。这就是我会做的(尝试过并且成功):不是在玩家死亡时传送玩家,而是在玩家重生时更改重生位置,如下所示:
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
FileConfiguration config = getConfig(); //Or whichever method you are using
World world = Bukkit.getWorld(config.getString("world"));
double x = config.getDouble("x");
double y = config.getDouble("y");
double z = config.getDouble("z");
float yaw = (float) config.getDouble("yaw");
float pitch = (float) config.getDouble("pitch");
event.setRespawnLocation(new Location(world, x, y, z, yaw, pitch));
}
这是我的测试config.yml文件:
world: world
x: 0
y: 80
z: 0
yaw: 0
pitch: 0
注意:我没有检查具有该名称的世界是否真的存在以及玩家传送到的位置是否安全。那将是您需要实施的事情。