gpt4 book ai didi

java - Bukkit config.yml 连接

转载 作者:行者123 更新时间:2023-12-01 23:36:40 24 4
gpt4 key购买 nike

好吧,我正在尝试编写一个 bukkit 插件,我需要从配置文件中获取值,我在以下位置查找了教程 http://wiki.bukkit.org/Configuration_API_Reference#The_Configuration_Object但这没有给我任何帮助。

所以我的 connect.java 代码是这样的:

package com.live.AlioGenerica.netherflight;

import java.util.Set;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

public class users {
this.getConfig().options().copyDefaults(true);

public static Boolean getValue(Player p) {
this.getConfig().getBoolean(p);
return true;
}

public static Object setValue(Player p, Boolean v) {
conval.myConfig().set("users." + p + ".boolean", v);
return true;
}
}

config.yml是这样的:

users:
username: false
userother:true

等等。

我到底如何连接,我找不到任何东西。我知道这很困惑,因为我不知道该怎么做。

最佳答案

我自己创建了 Bukkit 插件。 YamlConfiguration 非常易于使用。可以使用方法

public void set(String option, Object value)

将某个配置选项(option)设置为指定值(value)并且

public Object get(String option)

等来获取先前设置的值。要查找您可以使用的所有方法,请在 Bukkit's JavaDoc for YamlConfiguration 中查找。 .

如果您想保存 YamlConfiguration,请使用

public void save(File f) throws IOException

除此之外,您还应该致力于编码。这是您现在的代码的改进版本。

package com.live.AlioGenerica.netherflight;

import java.io.IOException;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

public class Users {
private static YamlConfiguration config = null;
// some code

// this method has to be static if you want to use it
// in another static method
public static YamlConfiguration getConfig() {
// load the configuration if it hasn't been loaded yet
if(config == null) {
config = YamlConfiguration.loadConfiguration("path/to/config.yml");
}
// some code
return config;
}

// this will return the boolean you want to get
public static Boolean getValue(Player p) {
return getConfig().getBoolean("users."+p.getName());
}

// use this code to set the value
public static void setValue(Player p, Boolean value) {
getConfig().set("users."+p.getName(), value);
}

// save the configuration
public static void save() {
try{
config.save(new File("path/to/config.yml"));
}catch(IOException e){
e.printStackTrace();
}
}
}

如果还有任何问题,请告诉我。

@编辑:刚刚意识到您想为玩家保存一个 boolean 值,以便它在配置中看起来像这样。

users:
player: true
anotherplayer: false
andsoon: false

关于java - Bukkit config.yml 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18566104/

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