commit f23a010a02f678a350bcd9a7d12b557f2aa1033b Author: thehrz Date: Fri Feb 7 20:42:50 2025 +0800 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f91f646 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,12 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux start script should use lf +/gradlew text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf + +# Binary files should be left untouched +*.jar binary + diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..e5078d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin/ +.gradle +build +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100755 index 0000000..bcb37bc --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# InfernalMobs + +This is the Infernal Mobs source code. + +Released under the License: (CC BY-NC-SA 2.5 CA) +https://creativecommons.org/licenses/by-nc-sa/2.5/ca/ + +You can use it for private purposes or for non-commercial purposes if you give the author credit. +You MUST also link your changed source. + +Thank you. \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..d5d3069 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,91 @@ +import java.util.* + +plugins { + kotlin("jvm") version "2.0.10" + kotlin("kapt") version "1.7.0" + id("com.github.johnrengelman.shadow") version "7.1.2" + id ("com.google.devtools.ksp") version "2.0.20-1.0.25" +} + +group = "io.github.thehrz.infernalmobs" +version = "1.0.0" + + +repositories { + mavenCentral() + maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") + maven("https://papermc.io/repo/repository/maven-public/") + maven("https://oss.sonatype.org/content/groups/public/") + maven("https://repo.codemc.org/repository/maven-public/") + maven("https://plugins.gradle.org/m2/") + maven("https://maven.enginehub.org/repo/") + maven("https://repo.triumphteam.dev/snapshots/") + maven("https://repo.dustplanet.de/artifactory/libs-release-local") + maven("https://jitpack.io") + maven("https://repo.dmulloy2.net/repository/public/") + +} + +dependencies { + implementation(kotlin("stdlib-jdk8")) +// implementation(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.2")) +// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core") + implementation("com.github.cryptomorin:XSeries:8.8.0") + implementation("me.mattstudios:triumph-msg-adventure:2.2.4-SNAPSHOT") + implementation("de.exlll:configlib-paper:4.5.0") + +// implementation("io.insert-koin:koin-core:4.0.2") + implementation("io.insert-koin:koin-core-jvm:4.0.2") + implementation("io.insert-koin:koin-annotations:1.4.0") + ksp("io.insert-koin:koin-ksp-compiler:1.3.1") + + compileOnly("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT") + compileOnly("org.apache.logging.log4j:log4j-core:2.17.2") + compileOnly("com.comphenix.protocol:ProtocolLib:5.3.0") + compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.5") + compileOnly("com.github.TownyAdvanced:Towny:0.97.1.0") + compileOnly("de.dustplanet:silkspawners:7.1.0") { + exclude(group = "*") + } +} + +tasks.build { + dependsOn("shadowJar") +} + +tasks.shadowJar { + val dependencyPackage = "${rootProject.group}.dependencies.${rootProject.name.lowercase(Locale.getDefault())}" + relocate("com.cryptomorin.xseries", "${dependencyPackage}.xseries") + relocate("javax.inject", "${dependencyPackage}.javax.inject") + + relocate("me.mattstudios.msg", "${dependencyPackage}.mfmsg") + relocate("org.jetbrains", "${dependencyPackage}.jetbrains") + relocate("org.intellij", "${dependencyPackage}.jetbrains.intellij") + relocate("toothpick", "${dependencyPackage}.toothpick") + exclude("ScopeJVMKt.class") + exclude("DebugProbesKt.bin") + exclude("META-INF/**") +} + +tasks.withType { + filesMatching("plugin.yml") { + expand("version" to version) + } +} + +val targetJavaVersion = 17 +kotlin { + jvmToolchain(targetJavaVersion) +} + +//tasks.withType { +// sourceCompatibility = javaVersion +// targetCompatibility = javaVersion +// options.encoding = "UTF-8" +//} + +//tasks.processResources { +// outputs.upToDateWhen { false } +// val main_class = "${project.group}.${project.name.toLowerCase()}.${project.name}" +// expand("name" to project.name, "version" to project.version, "mainClass" to main_class) +//} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..114680d --- /dev/null +++ b/gradle.properties @@ -0,0 +1,3 @@ +# This file was generated by the Gradle 'init' task. +# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..aae5ec4 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,10 @@ +# This file was generated by the Gradle 'init' task. +# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format + +[versions] +guava = "33.3.1-jre" +junit-jupiter = "5.11.1" + +[libraries] +guava = { module = "com.google.guava:guava", version.ref = "guava" } +junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..a4b76b9 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..cea7a79 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..f3b75f3 --- /dev/null +++ b/gradlew @@ -0,0 +1,251 @@ +#!/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. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# 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/HEAD/platforms/jvm/plugins-application/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 + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# 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 + if ! command -v java >/dev/null 2>&1 + then + 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 +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + 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 + + +# 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"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# 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..9d21a21 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@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 +@rem SPDX-License-Identifier: Apache-2.0 +@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=. +@rem This is normally unused +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% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +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% equ 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! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..d7a25ae --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,8 @@ +rootProject.name = "InfernalMobs" + +pluginManagement { + repositories { + gradlePluginPortal() + mavenCentral() + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/thehrz/infernalmobs/InfernalMobs.kt b/src/main/kotlin/io/github/thehrz/infernalmobs/InfernalMobs.kt new file mode 100755 index 0000000..4022cdf --- /dev/null +++ b/src/main/kotlin/io/github/thehrz/infernalmobs/InfernalMobs.kt @@ -0,0 +1,142 @@ +package io.github.thehrz.infernalmobs + +import io.github.thehrz.infernalmobs.handler.EffectHandle +import io.github.thehrz.infernalmobs.koin.pluginModule +import io.github.thehrz.infernalmobs.services.ConfigService +import io.github.thehrz.infernalmobs.services.InfernalMobService +import io.github.thehrz.infernalmobs.utils.ItemUtils +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.NamespacedKey +import org.bukkit.enchantments.Enchantment +import org.bukkit.event.Listener +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.ShapedRecipe +import org.bukkit.plugin.java.JavaPlugin +import org.koin.core.KoinApplication +import org.koin.dsl.koinApplication +import org.koin.ksp.generated.defaultModule +import java.io.File +import java.util.logging.Level + +class InfernalMobs : JavaPlugin() { + var serverTime: Long = 0L + + lateinit var koinApp: KoinApplication + + companion object { + lateinit var instance: InfernalMobs + } + + override fun onEnable() { + koinApp = koinApplication { + modules( + pluginModule(this@InfernalMobs), + defaultModule + ) + } + + // Registering event + logger.info("Registering event listeners...") + val listeners = koinApp.koin.getAll() + logger.info("Total possible listeners: ${listeners.size}") + listeners.forEach { + logger.info("Registering event listener: ${it::class.simpleName}") + Bukkit.getPluginManager().registerEvents(it, this) + } + + logger.info("Registered Events.") + + + //Folder + val dir = File(dataFolder.parentFile.path, this.name) + if (!dir.exists()) dir.mkdir() + + + //Old config check +// if (File(dataFolder, "config.yml").exists()) { +// if (config.getString("configVersion") == null) { +// logger.log(Level.INFO, "No config version found!") +// config["configVersion"] = Bukkit.getVersion() +// .split(":".toRegex()) +// .dropLastWhile { it.isEmpty() } +// .toTypedArray()[1] +// .replace(")", "") +// .trim { it <= ' ' } +// saveConfig() +// } +// if (Bukkit.getVersion() != config.getString("configVersion")) { +// println(Bukkit.getVersion() + " contains " + config.getString("configVersion")) +// logger.log(Level.INFO, "Old config found, deleting!") +// File(dataFolder.toString() + File.separator + "config.yml").delete() +// } +// } + + //Register Config + +// if (!File(dataFolder, "config.yml").exists()) { +// logger.log(Level.INFO, "No config.yml found, generating...") +// saveConfig() +// reloadConfig() +// } + + +// //Register Loots +// if (!lootYML.exists()) { +// logger.log(Level.INFO, "No loot.yml found, generating...") +// reloadLoot() +// } +// +// //Register Save File +// if (!saveYML.exists()) { +// try { +// saveYML.createNewFile() +// } catch (e: IOException) { +// e.printStackTrace() +// } +// } + koinApp.koin.get().loadConfigs() + + koinApp.koin.get().applyEffect() + + koinApp.koin.get().reloadPowers() + + koinApp.koin.get().showEffect() + + addRecipes() + } + +// fun reloadLoot() { +// if (this.lootYML == null) { +// this.lootYML = File(dataFolder, "loot.yml") +// } +// this.configService.lootConfig = YamlConfiguration.loadConfiguration(this.lootYML) +// +// val defConfig = YamlConfiguration.loadConfiguration(lootYML) +// configService.lootConfig.setDefaults(defConfig) +// } + + val diviningStaff: ItemStack + get() { + val s = ItemUtils.getItem( + Material.BLAZE_ROD, + "§6§lDivining Rod", + 1, + mutableListOf("Click to find infernal mobs.") + ) + val m = s.itemMeta + m.addEnchant(Enchantment.CHANNELING, 1, true) + s.setItemMeta(m) + return s + } + + private fun addRecipes() { + val staff = diviningStaff + val key = NamespacedKey(this, "divining_staff") + val sr = ShapedRecipe(key, staff) + sr.shape("ANA", "ASA", "ASA") + sr.setIngredient('N', Material.NETHER_STAR) + sr.setIngredient('S', Material.BLAZE_ROD) + Bukkit.addRecipe(sr) + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/thehrz/infernalmobs/commands/Command.kt b/src/main/kotlin/io/github/thehrz/infernalmobs/commands/Command.kt new file mode 100644 index 0000000..95241df --- /dev/null +++ b/src/main/kotlin/io/github/thehrz/infernalmobs/commands/Command.kt @@ -0,0 +1,669 @@ +package io.github.thehrz.infernalmobs.commands + +import io.github.thehrz.infernalmobs.InfernalMobs +import io.github.thehrz.infernalmobs.enums.Abilitiy +import io.github.thehrz.infernalmobs.gui.GUI +import io.github.thehrz.infernalmobs.handler.InfernalMobHandler +import io.github.thehrz.infernalmobs.handler.LootHandler +import io.github.thehrz.infernalmobs.models.InfernalMob +import io.github.thehrz.infernalmobs.services.ConfigService +import io.github.thehrz.infernalmobs.services.InfernalMobService +import io.github.thehrz.infernalmobs.utils.* +import org.bukkit.Bukkit +import org.bukkit.Location +import org.bukkit.Material +import org.bukkit.World +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.command.TabCompleter +import org.bukkit.entity.* +import org.bukkit.inventory.ItemStack +import org.koin.core.annotation.Single +import java.util.* +import java.util.function.Consumer +import java.util.logging.Level +import java.util.stream.Collectors + +@Single +class Command( + val plugin: InfernalMobs, + val configService: ConfigService, + val infernalMobService: InfernalMobService, + val lootHandler: LootHandler, + val infernalMobHandler: InfernalMobHandler, + val gui: GUI +) : CommandExecutor, TabCompleter { + var errorList = mutableListOf() + private val loops = 0 + + init { + plugin.getCommand("im")?.let { cmd -> + cmd.setExecutor(this) + cmd.tabCompleter = this + } + } + + private val randomMob: String + get() { + val mobList = configService.mainConfig.enabledMobs + if (mobList.isEmpty()) { + return "Zombie" + } + val mob = mobList[MathUtils.rand(1, mobList.size) - 1] + return mob + } + + override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array?): Boolean { + if ((cmd.name.equals("infernalmobs", ignoreCase = true)) || (cmd.name.equals("im", ignoreCase = true))) { + try { + if (sender !is Player) { + if (!args.isNullOrEmpty() && (!args[0].equals( + "cspawn", + ignoreCase = true + )) && (!args[0].equals("pspawn", ignoreCase = true)) && (!args[0].equals( + "giveloot", + ignoreCase = true + )) && (!args[0].equals("reload", ignoreCase = true)) && (!args[0].equals( + "killall", + ignoreCase = true + )) + ) { + sender.sendMessage("This command can only be run by a player!") + return true + } + } + + val player = sender as Player + if (sender.hasPermission("infernal_mobs.commands")) { + if (args!!.isEmpty()) { + throwError(sender) + return true + } + if (args[0].equals("slotTest", ignoreCase = true)) { + for (i in configService.mainConfig.enabledCharmSlots) { + player.inventory.setItem( + i, ItemStack( + Material.RED_STAINED_GLASS_PANE + ) + ) + } + + } else if ((args.size == 1) && (args[0].equals("fixloot", ignoreCase = true))) { + //TODO: ? what is items +// val list = +// configService.mainConfig.items.getKeys(false) +// +// for (i in configService.lootConfig.getConfigurationSection("loot")!!.getKeys(false)) { +// val oid = configService.lootConfig.getInt("loot.$i.item").toString() + "" +// println("loot.$i.item") +// println(oid + ": " + list.contains(oid)) +// if (list.contains(oid)) { +// configService.lootConfig["loot.$i.item"] = config.getString("items.$oid") +// } else println("ERROR: $oid") +// } +// try { +// configService.lootConfig.save(this.lootYML) +// } catch (ignored: IOException) { +// } +// sender.sendMessage("§eLoot Fixed!") + } else if ((args.size == 1) && (args[0].equals("reload", ignoreCase = true))) { + configService.reloadLoot() + sender.sendMessage("§eConfig reloaded!") + } else if (args[0] == "mobList") { + sender.sendMessage("§6Mob List:") + for (et in EntityType.entries) if (et.getName() != null) sender.sendMessage("§e" + et.getName()) + return true + } else if ((args.size == 1) && (args[0].equals("error", ignoreCase = true))) { + errorList.add(player) + sender.sendMessage("§eClick on a mob to send an error report about it.") + } else if ((args.size == 1) && (args[0].equals("info", ignoreCase = true))) { + sender.sendMessage("§eMounts: " + infernalMobService.mountList.size) + sender.sendMessage("§eLoops: " + this.loops) + sender.sendMessage("§eInfernals: " + infernalMobService.infernalList.size) + } else if ((args.size == 1) && (args[0].equals("worldInfo", ignoreCase = true))) { + val enWorldList = configService.mainConfig.mobWorlds + val world = player.world + var enabled = "is not" + if (enWorldList.contains(world.name) || enWorldList.contains("")) { + enabled = "is" + } + sender.sendMessage("The world you are currently in, $world $enabled enabled.") + sender.sendMessage("All the worlds that are enabled are: $enWorldList") + } else if ((args.size == 1) && (args[0].equals("help", ignoreCase = true))) { + throwError(sender) + } else if ((args.size == 1) && (args[0].equals("getloot", ignoreCase = true))) { + val min = configService.mainConfig.minpowers + val max = configService.mainConfig.maxpowers + val powers = MathUtils.rand(min, max) + val gottenLoot = lootHandler.getRandomLoot(player!!, randomMob, powers) + if (gottenLoot != null) { + player.inventory.addItem(gottenLoot) + } + sender.sendMessage("§eGave you some random loot!") + } else if ((args.size == 2) && (args[0].equals("getloot", ignoreCase = true))) { + try { + val index = args[1].toInt() + val i = lootHandler.getLoot(player, index) + if (i != null) { + player.inventory.addItem(i) + sender.sendMessage("§eGave you the loot at index §9$index") + return true + } + } catch (ignored: Exception) { + } + sender.sendMessage("§cUnable to get that loot!") + } else if ((args.size == 3) && (args[0].equals("giveloot", ignoreCase = true))) { + try { + val p = plugin.server.getPlayer(args[1]) + if (p != null) { + val index = args[2].toInt() + val i = lootHandler.getLoot(p, index) + if (i != null) { + p.inventory.addItem(i) + sender.sendMessage("§eGave the player the loot at index §9$index") + return true + } + } else { + sender.sendMessage("§cPlayer not found!!") + return true + } + } catch (ignored: Exception) { + } + sender.sendMessage("§cUnable to get that loot!") + } else if (((args.size == 2) && (args[0].equals( + "spawn", + ignoreCase = true + ))) || ((args[0].equals("cspawn", ignoreCase = true)) && (args.size == 6)) + ) { + if ((EntityType.fromName(args[1]) != null)) { + val exmsg: Boolean + val world: World? + val ent: Entity + if ((args[0].equals( + "cspawn", + ignoreCase = true + )) && (args[2] != null) && (args[3] != null) && (args[4] != null) && (args[5] != null) + ) { + if (Bukkit.getServer().getWorld(args[2]) == null) { + sender.sendMessage(args[2] + " dose not exist!") + return true + } + world = Bukkit.getServer().getWorld(args[2]) + val spoint = Location( + Bukkit.getServer().getWorld( + args[2] + ), + args[3].toInt().toDouble(), args[4].toInt().toDouble(), + args[5].toInt().toDouble() + ) + ent = world!!.spawnEntity( + spoint, + EntityType.fromName(args[1])!! + ) + exmsg = true + } else { + val farSpawnLoc = player.getTargetBlock(null, 200).location + farSpawnLoc.y += 1.0 + ent = player.world.spawnEntity( + farSpawnLoc, EntityType.fromName( + args[1] + )!! + ) + exmsg = false + } + val abList = infernalMobHandler.getAbilitiesAmount(ent) + val newMob: InfernalMob + val id = ent.uniqueId + newMob = if (abList.contains("oneup")) { + InfernalMob(ent, id, true, abList, 2, infernalMobService.effect) + } else { + InfernalMob(ent, id, true, abList, 1, infernalMobService.effect) + } + + if (abList.contains("flying")) { + EntityUtils.makeFly(ent) + } + infernalMobService.infernalList.add(newMob) + gui!!.setName(ent) + + infernalMobService.giveMobGear(ent, false) + infernalMobService.addHealth(ent, abList) + if (!exmsg) { + sender.sendMessage("Spawned a " + args[1]) + } else if (sender is Player) { + sender.sendMessage("Spawned a " + args[1] + " in " + args[2] + " at " + args[3] + ", " + args[4] + ", " + args[5]) + } + } else { + sender.sendMessage("Can't spawn a " + args[1] + "!") + return true + } + } else if (((args.size >= 3) && (args[0].equals( + "spawn", + ignoreCase = true + ))) || ((args[0].equals( + "cspawn", + ignoreCase = true + )) && (args.size >= 6)) || ((args[0].equals("pspawn", ignoreCase = true)) && (args.size >= 3)) + ) { + if (args[0].equals("spawn", ignoreCase = true)) { + if ((EntityType.fromName(args[1]) != null)) { + val farSpawnLoc = player!!.getTargetBlock(null, 200).location + farSpawnLoc.y += 1.0 + val ent = player.world.spawnEntity( + farSpawnLoc, EntityType.fromName( + args[1] + )!! + ) + val spesificAbList = mutableListOf() + for (i in 0..(args.size - 3)) { + if (Abilitiy.entries.map { it.value }.contains(args[(i + 2)])) { + spesificAbList.add(args[(i + 2)]) + } else { + sender.sendMessage(args[(i + 2)] + " is not a valid ability!") + return true + } + } + val newMob: InfernalMob + val id = ent.uniqueId + newMob = if (spesificAbList.contains("oneup")) { + InfernalMob(ent, id, true, spesificAbList, 2, infernalMobService.effect) + } else { + InfernalMob(ent, id, true, spesificAbList, 1, infernalMobService.effect) + } + if (spesificAbList.contains("flying")) { + EntityUtils.makeFly(ent) + } + infernalMobService.infernalList.add(newMob) + gui.setName(ent) + infernalMobService.giveMobGear(ent, false) + + infernalMobService.addHealth(ent, spesificAbList) + + sender.sendMessage("Spawned a " + args[1] + " with the abilities:") + sender.sendMessage(spesificAbList.toString()) + } else { + sender.sendMessage("Can't spawn a " + args[1] + "!") + } + } else if (args[0].equals("cspawn", ignoreCase = true)) { + //cspawn + if (Bukkit.getServer().getWorld(args[2]) == null) { + sender.sendMessage(args[2] + " dose not exist!") + return true + } + val world = Bukkit.getServer().getWorld(args[2]) + val spoint = Location( + world, + args[3].toInt().toDouble(), args[4].toInt().toDouble(), + args[5].toInt().toDouble() + ) + val abList = listOf(*args).subList(6, args.size) + if (cSpawn(sender, args[1], spoint, abList)) { + sender.sendMessage("Spawned a " + args[1] + " in " + args[2] + " at " + args[3] + ", " + args[4] + ", " + args[5] + " with the abilities:") + sender.sendMessage(abList.toString()) + } + } else { + //pspawn + val p = plugin.server.getPlayer(args[2]) + if (p == null) { + sender.sendMessage(args[2] + " is not online!") + return true + } + val abList = listOf(*args).subList(3, args.size) + if (cSpawn(sender, args[1], p.location, abList)) { + sender.sendMessage("Spawned a " + args[1] + " at " + p.name + " with the abilities:") + sender.sendMessage(abList.toString()) + } + } + } else if ((args.size == 1) && (args[0].equals("abilities", ignoreCase = true))) { + sender.sendMessage("--Infernal Mobs Abilities--") + sender.sendMessage("mama, molten, weakness, vengeance, webber, storm, sprint, lifesteal, ghastly, ender, cloaked, berserk, oneup, sapper, rust, bullwark, quicksand, thief, tosser, withering, blinding, armoured, poisonous, potions, explode, gravity, archer, necromancer, firework, flying, mounted, morph, ghost, confusing") + } else { + val oldMobAbilityList: List? + if ((args.size == 1) && (args[0].equals("showAbilities", ignoreCase = true))) { + if (PlayerUtils.getTarget(player) != null) { + val targeted = PlayerUtils.getTarget(player) + val mobId = targeted!!.uniqueId + if (infernalMobService.idSearch(mobId) != -1) { + oldMobAbilityList = infernalMobService.findMobAbilities(mobId) + if (!targeted.isDead) { + sender.sendMessage("--Targeted InfernalMob's Abilities--") + sender.sendMessage(oldMobAbilityList.toString()) + } + } else { + sender.sendMessage("§cThis " + targeted.type.getName() + " §cis not an infernal mob!") + } + } else { + sender.sendMessage("§cUnable to find mob!") + } + } else if ((args[0].equals("setInfernal", ignoreCase = true)) && (args.size == 2)) { + if (player!!.getTargetBlock(null, 25).type == Material.SPAWNER) { + val delay = args[1].toInt() + + val name = LocationUtils.getLocationName(player.getTargetBlock(null, 25).location) + + configService.mobSaveConfig["infernalSpanwers.$name"] = delay + configService.saveSave() + sender.sendMessage("§cSpawner set to infernal with a $delay second delay!") + } else { + sender.sendMessage("§cYou must be looking a spawner to make it infernal!") + } + } else if ((args[0].equals("kill", ignoreCase = true)) && (args.size == 2)) { + val size = args[1].toInt() + for (e in player.getNearbyEntities(size.toDouble(), size.toDouble(), size.toDouble())) { + val id = infernalMobService.idSearch(e.uniqueId) + if (id != -1) { + removeMob(id) + e.remove() + plugin.logger.log(Level.INFO, "Entity remove due to /kill") + } + } + sender.sendMessage("§eKilled all infernal mobs near you!") + } else if ((args[0].equals( + "killall", + ignoreCase = true + )) && (args.size == 1 || args.size == 2) + ) { + var w: World? = null + if (args.size == 1 && sender is Player) { + w = sender.world + } else if (args.size == 2) { + w = plugin.server.getWorld(args[1]) + } + + if (w != null) { + for (e in w.entities) { + val id = infernalMobService.idSearch(e.uniqueId) + if (id != -1) { + removeMob(id) + if (e is LivingEntity) { + e.customName = null + } + plugin.logger.log(Level.INFO, "Entity remove due to /killall") + e.remove() + } + } + sender.sendMessage("§eKilled all loaded infernal mobs in that world!") + } else { + sender.sendMessage("§cWorld not found!") + } + } else if (args[0].equals("mobs", ignoreCase = true)) { + sender.sendMessage("§6List of Mobs:") + for (e in EntityType.values()) if (e != null) sender.sendMessage(e.toString()) + } else if (args[0].equals("setloot", ignoreCase = true)) { + configService.setItem( + player.inventory.itemInMainHand, + "loot." + args[1], + configService.lootConfig + ) + sender.sendMessage("§eSet loot at index " + args[1] + " §eto item in hand.") + } else { + throwError(sender) + } + } + } else { + sender.sendMessage("§cYou don't have permission to use this command!") + } + } catch (x: Exception) { + throwError(sender) + x.printStackTrace() + } + } + return true + } + + private fun removeMob(id: Any) { + + } + + override fun onTabComplete(sender: CommandSender, cmd: Command, label: String, args: Array): List? { + val allAbilitiesList: List = ArrayList( + mutableListOf( + "confusing", + "ghost", + "morph", + "mounted", + "flying", + "gravity", + "firework", + "necromancer", + "archer", + "molten", + "mama", + "potions", + "explode", + "berserk", + "weakness", + "vengeance", + "webber", + "storm", + "sprint", + "lifesteal", + "ghastly", + "ender", + "cloaked", + "oneup", + "sapper", + "rust", + "bullwark", + "quicksand", + "thief", + "tosser", + "withering", + "blinding", + "armoured", + "poisonous" + ) + ) + val commands: Set = HashSet( + mutableListOf( + "reload", + "worldInfo", + "error", + "getloot", + "setloot", + "giveloot", + "abilities", + "showAbilities", + "setInfernal", + "spawn", + "cspawn", + "pspawn", + "kill", + "killall" + ) + ) + if (sender.hasPermission("infernal_mobs.commands")) { + val newTab: MutableList = ArrayList() + if (args.size == 1) { + if (args[0].isEmpty()) return ArrayList(commands) + commands.forEach(Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[0].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + if (args[0].equals("getloot", ignoreCase = true) || args[0].equals("setloot", ignoreCase = true)) { + if (args.size == 2) { + newTab.add("1") + } + } + if (args[0].equals("giveloot", ignoreCase = true)) { + if (args.size == 2) { + newTab.addAll( + Bukkit.getOnlinePlayers().stream().map { obj: HumanEntity -> obj.name }.collect( + Collectors.toList() + ) + ) + } + if (args.size == 3) { + newTab.add("1") + } + } + if (args[0].equals("setinfernal", ignoreCase = true)) { + if (args.size == 2) { + newTab.add("10") + } + } + if (args.size == 2) { + if (args[0].equals("spawn", ignoreCase = true) || args[0].equals( + "cspawn", + ignoreCase = true + ) || args[0].equals("pspawn", ignoreCase = true) + ) { + if (args[1].isEmpty()) newTab.addAll( + Arrays.stream(EntityType.values()).filter { m: EntityType -> m.isSpawnable && m.isAlive } + .map { obj: EntityType -> obj.name }.collect( + Collectors.toList() + ) + ) + else Arrays.stream(EntityType.values()).filter { m: EntityType -> m.isSpawnable && m.isAlive } + .map { obj: EntityType -> obj.name }.collect( + Collectors.toList() + ).forEach( + Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + if (args[0].equals("killall", ignoreCase = true)) { + if (args[args.size - 1].isEmpty()) newTab.addAll( + Bukkit.getWorlds().stream().map { obj: World -> obj.name }.collect( + Collectors.toList() + ) + ) + else Bukkit.getWorlds().stream().map { obj: World -> obj.name }.collect(Collectors.toList()) + .forEach( + Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + if (args[0].equals("kill", ignoreCase = true)) { + newTab.add("1") + } + } + if (args[0].equals("cspawn", ignoreCase = true)) { + if (args.size == 3) { + if (args[args.size - 1].isEmpty()) newTab.addAll( + Bukkit.getWorlds().stream().map { obj: World -> obj.name }.collect( + Collectors.toList() + ) + ) + else Bukkit.getWorlds().stream().map { obj: World -> obj.name }.collect(Collectors.toList()) + .forEach( + Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + if (args.size in 4..6) { + newTab.add("~") + } + if (args.size >= 7) { + if (args[args.size - 1].isEmpty()) newTab.addAll(allAbilitiesList) + else allAbilitiesList.forEach(Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + } + if (args[0].equals("pspawn", ignoreCase = true)) { + if (args.size == 3) { + if (args[args.size - 1].isEmpty()) newTab.addAll( + Bukkit.getOnlinePlayers().stream().map { obj: HumanEntity -> obj.name }.collect( + Collectors.toList() + ) + ) + else Bukkit.getOnlinePlayers().stream().map { obj: HumanEntity -> obj.name } + .collect(Collectors.toList()).forEach( + Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + if (args.size > 3) { + if (args[args.size - 1].isEmpty()) newTab.addAll(allAbilitiesList) + else allAbilitiesList.forEach(Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + } + if (args.size >= 3) { + if (args[0].equals("spawn", ignoreCase = true)) { + if (args[args.size - 1].isEmpty()) newTab.addAll(allAbilitiesList) + else allAbilitiesList.forEach(Consumer { tab: String -> + if (tab.lowercase(Locale.getDefault()).startsWith( + args[args.size - 1].lowercase(Locale.getDefault()) + ) + ) newTab.add(tab) + }) + } + } + return newTab + } + return null + } + + private fun throwError(sender: CommandSender) { +// sender.sendMessage("--Infernal Mobs v" + description.version + "--") + sender.sendMessage("Usage: /im reload") + sender.sendMessage("Usage: /im worldInfo") + sender.sendMessage("Usage: /im error") + sender.sendMessage("Usage: /im getloot ") + sender.sendMessage("Usage: /im setloot ") + sender.sendMessage("Usage: /im giveloot ") + sender.sendMessage("Usage: /im abilities") + sender.sendMessage("Usage: /im showAbilities") + sender.sendMessage("Usage: /im setInfernal