diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b63da45 --- /dev/null +++ b/.gitignore @@ -0,0 +1,42 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..30bab2a --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..ce1c62c --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..2287c51 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,28 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..0e65cea --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..fe0b0da --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..13c5c9d --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,35 @@ +plugins { + kotlin("jvm") version "1.7.21" +} + +group = "kr.cosine.randombox" +version = "1.0.0" + +repositories { + mavenCentral() + mavenLocal() + maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") + //maven("https://maven.hqservice.kr/repository/maven-public/") +} + +dependencies { + compileOnly("org.spigotmc", "spigot-api", "1.17.1-R0.1-SNAPSHOT") + + compileOnly("kr.hqservice", "hqframework-bukkit-core", "1.0.1-SNAPSHOT") + compileOnly("kr.hqservice", "hqframework-bukkit-command", "1.0.1-SNAPSHOT") + compileOnly("kr.hqservice", "hqframework-bukkit-inventory", "1.0.1-SNAPSHOT") + compileOnly("kr.hqservice", "hqframework-bukkit-nms", "1.0.1-SNAPSHOT") + + testImplementation(kotlin("test")) + testImplementation(kotlin("reflect")) +} + +tasks { + test { + useJUnitPlatform() + } + jar { + archiveFileName.set("${rootProject.name}-${rootProject.version}.jar") + destinationDirectory.set(file("D:\\서버\\1.20.1 - 개발\\plugins")) + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..7fc6f1f --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..06febab --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists \ No newline at end of file diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..495101b --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + } +} + +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" +} + +rootProject.name = "HQRandomBox" \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/HQRandomBox.kt b/src/main/kotlin/kr/cosine/randombox/HQRandomBox.kt new file mode 100644 index 0000000..3747e27 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/HQRandomBox.kt @@ -0,0 +1,5 @@ +package kr.cosine.randombox + +import kr.hqservice.framework.bukkit.core.HQBukkitPlugin + +class HQRandomBox : HQBukkitPlugin() \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/HQRandomBoxModule.kt b/src/main/kotlin/kr/cosine/randombox/HQRandomBoxModule.kt new file mode 100644 index 0000000..efc027b --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/HQRandomBoxModule.kt @@ -0,0 +1,28 @@ +package kr.cosine.randombox + +import kr.cosine.randombox.config.RandomBoxConfig +import kr.cosine.randombox.config.SettingConfig +import kr.cosine.randombox.scheduler.RandomBoxSaveScheduler +import kr.hqservice.framework.bukkit.core.HQBukkitPlugin +import kr.hqservice.framework.global.core.component.Component +import kr.hqservice.framework.global.core.component.HQModule +import kr.hqservice.framework.yaml.config.HQYamlConfiguration + +@Component +class HQRandomBoxModule( + private val plugin: HQBukkitPlugin, + private val settingConfig: SettingConfig, + private val randomBoxConfig: RandomBoxConfig +) : HQModule { + + override fun onEnable() { + settingConfig.load() + randomBoxConfig.load() + val autoSavePeriod = settingConfig.autoSavePeriod + RandomBoxSaveScheduler(randomBoxConfig).runTaskTimerAsynchronously(plugin, autoSavePeriod, autoSavePeriod) + } + + override fun onDisable() { + randomBoxConfig.save() + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/command/RandomBoxCommand.kt b/src/main/kotlin/kr/cosine/randombox/command/RandomBoxCommand.kt new file mode 100644 index 0000000..30f8c81 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/command/RandomBoxCommand.kt @@ -0,0 +1,82 @@ +package kr.cosine.randombox.command + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kr.cosine.randombox.command.argument.RandomBoxArgument +import kr.cosine.randombox.service.RandomBoxService +import kr.hqservice.framework.command.ArgumentLabel +import kr.hqservice.framework.command.Command +import kr.hqservice.framework.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +@Command(label = "랜덤박스관리", isOp = true) +class RandomBoxCommand( + private val randomBoxService: RandomBoxService +) { + + @CommandExecutor("생성", "랜덤 박스를 생성합니다.", priority = 1) + fun createRandomBox( + player: Player, + @ArgumentLabel("이름") key: String + ) { + if (randomBoxService.createRandomBox(key)) { + player.sendMessage("§a${key} 랜덤 박스를 생성하였습니다.") + } else { + player.sendMessage("§c이미 존재하는 랜덤 박스입니다.") + } + } + + @CommandExecutor("제거", "랜덤 박스를 제거합니다.", priority = 2) + fun removeRandomBox( + player: Player, + @ArgumentLabel("이름") randomBoxArgument: RandomBoxArgument + ) { + val key = randomBoxArgument.randomBox.key + randomBoxService.removeRandomBox(key) + player.sendMessage("§a${key} 랜덤 박스가 제거되었습니다.") + } + + @CommandExecutor("적용", "손에 든 아이템을 랜덤 박스로 만듭니다.", priority = 3) + fun applyRandomBox( + player: Player, + @ArgumentLabel("이름") randomBoxArgument: RandomBoxArgument + ) { + val itemStack = player.inventory.itemInMainHand + val key = randomBoxArgument.randomBox.key + if (randomBoxService.applyRandomBox(itemStack, key)) { + player.sendMessage("§a손에 든 아이템을 $key 랜덤 박스로 설정되었습니다.") + } else { + player.sendMessage("§c손에 아이템을 들어주세요.") + } + } + + @CommandExecutor("아이템설정", "랜덤 박스의 아이템을 설정합니다.", priority = 4) + fun openRandomBoxItemSettingView( + player: Player, + @ArgumentLabel("이름") randomBoxArgument: RandomBoxArgument + ) { + randomBoxService.openRandomBoxItemSettingView(player, randomBoxArgument.randomBox) + } + + @CommandExecutor("확률설정", "랜덤 박스 아이템의 확률을 설정합니다.", priority = 5) + fun openRandomBoxChanceSettingView( + player: Player, + @ArgumentLabel("이름") randomBoxArgument: RandomBoxArgument + ) { + randomBoxService.openRandomBoxChanceSettingView(player, randomBoxArgument.randomBox) + } + + @CommandExecutor("저장", "랜덤 박스의 변경된 사항을 수동으로 저장합니다.", priority = 6) + suspend fun save(sender: CommandSender) { + randomBoxService.save { + sender.sendMessage("§a랜덤 박스의 변경된 사항이 저장되었습니다.") + } + } + + @CommandExecutor("리로드", "config.yml을 리로드합니다.", priority = 6) + fun reload(sender: CommandSender) { + randomBoxService.reload() + sender.sendMessage("§aconfig.yml을 리로드하였습니다.") + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/command/argument/RandomBoxArgument.kt b/src/main/kotlin/kr/cosine/randombox/command/argument/RandomBoxArgument.kt new file mode 100644 index 0000000..44d29e6 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/command/argument/RandomBoxArgument.kt @@ -0,0 +1,7 @@ +package kr.cosine.randombox.command.argument + +import kr.cosine.randombox.data.RandomBox + +data class RandomBoxArgument( + val randomBox: RandomBox +) \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/command/provider/RandomBoxArgumentProvider.kt b/src/main/kotlin/kr/cosine/randombox/command/provider/RandomBoxArgumentProvider.kt new file mode 100644 index 0000000..f8f2cb3 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/command/provider/RandomBoxArgumentProvider.kt @@ -0,0 +1,25 @@ +package kr.cosine.randombox.command.provider + +import kr.cosine.randombox.command.argument.RandomBoxArgument +import kr.cosine.randombox.registry.RandomBoxRegistry +import kr.hqservice.framework.command.CommandArgumentProvider +import kr.hqservice.framework.command.CommandContext +import kr.hqservice.framework.command.argument.exception.ArgumentFeedback +import kr.hqservice.framework.global.core.component.Component +import org.bukkit.Location + +@Component +class RandomBoxArgumentProvider( + private val randomBoxRegistry: RandomBoxRegistry +) : CommandArgumentProvider { + + override suspend fun cast(context: CommandContext, argument: String?): RandomBoxArgument { + if (argument == null) throw ArgumentFeedback.Message("§c랜덤 박스의 이름을 입력해주세요.") + val randomBox = randomBoxRegistry.findRandomBox(argument) ?: throw ArgumentFeedback.Message("§c존재하지 않는 랜덤 박스입니다.") + return RandomBoxArgument(randomBox) + } + + override suspend fun getTabComplete(context: CommandContext, location: Location?): List { + return randomBoxRegistry.getKeys() + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/config/RandomBoxConfig.kt b/src/main/kotlin/kr/cosine/randombox/config/RandomBoxConfig.kt new file mode 100644 index 0000000..5b3e402 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/config/RandomBoxConfig.kt @@ -0,0 +1,59 @@ +package kr.cosine.randombox.config + +import kr.cosine.randombox.data.ChanceItem +import kr.cosine.randombox.data.RandomBox +import kr.cosine.randombox.registry.RandomBoxRegistry +import kr.hqservice.framework.bukkit.core.extension.toByteArray +import kr.hqservice.framework.bukkit.core.extension.toItemArray +import kr.hqservice.framework.global.core.component.Bean +import org.bukkit.configuration.file.YamlConfiguration +import org.bukkit.inventory.ItemStack +import org.bukkit.plugin.Plugin +import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder +import java.io.File + +@Bean +class RandomBoxConfig( + plugin: Plugin, + private val randomBoxRegistry: RandomBoxRegistry +) { + + private companion object { + const val RANDOM_BOX_SECTION_KEY = "random-box" + } + + private val file = File(plugin.dataFolder, "$RANDOM_BOX_SECTION_KEY.yml") + private val config = YamlConfiguration.loadConfiguration(file) + + fun load() { + if (!file.exists()) return + config.getConfigurationSection(RANDOM_BOX_SECTION_KEY)?.apply { + getKeys(false).forEach { key -> + val compressed = getString(key) + val byteArray = Base64Coder.decodeLines(compressed) + val itemStacks = byteArray.toItemArray() + val chanceItems = itemStacks.map { ChanceItem(it) }.toMutableList() + val randomBox = RandomBox(key, chanceItems) + randomBoxRegistry.setRandomBox(key, randomBox) + } + } + } + + fun save() { + val randomBoxMap = randomBoxRegistry.getRandomBoxMap().toMap() + val filteredRandomBoxMap = randomBoxMap.filter { it.value.isChanged } + if (filteredRandomBoxMap.isNotEmpty() || randomBoxRegistry.isRemoved) { + config.set(RANDOM_BOX_SECTION_KEY, null) + randomBoxMap.forEach { (key, randomBox) -> + val sectionKey = "$RANDOM_BOX_SECTION_KEY.$key" + val itemStacks = randomBox.getChanceItems().map { it.getItemStack() } as List + val byteArray = itemStacks.toTypedArray().toByteArray() + val compressed = Base64Coder.encodeLines(byteArray) + config.set(sectionKey, compressed) + randomBox.isChanged = false + } + config.save(file) + randomBoxRegistry.isRemoved = false + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/config/SettingConfig.kt b/src/main/kotlin/kr/cosine/randombox/config/SettingConfig.kt new file mode 100644 index 0000000..403bb4b --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/config/SettingConfig.kt @@ -0,0 +1,38 @@ +package kr.cosine.randombox.config + +import kr.cosine.randombox.data.PickEffect +import kr.hqservice.framework.bukkit.core.extension.colorize +import kr.hqservice.framework.global.core.component.Bean +import kr.hqservice.framework.yaml.config.HQYamlConfiguration + +@Bean +class SettingConfig( + private val config: HQYamlConfiguration +) { + + val autoSavePeriod get() = config.getLong("auto-save-period", 6000) + + var inventoryFullMessage = "§c인벤토리에 공간이 부족합니다." + private set + + var pickEffect = PickEffect() + private set + + fun load() { + config.getSection("message")?.apply { + inventoryFullMessage = getString("inventory-full").colorize() + } + config.getSection("pick-effect")?.apply { + val message = getString("message").colorize() + val sound = getString("sound.name") + val volume = getDouble("sound.volume").toFloat() + val pitch = getDouble("sound.pitch").toFloat() + pickEffect = PickEffect(message, sound, volume, pitch) + } + } + + fun reload() { + config.reload() + load() + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/data/ChanceItem.kt b/src/main/kotlin/kr/cosine/randombox/data/ChanceItem.kt new file mode 100644 index 0000000..3c84ab4 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/data/ChanceItem.kt @@ -0,0 +1,44 @@ +package kr.cosine.randombox.data + +import kr.cosine.randombox.view.RandomBoxViewModel +import kr.hqservice.framework.nms.extension.getNmsItemStack +import kr.hqservice.framework.nms.extension.nms +import org.bukkit.inventory.ItemStack + +class ChanceItem( + private val itemStack: ItemStack +) { + + private companion object { + const val CHANCE_KEY = "HQRandomBoxChance" + } + + fun hasChance(): Boolean { + val nmsItemStack = itemStack.getNmsItemStack() + if (!nmsItemStack.hasTag()) return false + val tag = nmsItemStack.getTag().getDoubleOrNull(CHANCE_KEY) + return tag != null + } + + fun getChance(): Double { + return itemStack.getNmsItemStack().getTag().getDouble(CHANCE_KEY) + } + + fun setChance(chance: Double = 100.0) { + itemStack.nms { + tag { + setDouble(CHANCE_KEY, chance) + } + } + } + + fun getItemStack(): ItemStack = itemStack.clone() + + fun getOriginalItemStack(): ItemStack { + return getItemStack().nms { + tag { + remove(CHANCE_KEY) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/data/PickEffect.kt b/src/main/kotlin/kr/cosine/randombox/data/PickEffect.kt new file mode 100644 index 0000000..0476502 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/data/PickEffect.kt @@ -0,0 +1,16 @@ +package kr.cosine.randombox.data + +import org.bukkit.entity.Player + +data class PickEffect( + private val message: String = "§6%item% 획득!", + private val sound: String = "minecraft:ui.toast.challenge_complete", + private val volume: Float = 1f, + private val pitch: Float = 1f +) { + + fun playEffect(player: Player, itemName: String) { + player.sendMessage(message.replace("%item%", itemName)) + player.playSound(player.location, sound, volume, pitch) + } +} diff --git a/src/main/kotlin/kr/cosine/randombox/data/RandomBox.kt b/src/main/kotlin/kr/cosine/randombox/data/RandomBox.kt new file mode 100644 index 0000000..0e8dd7d --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/data/RandomBox.kt @@ -0,0 +1,37 @@ +package kr.cosine.randombox.data + +import kotlin.math.ln +import kotlin.random.Random + +class RandomBox( + val key: String +) { + + private companion object { + val random by lazy { Random } + } + + private var chanceItems = mutableListOf() + + constructor( + key: String, + chanceItems: MutableList + ) : this(key) { + this.chanceItems = chanceItems + } + + var isChanged = false + + fun getRandomChanceItem(): ChanceItem { + return chanceItems.minByOrNull { + -ln(random.nextDouble()) / it.getChance() + } ?: throw IllegalArgumentException() + } + + fun getChanceItems(): List = chanceItems + + fun setChanceItems(chanceItems: List) { + this.chanceItems = chanceItems.toMutableList() + isChanged = true + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/extension/NumberExtension.kt b/src/main/kotlin/kr/cosine/randombox/extension/NumberExtension.kt new file mode 100644 index 0000000..204edfd --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/extension/NumberExtension.kt @@ -0,0 +1,12 @@ +package kr.cosine.randombox.extension + +import java.text.DecimalFormat + +private const val DEFAULT_FORMAT = "#,##0.###" +private val decimalFormat = DecimalFormat(DEFAULT_FORMAT) + +fun Double.format(): String = decimalFormat.format(this) + +fun Int.format(): String = decimalFormat.format(this) + +fun Long.format(): String = decimalFormat.format(this) \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/listener/RandomBoxListener.kt b/src/main/kotlin/kr/cosine/randombox/listener/RandomBoxListener.kt new file mode 100644 index 0000000..f73580a --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/listener/RandomBoxListener.kt @@ -0,0 +1,29 @@ +package kr.cosine.randombox.listener + +import kr.cosine.randombox.service.RandomBoxService +import kr.hqservice.framework.bukkit.core.listener.Listener +import kr.hqservice.framework.bukkit.core.listener.Subscribe +import org.bukkit.event.player.PlayerInteractEvent +import org.bukkit.inventory.EquipmentSlot + +@Listener +class RandomBoxListener( + private val randomBoxService: RandomBoxService +) { + + private companion object { + const val RIGHT_CLICK = "RIGHT_CLICK" + } + + @Subscribe + fun onRightClick(event: PlayerInteractEvent) { + if (event.hand != EquipmentSlot.HAND) return + if (!event.action.name.contains(RIGHT_CLICK)) return + val itemStack = event.item ?: return + if (itemStack.type.isAir) return + val player = event.player + if (randomBoxService.useRandomBox(player, itemStack)) { + event.isCancelled = true + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/registry/RandomBoxRegistry.kt b/src/main/kotlin/kr/cosine/randombox/registry/RandomBoxRegistry.kt new file mode 100644 index 0000000..3484865 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/registry/RandomBoxRegistry.kt @@ -0,0 +1,29 @@ +package kr.cosine.randombox.registry + +import kr.cosine.randombox.data.RandomBox +import kr.hqservice.framework.global.core.component.Bean + +@Bean +class RandomBoxRegistry { + + private val randomBoxMap = mutableMapOf() + + var isRemoved = false + + fun isRandomBox(key: String): Boolean = randomBoxMap.containsKey(key) + + fun findRandomBox(key: String): RandomBox? = randomBoxMap[key] + + fun setRandomBox(key: String, randomBox: RandomBox) { + randomBoxMap[key] = randomBox + } + + fun removeRandomBox(key: String) { + randomBoxMap.remove(key) + isRemoved = true + } + + fun getKeys(): List = randomBoxMap.keys.toList() + + fun getRandomBoxMap(): Map = randomBoxMap +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/scheduler/RandomBoxSaveScheduler.kt b/src/main/kotlin/kr/cosine/randombox/scheduler/RandomBoxSaveScheduler.kt new file mode 100644 index 0000000..84a3179 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/scheduler/RandomBoxSaveScheduler.kt @@ -0,0 +1,13 @@ +package kr.cosine.randombox.scheduler + +import kr.cosine.randombox.config.RandomBoxConfig +import org.bukkit.scheduler.BukkitRunnable + +class RandomBoxSaveScheduler( + private val randomBoxConfig: RandomBoxConfig +) : BukkitRunnable() { + + override fun run() { + randomBoxConfig.save() + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/service/RandomBoxService.kt b/src/main/kotlin/kr/cosine/randombox/service/RandomBoxService.kt new file mode 100644 index 0000000..9e00431 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/service/RandomBoxService.kt @@ -0,0 +1,99 @@ +package kr.cosine.randombox.service + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kr.cosine.randombox.config.RandomBoxConfig +import kr.cosine.randombox.config.SettingConfig +import kr.cosine.randombox.data.RandomBox +import kr.cosine.randombox.registry.RandomBoxRegistry +import kr.cosine.randombox.view.RandomBoxChanceSettingView +import kr.cosine.randombox.view.RandomBoxItemSettingView +import kr.cosine.randombox.view.RandomBoxViewModel +import kr.hqservice.framework.global.core.component.Service +import kr.hqservice.framework.nms.extension.getDisplayName +import kr.hqservice.framework.nms.extension.getNmsItemStack +import kr.hqservice.framework.nms.extension.nms +import org.bukkit.Material +import org.bukkit.entity.Player +import org.bukkit.inventory.Inventory +import org.bukkit.inventory.ItemStack + +@Service +class RandomBoxService( + private val settingConfig: SettingConfig, + private val randomBoxConfig: RandomBoxConfig, + private val randomBoxRegistry: RandomBoxRegistry, + private val randomBoxViewModel: RandomBoxViewModel +) { + + private companion object { + const val RANDOM_BOX_KEY = "HQRandomBox" + } + + fun createRandomBox(key: String): Boolean { + if (randomBoxRegistry.isRandomBox(key)) return false + val randomBox = RandomBox(key) + randomBox.isChanged = true + randomBoxRegistry.setRandomBox(key, randomBox) + return true + } + + fun removeRandomBox(key: String) { + randomBoxRegistry.removeRandomBox(key) + } + + fun applyRandomBox(itemStack: ItemStack, key: String): Boolean { + if (itemStack.type.isAir) return false + itemStack.nms { + tag { + setString(RANDOM_BOX_KEY, key) + } + } + return true + } + + fun openRandomBoxItemSettingView(player: Player, randomBox: RandomBox) { + RandomBoxItemSettingView(randomBoxViewModel, randomBox).open(player) + } + + fun openRandomBoxChanceSettingView(player: Player, randomBox: RandomBox) { + RandomBoxChanceSettingView(randomBoxViewModel, randomBox).open(player) + } + + suspend fun save(finishScope: () -> Unit) { + withContext(Dispatchers.IO) { + randomBoxConfig.save() + finishScope() + } + } + + fun reload() { + settingConfig.reload() + } + + fun useRandomBox(player: Player, itemStack: ItemStack): Boolean { + val randomBoxKey = itemStack.getRandomBoxKey() ?: return false + val randomBox = randomBoxRegistry.findRandomBox(randomBoxKey) ?: return false + val playerInventory = player.inventory + if (!playerInventory.isAddable()) { + player.sendMessage(settingConfig.inventoryFullMessage) + return true + } + val chanceItem = randomBox.getRandomChanceItem() + val chanceItemStack = chanceItem.getOriginalItemStack() + itemStack.amount-- + playerInventory.addItem(chanceItemStack) + settingConfig.pickEffect.playEffect(player, chanceItemStack.getDisplayName()) + return true + } + + private fun ItemStack.getRandomBoxKey(): String? { + val nmsItemStack = this.getNmsItemStack() + if (!nmsItemStack.hasTag()) return null + return nmsItemStack.getTag().getStringOrNull(RANDOM_BOX_KEY) + } + + private fun Inventory.isAddable(): Boolean { + return storageContents.any { it == null || it.type == Material.AIR } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/view/RandomBoxChanceSettingView.kt b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxChanceSettingView.kt new file mode 100644 index 0000000..3101366 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxChanceSettingView.kt @@ -0,0 +1,51 @@ +package kr.cosine.randombox.view + +import kr.cosine.randombox.data.RandomBox +import kr.cosine.randombox.extension.format +import kr.hqservice.framework.inventory.button.HQButtonBuilder +import kr.hqservice.framework.inventory.container.HQContainer +import kr.hqservice.framework.nms.extension.getDisplayName +import org.bukkit.entity.Player +import org.bukkit.event.inventory.ClickType +import org.bukkit.inventory.Inventory + +class RandomBoxChanceSettingView( + private val randomBoxViewModel: RandomBoxViewModel, + private val randomBox: RandomBox +) : HQContainer(54, "${randomBox.key} 랜덤 박스 - 확률 설정", true) { + + private val chanceItems = randomBox.getChanceItems() + + override fun initialize(inventory: Inventory) { + chanceItems.forEachIndexed { index, chanceItem -> + val itemStack = chanceItem.getItemStack() + HQButtonBuilder(itemStack).apply { + setLore(getLore() + listOf( + "", + "§a§l| §f확률: ${chanceItem.getChance().format()}%", + "", + "§a[ 클릭 시 확률을 설정합니다. ]" + )) + setClickFunction { event -> + if (event.getClickType() != ClickType.LEFT) return@setClickFunction + val player = event.getWhoClicked() + player.closeInventory() + player.sendMessage("§a${itemStack.getDisplayName()}§a이(가) 뽑힐 확률을 입력해주세요.") + randomBoxViewModel.setChance(player, chanceItem) { isSuccessful -> + reopen(player) + if (isSuccessful) { + randomBox.isChanged = true + } + } + } + }.build().setSlot(this, index) + } + } + + private fun reopen(player: Player) { + randomBoxViewModel.runSync { + refresh() + open(player) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/view/RandomBoxItemSettingView.kt b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxItemSettingView.kt new file mode 100644 index 0000000..cef8089 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxItemSettingView.kt @@ -0,0 +1,27 @@ +package kr.cosine.randombox.view + +import kr.cosine.randombox.data.RandomBox +import kr.hqservice.framework.inventory.container.HQContainer +import org.bukkit.event.inventory.InventoryCloseEvent +import org.bukkit.inventory.Inventory + +class RandomBoxItemSettingView( + private val randomBoxViewModel: RandomBoxViewModel, + private val randomBox: RandomBox +) : HQContainer(54, "${randomBox.key} 랜덤 박스 - 아이템 설정", false) { + + private val originalChanceItems = randomBox.getChanceItems() + + override fun initialize(inventory: Inventory) { + originalChanceItems.forEachIndexed { index, chanceItem -> + inventory.setItem(index, chanceItem.getItemStack()) + } + } + + override fun onClose(event: InventoryCloseEvent) { + val player = event.player + val itemStacks = event.inventory.contents.filter { it != null && !it.type.isAir } + randomBoxViewModel.setChanceItems(randomBox, itemStacks) + player.sendMessage("§a아이템이 설정되었습니다.") + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/cosine/randombox/view/RandomBoxViewModel.kt b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxViewModel.kt new file mode 100644 index 0000000..c335384 --- /dev/null +++ b/src/main/kotlin/kr/cosine/randombox/view/RandomBoxViewModel.kt @@ -0,0 +1,67 @@ +package kr.cosine.randombox.view + +import kr.cosine.randombox.data.ChanceItem +import kr.cosine.randombox.data.RandomBox +import kr.cosine.randombox.extension.format +import kr.hqservice.framework.bukkit.core.extension.editMeta +import kr.hqservice.framework.global.core.component.Bean +import kr.hqservice.framework.nms.extension.getDisplayName +import kr.hqservice.framework.nms.extension.virtual +import net.md_5.bungee.api.chat.TextComponent +import org.bukkit.Material +import org.bukkit.Server +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import org.bukkit.plugin.Plugin + +@Bean +class RandomBoxViewModel( + private val plugin: Plugin, + private val server: Server +) { + + private val anvilTitle = TextComponent("확률 설정") + private val baseItemStack = ItemStack(Material.PAPER).editMeta { setDisplayName("§r") } + private val resultItemStack = ItemStack(Material.PAPER).editMeta { setDisplayName("§f설정하기") } + + fun runSync(actionScope: () -> Unit) { + server.scheduler.runTask(plugin, actionScope) + } + + fun setChanceItems(randomBox: RandomBox, itemStacks: List) { + val chanceItems = itemStacks.map { + ChanceItem(it).apply { + if (!hasChance()) { + setChance() + } + } + } + randomBox.setChanceItems(chanceItems) + } + + fun setChance(player: Player, chanceItem: ChanceItem, openScope: (Boolean) -> Unit) { + player.virtual { + anvil(anvilTitle) { + setBaseItem(baseItemStack) + setResultItem(resultItemStack) + setConfirmHandler { input -> + if (input.isEmpty()) { + player.sendMessage("§c확률을 입력해주세요.") + return@setConfirmHandler false + } + val chance = input.toDoubleOrNull() ?: run { + player.sendMessage("§c숫자만 입력할 수 있습니다.") + return@setConfirmHandler false + } + chanceItem.setChance(chance) + player.sendMessage("§a${chanceItem.getItemStack().getDisplayName()}§a이(가) 뽑힐 확률을 ${chance.format()}%로 설정하였습니다.") + openScope(true) + return@setConfirmHandler true + } + setCloseHandler { + openScope(false) + } + } + } + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..5760523 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,14 @@ +# 자동 저장 주기 (틱 단위) +auto-save-period: 6000 + +message: + inventory-full: "&c인벤토리에 공간이 부족합니다." + +# 뽑았을 때 +pick-effect: + # %item% = 아이템 이름 + message: "&6%item% 획득!" + sound: + name: "minecraft:ui.toast.challenge_complete" + volume: 1 + pitch: 1 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..c17a518 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,7 @@ +name: HQRandomBox +main: kr.cosine.randombox.HQRandomBox +version: 1.0.0 +api-version: 1.17 +author: Cosine_A +depend: + - HQFramework \ No newline at end of file diff --git a/src/test/kotlin/kr/cosine/randombox/Test.kt b/src/test/kotlin/kr/cosine/randombox/Test.kt new file mode 100644 index 0000000..84f2456 --- /dev/null +++ b/src/test/kotlin/kr/cosine/randombox/Test.kt @@ -0,0 +1,18 @@ +package kr.cosine.randombox + +import org.junit.jupiter.api.Test + +class Test { + + class ItemStack( + val display: String, + val lore: List + ) {} + + @Test + fun instance_test() { + val list1 = listOf(ItemStack("", listOf("1"))) + val list2 = listOf(ItemStack("", listOf("1"))) + println("같은지: ${list1 == list2}") + } +} \ No newline at end of file