2301_78804227 2024-12-31 17:37 采纳率: 57.1%
浏览 8

bukkit插件中vector无法正确读取

请问Bukkit开发里面这个vector老是读不到正确的数值是什么情况啊,
getVectorFromConfig()方法老是读取不到正确数值

    public void loadRegions() {
        ConfigurationSection section = regionConfig.getConfigurationSection("regions");

        if (section != null) {
            for (String regionName : section.getKeys(false)) {
                String owner = section.getString(regionName + ".owner");
                Vector firstPoint = getVectorFromConfig(section, regionName, "firstPoint");
                Vector secondPoint = getVectorFromConfig(section, regionName, "secondPoint");


                System.out.println("Loaded region: " + regionName + ", owner: " + owner);
                System.out.println(String.format("First Point: %.2f, %.2f, %.2f", firstPoint.getX(), firstPoint.getY(), firstPoint.getZ()));
                System.out.println(String.format("Second Point: %.2f, %.2f, %.2f", secondPoint.getX(), secondPoint.getY(), secondPoint.getZ()));

                Region region = new Region(owner, regionName, firstPoint, secondPoint);
                regions.put(regionName, region);
            }
        } else {
            System.out.println("没有找到 regions 配置部分!");
        }
    }



    // 从配置中读取一个向量
    public Vector getVectorFromConfig(ConfigurationSection section, String regionName, String pointName) {
        // 构造配置路径
        String basePath = "regions." + regionName + "." + pointName;


        System.out.println("Loading vector from path: " + basePath);

        // 获取坐标
        double x = section.getDouble(basePath + ".x"); // 设置默认值为 0.0
        double y = section.getDouble(basePath + ".y");
        double z = section.getDouble(basePath + ".z");

        // 输出坐标
        System.out.println("x: " + x + ", y: " + y + ", z: " + z);

        // 返回坐标向量
        return new Vector(x, y, z);
    }

为什么控制台读出来的数和我的老是不一样,文件的owner和领地名称它能读到,说明文件和路径没问题,就是这个坐标读取有问题,哪位有经验的能帮忙看看
控制台输出:


[17:29:25] [Server thread/INFO]: [Residence] Enabling Residence v1.0_Alpha
[17:29:25] [Server thread/INFO]: [Residence] Starting Residence...
[17:29:25] [Server thread/INFO]: Loading vector from path: regions.666.firstPoint
[17:29:25] [Server thread/INFO]: x: 0.0, y: 0.0, z: 0.0
[17:29:25] [Server thread/INFO]: Loading vector from path: regions.666.secondPoint
[17:29:25] [Server thread/INFO]: x: 0.0, y: 0.0, z: 0.0
[17:29:25] [Server thread/INFO]: Loaded region: 666, owner: qianhe520
[17:29:25] [Server thread/INFO]: First Point: 0.00, 0.00, 0.00
[17:29:25] [Server thread/INFO]: Second Point: 0.00, 0.00, 0.00
[17:29:25] [Server thread/INFO]: [Residence] Regions loaded successfully.
[17:29:25] [Server thread/INFO]: [Residence] Residence plugin enabled.

我的文件

regions:
  666:
    owner: qianhe520
    firstPoint:
      x: 1.0
      y: 2.0
      z: 3.0
    secondPoint:
      x: 4.0
      y: 5.0
      z: 6.0
  • 写回答

2条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2024-12-31 17:40
    关注
    让【道友老李】来帮你解答,本回答参考通义千问大模型编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    从你提供的代码和配置文件来看,问题可能出在 `getVectorFromConfig` 方法中读取配置项的方式。具体来说,`ConfigurationSection.getDouble` 方法如果没有找到指定的路径,则会返回默认值 0.0。这可能是导致你读取到的坐标总是 (0.0, 0.0, 0.0) 的原因。

    问题分析

    1. 路径问题:确保配置文件中的路径与代码中使用的路径完全一致。
    2. 默认值问题getDouble 方法如果没有找到指定的路径,默认返回 0.0。你需要检查是否正确地获取了配置项。

    解决方案

    1. 检查路径:确保配置文件中的路径与代码中使用的路径完全一致。
    2. 使用 isSet 方法:在获取配置项之前,先检查该配置项是否存在。

    代码修改

    以下是修改后的代码:

    public void loadRegions() {
        ConfigurationSection section = regionConfig.getConfigurationSection("regions");
        if (section != null) {
            for (String regionName : section.getKeys(false)) {
                String owner = section.getString(regionName + ".owner");
                Vector firstPoint = getVectorFromConfig(section, regionName, "firstPoint");
                Vector secondPoint = getVectorFromConfig(section, regionName, "secondPoint");
    
                System.out.println("Loaded region: " + regionName + ", owner: " + owner);
                System.out.println(String.format("First Point: %.2f, %.2f, %.2f", firstPoint.getX(), firstPoint.getY(), firstPoint.getZ()));
                System.out.println(String.format("Second Point: %.2f, %.2f, %.2f", secondPoint.getX(), secondPoint.getY(), secondPoint.getZ()));
    
                Region region = new Region(owner, regionName, firstPoint, secondPoint);
                regions.put(regionName, region);
            }
        } else {
            System.out.println("没有找到 regions 配置部分!");
        }
    }
    
    // 从配置中读取一个向量
    public Vector getVectorFromConfig(ConfigurationSection section, String regionName, String pointName) {
        // 构造配置路径
        String basePath = "regions." + regionName + "." + pointName;
        System.out.println("Loading vector from path: " + basePath);
    
        // 获取坐标
        double x = section.isSet(basePath + ".x") ? section.getDouble(basePath + ".x") : 0.0;
        double y = section.isSet(basePath + ".y") ? section.getDouble(basePath + ".y") : 0.0;
        double z = section.isSet(basePath + ".z") ? section.getDouble(basePath + ".z") : 0.0;
    
        // 输出坐标
        System.out.println("x: " + x + ", y: " + y + ", z: " + z);
    
        // 返回坐标向量
        return new Vector(x, y, z);
    }
    

    检查配置文件

    确保你的配置文件格式正确,例如:

    regions:
      666:
        owner: qianhe520
        firstPoint:
          x: 1.0
          y: 2.0
          z: 3.0
        secondPoint:
          x: 4.0
          y: 5.0
          z: 6.0
    

    调试步骤

    1. 打印路径:在 getVectorFromConfig 方法中打印完整的路径,确保路径正确。
    2. 检查配置文件:确保配置文件中的路径与代码中使用的路径完全一致。
    3. 使用 isSet 方法:在获取配置项之前,先检查该配置项是否存在。

    通过这些步骤,你应该能够正确读取配置文件中的坐标值。如果问题仍然存在,请进一步检查配置文件的格式和内容。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月31日