개인 자료란 (JE)

  서버 커뮤니티

Profile useu 대표칭호 없음
Profile

자료 자바 에디션(JE) 플러그인 스크립트

창고 스크립트 [비동기 / 파일저장]

2024.06.04 조회 수 377 추천 수 0
장르 그 외에 장르 
게임버전 모든버전 
API 스피곳, 페이퍼 
원산지 국산 
개발자 본인(useu) 
저작권 스크립트 파일 내 제작자 표시만 남기고 자유롭게 이용 
자료 출처 https://본인 







# 제작자: useu / 디스코드: o1_o2
# Tested in
#   Skript: 2.8.5
#   Minecraft: 1.20.4

options:
    PERM_OPEN_CHEST: uchest.open
    PERM_MANAGE_CHEST: uchest.manage

import:
    org.bukkit.Bukkit
    org.bukkit.Material
    org.bukkit.inventory.ItemStack
    org.bukkit.configuration.file.YamlConfiguration

    java.io.File
    java.util.UUID
    java.lang.Runnable

    ch.njol.skript.Skript

on unload:
    set {-chest.unloadBySystem} to true
    loop {-chest::*}:
        unloadChestFromUUID(loop-index)
    delete {-chest::*}
    delete {-chest.unloadBySystem}

on load:
    loop all players:
        loadChestFromPlayer(loop-value)

function onAsyncChestLoaded(uuid: string):
    delete {-chest.running::%{_uuid}%}
    set {_p} to Bukkit.getOfflinePlayer(UUID.fromString({_uuid}))
    send "[창고] 로딩 완료" to {_p}
    if {_p} is not online:
        runAsnycUnloadChestFromUUID({_uuid})

function onAsyncChestUnloaded(uuid: string):
    delete {-chest.running::%{_uuid}%}
    if Bukkit.getOfflinePlayer(UUID.fromString({_uuid})) is online:
        runAsnycLoadChestFromUUID({_uuid})

function loadChestFromPlayer(p: player):
    loadChestFromUUID(uuid of {_p})

function loadChestFromUUID(uuid: string):
    set {_file} to loadPlayerChestDataFile({_uuid})
    set {_config} to YamlConfiguration.loadConfiguration({_file})
    set {_inv} to chest inventory with 6 rows named "창고"
    loop integers between 0 and 53:
        continue if {_config}.isSet("%loop-number%") is false
        set slot loop-number of {_inv} to {_config}.getItemStack("%loop-number%", new ItemStack(Material.AIR)) 
    set {-chest::%{_uuid}%} to {_inv}

function unloadChestFromPlayer(p: player):
    unloadChestFromUUID(uuid of {_p})

function unloadChestFromUUID(uuid: string):
    saveChestFromUUID({_uuid})
    delete {-chest::%{_uuid}%}

function saveChestFromPlayer(p: player):
    saveChestFromUUID(uuid of {_p})

function saveChestFromUUID(uuid: string):
    if {-chest::%{_uuid}%} is not set:
        exit
    set {_file} to loadPlayerChestDataFile({_uuid})
    set {_config} to new YamlConfiguration()
    loop integers between 0 and 53:
        continue if slot loop-number of {-chest::%{_uuid}%} is air
        set {_item} to slot loop-number of {-chest::%{_uuid}%}
        {_config}.set("%loop-number%", {_item})
    {_config}.save({_file})

function loadPlayerChestDataFile(uuid: string) :: object:
    set {_scriptsPath} to Skript.getInstance().getScriptsFolder().getAbsolutePath()
    set {_folder} to new File("%{_scriptsPath}%\UChest")
    {_folder}.mkdirs()
    set {_file} to new File({_folder}, "%{_uuid}%.yaml")
    if {_file}.exists() is false:
        try {_file}.createNewFile()
        if last java exception is set:
            Skript.error("[ERROR] 0001 (in %script name%)")
            (last java exception).printStackTrace()
            stop
    return {_file}

function runAsnycLoadChestFromUUID(uuid: string):
    create new section stored in {_function::run}:
        loadChestFromUUID({_uuid})
        create section stored in {_function::run}:
            onAsyncChestLoaded({_uuid})
        set {_proxy} to new proxy instance of Runnable using {_function::*}
        Bukkit.getScheduler().runTask(Skript.getInstance(), {_proxy})
    set {_proxy} to new proxy instance of Runnable using {_function::*}
    set {-chest.running::%{_uuid}%} to Bukkit.getScheduler().runTaskAsynchronously(Skript.getInstance(), {_proxy})

function runAsnycUnloadChestFromUUID(uuid: string):
    create new section stored in {_function::run}:
        unloadChestFromUUID({_uuid})
        create section stored in {_function::run}:
            onAsyncChestUnloaded({_uuid})
        set {_proxy} to new proxy instance of Runnable using {_function::*}
        Bukkit.getScheduler().runTask(Skript.getInstance(), {_proxy})
    set {_proxy} to new proxy instance of Runnable using {_function::*}
    set {-chest.running::%{_uuid}%} to Bukkit.getScheduler().runTaskAsynchronously(Skript.getInstance(), {_proxy})

function checkChestTaskRunning(uuid: string) :: boolean:
    if {-chest.running::%{_uuid}%} is not set:
        return false
    if Bukkit.getScheduler().isCurrentlyRunning({-chest.running::%{_uuid}%}.getTaskId()) is false:
        return false
    return true

command /창고:
    permission: {@PERM_OPEN_CHEST}
    aliases: ckdrh, chest, 상자, tkdwk
    executable by: players
    trigger:
        if {-chest::%uuid of player%} is not set:
            send "[창고] 창고가 아직 로딩되지 않았습니다."
            exit
        if {-chest.running::%uuid of player%} is set:
            send "[창고] 창고가 아직 로딩되지 않았습니다."
            exit
        open {-chest::%uuid of player%} to player

command /창고관리 :
    permission: {@PERM_MANAGE_CHEST}
    executable by: players
    trigger:
        if {-chest::%uuid of arg 1%} is not set:
            send "[창고] 창고가 아직 로딩되지 않았습니다."
            exit
        if {-chest.running::%uuid of arg 1%} is set:
            send "[창고] 창고가 아직 로딩되지 않았습니다."
            exit
        open {-chest::%uuid of arg 1%} to player

on join:
    stop if checkChestTaskRunning(uuid of player) is true
    send "[창고] 로딩 시작"
    runAsnycLoadChestFromUUID(uuid of player)

on quit:
    stop if checkChestTaskRunning(uuid of player) is true
    runAsnycUnloadChestFromUUID(uuid of player)


창고 스크립트를 비동기, 파일 저장 방식으로 만들어봤습니다.


[비동기] 유저 접속시 해당 유저의 창고 정보 파일을 읽어 인벤토리 생성 및 변수에 저장합니다.

[비동기] 유저 퇴장시 현재 변수에 저장된 인벤토리의 내용물을 파일에 저장합니다. / 인벤토리 변수를 삭제합니다.


해당 스크립트는 유저 접속시 아이템이 담긴 인벤토리 객체를 변수에 저장하는 방식으로 구현했습니다.

그냥 개인적인 궁금증으로 만들어본거라 이 방식이 문제가 있을지 확인해보진 않았습니다.


UChest.sk

Warning
댓글이 없습니다.

새로운 댓글을 등록해 주세요!

뉴스 및 창작물
/files/thumbnails/439/832/003/262x150.crop.jpg?20240711073840

레드스톤

【Ado】루루 (RuLe) | 피아노 & 노트블럭 커버

노트블럭전문가

2024-07-11

0

/files/thumbnails/600/818/003/262x150.crop.jpg?20240622093723

레드스톤

[마크 피아노커버] 에반게리온 잔혹한 천사의 테제 2

노트블럭전문가

2024-06-22

3

/files/thumbnails/647/816/003/262x150.crop.jpg?20240619112336

건축

숭례문(프리퀄) 4

KHC

2024-06-19

0

/files/thumbnails/308/815/003/262x150.crop.jpg?20240617084353

레드스톤

[피아노커버] 디스코드 - QWER 3

노트블럭전문가

2024-06-17

1

/files/thumbnails/932/812/003/262x150.crop.jpg?20240622110502

업데이트

마인크래프트 1.21 정식 업데이트

학교가기싫다

2024-06-14

1

/files/thumbnails/308/805/003/262x150.crop.jpg?20240605201646

레드스톤

[노트블럭 커버] 팬서비스/mona 【 HoneyWorks 】 1

노트블럭전문가

2024-06-05

1

/files/thumbnails/288/791/003/262x150.crop.jpg?20240602150002

아티클

《까다로운 시련》 업데이트 날짜 공개 및 정보

학교가기싫다

2024-06-02

0

/files/thumbnails/841/773/003/262x150.crop.jpg?20240526120908

레드스톤

마인크래프트 노트블록버전 『 봄을 고하다 (春を告げる) 』 [더블옥타브 버전]

노트블럭전문가

2024-05-26

0

/files/thumbnails/712/796/003/262x150.crop.jpg?20240525085438

레드스톤

마인크래프트 노트블록으로 만든 『 봄을 고하다 (春を告げる) 』 1

Sonttukk

2024-05-25

1

/files/thumbnails/268/789/003/262x150.crop.jpg?20240515192032

레드스톤

벌레먹은 돌 빠르게 제거하는 법

GlassesFilm

2024-05-15

0

/files/thumbnails/797/788/003/262x150.crop.jpg?20240515090924

건축

마인크래프트로 구현한 카르카손 보드게임

Warak

2024-05-15

3

/files/thumbnails/487/784/003/262x150.crop.jpg?20240508233607

모드

Windows Borderless 모드에서 악성코드 발견 3

학교가기싫다

2024-05-08

1