반응형

이전 강의

https://syntaxack.tistory.com/entry/minecraftplugin11

 

마인크래프트 플러그인 강좌 11강 - [config.yml] saveDefaultConfig(), saveConfig(), reloadConfig() 차이와 실시

이전 강의https://syntaxack.tistory.com/entry/minecraftplugin10 마인크래프트 플러그인 강좌 10강 - config.yml이란? 생성과 구조 이해하기이전 강의https://syntaxack.tistory.com/entry/minecraftplugin9 마인크래프트 플러그

syntaxack.tistory.com

 

이번 강의 목표

- config.yml에서 값을 불러오는 대표 메서드 3가지
👉 getString(), getInt(), getBoolean()

- 명령어로 설정값을 불러와서 메시지로 출력해보기

 

1. 프로젝트 생성하기

Intellij를 실행해서 새로운 마인크래프트 프로젝트를 만들어준다.

프로젝트 생성 방법을 모르면 아래 링크를 눌러 한 번만 보면 된다.

https://syntaxack.tistory.com/entry/minecraftplugin1

 

마인크래프트 플러그인 강좌 1강 - 첫 플러그인 만들기

이전 강의https://syntaxack.tistory.com/entry/minecraftplugin0 마인크래프트 플러그인 강좌 0강 - 준비하기마인크래프트 플러그인이란?마인크래프트 플러그인은 마인크래프트 내에서 기능을 확장할 수 있도

syntaxack.tistory.com

1. paper/spigot/sponge 템플릿에서 여러분이 사용하는 버킷 플러그인을 선택한다.

2. 빌드 시스템은 Gradle를 선택한다.

3. 언어는 자바를 선택한다.

4. 마인크래프트 버전은 여러분이 플러그인을 적용항 버전과 paper 버전이 일치하도록 선택한다.

예를 들어서 마인크래프트 버전 1.21.4, paper 버전 1.21.4 이면 1.21.4를 선택한다.

5. 플러그인 이름과 클래스 이름을 적는다.

 

생성을 눌러준다.

https://syntaxack.tistory.com/entry/minecraftpluginproblem1

 

마인크래프트 플러그인 강좌 - 잘못된 Gradle JVM 구성을 발견했습니다.

IntelliJ를 사용해서 마인크래프트 플러그인 프로젝트를 만들었을 때 잘못된 Gradle JVM 설정이라는 알람이 뜰 때가 있다.이는 JDK와 Gradle이 호환되지 않아서 발생하는 문제이다.이때는 인터넷 검색

syntaxack.tistory.com

만약 Gradle과 JVM 버전이 안 맞는다는 오류가 뜨면 위의 포스트를 보면 된다.

 

2. Gradle build 설정

우리는 jar 파일로 빌드할 때 빌드 위치를 바로 플러그인 폴더로 빌드되게 설정할 것이다.

build.gradle 파일을 열어준다.

tasks.jar{
    archiveFileName = 'configTest3.jar'
    destinationDirectory = file('C:\\Users\\kijoon\\Desktop\\Server\\plugins')
}

 

3. config.yml 파일 만들기

resource 폴더에 만들어준다.

 

4. config.yml 작성하기

plugin-name: "테스트 플러그인"
max-players: 50
maintenance-mode: true

플러그인 이름을 테스트 플러그인, 최대 인원 수를 50, 서버 점검 상태를 true로 config 파일에 설정했다.

 

5. ConfigTest3.java 작성하기

package org.blog.configTest3;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

public final class ConfigTest3 extends JavaPlugin {

    @Override
    public void onEnable() {
        // Plugin startup logic
        getLogger().info("플러그인이 활성화됐습니다.");
        saveDefaultConfig();
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNull String label, String @NotNull [] args) {
        if (command.getName().equalsIgnoreCase("configtest")) {
            String pluginName = getConfig().getString("plugin-name");
            int maxPlayers = getConfig().getInt("max-players");
            boolean maintenance = getConfig().getBoolean("maintenance-mode");

            sender.sendMessage("📦 플러그인 이름: " + pluginName);
            sender.sendMessage("👥 최대 인원: " + maxPlayers);
            sender.sendMessage("🔧 점검 모드: " + (maintenance ? "켜짐" : "꺼짐"));
            return true;
        }

        return false;
    }
}

 

6. 코드 설명

@Override
    public void onEnable() {
        // Plugin startup logic
        getLogger().info("플러그인이 활성화됐습니다.");
        saveDefaultConfig();
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

onEnable(), onDisable(), saveDefaultConfig()는 아래 강의를 참고하길 바란다.

https://syntaxack.tistory.com/entry/minecraftplugin11

 

마인크래프트 플러그인 강좌 11강 - [config.yml] saveDefaultConfig(), saveConfig(), reloadConfig() 차이와 실시

이전 강의https://syntaxack.tistory.com/entry/minecraftplugin10 마인크래프트 플러그인 강좌 10강 - config.yml이란? 생성과 구조 이해하기이전 강의https://syntaxack.tistory.com/entry/minecraftplugin9 마인크래프트 플러그

syntaxack.tistory.com

 

@Override
public boolean onCommand(@NotNull CommandSender sender, Command command, @NotNull String label, String @NotNull [] args)

onCommand에 대한 내용은 아래 강의를 참고 바란다.

https://syntaxack.tistory.com/entry/minecraftplugin2

 

마인크래프트 플러그인 강좌 2강 - 커스텀 커맨드 구현

이전 강의https://syntaxack.tistory.com/entry/minecraftplugin1 마인크래프트 플러그인 강좌 1강 - 첫 플러그인 만들기이전 강의https://syntaxack.tistory.com/entry/minecraftplugin0 마인크래프트 플러그인 강좌 0강 - 준비

syntaxack.tistory.com

 

if (command.getName().equalsIgnoreCase("configtest")) {

명령어 이름을 configtest인지 확인한다. 대소문자 구분하지 않는다.

 

String pluginName = getConfig().getString("plugin-name");

config.yml에서 plugin-name이라는 설정 값을 문자열로 읽어온다.

즉 "테스트 플러그인"을 pluginName에 넣는다.

 

int maxPlayers = getConfig().getInt("max-players");

config.yml에서 max-players이라는 설정 값을 정수로 읽어온다.

즉 50을 maxPlayers에 넣는다.

 

boolean maintenance = getConfig().getBoolean("maintenance-mode");

config.yml에서 maintenance-mode 항목을 boolean(참/거짓) 값으로 읽어온다.

true를 maintenance에 넣는다.

 

sender.sendMessage("📦 플러그인 이름: " + pluginName);
sender.sendMessage("👥 최대 인원: " + maxPlayers);
sender.sendMessage("🔧 점검 모드: " + (maintenance ? "켜짐" : "꺼짐"));

이는 플레이어에게 위에서 변수에 담은 값들을 출력한다.

(maintenance ? "켜짐" : "꺼짐") 문법은 maintenance가 true 면 켜짐, false면 꺼짐이 출력된다.

 

7. plugin.yml 작성하기

name: configTest3
version: '1.0-SNAPSHOT'
main: org.blog.configTest3.ConfigTest3
api-version: '1.21'
commands:
  configtest:
    description: 설정값을 확인합니다

 

8. jar  파일로 빌드하기

 

9. 테스트하기

/configtest를 입력하면 위와 같이 정상적으로 출력된다.

 

10. 다음 강의

다음 강의에서는 getStringList로 목록 값을 읽어오는 법을 알아보겠다.

예를 들어 금지어, 목록, 도움말 리스트 만들기를 실습하겠다.

🎯 다음 강의에서는 이런 걸 배운다.

✅ config.yml에서 List<String> 값을 불러오는 법
✅ getStringList("key") 사용법
✅ 반복문(for)을 활용한 출력
✅ 금지어 필터, 명령어 설명 목록 등 활용 예시

 

코드 질문이나 궁금한 점은 댓글 ㄱㄱ

반응형