├── settings.gradle ├── gradle.properties ├── res ├── plugin.yml └── command.ordn ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── src └── redempt │ └── plugwoman │ ├── Version.java │ ├── PluginEnableErrorHandler.java │ ├── PluginJarCache.java │ ├── CommandListener.java │ └── PlugWoman.java ├── gradlew.bat ├── .gitignore └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'PlugWoman' -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | serverDir=/home/redempt/Desktop/Spigot/1.15/plugins -------------------------------------------------------------------------------- /res/plugin.yml: -------------------------------------------------------------------------------- 1 | name: PlugWoman 2 | main: redempt.plugwoman.PlugWoman 3 | version: '${pluginVersion}' 4 | author: Redempt 5 | api-version: 1.13 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/redempt/plugwoman/Version.java: -------------------------------------------------------------------------------- 1 | package redempt.plugwoman; 2 | 3 | import org.bukkit.Bukkit; 4 | 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | public class Version { 9 | 10 | public static final int MID_VERSION = getMidVersion(); 11 | 12 | private static int getMidVersion() { 13 | Pattern pattern = Pattern.compile("1\\.([0-9]+)"); 14 | Matcher matcher = pattern.matcher(Bukkit.getBukkitVersion()); 15 | matcher.find(); 16 | return Integer.parseInt(matcher.group(1)); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /res/command.ordn: -------------------------------------------------------------------------------- 1 | plug,plugwoman { 2 | help = Manage plugins 3 | permission = plugwoman.admin 4 | reloadcommands,rlcmds { 5 | help = Reloads all commands on the server 6 | hook = reloadcommands 7 | permission = plugwoman.admin.reloadcommands 8 | } 9 | enable plugin[]:plugin { 10 | help = Enable a plugin 11 | permission = plugwoman.admin.enable 12 | hook = enable 13 | } 14 | disable plugin[]:plugin { 15 | help = Disable a plugin 16 | permission = plugwoman.admin.disable 17 | hook = disable 18 | } 19 | load jar[]:jarname { 20 | help = Load a plugin from a jar 21 | permission = plugwoman.admin.load 22 | hook = load 23 | } 24 | unload plugin[]:plugin { 25 | help = Unload a plugin 26 | permission = plugwoman.admin.unload 27 | hook = unload 28 | } 29 | delcmd command[]:command { 30 | help = Unregisters a command from the command map 31 | permission = plugwoman.admin.delcmd 32 | hook = delcmd 33 | } 34 | reload plugin[]:plugin --nodeep,-d --noconfirm,-c { 35 | help = Reload a plugin and all its dependents 36 | help = Pass --nodeep to only reload the target plugin 37 | help = Pass --noconfirm to skip the confirmation 38 | permission = plugwoman.admin.reload 39 | hook = reload 40 | } 41 | confirm { 42 | help = Confirm a reload 43 | permission = plugwoman.admin.reload 44 | hook = confirm 45 | } 46 | } -------------------------------------------------------------------------------- /src/redempt/plugwoman/PluginEnableErrorHandler.java: -------------------------------------------------------------------------------- 1 | package redempt.plugwoman; 2 | 3 | import org.bukkit.Bukkit; 4 | 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | import java.util.function.BiConsumer; 8 | import java.util.logging.Handler; 9 | import java.util.logging.Level; 10 | import java.util.logging.LogRecord; 11 | 12 | public class PluginEnableErrorHandler { 13 | 14 | private static Handler handler; 15 | private static Set> pluginErrorListeners = new HashSet<>(); 16 | 17 | public static void register() { 18 | handler = new Handler() { 19 | public void publish(LogRecord record) { 20 | if (record.getLevel() == Level.SEVERE) { 21 | String msg = record.getMessage(); 22 | if (msg.startsWith("Error occurred while enabling ")) { 23 | String name = msg.substring(30); 24 | String fname = name.substring(0, name.indexOf(' ')); 25 | pluginErrorListeners.forEach(c -> { 26 | c.accept(fname, record.getThrown()); 27 | }); 28 | } 29 | } 30 | } 31 | public void flush() {} 32 | public void close() throws SecurityException {} 33 | }; 34 | Bukkit.getServer().getLogger().addHandler(handler); 35 | } 36 | 37 | public static void unregister() { 38 | Bukkit.getServer().getLogger().removeHandler(handler); 39 | } 40 | 41 | public static void addListener(BiConsumer listener) { 42 | pluginErrorListeners.add(listener); 43 | } 44 | 45 | public static void removeListener(BiConsumer listener) { 46 | pluginErrorListeners.remove(listener); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/redempt/plugwoman/PluginJarCache.java: -------------------------------------------------------------------------------- 1 | package redempt.plugwoman; 2 | 3 | import org.bukkit.plugin.InvalidDescriptionException; 4 | import org.bukkit.plugin.Plugin; 5 | import org.bukkit.plugin.PluginDescriptionFile; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.net.URISyntaxException; 10 | import java.nio.file.Files; 11 | import java.nio.file.Path; 12 | import java.nio.file.Paths; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import java.util.jar.JarFile; 16 | import java.util.zip.ZipEntry; 17 | 18 | public class PluginJarCache { 19 | 20 | private static Map pluginMap = null; 21 | 22 | public static void populate() { 23 | pluginMap = new HashMap<>(); 24 | try { 25 | Files.list(Paths.get("plugins")).forEach(p -> { 26 | if (!p.toString().endsWith(".jar")) { 27 | return; 28 | } 29 | try { 30 | JarFile jar = new JarFile(p.toFile()); 31 | ZipEntry entry = jar.getEntry("plugin.yml"); 32 | if (entry == null) { 33 | return; 34 | } 35 | InputStream stream = jar.getInputStream(entry); 36 | PluginDescriptionFile description = new PluginDescriptionFile(stream); 37 | String name = description.getName(); 38 | pluginMap.put(name, p); 39 | } catch (IOException | InvalidDescriptionException e) { 40 | e.printStackTrace(); 41 | } 42 | }); 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | 48 | public static void clear() { 49 | pluginMap = null; 50 | } 51 | 52 | public static Path getJarPath(Plugin plugin) { 53 | try { 54 | Path path = Paths.get(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); 55 | if (Files.exists(path)) { 56 | return path; 57 | } 58 | if (pluginMap != null) { 59 | return pluginMap.get(plugin.getName()); 60 | } 61 | populate(); 62 | return pluginMap.get(plugin.getName()); 63 | } catch (URISyntaxException e) { 64 | e.printStackTrace(); 65 | return null; 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/java,intellij 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=java,intellij 4 | 5 | ### Intellij ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/artifacts 37 | # .idea/compiler.xml 38 | # .idea/jarRepositories.xml 39 | # .idea/modules.xml 40 | # .idea/*.iml 41 | # .idea/modules 42 | # *.iml 43 | # *.ipr 44 | 45 | build 46 | .idea 47 | .gradle 48 | 49 | # CMake 50 | cmake-build-*/ 51 | 52 | # Mongo Explorer plugin 53 | .idea/**/mongoSettings.xml 54 | 55 | # File-based project format 56 | *.iws 57 | 58 | # IntelliJ 59 | out/ 60 | 61 | # mpeltonen/sbt-idea plugin 62 | .idea_modules/ 63 | 64 | # JIRA plugin 65 | atlassian-ide-plugin.xml 66 | 67 | # Cursive Clojure plugin 68 | .idea/replstate.xml 69 | 70 | # Crashlytics plugin (for Android Studio and IntelliJ) 71 | com_crashlytics_export_strings.xml 72 | crashlytics.properties 73 | crashlytics-build.properties 74 | fabric.properties 75 | 76 | # Editor-based Rest Client 77 | .idea/httpRequests 78 | 79 | # Android studio 3.1+ serialized cache file 80 | .idea/caches/build_file_checksums.ser 81 | 82 | ### Intellij Patch ### 83 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 84 | 85 | # *.iml 86 | # modules.xml 87 | # .idea/misc.xml 88 | # *.ipr 89 | 90 | # Sonarlint plugin 91 | # https://plugins.jetbrains.com/plugin/7973-sonarlint 92 | .idea/**/sonarlint/ 93 | 94 | # SonarQube Plugin 95 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin 96 | .idea/**/sonarIssues.xml 97 | 98 | # Markdown Navigator plugin 99 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced 100 | .idea/**/markdown-navigator.xml 101 | .idea/**/markdown-navigator-enh.xml 102 | .idea/**/markdown-navigator/ 103 | 104 | # Cache file creation bug 105 | # See https://youtrack.jetbrains.com/issue/JBR-2257 106 | .idea/$CACHE_FILE$ 107 | 108 | # CodeStream plugin 109 | # https://plugins.jetbrains.com/plugin/12206-codestream 110 | .idea/codestream.xml 111 | 112 | ### Java ### 113 | # Compiled class file 114 | *.class 115 | 116 | # Log file 117 | *.log 118 | 119 | # BlueJ files 120 | *.ctxt 121 | 122 | # Mobile Tools for Java (J2ME) 123 | .mtj.tmp/ 124 | 125 | # Package Files # 126 | *.jar 127 | *.war 128 | *.nar 129 | *.ear 130 | *.zip 131 | *.tar.gz 132 | *.rar 133 | 134 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 135 | hs_err_pid* 136 | 137 | # End of https://www.toptal.com/developers/gitignore/api/java,intellij 138 | 139 | -------------------------------------------------------------------------------- /src/redempt/plugwoman/CommandListener.java: -------------------------------------------------------------------------------- 1 | package redempt.plugwoman; 2 | 3 | import org.bukkit.Bukkit; 4 | import org.bukkit.ChatColor; 5 | import org.bukkit.command.CommandSender; 6 | import org.bukkit.entity.Player; 7 | import org.bukkit.plugin.Plugin; 8 | import org.bukkit.plugin.SimplePluginManager; 9 | import redempt.ordinate.parser.metadata.CommandHook; 10 | 11 | import java.nio.file.Files; 12 | import java.nio.file.Path; 13 | import java.util.*; 14 | import java.util.stream.Collectors; 15 | 16 | public class CommandListener { 17 | 18 | private SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager(); 19 | private Map confirm = new HashMap<>(); 20 | 21 | @CommandHook("enable") 22 | public void enablePlugin(CommandSender sender, Plugin[] plugins) { 23 | for (Plugin plugin : plugins) { 24 | manager.enablePlugin(plugin); 25 | sender.sendMessage(ChatColor.GREEN + "Plugin " + plugin.getName() + " enabled!"); 26 | } 27 | } 28 | 29 | @CommandHook("disable") 30 | public void disablePlugin(CommandSender sender, Plugin[] plugins) { 31 | for (Plugin plugin : plugins) { 32 | manager.disablePlugin(plugin); 33 | sender.sendMessage(ChatColor.GREEN + "Plugin " + plugin.getName() + " disabled!"); 34 | } 35 | } 36 | 37 | @CommandHook("unload") 38 | public void unloadPlugin(CommandSender sender, Plugin[] plugins) { 39 | Map> commandsByPlugin = PlugWoman.getInstance().getCommandsByPlugin(); 40 | for (Plugin plugin : plugins) { 41 | PlugWoman.getInstance().unloadPlugin(plugin, commandsByPlugin); 42 | PlugWoman.getInstance().syncCommands(); 43 | sender.sendMessage(ChatColor.GREEN + "Plugin " + plugin.getName() + " unloaded!"); 44 | } 45 | } 46 | 47 | @CommandHook("reloadcommands") 48 | public void reloadCommands(CommandSender sender) { 49 | PlugWoman.getInstance().syncCommands(); 50 | sender.sendMessage(ChatColor.GREEN + "Server commands reloaded!"); 51 | } 52 | 53 | @CommandHook("load") 54 | public void loadPlugin(CommandSender sender, Path[] paths) { 55 | for (Path path : paths) { 56 | if (!Files.exists(path) || !path.toString().endsWith(".jar")) { 57 | sender.sendMessage(ChatColor.RED + "No such jar: " + path.getFileName().toString()); 58 | return; 59 | } 60 | String msg = PlugWoman.getInstance().loadPlugin(path).orElse(null); 61 | if (msg == null) { 62 | msg = ChatColor.GREEN + "Jar " + path.getFileName().toString() + " loaded!"; 63 | } else { 64 | msg = ChatColor.RED + msg; 65 | } 66 | PlugWoman.getInstance().syncCommands(); 67 | sender.sendMessage(msg); 68 | } 69 | } 70 | 71 | @CommandHook("reload") 72 | public void reload(CommandSender sender, boolean nodeep, boolean noconfirm, Plugin[] pluginArr) { 73 | PluginJarCache.clear(); 74 | List plugins; 75 | if (!nodeep) { 76 | plugins = PlugWoman.getInstance().getDeepReload(pluginArr); 77 | } else { 78 | plugins = new ArrayList<>(); 79 | Collections.addAll(plugins, pluginArr); 80 | } 81 | String list = plugins.stream().map(Plugin::getName).collect(Collectors.joining(", ")); 82 | sender.sendMessage(ChatColor.GREEN + "Plugins to reload: " + ChatColor.YELLOW + list); 83 | if (!noconfirm) { 84 | sender.sendMessage(ChatColor.GREEN + "Run /plug confirm to confirm reload"); 85 | sender.sendMessage(ChatColor.RED + "This confirmation will expire in 30 seconds"); 86 | confirm.put(sender, () -> reload(sender, plugins)); 87 | Bukkit.getScheduler().scheduleSyncDelayedTask(PlugWoman.getInstance(), () -> { 88 | confirm.remove(sender, plugins); 89 | }, 20 * 30); 90 | } else { 91 | reload(sender, plugins); 92 | } 93 | } 94 | 95 | private void reload(CommandSender sender, List plugins) { 96 | Map errors = PlugWoman.getInstance().reloadPlugins(plugins); 97 | if (errors.size() == 0) { 98 | sender.sendMessage(ChatColor.GREEN + "All plugins reloaded successfully!"); 99 | return; 100 | } 101 | sender.sendMessage(ChatColor.RED + "The following plugins could not be reloaded:"); 102 | errors.forEach((k, v) -> { 103 | sender.sendMessage(ChatColor.RED + k.getName() + ChatColor.YELLOW + ": " + v); 104 | }); 105 | } 106 | 107 | @CommandHook("confirm") 108 | public void confirm(CommandSender sender) { 109 | Runnable run = confirm.remove(sender); 110 | if (run == null) { 111 | sender.sendMessage(ChatColor.RED + "You have not queued an action!"); 112 | return; 113 | } 114 | run.run(); 115 | } 116 | 117 | @CommandHook("delcmd") 118 | public void unregisterCommand(CommandSender sender, String[] commands) { 119 | for (String command : commands) { 120 | if (command.equals("plug")) { 121 | sender.sendMessage(ChatColor.RED + "You cannot disable /plug!"); 122 | continue; 123 | } 124 | PlugWoman.getInstance().getCommandMap().remove(command); 125 | } 126 | sender.sendMessage(ChatColor.GREEN + "Commands unregistered!"); 127 | if (sender instanceof Player) { 128 | ((Player) sender).updateCommands(); 129 | } 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /src/redempt/plugwoman/PlugWoman.java: -------------------------------------------------------------------------------- 1 | package redempt.plugwoman; 2 | 3 | import org.bukkit.Bukkit; 4 | import org.bukkit.Keyed; 5 | import org.bukkit.NamespacedKey; 6 | import org.bukkit.command.Command; 7 | import org.bukkit.command.PluginCommandYamlParser; 8 | import org.bukkit.command.PluginIdentifiableCommand; 9 | import org.bukkit.command.SimpleCommandMap; 10 | import org.bukkit.event.Listener; 11 | import org.bukkit.plugin.*; 12 | import org.bukkit.plugin.java.JavaPlugin; 13 | import redempt.ordinate.command.ArgType; 14 | import redempt.ordinate.spigot.SpigotCommandManager; 15 | 16 | import java.io.IOException; 17 | import java.lang.reflect.Field; 18 | import java.lang.reflect.Method; 19 | import java.net.URLClassLoader; 20 | import java.nio.file.Files; 21 | import java.nio.file.Path; 22 | import java.nio.file.Paths; 23 | import java.util.*; 24 | import java.util.function.BiConsumer; 25 | 26 | public class PlugWoman extends JavaPlugin implements Listener { 27 | 28 | private SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager(); 29 | private Map commandMap; 30 | 31 | public static PlugWoman getInstance() { 32 | return JavaPlugin.getPlugin(PlugWoman.class); 33 | } 34 | 35 | @Override 36 | public void onEnable() { 37 | getServer().getPluginManager().registerEvents(this, this); 38 | SpigotCommandManager.getInstance(this).loadMessages() 39 | .getParser().setHookTargets(new CommandListener()) 40 | .addArgTypes( 41 | new ArgType<>("plugin", Bukkit.getPluginManager()::getPlugin) 42 | .completerStream((c, s) -> Arrays.stream(Bukkit.getPluginManager().getPlugins()).map(Plugin::getName)), 43 | new ArgType<>("jar", s -> Paths.get("plugins").resolve(s)) 44 | .completerStream((c, str) -> { 45 | try { 46 | return Files.list(Paths.get("plugins")).map(p -> p.getFileName().toString()).filter(s -> s.endsWith(".jar")); 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | return null; 50 | } 51 | }), 52 | new ArgType<>("command", s -> commandMap.containsKey(s) ? s : null).completerStream((c, s) -> commandMap.keySet().stream()) 53 | ).parse(getResource("command.ordn")).register(); 54 | PluginEnableErrorHandler.register(); 55 | Field commandMapField; 56 | try { 57 | commandMapField = manager.getClass().getDeclaredField("commandMap"); 58 | commandMapField.setAccessible(true); 59 | SimpleCommandMap commandMap = (SimpleCommandMap) commandMapField.get(manager); 60 | Class clazz = commandMap.getClass(); 61 | while (!clazz.getSimpleName().equals("SimpleCommandMap")) { 62 | clazz = clazz.getSuperclass(); 63 | } 64 | Field mapField = clazz.getDeclaredField("knownCommands"); 65 | mapField.setAccessible(true); 66 | this.commandMap = (Map) mapField.get(commandMap); 67 | } catch (NoSuchFieldException | IllegalAccessException e) { 68 | e.printStackTrace(); 69 | } 70 | } 71 | 72 | public Map getCommandMap() { 73 | return commandMap; 74 | } 75 | 76 | public Map> getCommandsByPlugin() { 77 | Map> map = new HashMap<>(); 78 | commandMap.values().stream().filter(PluginIdentifiableCommand.class::isInstance).forEach(c -> { 79 | Plugin plugin = ((PluginIdentifiableCommand) c).getPlugin(); 80 | map.computeIfAbsent(plugin, k -> new ArrayList<>()).add(c); 81 | }); 82 | return map; 83 | } 84 | 85 | @Override 86 | public void onDisable() { 87 | new Thread(() -> { 88 | try { 89 | Thread.sleep(5000); 90 | } catch (InterruptedException e) { 91 | e.printStackTrace(); 92 | } 93 | PluginEnableErrorHandler.unregister(); 94 | }).start(); 95 | } 96 | 97 | public Map> getDependencyMap() { 98 | Map> map = new HashMap<>(); 99 | for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { 100 | PluginDescriptionFile description = plugin.getDescription(); 101 | Set set = new HashSet<>(); 102 | for (String depend : description.getDepend()) { 103 | set.add(Bukkit.getPluginManager().getPlugin(depend)); 104 | } 105 | for (String depend : description.getSoftDepend()) { 106 | set.add(Bukkit.getPluginManager().getPlugin(depend)); 107 | } 108 | map.put(plugin, set); 109 | } 110 | return map; 111 | } 112 | 113 | public void unloadPlugin(Plugin plugin, Map> commandsByPlugin) { 114 | manager.disablePlugin(plugin); 115 | try { 116 | List commands = PluginCommandYamlParser.parse(plugin); 117 | commands.addAll(commandsByPlugin.getOrDefault(plugin, new ArrayList<>())); 118 | commands.forEach(c -> { 119 | commandMap.remove(c.getName()); 120 | c.getAliases().forEach(commandMap::remove); 121 | }); 122 | if (Version.MID_VERSION >= 9) { 123 | List toRemove = new ArrayList<>(); 124 | Bukkit.recipeIterator().forEachRemaining(recipe -> { 125 | if (recipe instanceof Keyed) { 126 | NamespacedKey key = ((Keyed) recipe).getKey(); 127 | if (key.getNamespace().equalsIgnoreCase(plugin.getName())) { 128 | toRemove.add(key); 129 | } 130 | } 131 | }); 132 | toRemove.forEach(Bukkit::removeRecipe); 133 | } 134 | List plugins = (List) accessible(manager.getClass().getDeclaredField("plugins")).get(manager); 135 | plugins.remove(plugin); 136 | Map lookupNames = (Map) accessible(manager.getClass().getDeclaredField("lookupNames")).get(manager);; 137 | lookupNames.remove(plugin.getName()); 138 | URLClassLoader loader = (URLClassLoader) plugin.getClass().getClassLoader(); 139 | if (Version.MID_VERSION < 13) { 140 | accessible(loader.getClass().getDeclaredField("plugin")).set(loader, null); 141 | accessible(loader.getClass().getDeclaredField("pluginInit")).set(loader, null); 142 | loader.close(); 143 | } 144 | } catch (Exception e) { 145 | e.printStackTrace(); 146 | } 147 | } 148 | 149 | private Field accessible(Field field) { 150 | field.setAccessible(true); 151 | return field; 152 | } 153 | 154 | public Optional loadPlugin(Path path) { 155 | try { 156 | if (path == null || !Files.exists(path)) { 157 | return Optional.of("Plugin jar does not exist"); 158 | } 159 | Plugin plugin = Bukkit.getPluginManager().loadPlugin(path.toFile()); 160 | try { 161 | plugin.onLoad(); 162 | String[] err = {null}; 163 | BiConsumer listener = (s, t) -> { 164 | if (s.equals(plugin.getName())) { 165 | StringBuilder builder = new StringBuilder("Error on enable: ").append(t.getClass().getSimpleName()); 166 | Throwable cause = t.getCause(); 167 | while (cause != null) { 168 | builder.append(" -> " + cause.getClass().getSimpleName()); 169 | cause = cause.getCause(); 170 | } 171 | err[0] = builder.toString(); 172 | } 173 | }; 174 | PluginEnableErrorHandler.addListener(listener); 175 | Bukkit.getPluginManager().enablePlugin(plugin); 176 | PluginEnableErrorHandler.removeListener(listener); 177 | if (err[0] != null) { 178 | return Optional.of(err[0]); 179 | } 180 | } catch (Exception e) { 181 | e.printStackTrace(); 182 | return Optional.of("Plugin could not be enabled"); 183 | } 184 | return Optional.empty(); 185 | } catch (UnknownDependencyException e) { 186 | e.printStackTrace(); 187 | return Optional.of("Plugin missing dependency"); 188 | } catch (InvalidDescriptionException e) { 189 | e.printStackTrace(); 190 | return Optional.of("Plugin has invalid plugin.yml"); 191 | } catch (InvalidPluginException e) { 192 | e.printStackTrace(); 193 | return Optional.of("Plugin is invalid"); 194 | } 195 | } 196 | 197 | public List getDeepReload(Plugin[] p) { 198 | Map> map = getDependencyMap(); 199 | HashSet set = new HashSet<>(); 200 | List toReload = new ArrayList<>(); 201 | Collections.addAll(set, p); 202 | Collections.addAll(toReload, p); 203 | for (int i = 0; i < toReload.size(); i++) { 204 | Plugin plugin = toReload.get(i); 205 | map.forEach((k, v) -> { 206 | if (v.contains(plugin)) { 207 | if (set.add(k)) { 208 | toReload.add(k); 209 | } 210 | } 211 | }); 212 | } 213 | boolean swap = false; 214 | do { 215 | swap = false; 216 | for (int i = 0; i < toReload.size(); i++) { 217 | Plugin plugin = toReload.get(i); 218 | for (Plugin depend : map.get(plugin)) { 219 | int first = toReload.indexOf(depend); 220 | if (first > i) { 221 | if (map.get(depend).contains(plugin) && map.get(plugin).contains(depend)) { 222 | continue; 223 | } 224 | toReload.set(first, plugin); 225 | toReload.set(i, depend); 226 | swap = true; 227 | } 228 | } 229 | } 230 | } while (swap); 231 | return toReload; 232 | } 233 | 234 | public void syncCommands() { 235 | if (Version.MID_VERSION >= 13) { 236 | try { 237 | Method method = getServer().getClass().getDeclaredMethod("syncCommands"); 238 | method.setAccessible(true); 239 | method.invoke(getServer()); 240 | } catch (Exception e) { 241 | throw new RuntimeException(e); 242 | } 243 | } 244 | } 245 | 246 | public Map reloadPlugins(List plugins) { 247 | Map> commandsByPlugin = getCommandsByPlugin(); 248 | for (int i = plugins.size() - 1; i >= 0; i--) { 249 | unloadPlugin(plugins.get(i), commandsByPlugin); 250 | } 251 | Map errors = new HashMap<>(); 252 | for (Plugin plugin : plugins) { 253 | loadPlugin(PluginJarCache.getJarPath(plugin)).ifPresent(s -> errors.put(plugin, s)); 254 | } 255 | syncCommands(); 256 | return errors; 257 | } 258 | 259 | } 260 | --------------------------------------------------------------------------------