2 Commits

17 changed files with 1039 additions and 734 deletions

1
.gitignore vendored
View File

@@ -15,6 +15,7 @@
# Gradle
.gradle/
.gradle-user-home/
build/
!gradle-wrapper.jar
!**/src/main/**/build/

View File

@@ -1,3 +1,8 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.language.jvm.tasks.ProcessResources
import java.util.Collections
plugins {
id 'java'
id 'io.papermc.paperweight.userdev' version '2.0.0-beta.21'
@@ -23,207 +28,180 @@ java {
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
}
def excludedModules = project.hasProperty('excludeModules') ?
project.property('excludeModules').split(',')*.trim() : []
def moduleExcludes = [
'blocks': [
'**/commands/WorkbenchCommand.java',
'**/commands/AnvilCommand.java',
'**/commands/CartographyTableCommand.java',
'**/commands/GrindstoneCommand.java',
'**/commands/LoomCommand.java',
'**/commands/SmithingTableCommand.java',
'**/commands/StonecutterCommand.java',
'**/commands/EnderChestCommand.java',
'**/commands/BlocksMenuCommand.java',
'**/listeners/ShulkerBoxListener.java'
],
'player': [
'**/commands/FlyCommand.java',
'**/commands/HealCommand.java',
'**/commands/FeedCommand.java',
'**/commands/VanishCommand.java',
'**/commands/SeenCommand.java',
'**/commands/HatCommand.java',
'**/commands/SuicideCommand.java',
'**/commands/RepairCommand.java'
],
'jei-fix': [
'**/listeners/JeiRecipeSyncListener.java'
],
'mob-drops': [
'**/listeners/MobDropListener.java',
'**/listeners/MobDropMenuListener.java',
'**/commands/MobDropCommand.java'
]
]
def includeBlocks = !excludedModules.contains('blocks')
def includePlayer = !excludedModules.contains('player')
def includeJeiFix = !excludedModules.contains('jei-fix')
def includeMobDrops = !excludedModules.contains('mob-drops')
def variantDefinitions = [
standard: [
archiveFileName: "EssentialsC-${project.version}.jar",
excludedModules: ['mob-drops']
],
all: [
archiveFileName: "EssentialsC-all-${project.version}.jar",
excludedModules: []
],
lite: [
archiveFileName: "EssentialsC-lite-${project.version}.jar",
excludedModules: ['blocks']
]
]
println "\n📦 EssentialsC 模块配置:"
println " ✅ Core (核心)"
println " ${includeBlocks ? '✅' : '❌'} Blocks (便捷方块)"
println " ${includePlayer ? '✅' : '❌'} Player (玩家管理)"
println " ${includeJeiFix ? '✅' : '❌'} JEI Fix (JEI 修复)"
println " ${includeMobDrops ? '✅' : '❌'} Mob Drops (生物掉落物)"
println ""
if (project.hasProperty('excludeModules')) {
def customExcludedModules = project.property('excludeModules')
.split(',')
.collect { it.trim() }
.findAll { !it.isEmpty() }
sourceSets {
main {
java {
if (!includeBlocks) {
exclude '**/commands/WorkbenchCommand.java'
exclude '**/commands/AnvilCommand.java'
exclude '**/commands/CartographyTableCommand.java'
exclude '**/commands/GrindstoneCommand.java'
exclude '**/commands/LoomCommand.java'
exclude '**/commands/SmithingTableCommand.java'
exclude '**/commands/StonecutterCommand.java'
exclude '**/commands/EnderChestCommand.java'
exclude '**/commands/BlocksMenuCommand.java'
exclude '**/listeners/ShulkerBoxListener.java'
}
variantDefinitions.custom = [
archiveFileName: "EssentialsC-custom-${project.version}.jar",
excludedModules: customExcludedModules
]
}
if (!includePlayer) {
exclude '**/commands/FlyCommand.java'
exclude '**/commands/HealCommand.java'
exclude '**/commands/FeedCommand.java'
exclude '**/commands/VanishCommand.java'
exclude '**/commands/SeenCommand.java'
exclude '**/commands/HatCommand.java'
exclude '**/commands/SuicideCommand.java'
exclude '**/commands/RepairCommand.java'
}
def resolveExcludePatterns = { Collection<String> modules ->
modules.collectMany { module -> moduleExcludes.get(module, Collections.emptyList()) }.unique()
}
if (!includeJeiFix) {
exclude '**/listeners/JeiRecipeSyncListener.java'
variantDefinitions.each { variantName, variantConfig ->
def unknownModules = variantConfig.excludedModules.findAll { !moduleExcludes.containsKey(it) }
if (!unknownModules.isEmpty()) {
throw new GradleException("Unknown modules for variant '${variantName}': ${unknownModules.join(', ')}")
}
}
if (!includeMobDrops) {
exclude '**/listeners/MobDropListener.java'
exclude '**/listeners/MobDropMenuListener.java'
exclude '**/commands/MobDropCommand.java'
}
}
def variantSourceSets = [:]
variantDefinitions.each { variantName, variantConfig ->
def sourceSet = sourceSets.create(variantName)
sourceSet.java.srcDirs = sourceSets.main.java.srcDirs
sourceSet.resources.srcDirs = sourceSets.main.resources.srcDirs
resolveExcludePatterns(variantConfig.excludedModules).each { pattern ->
sourceSet.java.exclude(pattern)
}
sourceSet.compileClasspath += sourceSets.main.compileClasspath
sourceSet.runtimeClasspath += sourceSet.output + sourceSet.compileClasspath
variantSourceSets[variantName] = sourceSet
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}
processResources {
tasks.withType(ProcessResources).configureEach {
filteringCharset = 'UTF-8'
}
variantDefinitions.keySet().each { variantName ->
def sourceSet = variantSourceSets[variantName]
def processTaskName = sourceSet.processResourcesTaskName
tasks.named(processTaskName, ProcessResources).configure {
inputs.property('version', project.version)
filesMatching('paper-plugin.yml') {
expand('version': project.version)
}
}
}
shadowJar {
tasks.named('jar').configure {
enabled = false
}
// ========== 多版本构建任务 ==========
tasks.named('shadowJar').configure {
enabled = false
}
task shadowJarStandard(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
from sourceSets.main.output
def variantJarTasks = variantDefinitions.collect { variantName, variantConfig ->
def taskName = "shadowJar${variantName.capitalize()}"
def sourceSet = variantSourceSets[variantName]
tasks.register(taskName, ShadowJar) {
group = 'build'
description = "Builds the ${variantName} plugin jar."
archiveFileName.set(variantConfig.archiveFileName as String)
from(sourceSet.output)
configurations = [project.configurations.runtimeClasspath]
exclude '**/listeners/MobDropListener.class'
exclude '**/listeners/MobDropMenuListener.class'
exclude '**/commands/MobDropCommand.class'
archiveFileName.set("EssentialsC-${project.version}.jar")
}
task shadowJarAll(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
from sourceSets.main.output
configurations = [project.configurations.runtimeClasspath]
archiveFileName.set("EssentialsC-all-${project.version}.jar")
}
task shadowJarLite(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
from sourceSets.main.output
configurations = [project.configurations.runtimeClasspath]
exclude '**/commands/WorkbenchCommand.class'
exclude '**/commands/AnvilCommand.class'
exclude '**/commands/CartographyTableCommand.class'
exclude '**/commands/GrindstoneCommand.class'
exclude '**/commands/LoomCommand.class'
exclude '**/commands/SmithingTableCommand.class'
exclude '**/commands/StonecutterCommand.class'
exclude '**/commands/EnderChestCommand.class'
exclude '**/commands/BlocksMenuCommand.class'
exclude '**/listeners/ShulkerBoxListener.class'
archiveFileName.set("EssentialsC-lite-${project.version}.jar")
}
build {
dependsOn shadowJarAll
}
// ========== 部署任务 ==========
task deployToPaper12111(type: Copy) {
group = 'deployment'
description = '部署到 Paper 1.21.11 测试服务器(仅完整版)'
dependsOn shadowJarAll
from shadowJarAll
into "${projectDir}/test-server/paper-1.21.11/plugins"
doLast {
println "✅ 插件已部署到 Paper 1.21.11: ${projectDir}/test-server/paper-1.21.11/plugins"
dependsOn(tasks.named(sourceSet.classesTaskName))
}
}
task deployToPaper26 {
tasks.named('assemble').configure {
dependsOn(variantJarTasks)
}
tasks.register('buildAllVersions') {
group = 'build'
description = 'Builds standard, all, and lite plugin jars.'
dependsOn(variantJarTasks)
}
tasks.register('deployToPaper12111', Copy) {
group = 'deployment'
description = '部署到 Paper 26.1.2 测试服务器(仅完整版)'
dependsOn shadowJarAll
description = 'Deploys the all variant to the local Paper 1.21.11 test server.'
def artifact = tasks.named('shadowJarAll').flatMap { it.archiveFile }
dependsOn(tasks.named('shadowJarAll'))
from(artifact)
into(layout.projectDirectory.dir('test-server/paper-1.21.11/plugins'))
}
tasks.register('deployToPaper26') {
group = 'deployment'
description = 'Deploys the all variant to the local Paper 26.1.2 test server.'
dependsOn(tasks.named('shadowJarAll'))
doFirst {
def pluginsDir = file("${projectDir}/test-server/paper-26.1.2/plugins")
if (pluginsDir.exists()) {
// 删除所有 EssentialsC 相关的 JAR 文件
fileTree(pluginsDir).include('EssentialsC*.jar').each { file ->
println "🗑️ 删除旧插件: ${file.name}"
file.delete()
if (!pluginsDir.exists()) {
return
}
// 删除配置文件夹
def configDir = file("${pluginsDir}/EssentialsC")
if (configDir.exists()) {
println "🗑️ 删除旧配置文件夹"
configDir.deleteDir()
}
println "✅ 清理完成"
} else {
println "⚠️ plugins 目录不存在,跳过清理"
fileTree(pluginsDir).matching {
include 'EssentialsC*.jar'
}.each { pluginJar ->
pluginJar.delete()
}
}
doLast {
def artifact = tasks.named('shadowJarAll').flatMap { it.archiveFile }
copy {
from shadowJarAll.archiveFile
into "${projectDir}/test-server/paper-26.1.2/plugins"
from(artifact)
into("${projectDir}/test-server/paper-26.1.2/plugins")
}
println "✅ 插件已部署到 Paper 26.1.2: ${projectDir}/test-server/paper-26.1.2/plugins"
}
}
// Paper 26.1.2 一键构建和部署任务
task buildAndDeployToPaper26 {
tasks.register('buildAndDeployToPaper26') {
group = 'deployment'
description = '一键构建并部署到 Paper 26.1.2'
dependsOn clean, deployToPaper26
}
// 默认部署到 Paper 1.21.11
task deployToTestServer {
dependsOn deployToPaper12111
}
build.finalizedBy deployToTestServer
task buildAllVersions {
group = 'build'
description = '构建所有版本的插件'
dependsOn shadowJarStandard, shadowJarAll, shadowJarLite
doLast {
println "\n🎉 所有版本构建完成!"
println "📦 标准版: EssentialsC-${project.version}.jar"
println "📦 完整版: EssentialsC-all-${project.version}.jar"
println "📦 轻量版: EssentialsC-lite-${project.version}.jar"
println "📁 输出目录: ${buildDir}/libs/"
def standardFile = file("${buildDir}/libs/EssentialsC-${project.version}.jar")
def allFile = file("${buildDir}/libs/EssentialsC-all-${project.version}.jar")
def liteFile = file("${buildDir}/libs/EssentialsC-lite-${project.version}.jar")
if (!standardFile.exists()) {
throw new GradleException("❌ 标准版文件不存在!")
}
if (!allFile.exists()) {
throw new GradleException("❌ 完整版文件不存在!")
}
if (!liteFile.exists()) {
throw new GradleException("❌ 轻量版文件不存在!")
}
println "✅ 所有文件验证通过!"
}
description = 'Builds and deploys the all variant to the local Paper 26.1.2 test server.'
dependsOn(tasks.named('clean'), tasks.named('deployToPaper26'))
}

BIN
gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip
distributionUrl=https\://mirrors.aliyun.com/gradle/distributions/v9.1.0/gradle-9.1.0-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME

248
gradlew vendored Normal file
View File

@@ -0,0 +1,248 @@
#!/bin/sh
#
# Copyright © 2015 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
# 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" )
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, 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" \
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@"
# 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" "$@"

93
gradlew.bat vendored Normal file
View File

@@ -0,0 +1,93 @@
@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
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
: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

View File

@@ -1,6 +1,8 @@
package cn.infstar.essentialsC;
import cn.infstar.essentialsC.commands.*;
import cn.infstar.essentialsC.commands.BaseCommand;
import cn.infstar.essentialsC.commands.CommandRegistry;
import cn.infstar.essentialsC.commands.HelpCommand;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -19,44 +21,36 @@ public final class EssentialsC extends JavaPlugin {
registerListeners();
registerCommands();
getLogger().info("插件已启用!版本: " + getDescription().getVersion());
getLogger().info("EssentialsC enabled. Version: " + getDescription().getVersion());
}
@Override
public void onDisable() {
getLogger().info("EssentialsC 插件已禁用!");
getLogger().info("EssentialsC disabled.");
}
public static LangManager getLangManager() {
return langManager;
}
/**
* 注册 JEI 配方同步所需的插件频道
*/
private void registerPluginChannels() {
org.bukkit.plugin.messaging.Messenger messenger = getServer().getMessenger();
// 注册 Fabric 和 NeoForge 的配方同步频道
messenger.registerOutgoingPluginChannel(this, "fabric:recipe_sync");
messenger.registerOutgoingPluginChannel(this, "neoforge:recipe_content");
}
private void registerListeners() {
if (registerListener("cn.infstar.essentialsC.listeners.ShulkerBoxListener")) {
getLogger().info("- 潜影盒模块");
getLogger().info("- Shulker box module");
}
if (registerListener("cn.infstar.essentialsC.listeners.JeiRecipeSyncListener")) {
getLogger().info("- JEI 配方同步");
getLogger().info("- JEI recipe sync");
}
if (registerListener("cn.infstar.essentialsC.listeners.MobDropListener")) {
try {
Class.forName("cn.infstar.essentialsC.listeners.MobDropMenuListener");
new cn.infstar.essentialsC.listeners.MobDropMenuListener(this);
} catch (ClassNotFoundException e) {
}
getLogger().info("- 生物掉落控制");
createOptionalInstance("cn.infstar.essentialsC.listeners.MobDropMenuListener");
getLogger().info("- Mob drop control");
}
}
@@ -66,119 +60,41 @@ public final class EssentialsC extends JavaPlugin {
Object listenerInstance = listenerClass.getConstructor(EssentialsC.class).newInstance(this);
getServer().getPluginManager().registerEvents((org.bukkit.event.Listener) listenerInstance, this);
return true;
} catch (Exception e) {
} catch (Exception ignored) {
return false;
}
}
private void createOptionalInstance(String className) {
try {
Class<?> targetClass = Class.forName(className);
targetClass.getConstructor(EssentialsC.class).newInstance(this);
} catch (Exception ignored) {
}
}
private void registerCommands() {
try {
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
bukkitCommandMap.setAccessible(true);
org.bukkit.command.CommandMap commandMap = (org.bukkit.command.CommandMap) bukkitCommandMap.get(Bukkit.getServer());
int commandCount = 0;
if (classExists("cn.infstar.essentialsC.commands.WorkbenchCommand")) {
registerCommandWithAliases(commandMap, "workbench", new WorkbenchCommand(), "wb");
commandCount++;
for (CommandRegistry.CommandSpec spec : CommandRegistry.getCommandSpecs()) {
BaseCommand executor = CommandRegistry.getCommand(spec.name());
if (executor == null) {
continue;
}
if (classExists("cn.infstar.essentialsC.commands.AnvilCommand")) {
registerCommandWithAliases(commandMap, "anvil", new AnvilCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.CartographyTableCommand")) {
registerCommandWithAliases(commandMap, "cartographytable", new CartographyTableCommand(), "ct", "cartography");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.GrindstoneCommand")) {
registerCommandWithAliases(commandMap, "grindstone", new GrindstoneCommand(), "gs");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.LoomCommand")) {
registerCommandWithAliases(commandMap, "loom", new LoomCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SmithingTableCommand")) {
registerCommandWithAliases(commandMap, "smithingtable", new SmithingTableCommand(), "st", "smithing");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.StonecutterCommand")) {
registerCommandWithAliases(commandMap, "stonecutter", new StonecutterCommand(), "sc");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.EnderChestCommand")) {
registerCommandWithAliases(commandMap, "enderchest", new EnderChestCommand(), "ec");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.BlocksMenuCommand")) {
registerCommandWithAliases(commandMap, "blocks", new BlocksMenuCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.FlyCommand")) {
registerCommandWithAliases(commandMap, "fly", new FlyCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.HealCommand")) {
registerCommandWithAliases(commandMap, "heal", new HealCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.FeedCommand")) {
registerCommandWithAliases(commandMap, "feed", new FeedCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.VanishCommand")) {
registerCommandWithAliases(commandMap, "vanish", new VanishCommand(), "v");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SeenCommand")) {
registerCommandWithAliases(commandMap, "seen", new SeenCommand(), "info");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.HatCommand")) {
registerCommandWithAliases(commandMap, "hat", new HatCommand());
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.SuicideCommand")) {
registerCommandWithAliases(commandMap, "suicide", new SuicideCommand(), "die");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.RepairCommand")) {
registerCommandWithAliases(commandMap, "repair", new RepairCommand(), "rep");
commandCount++;
}
if (classExists("cn.infstar.essentialsC.commands.MobDropCommand")) {
registerCommandWithAliases(commandMap, "mobdrops", new MobDropCommand());
commandCount++;
registerCommandWithAliases(commandMap, spec.name(), executor, spec.aliases().toArray(String[]::new));
}
registerCommandWithAliases(commandMap, "essentialsc", new HelpCommand(), "essc");
commandCount++;
} catch (Exception e) {
getLogger().severe("无法注册命令: " + e.getMessage());
getLogger().severe("Failed to register commands: " + e.getMessage());
e.printStackTrace();
}
}
private boolean classExists(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
/**
* 注册命令并支持别名
* @param commandMap Bukkit CommandMap
* @param name 主命令名
* @param executor 命令执行器
* @param aliases 别名列表(可选)
*/
private void registerCommandWithAliases(org.bukkit.command.CommandMap commandMap, String name, cn.infstar.essentialsC.commands.BaseCommand executor, String... aliases) {
private void registerCommandWithAliases(org.bukkit.command.CommandMap commandMap, String name, BaseCommand executor, String... aliases) {
Command command = new Command(name) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
@@ -187,18 +103,16 @@ public final class EssentialsC extends JavaPlugin {
@Override
public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter) {
return ((org.bukkit.command.TabCompleter) executor).onTabComplete(sender, this, alias, args);
if (executor instanceof org.bukkit.command.TabCompleter completer) {
return completer.onTabComplete(sender, this, alias, args);
}
return super.tabComplete(sender, alias, args);
}
};
command.setPermission(executor.getPermission());
// 注册到默认命名空间,使玩家可以直接使用 /workbench 而不是 /essentialsc:workbench
commandMap.register("", command);
// 注册别名
for (String alias : aliases) {
Command aliasCmd = new Command(alias) {
@Override
@@ -207,11 +121,11 @@ public final class EssentialsC extends JavaPlugin {
}
@Override
public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter) {
return ((org.bukkit.command.TabCompleter) executor).onTabComplete(sender, this, alias, args);
public java.util.List<String> tabComplete(CommandSender sender, String label, String[] args) throws IllegalArgumentException {
if (executor instanceof org.bukkit.command.TabCompleter completer) {
return completer.onTabComplete(sender, this, label, args);
}
return super.tabComplete(sender, alias, args);
return super.tabComplete(sender, label, args);
}
};
aliasCmd.setPermission(executor.getPermission());

View File

@@ -1,5 +1,6 @@
package cn.infstar.essentialsC;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
@@ -147,6 +148,6 @@ public class LangManager {
* 翻译颜色代码
*/
private String translateColorCodes(String text) {
return text.replace("&", "§");
return text == null ? "" : ChatColor.translateAlternateColorCodes('&', text);
}
}

View File

@@ -6,7 +6,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public abstract class BaseCommand implements CommandExecutor {
@@ -32,7 +31,7 @@ public abstract class BaseCommand implements CommandExecutor {
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player player) {
if (!player.hasPermission(permission)) {
String message = getLang().getString("messages.no-permission",

View File

@@ -1,6 +1,7 @@
package cn.infstar.essentialsC.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
@@ -8,41 +9,63 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class BlocksMenuCommand extends BaseCommand implements Listener {
private static final int MENU_SIZE = 36;
private static boolean listenerRegistered = false;
private final NamespacedKey blockKey;
public BlocksMenuCommand() {
super("essentialsc.command.blocks");
plugin.getServer().getPluginManager().registerEvents(this, plugin);
blockKey = new NamespacedKey(plugin, "block_key");
private static final class BlocksMenuHolder implements InventoryHolder {
private final Inventory inventory;
private BlocksMenuHolder(String title) {
this.inventory = Bukkit.createInventory(this, MENU_SIZE, title);
}
@Override
protected boolean execute(@NotNull Player player, String[] args) {
public Inventory getInventory() {
return inventory;
}
}
public BlocksMenuCommand() {
super("essentialsc.command.blocks");
if (!listenerRegistered) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
listenerRegistered = true;
}
this.blockKey = new NamespacedKey(plugin, "block_key");
}
@Override
protected boolean execute(Player player, String[] args) {
openMenu(player);
return true;
}
private void openMenu(Player player) {
String title = plugin.getConfig().getString("blocks-menu.title", "&6&lEssentialsC &8- &e&l功能方块菜单");
Inventory menu = Bukkit.createInventory(null, MENU_SIZE, translateColor(title));
String title = plugin.getConfig().getString("blocks-menu.title", "&6&lEssentialsC &8- &e&lBlocks Menu");
Inventory menu = new BlocksMenuHolder(translateColor(title)).getInventory();
// 从配置中读取所有物品
var itemsConfig = plugin.getConfig().getConfigurationSection("blocks-menu.items");
if (itemsConfig == null) return;
if (itemsConfig == null) {
return;
}
for (String key : itemsConfig.getKeys(false)) {
var section = itemsConfig.getConfigurationSection(key);
if (section == null) continue;
if (section == null) {
continue;
}
// 检查权限
String permission = section.getString("permission");
if (permission != null && !player.hasPermission(permission)) {
continue;
@@ -50,12 +73,14 @@ public class BlocksMenuCommand extends BaseCommand implements Listener {
int slot = section.getInt("slot");
Material material = Material.matchMaterial(section.getString("material", "STONE"));
if (material == null) material = Material.STONE;
if (material == null) {
material = Material.STONE;
}
String name = translateColor(section.getString("name", "&fItem"));
java.util.List<String> lore = section.getStringList("lore").stream()
List<String> lore = section.getStringList("lore").stream()
.map(this::translateColor)
.collect(java.util.stream.Collectors.toList());
.toList();
addItem(menu, slot, material, name, lore, key);
}
@@ -63,7 +88,7 @@ public class BlocksMenuCommand extends BaseCommand implements Listener {
player.openInventory(menu);
}
private void addItem(Inventory inv, int slot, Material material, String name, java.util.List<String> lore, String key) {
private void addItem(Inventory inv, int slot, Material material, String name, List<String> lore, String key) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
@@ -77,30 +102,28 @@ public class BlocksMenuCommand extends BaseCommand implements Listener {
@EventHandler
public void onMenuClick(InventoryClickEvent event) {
// 动态获取配置的标题
String configTitle = plugin.getConfig().getString("blocks-menu.title", "&6&lEssentialsC &8- &e&l功能方块菜单");
String actualTitle = translateColor(configTitle);
if (!event.getView().getTitle().equals(actualTitle)) return;
if (!(event.getWhoClicked() instanceof Player player)) return;
if (!(event.getView().getTopInventory().getHolder(false) instanceof BlocksMenuHolder)) {
return;
}
if (!(event.getWhoClicked() instanceof Player player)) {
return;
}
event.setCancelled(true);
ItemStack clicked = event.getCurrentItem();
if (clicked == null || !clicked.hasItemMeta()) return;
if (clicked == null || !clicked.hasItemMeta()) {
return;
}
ItemMeta meta = clicked.getItemMeta();
String key = meta.getPersistentDataContainer().get(this.blockKey, PersistentDataType.STRING);
// 点击后执行对应命令并播放音效(如果有)
if (key != null && HelpCommand.COMMAND_CACHE.containsKey(key)) {
BaseCommand blockCommand = CommandRegistry.getCommand(key);
if (blockCommand != null) {
playBlockOpenSound(player, key);
HelpCommand.COMMAND_CACHE.get(key).execute(player, new String[]{});
blockCommand.execute(player, new String[0]);
}
}
/**
* 播放对应方块的打开音效(优先使用交互音效)
*/
private void playBlockOpenSound(Player player, String key) {
org.bukkit.Sound sound = switch (key) {
case "workbench" -> org.bukkit.Sound.BLOCK_WOOD_HIT;
@@ -119,10 +142,7 @@ public class BlocksMenuCommand extends BaseCommand implements Listener {
}
}
/**
* 转换颜色代码 & -> §
*/
private String translateColor(String text) {
return text.replace("&", "§");
return text == null ? "" : ChatColor.translateAlternateColorCodes('&', text);
}
}

View File

@@ -0,0 +1,111 @@
package cn.infstar.essentialsC.commands;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class CommandRegistry {
private static final Map<String, CommandSpec> COMMANDS = new LinkedHashMap<>();
private static final Map<String, String> ALIAS_TO_COMMAND = new HashMap<>();
private static final Map<String, BaseCommand> COMMAND_CACHE = new HashMap<>();
private static final Set<String> UNAVAILABLE_COMMANDS = new java.util.HashSet<>();
static {
register("workbench", "essentialsc.command.workbench", "cn.infstar.essentialsC.commands.WorkbenchCommand", "wb");
register("anvil", "essentialsc.command.anvil", "cn.infstar.essentialsC.commands.AnvilCommand");
register("cartographytable", "essentialsc.command.cartographytable", "cn.infstar.essentialsC.commands.CartographyTableCommand", "ct", "cartography");
register("grindstone", "essentialsc.command.grindstone", "cn.infstar.essentialsC.commands.GrindstoneCommand", "gs");
register("loom", "essentialsc.command.loom", "cn.infstar.essentialsC.commands.LoomCommand");
register("smithingtable", "essentialsc.command.smithingtable", "cn.infstar.essentialsC.commands.SmithingTableCommand", "st", "smithing");
register("stonecutter", "essentialsc.command.stonecutter", "cn.infstar.essentialsC.commands.StonecutterCommand", "sc");
register("enderchest", "essentialsc.command.enderchest", "cn.infstar.essentialsC.commands.EnderChestCommand", "ec");
register("blocks", "essentialsc.command.blocks", "cn.infstar.essentialsC.commands.BlocksMenuCommand");
register("hat", "essentialsc.command.hat", "cn.infstar.essentialsC.commands.HatCommand");
register("suicide", "essentialsc.command.suicide", "cn.infstar.essentialsC.commands.SuicideCommand", "die");
register("fly", "essentialsc.command.fly", "cn.infstar.essentialsC.commands.FlyCommand");
register("heal", "essentialsc.command.heal", "cn.infstar.essentialsC.commands.HealCommand");
register("vanish", "essentialsc.command.vanish", "cn.infstar.essentialsC.commands.VanishCommand", "v");
register("seen", "essentialsc.command.seen", "cn.infstar.essentialsC.commands.SeenCommand", "info");
register("feed", "essentialsc.command.feed", "cn.infstar.essentialsC.commands.FeedCommand");
register("repair", "essentialsc.command.repair", "cn.infstar.essentialsC.commands.RepairCommand", "rep");
register("mobdrops", "essentialsc.mobdrops.enderman", "cn.infstar.essentialsC.commands.MobDropCommand");
}
private CommandRegistry() {
}
private static void register(String name, String permission, String className, String... aliases) {
List<String> aliasList = List.of(aliases);
CommandSpec spec = new CommandSpec(name, permission, className, aliasList);
COMMANDS.put(name, spec);
ALIAS_TO_COMMAND.put(name, name);
for (String alias : aliasList) {
ALIAS_TO_COMMAND.put(alias, name);
}
}
public static Collection<CommandSpec> getCommandSpecs() {
return Collections.unmodifiableCollection(COMMANDS.values());
}
public static String resolveCommandName(String input) {
if (input == null) {
return null;
}
return ALIAS_TO_COMMAND.get(input.toLowerCase());
}
public static boolean isAvailable(String name) {
return getCommand(name) != null;
}
public static String getPermission(String name) {
CommandSpec spec = COMMANDS.get(name);
return spec == null ? null : spec.permission();
}
public static BaseCommand getCommand(String name) {
String resolvedName = resolveCommandName(name);
if (resolvedName == null) {
return null;
}
BaseCommand cached = COMMAND_CACHE.get(resolvedName);
if (cached != null) {
return cached;
}
if (UNAVAILABLE_COMMANDS.contains(resolvedName)) {
return null;
}
CommandSpec spec = COMMANDS.get(resolvedName);
if (spec == null) {
return null;
}
try {
Class<?> rawClass = Class.forName(spec.className());
if (!BaseCommand.class.isAssignableFrom(rawClass)) {
UNAVAILABLE_COMMANDS.add(resolvedName);
return null;
}
Constructor<? extends BaseCommand> constructor = rawClass.asSubclass(BaseCommand.class).getDeclaredConstructor();
BaseCommand command = constructor.newInstance();
COMMAND_CACHE.put(resolvedName, command);
return command;
} catch (ReflectiveOperationException | LinkageError ignored) {
UNAVAILABLE_COMMANDS.add(resolvedName);
return null;
}
}
public record CommandSpec(String name, String permission, String className, List<String> aliases) {
}
}

View File

@@ -7,49 +7,24 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class HelpCommand extends BaseCommand implements TabCompleter {
// 缓存命令实例,避免重复创建
static final java.util.Map<String, BaseCommand> COMMAND_CACHE = new java.util.HashMap<>();
static {
COMMAND_CACHE.put("workbench", new WorkbenchCommand());
COMMAND_CACHE.put("anvil", new AnvilCommand());
COMMAND_CACHE.put("cartographytable", new CartographyTableCommand());
COMMAND_CACHE.put("grindstone", new GrindstoneCommand());
COMMAND_CACHE.put("loom", new LoomCommand());
COMMAND_CACHE.put("smithingtable", new SmithingTableCommand());
COMMAND_CACHE.put("stonecutter", new StonecutterCommand());
COMMAND_CACHE.put("enderchest", new EnderChestCommand());
COMMAND_CACHE.put("hat", new HatCommand());
COMMAND_CACHE.put("suicide", new SuicideCommand());
COMMAND_CACHE.put("fly", new FlyCommand());
COMMAND_CACHE.put("heal", new HealCommand());
COMMAND_CACHE.put("vanish", new VanishCommand());
COMMAND_CACHE.put("seen", new SeenCommand());
COMMAND_CACHE.put("feed", new FeedCommand());
COMMAND_CACHE.put("repair", new RepairCommand());
COMMAND_CACHE.put("blocks", new BlocksMenuCommand());
COMMAND_CACHE.put("mobdrops", new MobDropCommand());
}
public HelpCommand() {
super("essentialsc.command.help");
}
@Override
protected boolean execute(@NotNull Player player, String[] args) {
protected boolean execute(Player player, String[] args) {
return handleCommand(player, player, args);
}
@Override
protected boolean executeConsole(org.bukkit.command.CommandSender sender, String[] args) {
protected boolean executeConsole(CommandSender sender, String[] args) {
if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("essentialsc.command.reload")) {
sender.sendMessage(getLang().getString("messages.no-permission"));
@@ -57,7 +32,7 @@ public class HelpCommand extends BaseCommand implements TabCompleter {
}
plugin.reloadConfig();
EssentialsC.getLangManager().reload();
sender.sendMessage(getLang().getString("prefix") + "§a配置已重载");
sender.sendMessage(getLang().getString("prefix") + "Configuration reloaded.");
return true;
}
sender.sendMessage(getLang().getString("messages.player-only"));
@@ -75,83 +50,82 @@ public class HelpCommand extends BaseCommand implements TabCompleter {
}
plugin.reloadConfig();
EssentialsC.getLangManager().reload();
sender.sendMessage(getLang().getString("prefix") + "§a配置已重载");
sender.sendMessage(getLang().getString("prefix") + "Configuration reloaded.");
return true;
}
// 功能方块和其他命令 - 使用别名映射
String actualCommand = getActualCommand(subCommand);
if (actualCommand != null && COMMAND_CACHE.containsKey(actualCommand)) {
String permission = getPermissionForCommand(actualCommand);
if (!player.hasPermission(permission)) {
BaseCommand targetCommand = CommandRegistry.getCommand(actualCommand);
if (actualCommand != null && targetCommand != null) {
String permission = CommandRegistry.getPermission(actualCommand);
if (permission != null && !player.hasPermission(permission)) {
player.sendMessage(getLang().getString("messages.no-permission"));
return true;
}
// seen 需要特殊处理参数
if (actualCommand.equals("seen")) {
if (args.length < 2) {
player.sendMessage(getLang().getString("prefix") + getLang().getString("messages.seen-usage-console"));
return true;
}
COMMAND_CACHE.get("seen").execute(player, new String[]{args[1]});
targetCommand.execute(player, new String[]{args[1]});
} else {
COMMAND_CACHE.get(actualCommand).execute(player, new String[]{});
targetCommand.execute(player, new String[0]);
}
return true;
} else if (subCommand.equals("version") || subCommand.equals("v")) {
player.sendMessage(getLang().getString("prefix") + "§6EssentialsC §fv" + plugin.getDescription().getVersion());
player.sendMessage(getLang().getString("prefix") + "§7运行在 Paper " + Bukkit.getVersion());
}
if (subCommand.equals("version") || subCommand.equals("v")) {
player.sendMessage(getLang().getString("prefix") + "EssentialsC v" + plugin.getDescription().getVersion());
player.sendMessage(getLang().getString("prefix") + "Running on Paper " + Bukkit.getVersion());
return true;
} else {
}
player.sendMessage(getLang().getString("prefix") + getLang().getString("messages.unknown-subcommand",
java.util.Map.of("command", subCommand)));
Map.of("command", subCommand)));
player.sendMessage(getLang().getString("prefix") + getLang().getString("messages.help-usage"));
return true;
}
}
// 显示帮助
LangManager lang = getLang();
String version = plugin.getDescription().getVersion();
player.sendMessage(lang.getString("help.title"));
player.sendMessage(lang.getString("help.version",
java.util.Map.of("version", version)));
player.sendMessage(lang.getString("help.version", Map.of("version", version)));
player.sendMessage("");
// 功能方块命令(检查权限后显示)
boolean hasBlockCommands = false;
StringBuilder blockCommands = new StringBuilder();
if (player.hasPermission("essentialsc.command.workbench")) {
if (CommandRegistry.isAvailable("workbench") && player.hasPermission("essentialsc.command.workbench")) {
blockCommands.append(lang.getString("help.commands.workbench")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.anvil")) {
if (CommandRegistry.isAvailable("anvil") && player.hasPermission("essentialsc.command.anvil")) {
blockCommands.append(lang.getString("help.commands.anvil")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.cartographytable")) {
if (CommandRegistry.isAvailable("cartographytable") && player.hasPermission("essentialsc.command.cartographytable")) {
blockCommands.append(lang.getString("help.commands.cartographytable")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.grindstone")) {
if (CommandRegistry.isAvailable("grindstone") && player.hasPermission("essentialsc.command.grindstone")) {
blockCommands.append(lang.getString("help.commands.grindstone")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.loom")) {
if (CommandRegistry.isAvailable("loom") && player.hasPermission("essentialsc.command.loom")) {
blockCommands.append(lang.getString("help.commands.loom")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.smithingtable")) {
if (CommandRegistry.isAvailable("smithingtable") && player.hasPermission("essentialsc.command.smithingtable")) {
blockCommands.append(lang.getString("help.commands.smithingtable")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.stonecutter")) {
if (CommandRegistry.isAvailable("stonecutter") && player.hasPermission("essentialsc.command.stonecutter")) {
blockCommands.append(lang.getString("help.commands.stonecutter")).append("\n");
hasBlockCommands = true;
}
if (player.hasPermission("essentialsc.command.enderchest")) {
if (CommandRegistry.isAvailable("enderchest") && player.hasPermission("essentialsc.command.enderchest")) {
blockCommands.append(lang.getString("help.commands.enderchest")).append("\n");
hasBlockCommands = true;
}
@@ -162,31 +136,30 @@ public class HelpCommand extends BaseCommand implements TabCompleter {
player.sendMessage("");
}
// 其他命令(检查权限后显示)
boolean hasOtherCommands = false;
StringBuilder otherCommands = new StringBuilder();
if (player.hasPermission("essentialsc.command.hat")) {
if (CommandRegistry.isAvailable("hat") && player.hasPermission("essentialsc.command.hat")) {
otherCommands.append(lang.getString("help.commands.hat")).append("\n");
hasOtherCommands = true;
}
if (player.hasPermission("essentialsc.command.suicide")) {
if (CommandRegistry.isAvailable("suicide") && player.hasPermission("essentialsc.command.suicide")) {
otherCommands.append(lang.getString("help.commands.suicide")).append("\n");
hasOtherCommands = true;
}
if (player.hasPermission("essentialsc.command.fly")) {
if (CommandRegistry.isAvailable("fly") && player.hasPermission("essentialsc.command.fly")) {
otherCommands.append(lang.getString("help.commands.fly")).append("\n");
hasOtherCommands = true;
}
if (player.hasPermission("essentialsc.command.heal")) {
if (CommandRegistry.isAvailable("heal") && player.hasPermission("essentialsc.command.heal")) {
otherCommands.append(lang.getString("help.commands.heal")).append("\n");
hasOtherCommands = true;
}
if (player.hasPermission("essentialsc.command.vanish")) {
if (CommandRegistry.isAvailable("vanish") && player.hasPermission("essentialsc.command.vanish")) {
otherCommands.append(lang.getString("help.commands.vanish")).append("\n");
hasOtherCommands = true;
}
if (player.hasPermission("essentialsc.command.seen")) {
if (CommandRegistry.isAvailable("seen") && player.hasPermission("essentialsc.command.seen")) {
otherCommands.append(lang.getString("help.commands.seen")).append("\n");
hasOtherCommands = true;
}
@@ -201,38 +174,16 @@ public class HelpCommand extends BaseCommand implements TabCompleter {
return true;
}
/**
* 将别名映射到实际命令名
*/
private String getActualCommand(String alias) {
return switch (alias) {
case "wb" -> "workbench";
case "cartography", "ct" -> "cartographytable";
case "gs" -> "grindstone";
case "smithing", "st" -> "smithingtable";
case "sc" -> "stonecutter";
case "ec" -> "enderchest";
case "die" -> "suicide";
case "info" -> "seen";
case "rep" -> "repair";
default -> alias;
};
}
/**
* 获取命令对应的权限节点
*/
private String getPermissionForCommand(String command) {
return "essentialsc.command." + command;
return CommandRegistry.resolveCommandName(alias);
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1) {
List<String> completions = new ArrayList<>();
String partial = args[0].toLowerCase();
// 所有可能的子命令及其权限(包括别名)
String[][] subCommands = {
{"reload", "essentialsc.command.reload"},
{"blocks", "essentialsc.command.blocks"},
@@ -270,15 +221,21 @@ public class HelpCommand extends BaseCommand implements TabCompleter {
};
for (String[] subCmd : subCommands) {
if (subCmd[0].startsWith(partial)) {
if (subCmd[1] == null || sender.hasPermission(subCmd[1])) {
completions.add(subCmd[0]);
if (!subCmd[0].startsWith(partial)) {
continue;
}
String actualCommand = getActualCommand(subCmd[0]);
boolean available = actualCommand == null || CommandRegistry.isAvailable(actualCommand);
if (available && (subCmd[1] == null || sender.hasPermission(subCmd[1]))) {
completions.add(subCmd[0]);
}
}
return completions;
} else if (args.length == 2) {
}
if (args.length == 2) {
String subCmd = args[0].toLowerCase();
if ((subCmd.equals("seen") || subCmd.equals("info")) && sender.hasPermission("essentialsc.command.seen")) {
List<String> players = new ArrayList<>();

View File

@@ -5,6 +5,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@@ -16,6 +17,19 @@ import java.util.Arrays;
*/
public class MobDropCommand extends BaseCommand {
public static final class MobDropMenuHolder implements InventoryHolder {
private final Inventory inventory;
public MobDropMenuHolder(String title) {
this.inventory = Bukkit.createInventory(this, 27, title);
}
@Override
public Inventory getInventory() {
return inventory;
}
}
public MobDropCommand() {
super("essentialsc.mobdrops.enderman");
}
@@ -34,7 +48,7 @@ public class MobDropCommand extends BaseCommand {
boolean endermanEnabled = plugin.getConfig().getBoolean("mob-drops.enderman.enabled", true);
// 创建菜单
Inventory menu = Bukkit.createInventory(null, 27, "§6§l生物掉落控制");
Inventory menu = new MobDropMenuHolder("§6§l生物掉落控制").getInventory();
// 末影人控制项
ItemStack endermanItem = new ItemStack(Material.ENDER_PEARL);

View File

@@ -21,7 +21,6 @@ public class MobDropListener implements Listener {
loadConfig();
// 注册监听器
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
/**

View File

@@ -25,7 +25,7 @@ public class MobDropMenuListener implements Listener {
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!event.getView().getTitle().equals("§6§l生物掉落控制")) {
if (!(event.getView().getTopInventory().getHolder(false) instanceof cn.infstar.essentialsC.commands.MobDropCommand.MobDropMenuHolder)) {
return;
}
@@ -71,7 +71,7 @@ public class MobDropMenuListener implements Listener {
private void openMobDropMenu(Player player) {
boolean endermanEnabled = plugin.getConfig().getBoolean("mob-drops.enderman.enabled", true);
Inventory menu = Bukkit.createInventory(null, 27, "§6§l生物掉落控制");
Inventory menu = new cn.infstar.essentialsC.commands.MobDropCommand.MobDropMenuHolder("§6§l生物掉落控制").getInventory();
ItemStack endermanItem = new ItemStack(Material.ENDER_PEARL);
ItemMeta endermanMeta = endermanItem.getItemMeta();

View File

@@ -2,19 +2,28 @@ package cn.infstar.essentialsC.listeners;
import cn.infstar.essentialsC.EssentialsC;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
@@ -23,11 +32,8 @@ import java.util.UUID;
public class ShulkerBoxListener implements Listener {
private final EssentialsC plugin;
// 存储玩家打开的潜影盒玩家UUID -> (原始物品快照, 当前物品引用)
private final Map<UUID, ShulkerBoxData> openShulkerBoxes = new HashMap<>();
private static final int SHULKER_SIZE = 27;
// 预定义所有潜影盒材质(性能优化)
private static final Set<Material> SHULKER_BOX_MATERIALS = Set.of(
Material.SHULKER_BOX,
Material.WHITE_SHULKER_BOX,
@@ -48,105 +54,136 @@ public class ShulkerBoxListener implements Listener {
Material.BLACK_SHULKER_BOX
);
/**
* 潜影盒 Inventory Holder - 用于识别自定义 inventory
*/
private static class ShulkerBoxHolder implements org.bukkit.inventory.InventoryHolder {
private final Inventory inventory;
private final ItemStack shulkerBoxItem;
private final ItemStack[] currentContents; // 实时追踪的物品内容
private final EssentialsC plugin;
private final Map<UUID, OpenShulkerSession> openShulkerBoxes = new HashMap<>();
ShulkerBoxHolder(ItemStack shulkerBoxItem, String title) {
this.shulkerBoxItem = shulkerBoxItem;
this.inventory = Bukkit.createInventory(this, 27, title);
this.currentContents = new ItemStack[27];
private static final class ShulkerBoxHolder implements InventoryHolder {
private final Inventory inventory;
private ShulkerBoxHolder(String title) {
this.inventory = Bukkit.createInventory(this, SHULKER_SIZE, title);
}
@Override
public @NotNull Inventory getInventory() {
public Inventory getInventory() {
return this.inventory;
}
public ItemStack getShulkerBoxItem() {
return shulkerBoxItem;
}
/**
* 更新指定槽位的物品(供外部调用)
*/
public void updateSlot(int slot, ItemStack item) {
if (slot >= 0 && slot < 27) {
currentContents[slot] = item;
}
}
private static final class OpenShulkerSession {
private final ItemStack sourceItem;
private final EquipmentSlot sourceHand;
private final int preferredSlot;
/**
* 获取当前追踪的所有物品内容
*/
public ItemStack[] getCurrentContents() {
return currentContents.clone();
}
}
/**
* 潜影盒数据记录
*/
private static class ShulkerBoxData {
int slotIndex; // 玩家背包中的槽位索引
ItemStack originalSnapshot; // 打开时的物品快照(用于验证)
int totalItems; // 打开时的物品总数(用于防刷)
ShulkerBoxData(int slot, ItemStack snapshot, int items) {
this.slotIndex = slot;
this.originalSnapshot = snapshot;
this.totalItems = items;
private OpenShulkerSession(ItemStack sourceItem, EquipmentSlot sourceHand, int preferredSlot) {
this.sourceItem = sourceItem;
this.sourceHand = sourceHand;
this.preferredSlot = preferredSlot;
}
}
public ShulkerBoxListener(EssentialsC plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
// 只处理右键点击空气或方块的事件
if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
Player player = event.getPlayer();
// 检查权限
if (!player.hasPermission("essentialsc.shulkerbox.open")) {
if (!player.isSneaking() || !player.hasPermission("essentialsc.shulkerbox.open")) {
return;
}
ItemStack item = event.getItem();
if (item == null || !isShulkerBox(item)) {
if (openShulkerBoxes.containsKey(player.getUniqueId())) {
return;
}
// 只有潜行+右键才打开潜影盒
if (!player.isSneaking()) {
EquipmentSlot hand = event.getHand() == EquipmentSlot.OFF_HAND ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND;
ItemStack sourceItem = getItemFromHand(player, hand);
if (!isShulkerBox(sourceItem)) {
return;
}
// 取消默认行为(防止放置潜影盒)
if (!(sourceItem.getItemMeta() instanceof BlockStateMeta blockStateMeta)) {
return;
}
if (!(blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox)) {
return;
}
event.setUseItemInHand(org.bukkit.event.Event.Result.DENY);
event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
ItemStack sourceSnapshot = sourceItem.clone();
plugin.getServer().getScheduler().runTask(plugin, () -> {
if (!player.isOnline() || openShulkerBoxes.containsKey(player.getUniqueId())) {
return;
}
openShulkerBox(player, hand, sourceSnapshot, shulkerBox);
});
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) {
return;
}
InventoryView view = event.getView();
Inventory topInventory = view.getTopInventory();
if (!(topInventory.getHolder(false) instanceof ShulkerBoxHolder)) {
return;
}
int topSize = topInventory.getSize();
boolean clickTopInventory = event.getRawSlot() >= 0 && event.getRawSlot() < topSize;
if (clickTopInventory && isShulkerBox(event.getCursor())) {
event.setCancelled(true);
sendNestedMessage(player);
return;
}
// 查找物品在玩家背包中的槽位
int slotIndex = -1;
for (int i = 0; i < player.getInventory().getSize(); i++) {
ItemStack invItem = player.getInventory().getItem(i);
if (invItem != null && invItem.isSimilar(item)) {
slotIndex = i;
break;
if (clickTopInventory && event.getClick() == ClickType.NUMBER_KEY) {
ItemStack hotbarItem = player.getInventory().getItem(event.getHotbarButton());
if (isShulkerBox(hotbarItem)) {
event.setCancelled(true);
sendNestedMessage(player);
return;
}
}
// 打开潜影盒(传入槽位索引)
openShulkerBox(player, item, slotIndex);
if (event.isShiftClick() && isShulkerBox(event.getCurrentItem())) {
event.setCancelled(true);
sendNestedMessage(player);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryDrag(InventoryDragEvent event) {
InventoryView view = event.getView();
Inventory topInventory = view.getTopInventory();
if (!(topInventory.getHolder(false) instanceof ShulkerBoxHolder)) {
return;
}
if (!isShulkerBox(event.getOldCursor())) {
return;
}
int topSize = topInventory.getSize();
for (int rawSlot : event.getRawSlots()) {
if (rawSlot >= 0 && rawSlot < topSize) {
event.setCancelled(true);
if (event.getWhoClicked() instanceof Player player) {
sendNestedMessage(player);
}
return;
}
}
}
@EventHandler
@@ -155,202 +192,131 @@ public class ShulkerBoxListener implements Listener {
return;
}
Inventory closedInventory = event.getInventory();
// 检查是否是潜影盒 inventory
if (!(closedInventory.getHolder(false) instanceof ShulkerBoxHolder holder)) {
return;
}
UUID playerId = player.getUniqueId();
ShulkerBoxData data = openShulkerBoxes.remove(playerId);
if (data == null) {
return;
}
plugin.getLogger().info("=== 潜影盒关闭(数据已在点击时实时保存) ===");
commitOpenShulker(player, event.getInventory());
}
@EventHandler
public void onInventoryOpen(org.bukkit.event.inventory.InventoryOpenEvent event) {
if (!(event.getPlayer() instanceof Player player)) {
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
commitOpenShulker(player, player.getOpenInventory().getTopInventory());
}
@EventHandler
public void onPluginDisable(PluginDisableEvent event) {
if (event.getPlugin() != plugin) {
return;
}
Inventory openedInventory = event.getInventory();
if (openedInventory.getHolder(false) instanceof ShulkerBoxHolder) {
plugin.getLogger().info("[Open] ✅ 潜影盒 inventory 已打开");
for (Player player : Bukkit.getOnlinePlayers()) {
commitOpenShulker(player, player.getOpenInventory().getTopInventory());
}
}
@EventHandler(priority = org.bukkit.event.EventPriority.LOWEST)
public void onInventoryClickDebug(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) {
private void commitOpenShulker(Player player, Inventory inventory) {
if (!(inventory.getHolder(false) instanceof ShulkerBoxHolder)) {
return;
}
Inventory clickedInventory = event.getClickedInventory();
if (clickedInventory == null) {
OpenShulkerSession session = openShulkerBoxes.remove(player.getUniqueId());
if (session == null) {
return;
}
if (clickedInventory.getHolder(false) instanceof ShulkerBoxHolder) {
plugin.getLogger().info("[Click-LOWEST] 检测到点击事件 | 槽位: " + event.getSlot() +
" | 物品: " + (event.getCurrentItem() != null ? event.getCurrentItem().getType() : "null"));
ItemStack updatedShulker = session.sourceItem.clone();
writeInventoryBack(updatedShulker, inventory.getContents());
restoreItemToPlayer(player, session, updatedShulker);
}
private void openShulkerBox(Player player, EquipmentSlot hand, ItemStack sourceItem, ShulkerBox shulkerBox) {
removeItemFromHand(player, hand);
ShulkerBoxHolder holder = new ShulkerBoxHolder(resolveTitle(sourceItem));
holder.getInventory().setContents(cloneContents(shulkerBox.getInventory().getContents()));
int preferredSlot = hand == EquipmentSlot.HAND ? player.getInventory().getHeldItemSlot() : -1;
openShulkerBoxes.put(player.getUniqueId(), new OpenShulkerSession(sourceItem, hand, preferredSlot));
player.openInventory(holder.getInventory());
}
if (!(event.getWhoClicked() instanceof Player player)) {
private void writeInventoryBack(ItemStack shulkerItem, ItemStack[] contents) {
if (!(shulkerItem.getItemMeta() instanceof BlockStateMeta blockStateMeta)) {
plugin.getLogger().warning("Failed to save shulker box contents: missing BlockStateMeta.");
return;
}
Inventory clickedInventory = event.getClickedInventory();
if (clickedInventory == null) {
if (!(blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox)) {
plugin.getLogger().warning("Failed to save shulker box contents: block state is not a ShulkerBox.");
return;
}
// 检查是否是潜影盒 inventory
if (!(clickedInventory.getHolder(false) instanceof ShulkerBoxHolder holder)) {
return;
}
UUID playerId = player.getUniqueId();
ShulkerBoxData data = openShulkerBoxes.get(playerId);
if (data == null) {
return;
}
// 阻止嵌套潜影盒
ItemStack clickedItem = event.getCurrentItem();
if (clickedItem != null && isShulkerBox(clickedItem)) {
event.setCancelled(true);
player.sendMessage(EssentialsC.getLangManager().getString("prefix") +
EssentialsC.getLangManager().getString("messages.shulkerbox-nested"));
return;
}
// ✅ CMILib 方式:延迟 1 tick 后立即保存到潜影盒 NBT
plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.getLogger().info("[Click] === 开始处理点击事件 ===");
// 读取当前 inventory 内容
ItemStack[] contents = clickedInventory.getContents();
// 调试
int nonEmpty = 0;
StringBuilder items = new StringBuilder();
for (int i = 0; i < contents.length; i++) {
if (contents[i] != null && !contents[i].getType().isAir()) {
nonEmpty++;
items.append(String.format("\n [%d] %s x%d", i, contents[i].getType(), contents[i].getAmount()));
}
}
plugin.getLogger().info("[Click] 非空槽位: " + nonEmpty);
plugin.getLogger().info("[Click] 物品详情:" + items);
// 获取玩家背包中的潜影盒物品
ItemStack shulkerItem = null;
if (data.slotIndex >= 0 && data.slotIndex < player.getInventory().getSize()) {
shulkerItem = player.getInventory().getItem(data.slotIndex);
plugin.getLogger().info("[Click] 槽位 " + data.slotIndex + " 物品: " +
(shulkerItem != null ? shulkerItem.getType() : "null"));
}
if (shulkerItem == null || shulkerItem.getType().isAir()) {
plugin.getLogger().warning("[Click] ❌ 潜影盒物品不存在");
return;
}
// 更新潜影盒的 BlockState
if (shulkerItem.getItemMeta() instanceof BlockStateMeta blockStateMeta) {
if (blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox) {
// 设置 inventory 内容
for (int i = 0; i < 27 && i < contents.length; i++) {
shulkerBox.getInventory().setItem(i, contents[i]);
}
// 更新元数据
shulkerBox.getInventory().setContents(cloneContents(contents));
blockStateMeta.setBlockState(shulkerBox);
shulkerItem.setItemMeta(blockStateMeta);
// 写回玩家背包
player.getInventory().setItem(data.slotIndex, shulkerItem);
plugin.getLogger().info("[Click] ✅ 已实时保存 " + nonEmpty + " 个物品槽");
} else {
plugin.getLogger().warning("[Click] ❌ BlockState 不是 ShulkerBox");
}
} else {
plugin.getLogger().warning("[Click] ❌ 没有 BlockStateMeta");
}
});
}
private void restoreItemToPlayer(Player player, OpenShulkerSession session, ItemStack shulkerItem) {
PlayerInventory inventory = player.getInventory();
/**
* 检查物品是否为潜影盒O(1) 时间复杂度)
*/
private boolean isShulkerBox(ItemStack item) {
return SHULKER_BOX_MATERIALS.contains(item.getType());
if (session.sourceHand == EquipmentSlot.OFF_HAND) {
if (isEmpty(inventory.getItemInOffHand())) {
inventory.setItemInOffHand(shulkerItem);
return;
}
/**
* 打开潜影盒
*/
private void openShulkerBox(Player player, ItemStack shulkerBox, int slotIndex) {
// 获取潜影盒的 BlockStateMeta
if (!(shulkerBox.getItemMeta() instanceof BlockStateMeta blockStateMeta)) {
} else if (session.preferredSlot >= 0 && isEmpty(inventory.getItem(session.preferredSlot))) {
inventory.setItem(session.preferredSlot, shulkerItem);
return;
}
// 获取潜影盒的方块状态
if (!(blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBoxBlock)) {
return;
}
// 创建物品快照(用于后续验证)
ItemStack snapshot = shulkerBox.clone();
// 计算当前物品总数(用于防刷检查)
int totalItems = 0;
for (ItemStack item : shulkerBoxBlock.getInventory().getContents()) {
if (item != null && !item.getType().isAir()) {
totalItems += item.getAmount();
Map<Integer, ItemStack> leftovers = inventory.addItem(shulkerItem);
for (ItemStack leftover : leftovers.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), leftover);
}
}
// 获取潜影盒的自定义名称,如果没有则使用配置中的默认标题
String title;
private ItemStack[] cloneContents(ItemStack[] contents) {
ItemStack[] copied = new ItemStack[SHULKER_SIZE];
for (int i = 0; i < SHULKER_SIZE && i < contents.length; i++) {
copied[i] = contents[i] == null ? null : contents[i].clone();
}
return copied;
}
private ItemStack getItemFromHand(Player player, EquipmentSlot hand) {
return hand == EquipmentSlot.OFF_HAND
? player.getInventory().getItemInOffHand()
: player.getInventory().getItemInMainHand();
}
private void removeItemFromHand(Player player, EquipmentSlot hand) {
if (hand == EquipmentSlot.OFF_HAND) {
player.getInventory().setItemInOffHand(null);
} else {
player.getInventory().setItem(player.getInventory().getHeldItemSlot(), null);
}
}
private String resolveTitle(ItemStack shulkerBox) {
if (shulkerBox.hasItemMeta() && shulkerBox.getItemMeta().hasDisplayName()) {
// 使用潜影盒的自定义名称
title = shulkerBox.getItemMeta().getDisplayName();
} else {
// 使用配置文件中的默认标题
return shulkerBox.getItemMeta().getDisplayName();
}
String defaultTitle = plugin.getConfig().getString("shulkerbox.default-title", "");
if (defaultTitle != null && !defaultTitle.isEmpty()) {
// 转换颜色代码 & -> §
title = defaultTitle.replace('&', '§');
} else {
// 如果配置为空,使用 "Shulker Box"(客户端会自动翻译)
title = "Shulker Box";
return ChatColor.translateAlternateColorCodes('&', defaultTitle);
}
return "Shulker Box";
}
// 创建 ShulkerBoxHolder会自动创建 inventory
ShulkerBoxHolder holder = new ShulkerBoxHolder(shulkerBox, title);
Inventory inventory = holder.getInventory();
// 复制潜影盒的内容到 inventory
ItemStack[] contents = shulkerBoxBlock.getInventory().getContents();
for (int i = 0; i < 27 && i < contents.length; i++) {
inventory.setItem(i, contents[i]);
private void sendNestedMessage(Player player) {
player.sendMessage(EssentialsC.getLangManager().getString("prefix")
+ EssentialsC.getLangManager().getString("messages.shulkerbox-nested"));
}
// 记录玩家打开的潜影盒(保存槽位索引和快照)
openShulkerBoxes.put(player.getUniqueId(), new ShulkerBoxData(slotIndex, snapshot, totalItems));
private boolean isShulkerBox(ItemStack item) {
return item != null && !item.getType().isAir() && SHULKER_BOX_MATERIALS.contains(item.getType());
}
// 打开 inventory
player.openInventory(inventory);
private boolean isEmpty(ItemStack item) {
return item == null || item.getType().isAir();
}
}

View File

@@ -5,7 +5,7 @@ version: ${version}
main: cn.infstar.essentialsC.EssentialsC
api-version: '1.21'
load: POSTWORLD
folia-supported: true
folia-supported: false
authors: [ Coldsmiles_7 ]
website: www.infstar.cn
@@ -62,6 +62,9 @@ permissions:
essentialsc.command.repair:
description: Allows use of /repair command
default: op
essentialsc.command.repair.all:
description: Allows use of /repair all
default: op
essentialsc.command.blocks:
description: Allows use of /essc blocks command
default: true
@@ -99,6 +102,7 @@ permissions:
essentialsc.command.reload: true
essentialsc.command.feed: true
essentialsc.command.repair: true
essentialsc.command.repair.all: true
essentialsc.command.help: true
essentialsc.shulkerbox.open: true
essentialsc.mobdrops.enderman: true