我想在初始化时向我的对象“ yamlFile”添加一个新对象。
public yamlFile config = new yamlFile() {
protected ServerGroup GROUP_BELOW_16, GROUP_16, GROUP_17, GROUP_18, GROUP_ABOVE_18;
@Override
public void load() {
super.load();
try {
GROUP_BELOW_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.-16"));
GROUP_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.16"));
GROUP_17 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.17"));
GROUP_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.18"));
GROUP_ABOVE_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.+18"));
} catch (final NullPointerException e) {
Bot.getLogger().warning("Path existiert nicht");
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
}
}.setFolder("data").setFilename("config.yml").build();
因此,在对象yamlFile中没有“ ServerGroup”对象。所以我想添加一些。所以我在初始化时添加了它们,但是它们不能从其他任何地方访问。所以我不能在其他方法中使用它们,例如:
public void method(){
config.GROUP_16 = null;
}
我的问题:我该怎么做才能使ServerGroup对象可访问而不在“原始” yamlFile类中实现它们?
原始yamlFile类:
public class yamlFile {
/**
*
* var methods
*
**/
private String folder;
private String filename;
public String getFolder() {
return folder;
}
public yamlFile setFolder(String folder) {
this.folder = folder;
return this;
}
public String getFilename() {
return filename;
}
public yamlFile setFilename(String filename) {
this.filename = filename;
return this;
}
/**
*
* file methods
*
**/
private File file;
public yamlFile set() {
file = new File(getFolder(), getFilename());
return this;
}
final private YamlConfiguration yamlConfiguration = new YamlConfiguration();
/**
* Checks for the specific file if it exists.
*/
public void fileExists() throws FileNotFoundException {
try {
if (!file.exists()) {
yamlConfiguration.options().copyDefaults(true);
file = new File(getFolder(), getFilename());
yamlConfiguration.save(file);
}
} catch (final NullPointerException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
}
public void load() {
try {
fileExists();
} catch (final FileNotFoundException e) {
Bot.getLogger().info("Creating file: " + getFolder() + "/" + getFilename());
}
try {
yamlConfiguration.load(file);
} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final InvalidConfigurationException e) {
e.printStackTrace();
}
}
public void save() {
try {
yamlConfiguration.save(file);
} catch (final IOException e) {
e.printStackTrace();
}
}
public YamlConfiguration getFile() {
return yamlConfiguration;
}
}
答案 0 :(得分:0)
我将创建一个扩展yamlFile的接口(例如ServerGroupYamlFile)。该接口将包括您的方法。
public ServerGroup getGroup16(){
return config.GROUP_16;
}
然后,您实现您的界面:
public yamlFile config = new ServerGroupYamlFile() { ....
然后,您可以灵活地在yaml类中添加任何方法。
如果组列表是动态的,则只需在界面上提供一种返回组列表的方法即可。
public List<ServerGroup> getGroups(){
return yourGroups;
}
答案 1 :(得分:0)
感谢您的回答!由于lan McLaird的评论,我刚刚找到了解决方案! 我只是创建了一个扩展yamlFile的新内部类,然后在其中添加我的属性。
如果有人需要此代码,请在此处发布:
public class FileManager {
public class config extends yamlFile {
public ServerGroup GROUP_BELOW_16, GROUP_16, GROUP_17, GROUP_18, GROUP_ABOVE_18;
@Override
public void load() {
super.load();
try {
GROUP_BELOW_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.-16"));
GROUP_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.16"));
GROUP_17 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.17"));
GROUP_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.18"));
GROUP_ABOVE_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.+18"));
} catch (final NullPointerException e) {
Bot.getLogger().warning("Path existiert nicht");
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
}
}
public config config = (api.filemanager.FileManager.config) new config().setFolder("data").setFilename("config.yml").set();
public YamlConfiguration getConfigConfiguration() {
return config.getFile();
}
public config getConfig() {
return config;
}
}