Java(Bukkit)如何在没有这个的情况下访问Config?

标签 java config bukkit

我有两个类“main”用于命令和更多内容,“events”用于事件,现在 我想让事件“PlayerdeathEvent”将我传送到一个点,并将该点的坐标保存在配置中,现在我测试一下:

  1. 我在事件类中做到了

    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()

  • 我在主类中使用 static 实现了它

    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 类继承的,只有一个类应该扩展该类。不过,您可以通过多种方式从另一个类访问配置文件。例如,可以保留对“主”类或插件(扩展 JavaPlugin 的类或插件)的引用,然后使用该对象/引用调用 getConfig() 。您还可以创建一个静态 getter 方法来返回插件的实例(不确定这是否是一个好的做法)。 Eclipse 可能会告诉您创建 getConfig() 方法,因为您在不扩展 JavaPlugin 的类中调用它。

    例如,假设您的扩展 JavaPlugin 的类名为“Main”。例如,如果您有一个监听器类想要访问您的 Main 类,则可以在监听器类的构造函数中请求此信息。在您的监听器类中,您将拥有:

    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 类中的任何位置使用 plugin.getConfig()。您可以使用 new MainListener(this); 实例化监听器(例如在 Main 类的 onEnable() 方法中)。当然不要忘记注册监听器。

    我明白你想在这里做什么,但我相信有更好的方法来做到这一点(在玩家死亡时传送玩家是有问题的,或者简单的行不通)。我不确定为什么你的方法是静态的,你是在事件方法中调用它吗? P1p2 未作为方法中的参数传递。这就是我要做的(尝试过这个并且它有效):不要在死亡时传送玩家,而是在玩家重生时更改重生位置,如下所示:

    @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
    

    注意:我没有检查这个名字的世界是否真的存在,以及该位置是否是玩家传送到的安全位置。这将是您需要实现的事情。

    关于Java(Bukkit)如何在没有这个的情况下访问Config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763834/

    相关文章:

    java - 写入文本文件时如何添加新行?

    java - 如何识别哪些代码行参与了Java程序的特定执行?

    java - 无法让Spring事务回滚(java+mysql)

    node.js - 保护 nodeJS 数据库配置文件

    testing - Telegraf 测试输入和输出插件错误多个 conf 文件

    javascript - 在解析为 JSON 之前执行 JS 配置(对象)

    java - 将对象传递给事件

    java - 找不到文件异常 - 在 android 中的 sdcard 上创建新文件时

    java - 如何使用noteblockapi bukkit?

    java - Bukkit 事件多次触发