init
This commit is contained in:
commit
f8e79ea869
13 changed files with 1335 additions and 0 deletions
72
build.gradle.kts
Normal file
72
build.gradle.kts
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
plugins {
|
||||||
|
id("java")
|
||||||
|
id("java-library")
|
||||||
|
id("maven-publish")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "ru.d3st0ny"
|
||||||
|
version = "0.1"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
maven { url = uri("https://libraries.minecraft.net") }
|
||||||
|
maven { url = uri("https://papermc.io/repo/repository/maven-public/") }
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api("com.mojang:brigadier:1.0.18") {
|
||||||
|
exclude("com.google.guava", "guava")
|
||||||
|
}
|
||||||
|
compileOnly("io.papermc.paper:paper-api:1.19-R0.1-SNAPSHOT")
|
||||||
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
create<MavenPublication>("maven"){
|
||||||
|
artifactId = project.name.toLowerCase()
|
||||||
|
groupId = "${project.group}"
|
||||||
|
version = "${project.version}"
|
||||||
|
from(components["java"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
url = uri("https://gitea.destony.dev/api/packages/Polygon/maven")
|
||||||
|
name = "gitea"
|
||||||
|
credentials {
|
||||||
|
username = System.getenv("gitUsername")
|
||||||
|
password = System.getenv("gitPassword")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val javaVersion = 17
|
||||||
|
|
||||||
|
tasks {
|
||||||
|
|
||||||
|
// USE THIS TASK FOR BUILD & PUBLISH TO MAVEN REPO
|
||||||
|
publish {
|
||||||
|
dependsOn(assemble)
|
||||||
|
}
|
||||||
|
|
||||||
|
// USE THIS TASK FOR BUILD
|
||||||
|
assemble {}
|
||||||
|
|
||||||
|
compileJava {
|
||||||
|
options.encoding = Charsets.UTF_8.name()
|
||||||
|
options.release.set(javaVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
javadoc {
|
||||||
|
options.encoding = Charsets.UTF_8.name()
|
||||||
|
}
|
||||||
|
|
||||||
|
processResources {
|
||||||
|
filteringCharset = Charsets.UTF_8.name()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
toolchain.languageVersion.set(JavaLanguageVersion.of(javaVersion))
|
||||||
|
}
|
||||||
1
gradle.properties
Normal file
1
gradle.properties
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
kotlin.code.style=official
|
||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
234
gradlew
vendored
Executable file
234
gradlew
vendored
Executable file
|
|
@ -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" "$@"
|
||||||
89
gradlew.bat
vendored
Normal file
89
gradlew.bat
vendored
Normal file
|
|
@ -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
|
||||||
1
settings.gradle.kts
Normal file
1
settings.gradle.kts
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
rootProject.name = "adajency"
|
||||||
115
src/main/java/ru/d3st0ny/adajency/Adajency.java
Normal file
115
src/main/java/ru/d3st0ny/adajency/Adajency.java
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||||
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||||
|
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility for using Minecraft's 1.13 'brigadier' library in Bukkit plugins.
|
||||||
|
*/
|
||||||
|
public interface Adajency {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher, against all
|
||||||
|
* aliases defined for the {@code command}.
|
||||||
|
*
|
||||||
|
* <p>Additionally applies the CraftBukkit {@link SuggestionProvider}
|
||||||
|
* to all arguments within the node, so ASK_SERVER suggestions can continue
|
||||||
|
* to function for the command.</p>
|
||||||
|
*
|
||||||
|
* <p>Players will only be sent argument data if they pass the provided
|
||||||
|
* {@code permissionTest}.</p>
|
||||||
|
*
|
||||||
|
* @param command the command to read aliases from
|
||||||
|
* @param node the argument data
|
||||||
|
* @param permissionTest the predicate to check whether players should be sent argument data
|
||||||
|
*/
|
||||||
|
void register(Command command, LiteralCommandNode<?> node, Predicate<? super Player> permissionTest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher, against all
|
||||||
|
* aliases defined for the {@code command}.
|
||||||
|
*
|
||||||
|
* <p>Additionally applies the CraftBukkit {@link SuggestionProvider}
|
||||||
|
* to all arguments within the node, so ASK_SERVER suggestions can continue
|
||||||
|
* to function for the command.</p>
|
||||||
|
*
|
||||||
|
* <p>Players will only be sent argument data if they pass the provided
|
||||||
|
* {@code permissionTest}.</p>
|
||||||
|
*
|
||||||
|
* @param command the command to read aliases from
|
||||||
|
* @param argumentBuilder the argument data, in a builder form
|
||||||
|
* @param permissionTest the predicate to check whether players should be sent argument data
|
||||||
|
*/
|
||||||
|
default void register(Command command, LiteralArgumentBuilder<?> argumentBuilder, Predicate<? super Player> permissionTest) {
|
||||||
|
Objects.requireNonNull(command, "command");
|
||||||
|
Objects.requireNonNull(argumentBuilder, "argumentBuilder");
|
||||||
|
Objects.requireNonNull(permissionTest, "permissionTest");
|
||||||
|
register(command, argumentBuilder.build(), permissionTest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher, against all
|
||||||
|
* aliases defined for the {@code command}.
|
||||||
|
*
|
||||||
|
* <p>Additionally applies the CraftBukkit {@link SuggestionProvider}
|
||||||
|
* to all arguments within the node, so ASK_SERVER suggestions can continue
|
||||||
|
* to function for the command.</p>
|
||||||
|
*
|
||||||
|
* @param command the command to read aliases from
|
||||||
|
* @param node the argument data
|
||||||
|
*/
|
||||||
|
void register(Command command, LiteralCommandNode<?> node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher, against all
|
||||||
|
* aliases defined for the {@code command}.
|
||||||
|
*
|
||||||
|
* <p>Additionally applies the CraftBukkit {@link SuggestionProvider}
|
||||||
|
* to all arguments within the node, so ASK_SERVER suggestions can continue
|
||||||
|
* to function for the command.</p>
|
||||||
|
*
|
||||||
|
* @param command the command to read aliases from
|
||||||
|
* @param argumentBuilder the argument data, in a builder form
|
||||||
|
*/
|
||||||
|
default void register(Command command, LiteralArgumentBuilder<?> argumentBuilder) {
|
||||||
|
Objects.requireNonNull(command, "command");
|
||||||
|
Objects.requireNonNull(argumentBuilder, "argumentBuilder");
|
||||||
|
register(command, argumentBuilder.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher.
|
||||||
|
*
|
||||||
|
* <p>Equivalent to calling
|
||||||
|
* {@link CommandDispatcher#register(LiteralArgumentBuilder)}.</p>
|
||||||
|
*
|
||||||
|
* <p>Prefer using {@link #register(Command, LiteralCommandNode)}.</p>
|
||||||
|
*
|
||||||
|
* @param node the argument data
|
||||||
|
*/
|
||||||
|
void register(LiteralCommandNode<?> node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the provided argument data to the dispatcher.
|
||||||
|
*
|
||||||
|
* <p>Equivalent to calling
|
||||||
|
* {@link CommandDispatcher#register(LiteralArgumentBuilder)}.</p>
|
||||||
|
*
|
||||||
|
* <p>Prefer using {@link #register(Command, LiteralArgumentBuilder)}.</p>
|
||||||
|
*
|
||||||
|
* @param argumentBuilder the argument data
|
||||||
|
*/
|
||||||
|
default void register(LiteralArgumentBuilder<?> argumentBuilder) {
|
||||||
|
Objects.requireNonNull(argumentBuilder, "argumentBuilder");
|
||||||
|
register(argumentBuilder.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
311
src/main/java/ru/d3st0ny/adajency/AdajencyImpl.java
Normal file
311
src/main/java/ru/d3st0ny/adajency/AdajencyImpl.java
Normal file
|
|
@ -0,0 +1,311 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
|
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||||
|
import com.mojang.brigadier.tree.ArgumentCommandNode;
|
||||||
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
|
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||||
|
import com.mojang.brigadier.tree.RootCommandNode;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.player.PlayerCommandSendEvent;
|
||||||
|
import org.bukkit.event.server.ServerLoadEvent;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
final class AdajencyImpl implements Adajency {
|
||||||
|
|
||||||
|
// obc.CraftServer#console field
|
||||||
|
private static final Field CONSOLE_FIELD;
|
||||||
|
|
||||||
|
// nms.MinecraftServer#getCommandDispatcher method
|
||||||
|
private static final Method GET_COMMAND_DISPATCHER_METHOD;
|
||||||
|
|
||||||
|
// nms.CommandDispatcher#getDispatcher (obfuscated) method
|
||||||
|
private static final Method GET_BRIGADIER_DISPATCHER_METHOD;
|
||||||
|
|
||||||
|
// obc.command.BukkitCommandWrapper constructor
|
||||||
|
private static final Constructor<?> COMMAND_WRAPPER_CONSTRUCTOR;
|
||||||
|
|
||||||
|
// ArgumentCommandNode#customSuggestions field
|
||||||
|
private static final Field CUSTOM_SUGGESTIONS_FIELD;
|
||||||
|
|
||||||
|
// CommandNode#command
|
||||||
|
private static final Field COMMAND_EXECUTE_FUNCTION_FIELD;
|
||||||
|
|
||||||
|
// CommandNode#children, CommandNode#literals, CommandNode#arguments fields
|
||||||
|
private static final Field CHILDREN_FIELD;
|
||||||
|
private static final Field LITERALS_FIELD;
|
||||||
|
private static final Field ARGUMENTS_FIELD;
|
||||||
|
|
||||||
|
// An array of the CommandNode fields above: [#children, #literals, #arguments]
|
||||||
|
private static final Field[] CHILDREN_FIELDS;
|
||||||
|
|
||||||
|
// Dummy instance of Command used to ensure the executable bit gets set on
|
||||||
|
// mock commands when they're encoded into data sent to the client
|
||||||
|
private static final com.mojang.brigadier.Command<?> DUMMY_COMMAND;
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
final Class<?> minecraftServer;
|
||||||
|
final Class<?> commandDispatcher;
|
||||||
|
|
||||||
|
if (ReflectionUtil.minecraftVersion() > 16) {
|
||||||
|
minecraftServer = ReflectionUtil.mcClass("server.MinecraftServer");
|
||||||
|
commandDispatcher = ReflectionUtil.mcClass("commands.CommandDispatcher");
|
||||||
|
} else {
|
||||||
|
minecraftServer = ReflectionUtil.nmsClass("MinecraftServer");
|
||||||
|
commandDispatcher = ReflectionUtil.nmsClass("CommandDispatcher");
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> craftServer = ReflectionUtil.obcClass("CraftServer");
|
||||||
|
CONSOLE_FIELD = craftServer.getDeclaredField("console");
|
||||||
|
CONSOLE_FIELD.setAccessible(true);
|
||||||
|
|
||||||
|
GET_COMMAND_DISPATCHER_METHOD = Arrays.stream(minecraftServer.getDeclaredMethods())
|
||||||
|
.filter(method -> method.getParameterCount() == 0)
|
||||||
|
.filter(method -> commandDispatcher.isAssignableFrom(method.getReturnType()))
|
||||||
|
.findFirst().orElseThrow(NoSuchMethodException::new);
|
||||||
|
GET_COMMAND_DISPATCHER_METHOD.setAccessible(true);
|
||||||
|
|
||||||
|
GET_BRIGADIER_DISPATCHER_METHOD = Arrays.stream(commandDispatcher.getDeclaredMethods())
|
||||||
|
.filter(method -> method.getParameterCount() == 0)
|
||||||
|
.filter(method -> CommandDispatcher.class.isAssignableFrom(method.getReturnType()))
|
||||||
|
.findFirst().orElseThrow(NoSuchMethodException::new);
|
||||||
|
GET_BRIGADIER_DISPATCHER_METHOD.setAccessible(true);
|
||||||
|
|
||||||
|
Class<?> commandWrapperClass = ReflectionUtil.obcClass("command.BukkitCommandWrapper");
|
||||||
|
COMMAND_WRAPPER_CONSTRUCTOR = commandWrapperClass.getConstructor(craftServer, Command.class);
|
||||||
|
|
||||||
|
CUSTOM_SUGGESTIONS_FIELD = ArgumentCommandNode.class.getDeclaredField("customSuggestions");
|
||||||
|
CUSTOM_SUGGESTIONS_FIELD.setAccessible(true);
|
||||||
|
|
||||||
|
COMMAND_EXECUTE_FUNCTION_FIELD = CommandNode.class.getDeclaredField("command");
|
||||||
|
COMMAND_EXECUTE_FUNCTION_FIELD.setAccessible(true);
|
||||||
|
|
||||||
|
CHILDREN_FIELD = CommandNode.class.getDeclaredField("children");
|
||||||
|
LITERALS_FIELD = CommandNode.class.getDeclaredField("literals");
|
||||||
|
ARGUMENTS_FIELD = CommandNode.class.getDeclaredField("arguments");
|
||||||
|
CHILDREN_FIELDS = new Field[]{CHILDREN_FIELD, LITERALS_FIELD, ARGUMENTS_FIELD};
|
||||||
|
for (Field field : CHILDREN_FIELDS) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
DUMMY_COMMAND = (ctx) -> { throw new UnsupportedOperationException(); };
|
||||||
|
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new ExceptionInInitializerError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Plugin plugin;
|
||||||
|
private final List<LiteralCommandNode<?>> registeredNodes = new ArrayList<>();
|
||||||
|
|
||||||
|
AdajencyImpl(Plugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
this.plugin.getServer().getPluginManager().registerEvents(new ServerReloadListener(this), this.plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommandDispatcher<?> getDispatcher() {
|
||||||
|
try {
|
||||||
|
Object mcServerObject = CONSOLE_FIELD.get(Bukkit.getServer());
|
||||||
|
Object commandDispatcherObject = GET_COMMAND_DISPATCHER_METHOD.invoke(mcServerObject);
|
||||||
|
return (CommandDispatcher<?>) GET_BRIGADIER_DISPATCHER_METHOD.invoke(commandDispatcherObject);
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
|
@Override
|
||||||
|
public void register(LiteralCommandNode<?> node) {
|
||||||
|
Objects.requireNonNull(node, "node");
|
||||||
|
|
||||||
|
CommandDispatcher dispatcher = getDispatcher();
|
||||||
|
RootCommandNode root = dispatcher.getRoot();
|
||||||
|
|
||||||
|
removeChild(root, node.getName());
|
||||||
|
root.addChild(node);
|
||||||
|
this.registeredNodes.add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void register(Command command, LiteralCommandNode<?> node, Predicate<? super Player> permissionTest) {
|
||||||
|
Objects.requireNonNull(command, "command");
|
||||||
|
Objects.requireNonNull(node, "node");
|
||||||
|
Objects.requireNonNull(permissionTest, "permissionTest");
|
||||||
|
|
||||||
|
try {
|
||||||
|
SuggestionProvider<?> wrapper = (SuggestionProvider<?>) COMMAND_WRAPPER_CONSTRUCTOR.newInstance(this.plugin.getServer(), command);
|
||||||
|
setRequiredHackyFieldsRecursively(node, wrapper);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<String> aliases = getAliases(command);
|
||||||
|
if (!aliases.contains(node.getLiteral())) {
|
||||||
|
node = renameLiteralNode(node, command.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String alias : aliases) {
|
||||||
|
if (node.getLiteral().equals(alias)) {
|
||||||
|
register(node);
|
||||||
|
} else {
|
||||||
|
register(LiteralArgumentBuilder.literal(alias).redirect((LiteralCommandNode<Object>)node).build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.plugin.getServer().getPluginManager().registerEvents(new CommandDataSendListener(command, permissionTest), this.plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void register(Command command, LiteralCommandNode<?> node) {
|
||||||
|
Objects.requireNonNull(command, "command");
|
||||||
|
Objects.requireNonNull(node, "node");
|
||||||
|
|
||||||
|
register(command, node, command::testPermissionSilent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
private static void removeChild(RootCommandNode root, String name) {
|
||||||
|
try {
|
||||||
|
for (Field field : CHILDREN_FIELDS) {
|
||||||
|
Map<String, ?> children = (Map<String, ?>) field.get(root);
|
||||||
|
children.remove(name);
|
||||||
|
}
|
||||||
|
} catch (ReflectiveOperationException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setRequiredHackyFieldsRecursively(CommandNode<?> node, SuggestionProvider<?> suggestionProvider) {
|
||||||
|
// set command execution function so the server sets the executable flag on the command
|
||||||
|
try {
|
||||||
|
COMMAND_EXECUTE_FUNCTION_FIELD.set(node, DUMMY_COMMAND);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node instanceof ArgumentCommandNode<?, ?> argumentNode) {
|
||||||
|
|
||||||
|
// set the custom suggestion provider field so tab completions work
|
||||||
|
try {
|
||||||
|
CUSTOM_SUGGESTIONS_FIELD.set(argumentNode, suggestionProvider);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (CommandNode<?> child : node.getChildren()) {
|
||||||
|
setRequiredHackyFieldsRecursively(child, suggestionProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <S> LiteralCommandNode<S> renameLiteralNode(LiteralCommandNode<S> node, String newLiteral) {
|
||||||
|
LiteralCommandNode<S> clone = new LiteralCommandNode<>(newLiteral, node.getCommand(), node.getRequirement(), node.getRedirect(), node.getRedirectModifier(), node.isFork());
|
||||||
|
for (CommandNode<S> child : node.getChildren()) {
|
||||||
|
clone.addChild(child);
|
||||||
|
}
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listens for server (re)loads, and re-adds all registered nodes to the dispatcher.
|
||||||
|
*/
|
||||||
|
private record ServerReloadListener(AdajencyImpl adajency) implements Listener {
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
|
@EventHandler
|
||||||
|
public void onLoad(ServerLoadEvent e) {
|
||||||
|
CommandDispatcher dispatcher = this.adajency.getDispatcher();
|
||||||
|
RootCommandNode root = dispatcher.getRoot();
|
||||||
|
|
||||||
|
for (LiteralCommandNode<?> node : this.adajency.registeredNodes) {
|
||||||
|
removeChild(root, node.getName());
|
||||||
|
root.addChild(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes minecraft namespaced argument data, & data for players without permission to view the
|
||||||
|
* corresponding commands.
|
||||||
|
*/
|
||||||
|
private static final class CommandDataSendListener implements Listener {
|
||||||
|
private final Set<String> aliases;
|
||||||
|
private final Set<String> minecraftPrefixedAliases;
|
||||||
|
private final Predicate<? super Player> permissionTest;
|
||||||
|
|
||||||
|
CommandDataSendListener(Command pluginCommand, Predicate<? super Player> permissionTest) {
|
||||||
|
this.aliases = new HashSet<>(getAliases(pluginCommand));
|
||||||
|
this.minecraftPrefixedAliases = this.aliases.stream().map(alias -> "minecraft:" + alias).collect(Collectors.toSet());
|
||||||
|
this.permissionTest = permissionTest;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onCommandSend(PlayerCommandSendEvent e) {
|
||||||
|
// always remove 'minecraft:' prefixed aliases added by craftbukkit.
|
||||||
|
// this happens because bukkit thinks our injected commands are vanilla commands.
|
||||||
|
e.getCommands().removeAll(this.minecraftPrefixedAliases);
|
||||||
|
|
||||||
|
// remove the actual aliases if the player doesn't pass the permission test
|
||||||
|
if (!this.permissionTest.test(e.getPlayer())) {
|
||||||
|
e.getCommands().removeAll(this.aliases);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the aliases known for the given command.
|
||||||
|
*
|
||||||
|
* <p>This will include the main label, as well as defined aliases, and
|
||||||
|
* aliases including the fallback prefix added by Bukkit.</p>
|
||||||
|
*
|
||||||
|
* @param command the command
|
||||||
|
* @return the aliases
|
||||||
|
*/
|
||||||
|
private static Collection<String> getAliases(Command command) {
|
||||||
|
Objects.requireNonNull(command, "command");
|
||||||
|
|
||||||
|
Stream<String> aliasesStream = Stream.concat(
|
||||||
|
Stream.of(command.getLabel()),
|
||||||
|
command.getAliases().stream()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (command instanceof PluginCommand) {
|
||||||
|
String fallbackPrefix = ((PluginCommand) command).getPlugin().getName().toLowerCase().trim();
|
||||||
|
aliasesStream = aliasesStream.flatMap(alias -> Stream.of(
|
||||||
|
alias,
|
||||||
|
fallbackPrefix + ":" + alias
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return aliasesStream.distinct().collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ensureSetup() {
|
||||||
|
// do nothing - this is only called to trigger the static initializer
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
56
src/main/java/ru/d3st0ny/adajency/AdajencyProvider.java
Normal file
56
src/main/java/ru/d3st0ny/adajency/AdajencyProvider.java
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for obtaining instances of {@link Adajency}.
|
||||||
|
*/
|
||||||
|
public final class AdajencyProvider {
|
||||||
|
private AdajencyProvider() {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Throwable SETUP_EXCEPTION = checkSupported();
|
||||||
|
|
||||||
|
private static Throwable checkSupported() {
|
||||||
|
try {
|
||||||
|
Class.forName("com.mojang.brigadier.CommandDispatcher");
|
||||||
|
AdajencyImpl.ensureSetup();
|
||||||
|
MinecraftArgumentType.ensureSetup();
|
||||||
|
return null;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (System.getProperty("adajency.debug") != null) {
|
||||||
|
System.err.println("Exception while initialising adajency:");
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks to see if the Brigadier command system is supported by the server.
|
||||||
|
*
|
||||||
|
* @return true if adajency is supported.
|
||||||
|
*/
|
||||||
|
public static boolean isSupported() {
|
||||||
|
return SETUP_EXCEPTION == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains a {@link Adajency} instance for the given plugin.
|
||||||
|
*
|
||||||
|
* @param plugin the plugin
|
||||||
|
* @return the adajency instance
|
||||||
|
* @throws BrigadierUnsupportedException if brigadier is not {@link #isSupported() supported}
|
||||||
|
* by the server.
|
||||||
|
*/
|
||||||
|
public static Adajency getAdajency(Plugin plugin) throws BrigadierUnsupportedException {
|
||||||
|
Objects.requireNonNull(plugin, "plugin");
|
||||||
|
if (SETUP_EXCEPTION != null) {
|
||||||
|
throw new BrigadierUnsupportedException("Brigadier is not supported by the server.", SETUP_EXCEPTION);
|
||||||
|
}
|
||||||
|
return new AdajencyImpl(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception thrown when Brigadier is not supported by the server.
|
||||||
|
*/
|
||||||
|
public final class BrigadierUnsupportedException extends UnsupportedOperationException {
|
||||||
|
|
||||||
|
public BrigadierUnsupportedException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
377
src/main/java/ru/d3st0ny/adajency/MinecraftArgumentType.java
Normal file
377
src/main/java/ru/d3st0ny/adajency/MinecraftArgumentType.java
Normal file
|
|
@ -0,0 +1,377 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.arguments.ArgumentType;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An enumeration for containing Minecraft's built-in {@link ArgumentType}s
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public enum MinecraftArgumentType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A selector, player name, or UUID.
|
||||||
|
* <p>
|
||||||
|
* Parameters:
|
||||||
|
* - boolean single
|
||||||
|
* - boolean playerOnly
|
||||||
|
*/
|
||||||
|
Entity("ArgumentEntity", boolean.class, boolean.class),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A player, online or not. Can also use a selector, which may match one or more
|
||||||
|
* players (but not entities).
|
||||||
|
*/
|
||||||
|
GameProfile("ArgumentProfile"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A chat color. One of the names from <a href="https://wiki.vg/Chat#Colors">colors</a>, or {@code reset}.
|
||||||
|
* Case-insensitive.
|
||||||
|
*/
|
||||||
|
Color("ArgumentChatFormat"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JSON Chat component.
|
||||||
|
*/
|
||||||
|
Component("ArgumentChatComponent"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A regular message, potentially including selectors.
|
||||||
|
*/
|
||||||
|
Message("ArgumentChat"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An NBT value, parsed using JSON-NBT rules. This represents a full NBT tag.
|
||||||
|
*/
|
||||||
|
NBT("ArgumentNBTTag"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a partial NBT tag, usable in data modify command.
|
||||||
|
*/
|
||||||
|
NBT_TAG("ArgumentNBTBase"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A path within an NBT value, allowing for array and member accesses.
|
||||||
|
*/
|
||||||
|
NBT_PATH("ArgumentNBTKey"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A scoreboard objective.
|
||||||
|
*/
|
||||||
|
ScoreboardObjective("ArgumentScoreboardObjective"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A single score criterion.
|
||||||
|
*/
|
||||||
|
ObjectiveCriteria("ArgumentScoreboardCriteria"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A scoreboard operator.
|
||||||
|
*/
|
||||||
|
ScoreboardSlot("ArgumentScoreboardSlot"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Something that can join a team. Allows selectors and *.
|
||||||
|
*/
|
||||||
|
ScoreHolder("ArgumentScoreholder"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of a team. Parsed as an unquoted string.
|
||||||
|
*/
|
||||||
|
Team("ArgumentScoreboardTeam"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A scoreboard operator.
|
||||||
|
*/
|
||||||
|
Operation("ArgumentMathOperation"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A particle effect (an identifier with extra information following it for
|
||||||
|
* specific particles, mirroring the Particle packet)
|
||||||
|
*/
|
||||||
|
Particle("ArgumentParticle"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an angle.
|
||||||
|
*/
|
||||||
|
Angle("ArgumentAngle"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A name for an inventory slot.
|
||||||
|
*/
|
||||||
|
ItemSlot("ArgumentInventorySlot"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An Identifier.
|
||||||
|
*/
|
||||||
|
ResourceLocation("ArgumentMinecraftKeyRegistered"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A potion effect.
|
||||||
|
*/
|
||||||
|
PotionEffect("ArgumentMobEffect"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a item enchantment.
|
||||||
|
*/
|
||||||
|
Enchantment("ArgumentEnchantment"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an entity summon.
|
||||||
|
*/
|
||||||
|
EntitySummon("ArgumentEntitySummon"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a dimension.
|
||||||
|
*/
|
||||||
|
Dimension("ArgumentDimension"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a time duration.
|
||||||
|
*/
|
||||||
|
Time("ArgumentTime"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a UUID value.
|
||||||
|
*
|
||||||
|
* @since Minecraft 1.16
|
||||||
|
*/
|
||||||
|
UUID("ArgumentUUID"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A location, represented as 3 numbers (which must be integers). May use relative locations
|
||||||
|
* with ~
|
||||||
|
*/
|
||||||
|
BlockPosition("coordinates.ArgumentPosition"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A column location, represented as 3 numbers (which must be integers). May use relative locations
|
||||||
|
* with ~.
|
||||||
|
*/
|
||||||
|
ColumnPosition("coordinates.ArgumentVec2I"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A location, represented as 3 numbers (which may have a decimal point, but will be moved to the
|
||||||
|
* center of a block if none is specified). May use relative locations with ~.
|
||||||
|
*/
|
||||||
|
Vector3D("coordinates.ArgumentVec3"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A location, represented as 2 numbers (which may have a decimal point, but will be moved to the center
|
||||||
|
* of a block if none is specified). May use relative locations with ~.
|
||||||
|
*/
|
||||||
|
Vector2D("coordinates.ArgumentVec2"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An angle, represented as 2 numbers (which may have a decimal point, but will be moved to the
|
||||||
|
* center of a block if none is specified). May use relative locations with ~.
|
||||||
|
*/
|
||||||
|
Rotation("coordinates.ArgumentRotation"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection of up to 3 axes.
|
||||||
|
*/
|
||||||
|
Swizzle("coordinates.ArgumentRotationAxis"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A block state, optionally including NBT and state information.
|
||||||
|
*/
|
||||||
|
BlockState("blocks.ArgumentTile"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A block, or a block tag.
|
||||||
|
*/
|
||||||
|
BlockPredicate("blocks.ArgumentBlockPredicate"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An item, optionally including NBT.
|
||||||
|
*/
|
||||||
|
ItemStack("item.ArgumentItemStack"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An item, or an item tag.
|
||||||
|
*/
|
||||||
|
ItemPredicate("item.ArgumentItemPredicate"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function.
|
||||||
|
*/
|
||||||
|
Function("item.ArgumentTag"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity anchor related to the facing argument in the teleport command,
|
||||||
|
* is feet or eyes.
|
||||||
|
*/
|
||||||
|
EntityAnchor("ArgumentAnchor"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An integer range of values with a min and a max.
|
||||||
|
*/
|
||||||
|
IntRange("ArgumentCriterionValue$b"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A floating-point range of values with a min and a max.
|
||||||
|
*/
|
||||||
|
FloatRange("ArgumentCriterionValue$a"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template mirror
|
||||||
|
*
|
||||||
|
* @since Minecraft 1.19
|
||||||
|
*/
|
||||||
|
TemplateMirror("TemplateMirrorArgument"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template rotation
|
||||||
|
*
|
||||||
|
* @since Minecraft 1.19
|
||||||
|
*/
|
||||||
|
TemplateRotation("TemplateRotationArgument");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A ready-to-use instance used for singleton argument types
|
||||||
|
*/
|
||||||
|
private ArgumentType<?> argumentType;
|
||||||
|
private Constructor<? extends ArgumentType> argumentConstructor;
|
||||||
|
private final Class<?>[] parameters;
|
||||||
|
|
||||||
|
MinecraftArgumentType(String name, Class<?>... parameters) {
|
||||||
|
Class<?> argumentClass = resolveArgumentClass(name);
|
||||||
|
this.parameters = parameters;
|
||||||
|
if (argumentClass == null) {
|
||||||
|
argumentType = null;
|
||||||
|
argumentConstructor = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
argumentConstructor = argumentClass.asSubclass(ArgumentType.class).getDeclaredConstructor(parameters);
|
||||||
|
if (!argumentConstructor.isAccessible())
|
||||||
|
argumentConstructor.setAccessible(true);
|
||||||
|
if (parameters.length == 0) {
|
||||||
|
argumentType = argumentConstructor.newInstance();
|
||||||
|
} else {
|
||||||
|
argumentType = null;
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
argumentType = null;
|
||||||
|
argumentConstructor = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this argument type is supported in this Minecraft version
|
||||||
|
*
|
||||||
|
* @return If this is supported
|
||||||
|
*/
|
||||||
|
public boolean isSupported() {
|
||||||
|
return argumentConstructor != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this argument type requires parameters
|
||||||
|
*
|
||||||
|
* @return If this requires parameters
|
||||||
|
*/
|
||||||
|
public boolean requiresParameters() {
|
||||||
|
return parameters.length != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the argument type represented by this enum value, otherwise
|
||||||
|
* throws an exception
|
||||||
|
*
|
||||||
|
* @param <T> The argument type
|
||||||
|
* @return The argument type
|
||||||
|
* @throws IllegalArgumentException if not supported in this version
|
||||||
|
* @throws IllegalArgumentException if this argument requires arguments. See {@link #create(Object...)}
|
||||||
|
*/
|
||||||
|
public <T> ArgumentType<T> get() {
|
||||||
|
if (argumentConstructor == null)
|
||||||
|
throw new IllegalArgumentException("Argument type '" + name().toLowerCase() + "' is not available on this version.");
|
||||||
|
if (argumentType != null)
|
||||||
|
return (ArgumentType<T>) argumentType;
|
||||||
|
throw new IllegalArgumentException("This argument type requires " + parameters.length + " parameter(s) of type(s) " +
|
||||||
|
Arrays.stream(parameters).map(Class::getName).collect(Collectors.joining(", ")) + ". Use #create() instead.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance of this argument type
|
||||||
|
*
|
||||||
|
* @param arguments Arguments to construct the argument type with
|
||||||
|
* @param <T> The argument ttype
|
||||||
|
* @return The created argument type.
|
||||||
|
* @throws IllegalArgumentException if not supported in this version
|
||||||
|
*/
|
||||||
|
public <T> ArgumentType<T> create(Object... arguments) {
|
||||||
|
if (argumentConstructor == null)
|
||||||
|
throw new IllegalArgumentException("Argument type '" + name().toLowerCase() + "' is not available on this version.");
|
||||||
|
if (argumentType != null && arguments.length == 0)
|
||||||
|
return (ArgumentType<T>) argumentType;
|
||||||
|
try {
|
||||||
|
return argumentConstructor.newInstance(arguments);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the argument type represented by this enum value, wrapped
|
||||||
|
* inside an {@link Optional}
|
||||||
|
*
|
||||||
|
* @param <T> The argument type
|
||||||
|
* @return The argument type optional
|
||||||
|
* @throws IllegalArgumentException if this argument requires arguments. See {@link #createIfPresent(Object...)}
|
||||||
|
*/
|
||||||
|
public <T> Optional<ArgumentType<T>> getIfPresent() {
|
||||||
|
if (argumentConstructor == null)
|
||||||
|
return Optional.empty();
|
||||||
|
if (argumentType != null)
|
||||||
|
return Optional.of((ArgumentType<T>) argumentType);
|
||||||
|
throw new IllegalArgumentException("This argument type requires " + parameters.length + " parameter(s) of type(s) " +
|
||||||
|
Arrays.stream(parameters).map(Class::getName).collect(Collectors.joining(", ")) + ". Use #createIfPresent() instead.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an instance of this argument type, wrapped in an optional.
|
||||||
|
*
|
||||||
|
* @param arguments Arguments to construct the argument type with
|
||||||
|
* @param <T> The argument ttype
|
||||||
|
* @return The created argument type optional.
|
||||||
|
*/
|
||||||
|
public <T> Optional<ArgumentType<T>> createIfPresent(Object... arguments) {
|
||||||
|
if (argumentConstructor == null)
|
||||||
|
return Optional.empty();
|
||||||
|
if (argumentType != null && arguments.length == 0)
|
||||||
|
return Optional.of((ArgumentType<T>) argumentType);
|
||||||
|
try {
|
||||||
|
return Optional.of(argumentConstructor.newInstance(arguments));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<?> resolveArgumentClass(String name) {
|
||||||
|
try {
|
||||||
|
if (ReflectionUtil.minecraftVersion() > 16) {
|
||||||
|
return ReflectionUtil.mcClass("commands.arguments." + name);
|
||||||
|
} else {
|
||||||
|
String stripped;
|
||||||
|
if (name.lastIndexOf('.') != -1)
|
||||||
|
stripped = name.substring(name.lastIndexOf('.'));
|
||||||
|
else
|
||||||
|
stripped = name;
|
||||||
|
return ReflectionUtil.nmsClass(stripped);
|
||||||
|
}
|
||||||
|
} catch (Throwable t) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ensureSetup() {
|
||||||
|
// do nothing - this is only called to trigger the static initializer
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/main/java/ru/d3st0ny/adajency/ReflectionUtil.java
Normal file
63
src/main/java/ru/d3st0ny/adajency/ReflectionUtil.java
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
package ru.d3st0ny.adajency;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
final class ReflectionUtil {
|
||||||
|
private static final String SERVER_VERSION = getServerVersion();
|
||||||
|
|
||||||
|
private static String getServerVersion() {
|
||||||
|
Class<?> server = Bukkit.getServer().getClass();
|
||||||
|
if (!server.getSimpleName().equals("CraftServer")) {
|
||||||
|
return ".";
|
||||||
|
}
|
||||||
|
if (server.getName().equals("org.bukkit.craftbukkit.CraftServer")) {
|
||||||
|
// Non versioned class
|
||||||
|
return ".";
|
||||||
|
} else {
|
||||||
|
String version = server.getName().substring("org.bukkit.craftbukkit".length());
|
||||||
|
return version.substring(0, version.length() - "CraftServer".length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String mc(String name) {
|
||||||
|
return "net.minecraft." + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String nms(String className) {
|
||||||
|
return "net.minecraft.server" + SERVER_VERSION + className;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> mcClass(String className) throws ClassNotFoundException {
|
||||||
|
return Class.forName(mc(className));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> nmsClass(String className) throws ClassNotFoundException {
|
||||||
|
return Class.forName(nms(className));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String obc(String className) {
|
||||||
|
return "org.bukkit.craftbukkit" + SERVER_VERSION + className;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Class<?> obcClass(String className) throws ClassNotFoundException {
|
||||||
|
return Class.forName(obc(className));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int minecraftVersion() {
|
||||||
|
try {
|
||||||
|
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?( .*)?\\)").matcher(Bukkit.getVersion());
|
||||||
|
if (matcher.find()) {
|
||||||
|
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException(String.format("No match found in '%s'", Bukkit.getVersion()));
|
||||||
|
}
|
||||||
|
} catch (final IllegalArgumentException ex) {
|
||||||
|
throw new RuntimeException("Failed to determine Minecraft version", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ReflectionUtil() {}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue