플러그인을 작성하다 보면 하나의 config.yml
파일에 모든 정보를 다 넣자니 효율적이지 못하다는 느낌을 받는다. 그래서 새로운 config.yml
파일을 만들기로 했다.
본 게시글은 이곳을 참고하여 작성되었습니다.
config.yml
과 같이 resources 폴더에 파일을 만들어 준다.
자신이 원하는 대로 파일을 작성해 준다.
# type list: loop
EventList:
defualtEvent:
name: "기본 보상"
type: "loop"
date: ""
time: "21:00"
item: "paper"
Amount: 1
DisplayName: "9시 접속 보상"
lore:
- "접속 보상"
Enchant:
- "luck_of_the_sea"
메인 클래스에 서버가 실행될 때 리소스 폴더에 있는 파일이 데이터 폴더에 저장되어야 한다.
private FileConfiguration eventConfig = null;
private File eventConfigFile = null;
public void saveEventConfig() {
if (eventConfig == null) {
eventConfigFile = new File(getDataFolder(), "event.yml");
}
if (!eventConfigFile.exists()) {
saveResource("event.yml", false);
}
}
그러면 사진과 같이 플러그인 폴더에 event.yml
파일이 생성되는 것을 볼 수 있다.
public void reloadEventConfig() {
if (eventConfigFile == null) {
eventConfigFile = new File(getDataFolder(), "event.yml");
}
eventConfig = YamlConfiguration.loadConfiguration(eventConfigFile);
Reader defConfigStream = new InputStreamReader(this.getResource("event.yml"), StandardCharsets.UTF_8);
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
eventConfig.setDefaults(defConfig);
}
}
위 코드를 통해서 event.yml
파일을 다시 불러올 수 있다.
public FileConfiguration getEventConfig() {
if (eventConfig == null) {
reloadEventConfig();
}
return eventConfig;
}
eventConfig.getString""
과 같이 사용하면 파일이 없을 때 오류가 생길 수 있기 때문에, 다음과 같이 사용해 준다.
getEventConfig().getString()
과 같이 사용해 주면 된다.
getLogger().info(getEventConfig().getString("EventList.defualtEvent.name"));
서버가 시작될 때 다음과 같이 event.yml
파일에 있는 값을 가져오도록 했다.