본문 바로가기

마인크래프트 애드온 제작

스크립트 api의 첫걸음, chatSend 채팅명령어 만들기 #애드온제작

 

채팅창에 어떤 메시지를 입력하면 약속한 커맨드가 실행된다.


복붙용 코드


import * as server from "@minecraft/server"
import * as ui from "@minecraft/server-ui"

const { world } = server;
// 커스텀 명령어
world.afterEvents.chatSend.subscribe(e => {

    const msg = e.message

    if (msg == "명령어1") {
        e.sender.runCommand("say 명령어1을 실행함")
    }
    if (msg == "명령어2") {
        e.sender.runCommand("say 명령어2를 실행함")
        e.sender.runCommand("give @s apple")
    }
    if (msg == "명령어3") {
        e.sender.runCommand("say 명령어3을 실행함")
        e.sender.runCommand("effect @s speed")
        e.sender.runCommand("")
    }
})


main.js파일에서 코딩을 한 모습입니다. 이 코드는 사용자가 특정 메시지를  채팅창에 입력하면 특정 커맨드를 실행시키는 것을 구현했습니다. 

 

중요한 몇 가지만 살펴보도록 하겠습니다. 

 

📝 라이브러리 불러오고 변수 선언합니다. 관련 함수를 쓰기 위해서 꼭 필요합니다. 

import * as server from "@minecraft/server"

const { world } = server;

 

🚀 chatSend 이벤트를 구독합니다. 채팅창에 입력하면 e가 실행된다는 뜻입니다. 

world.afterEvents.chatSend.subscribe(e => {


🧑‍💻 e.sender는 메시지를 쓴 사람, e.messge는 메시지 내용입니다. 이것은 msg로 다시 선언되었습니다. 

const msg = e.message


📤 플레이어가 명령어를 실행합니다. 이것은 조건문에서 메시지 내용을 확인해서 일치할 때만 실행됩니다. 

if (msg == "명령어2") {

e.sender.runCommand("say 명령어2를 실행함") e.sender.runCommand("give @s apple")

}


📚  if문을 추가해서 다른 명령어도 약속할 수 있습니다. 

따라하기 어렵다면 자세한 영상을 확인해보세요.