├── .gitignore ├── .travis.yml ├── HEADER.txt ├── LICENSE.txt ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── modularframework-core ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── me4502 │ └── modularframework │ ├── ModuleController.java │ ├── exception │ ├── ModuleDisableException.java │ ├── ModuleEnableException.java │ └── ModuleNotInstantiatedException.java │ └── module │ ├── Module.java │ ├── ModuleWrapper.java │ └── guice │ └── ModuleContainer.java ├── modularframework-spigot ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── me4502 │ └── modularframework │ ├── BukkitModuleController.java │ ├── ModularFramework.java │ ├── ShadedModularFramework.java │ └── module │ ├── BukkitModuleWrapper.java │ └── guice │ └── ModuleInjector.java ├── modularframework-sponge ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── me4502 │ └── modularframework │ ├── ModularFramework.java │ ├── ShadedModularFramework.java │ ├── SpongeModuleController.java │ └── module │ ├── SpongeModuleWrapper.java │ └── guice │ ├── ModuleConfiguration.java │ └── ModuleInjector.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.war 8 | *.ear 9 | 10 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 11 | hs_err_pid* 12 | 13 | *.iml 14 | .idea 15 | .gradle 16 | build 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: java 4 | jdk: 5 | - oraclejdk8 6 | 7 | env: GRADLE_OPTS="-Xms1g -Xmx3g" 8 | install: true 9 | script: ./gradlew build 10 | 11 | notifications: 12 | email: false 13 | -------------------------------------------------------------------------------- /HEADER.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) Me4502 (Madeline Miller) 2 | Copyright (c) Contributors 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Me4502 (Madeline Miller) 4 | Copyright (c) Contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ModularFramework [![Build Status](https://travis-ci.org/me4502/ModularFramework.svg?branch=master)](https://travis-ci.org/me4502/ModularFramework) [ ![Download](https://api.bintray.com/packages/me4502/maven/ModularFramework/images/download.svg) ](https://bintray.com/me4502/maven/ModularFramework/_latestVersion) 2 | ============= 3 | A modular plugin framework built for Sponge. 4 | ## Features 5 | * Lightweight and small module framework. 6 | * Lazy-loading of classes, unused modules are not loaded. 7 | 8 | ## Prerequisites 9 | * [Java] 8 10 | * Sponge 11 | 12 | ## Building 13 | __Note:__ If you do not have [Gradle] installed then use ./gradlew for Unix systems or Git Bash and gradlew.bat for Windows systems in place of any 'gradle' command. 14 | 15 | In order to build ModularFramework you simply need to run the `gradle build` command. You can find the compiled JAR file in `./build/libs` labeled similarly to 'ModularFramework-x.x.x-SNAPSHOT.jar'. 16 | 17 | ## Getting ModularFramework 18 | ### Using Gradle or Maven 19 | ModularFramework is available on the following maven repository, 20 | ``` 21 | https://dl.bintray.com/me4502/maven 22 | ``` 23 | 24 | The following line will add the repository to gradle when placed in the repositories closure, 25 | ``` 26 | maven { url 'https://dl.bintray.com/me4502/maven' } 27 | ``` 28 | 29 | The following line will add the dependency when placed in the dependencies closure, 30 | ``` 31 | compile 'com.me4502:ModularFramework:VERSION' 32 | ``` 33 | 34 | ### Direct Download 35 | ModularFramework can be downloaded [here](https://bintray.com/me4502/maven/ModularFramework/_latestVersion). This version can be used as both a plugin and a library. 36 | 37 | ## Using ModularFramework 38 | To get started with ModularFramework, call the static method in the ModularFramework class titled registerModuleController. From here, you have access to methods to register modules annotated with an @Module annotation. 39 | 40 | ### Using ModularFramework as a shaded dependency 41 | When shading ModularFramework, it is important to do a relocate on the shade. Inside the relocate however, the ModularFramework class should be excluded. Failure to do so will remove some crucial functionality. 42 | ``` 43 | relocate ("com.me4502.modularframework", "your.plugin.package.modularframework") { 44 | include(dependency("com.me4502:ModularFramework")) 45 | exclude "com.me4502.modularframework.ModularFramework" 46 | } 47 | ``` 48 | 49 | Outside of the relocate closure, the file that was excluded still needs to be removed from the jar. The following line accomplishes that. 50 | ``` 51 | exclude "com/me4502/modularframework/ModularFramework.class" 52 | ``` 53 | Ensure that the exclude statement occurs **after** the relocate statement. 54 | 55 | From this point, continue to use the library as you normally would, except with ShadedModularFramework instead of ModularFramework. 56 | 57 | ## Plugins using ModularFramework 58 | * CraftBook 59 | 60 | Have a plugin using ModularFramework? Contact me and I'll add it. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "java" 3 | id "maven" 4 | id "maven-publish" 5 | id "eclipse" 6 | id "idea" 7 | id "signing" 8 | id "com.github.hierynomus.license" version "0.12.1" 9 | id "com.jfrog.bintray" version "1.7.3" 10 | } 11 | 12 | defaultTasks 'licenseFormat', 'build' 13 | 14 | allprojects { 15 | project.ext.versionNumber = '1.9.4' 16 | 17 | group = 'com.me4502' 18 | version = project.ext.versionNumber + '-SNAPSHOT' 19 | } 20 | 21 | subprojects { 22 | apply plugin: "java" 23 | apply plugin: "maven" 24 | apply plugin: "maven-publish" 25 | apply plugin: "signing" 26 | apply plugin: "com.github.hierynomus.license" 27 | apply plugin: "com.jfrog.bintray" 28 | 29 | sourceCompatibility = 1.8 30 | 31 | repositories { 32 | mavenLocal() 33 | mavenCentral() 34 | jcenter() 35 | maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" } 36 | } 37 | 38 | dependencies { 39 | testCompile group: 'junit', name: 'junit', version: '4.11' 40 | } 41 | 42 | task javadocJar(type: Jar, dependsOn: javadoc) { 43 | classifier = 'javadoc' 44 | from javadoc.destinationDir 45 | } 46 | 47 | task sourceJar(type: Jar) { 48 | classifier = 'sources' 49 | from sourceSets.main.allSource 50 | } 51 | 52 | artifacts { 53 | archives sourceJar 54 | archives javadocJar 55 | } 56 | 57 | signing { 58 | required false 59 | sign configurations.archives 60 | } 61 | 62 | license { 63 | ext { 64 | year = Calendar.getInstance().get(Calendar.YEAR) 65 | } 66 | 67 | header = file('../HEADER.txt') 68 | include '**/*.java' 69 | 70 | ignoreFailures = false 71 | strictCheck = true 72 | 73 | mapping { 74 | java = 'SLASHSTAR_STYLE' 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me4502/ModularFramework/16027f8414888e90d2caf932d27acaaf922c3b9b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Jan 29 17:09:44 AEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save ( ) { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /modularframework-core/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile 'com.google.inject:guice:4.0' 3 | } 4 | 5 | publishing { 6 | publications { 7 | MyPublication(MavenPublication) { 8 | from components.java 9 | artifact sourceJar 10 | artifact javadocJar 11 | groupId 'com.me4502' 12 | artifactId 'modularframework-core' 13 | version versionNumber 14 | } 15 | } 16 | } 17 | 18 | bintray { 19 | user = System.getProperty('BINTRAY_USER') 20 | key = System.getProperty('BINTRAY_KEY') 21 | publications = ['MyPublication'] 22 | pkg { 23 | repo = 'maven' 24 | name = 'modularframework-core' 25 | licenses = ['MIT'] 26 | vcsUrl = 'https://github.com/me4502/ModularFramework.git' 27 | publicDownloadNumbers = true 28 | version { 29 | name = 'modularframework-core-' + versionNumber 30 | desc = name 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/ModuleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import com.me4502.modularframework.exception.ModuleDisableException; 26 | import com.me4502.modularframework.exception.ModuleEnableException; 27 | import com.me4502.modularframework.module.Module; 28 | import com.me4502.modularframework.module.ModuleWrapper; 29 | 30 | import java.io.IOException; 31 | import java.lang.reflect.InvocationTargetException; 32 | import java.util.Collections; 33 | import java.util.LinkedHashSet; 34 | import java.util.Optional; 35 | import java.util.Set; 36 | import java.util.function.Predicate; 37 | 38 | /** 39 | * A per-plugin class for managing registered modules. 40 | */ 41 | public abstract class ModuleController { 42 | 43 | private final T plugin; 44 | 45 | public final Set moduleSet = new LinkedHashSet<>(); 46 | 47 | /** 48 | * Constructs a new ModuleController. 49 | * 50 | * @param plugin The plugin instance. 51 | */ 52 | public ModuleController(T plugin) { 53 | this.plugin = plugin; 54 | } 55 | 56 | public T getPlugin() { 57 | return this.plugin; 58 | } 59 | 60 | /** 61 | * Register a class to be a module. 62 | * @param clazz The class containing the module. 63 | */ 64 | @Deprecated 65 | public abstract void registerModule(Class clazz); 66 | 67 | /** 68 | * Register a class to be a module. 69 | * @param className The name of the class to use. 70 | */ 71 | public abstract void registerModule(String className); 72 | 73 | /** 74 | * Gets an immutable set of all registered modules in this controller. 75 | * @return A set of registered modules 76 | */ 77 | public Set getModules() { 78 | return Collections.unmodifiableSet(this.moduleSet); 79 | } 80 | 81 | /** 82 | * Gets the {@link ModuleWrapper} of the specified class, if it exists. 83 | * 84 | * @param clazz The class of the {@link Module} 85 | * @param The module type. 86 | * @return The {@link ModuleWrapper}, if it exists 87 | */ 88 | public Optional> getModule(Class clazz) { 89 | for(ModuleWrapper wrapper : this.moduleSet) { 90 | try { 91 | if(clazz.isInstance(wrapper.getModuleClass().getClass())) { 92 | return Optional.of(wrapper); 93 | } 94 | } catch (ClassNotFoundException ignored) { 95 | } 96 | } 97 | 98 | return Optional.empty(); 99 | } 100 | 101 | /** 102 | * Gets the {@link ModuleWrapper} by its id, if it exists. 103 | * 104 | * @param moduleId The id of the {@link Module} 105 | * @return The {@link ModuleWrapper}, if it exists 106 | */ 107 | public Optional getModule(String moduleId) { 108 | return this.moduleSet.stream().filter(wrapper -> wrapper.getId().equals(moduleId)).findFirst(); 109 | } 110 | 111 | /** 112 | * Enable all registered modules in this controller. 113 | */ 114 | public void enableModules() { 115 | enableModules(wrapper -> true); 116 | } 117 | 118 | /** 119 | * Enable registered modules if they pass a supplied predicate. 120 | * @param modulePredicate The predicate to test if a module is enabled 121 | */ 122 | public void enableModules(Predicate modulePredicate) { 123 | this.moduleSet.stream().filter(modulePredicate).forEach(wrapper -> { 124 | try { 125 | wrapper.enableModule(); 126 | } catch (Throwable e) { 127 | e.addSuppressed(new ModuleEnableException("Failed to enable module " + wrapper.getName())); 128 | e.printStackTrace(); 129 | } 130 | }); 131 | } 132 | 133 | /** 134 | * Disable all registered modules in this controller. 135 | */ 136 | public void disableModules() { 137 | disableModules(wrapper -> true); 138 | } 139 | 140 | /** 141 | * Disable registered modules if they pass a supplied predicate. 142 | * @param modulePredicate The predicate to test if a module is disabled 143 | */ 144 | public void disableModules(Predicate modulePredicate) { 145 | this.moduleSet.stream().filter(ModuleWrapper::isEnabled).filter(modulePredicate).forEach(wrapper -> { 146 | try { 147 | wrapper.disableModule(); 148 | } catch (Throwable e) { 149 | e.addSuppressed(new ModuleDisableException("Failed to disable module " + wrapper.getName())); 150 | e.printStackTrace(); 151 | } 152 | }); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/exception/ModuleDisableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.exception; 24 | 25 | public class ModuleDisableException extends Exception { 26 | 27 | public ModuleDisableException(String message) { 28 | super(message); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/exception/ModuleEnableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.exception; 24 | 25 | public class ModuleEnableException extends Exception { 26 | 27 | public ModuleEnableException(String message) { 28 | super(message); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/exception/ModuleNotInstantiatedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.exception; 24 | 25 | public class ModuleNotInstantiatedException extends Exception { 26 | } 27 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/module/Module.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module; 24 | 25 | import java.lang.annotation.ElementType; 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.RetentionPolicy; 28 | import java.lang.annotation.Target; 29 | 30 | /** 31 | * Marks a class as a Module. Any class that is a module is capable of being loaded with this framework. 32 | */ 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target(ElementType.TYPE) 35 | public @interface Module { 36 | 37 | /** 38 | * The id of this module. This is required. 39 | * @return The id of this module 40 | */ 41 | String id(); 42 | 43 | /** 44 | * The name of this module. 45 | * @return The name of this module 46 | */ 47 | String name() default ""; 48 | 49 | /** 50 | * The version of this module. 51 | * @return The version of this module 52 | */ 53 | String version() default "1.0.0"; 54 | 55 | /** 56 | * The authors of this module. 57 | * @return The authors of this module 58 | */ 59 | String[] authors() default {}; 60 | 61 | /** 62 | * Whether the module requires an event listener registration. 63 | * @return If the module listens to events 64 | */ 65 | boolean eventListener() default true; 66 | 67 | /** 68 | * The name of a method called when this module is enabled. 69 | * @return The method to call when enabled. 70 | */ 71 | String onEnable() default ""; 72 | 73 | /** 74 | * The name of a method called when this module is disabled. 75 | * @return The method to call when disabled. 76 | */ 77 | String onDisable() default ""; 78 | } 79 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/module/ModuleWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module; 24 | 25 | import com.me4502.modularframework.ModuleController; 26 | 27 | import java.io.IOException; 28 | import java.lang.reflect.InvocationTargetException; 29 | import java.util.Arrays; 30 | import java.util.List; 31 | import java.util.Optional; 32 | 33 | /** 34 | * Wraps a {@link Module} in a tangible object. 35 | */ 36 | public abstract class ModuleWrapper { 37 | 38 | private final ModuleController owner; 39 | private final String moduleClassName; 40 | 41 | private Class moduleClass; 42 | private Module annotation; 43 | 44 | public ModuleWrapper(ModuleController owner, String moduleClassName) { 45 | this.owner = owner; 46 | this.moduleClassName = moduleClassName; 47 | } 48 | 49 | public ModuleWrapper(ModuleController owner, Class clazz) { 50 | this.owner = owner; 51 | this.moduleClassName = clazz.getName(); 52 | this.moduleClass = clazz; 53 | } 54 | 55 | public Class getModuleClass() throws ClassNotFoundException { 56 | if(this.moduleClass == null) { 57 | this.moduleClass = (Class) Class.forName(this.moduleClassName); 58 | if(!this.moduleClass.isAnnotationPresent(Module.class)) 59 | throw new IllegalArgumentException("Module " + moduleClassName + " is not a module!"); 60 | } 61 | return this.moduleClass; 62 | } 63 | 64 | public abstract boolean isEnabled(); 65 | 66 | public ModuleController getOwner() { 67 | return this.owner; 68 | } 69 | 70 | /** 71 | * Gets the module object, if it exists. 72 | * 73 | * @return The module object 74 | */ 75 | public abstract Optional getModule(); 76 | 77 | public abstract void enableModule() throws IllegalAccessException, InstantiationException, ClassNotFoundException, 78 | NoSuchMethodException, InvocationTargetException, IOException; 79 | 80 | public abstract void disableModule() throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, 81 | InvocationTargetException, IOException; 82 | 83 | public Module getAnnotation() throws ClassNotFoundException { 84 | if (this.annotation == null) 85 | this.annotation = this.getModuleClass().getAnnotation(Module.class); 86 | return this.annotation; 87 | } 88 | 89 | public String getId() { 90 | try { 91 | return getAnnotation().id().toLowerCase(); 92 | } catch (ClassNotFoundException e) { 93 | e.printStackTrace(); 94 | } 95 | 96 | return null; 97 | } 98 | 99 | public String getName() { 100 | try { 101 | if(getAnnotation().name().isEmpty()) { 102 | return getId(); 103 | } 104 | return getAnnotation().name(); 105 | } catch (ClassNotFoundException e) { 106 | e.printStackTrace(); 107 | } 108 | 109 | return null; 110 | } 111 | 112 | public String getVersion() { 113 | try { 114 | return getAnnotation().version(); 115 | } catch (ClassNotFoundException e) { 116 | e.printStackTrace(); 117 | } 118 | 119 | return null; 120 | } 121 | 122 | public List getAuthors() { 123 | try { 124 | return Arrays.asList(getAnnotation().authors()); 125 | } catch (ClassNotFoundException e) { 126 | e.printStackTrace(); 127 | } 128 | 129 | return null; 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /modularframework-core/src/main/java/com/me4502/modularframework/module/guice/ModuleContainer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module.guice; 24 | 25 | import com.google.inject.BindingAnnotation; 26 | 27 | import java.lang.annotation.ElementType; 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.RetentionPolicy; 30 | import java.lang.annotation.Target; 31 | 32 | @BindingAnnotation 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target({ElementType.PARAMETER, ElementType.FIELD}) 35 | public @interface ModuleContainer { 36 | } 37 | -------------------------------------------------------------------------------- /modularframework-spigot/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | // id "tech.ferus.gradle.spigotgradle" version "1.0.5" 3 | id 'com.github.johnrengelman.shadow' version '2.0.0' 4 | } 5 | 6 | repositories { 7 | maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' } 8 | } 9 | 10 | dependencies { 11 | compile project(':modularframework-core') 12 | compile 'org.spigotmc:spigot-api:1.12-R0.1-SNAPSHOT' 13 | compile 'com.google.inject:guice:4.0' 14 | } 15 | 16 | //spigot { 17 | // meta { 18 | // name = project.name 19 | // version = project.version 20 | // description = "A module loading system." 21 | // main = "tld.domain.myplugin.MyPlugin" 22 | // author = "me4502" 23 | // authors = ["me4502"] 24 | // } 25 | //} 26 | 27 | shadowJar { 28 | classifier = 'dist' 29 | exclude 'docs' 30 | exclude 'GradleStart**' 31 | exclude '.cache' 32 | exclude 'LICENSE*' 33 | 34 | dependencies { 35 | include(dependency('com.google.inject:guice')) 36 | } 37 | } 38 | 39 | build.dependsOn(shadowJar) 40 | 41 | publishing { 42 | publications { 43 | MyPublication(MavenPublication) { 44 | from components.java 45 | artifact sourceJar 46 | artifact javadocJar 47 | groupId 'com.me4502' 48 | artifactId 'modularframework-spigot' 49 | version versionNumber 50 | } 51 | } 52 | } 53 | 54 | bintray { 55 | user = System.getProperty('BINTRAY_USER') 56 | key = System.getProperty('BINTRAY_KEY') 57 | publications = ['MyPublication'] 58 | pkg { 59 | repo = 'maven' 60 | name = 'modularframework-spigot' 61 | licenses = ['MIT'] 62 | vcsUrl = 'https://github.com/me4502/ModularFramework.git' 63 | publicDownloadNumbers = true 64 | version { 65 | name = 'modularframework-spigot-' + versionNumber 66 | desc = name 67 | } 68 | } 69 | } -------------------------------------------------------------------------------- /modularframework-spigot/src/main/java/com/me4502/modularframework/BukkitModuleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import com.me4502.modularframework.module.BukkitModuleWrapper; 26 | import com.me4502.modularframework.module.Module; 27 | 28 | import java.io.File; 29 | 30 | /** 31 | * A per-plugin class for managing registered modules. 32 | */ 33 | public class BukkitModuleController extends ModuleController { 34 | 35 | private File configurationDirectory; 36 | private boolean overrideConfigurationNode = false; 37 | 38 | BukkitModuleController(T plugin) { 39 | super(plugin); 40 | } 41 | 42 | public void setConfigurationDirectory(File configurationDirectory) { 43 | this.configurationDirectory = configurationDirectory; 44 | } 45 | 46 | public File getConfigurationDirectory() { 47 | return this.configurationDirectory; 48 | } 49 | 50 | /** 51 | * Register a class to be a module. 52 | * @param clazz The class containing the module. 53 | */ 54 | @Override 55 | @Deprecated 56 | public void registerModule(Class clazz) { 57 | if(!clazz.isAnnotationPresent(Module.class)) 58 | throw new IllegalArgumentException("Passed class is not a Module!"); 59 | 60 | this.moduleSet.add(new BukkitModuleWrapper<>(this, clazz)); 61 | } 62 | 63 | /** 64 | * Register a class to be a module. 65 | * @param className The name of the class to use. 66 | */ 67 | @Override 68 | public void registerModule(String className) { 69 | moduleSet.add(new BukkitModuleWrapper(this, className)); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /modularframework-spigot/src/main/java/com/me4502/modularframework/ModularFramework.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import org.bukkit.plugin.java.JavaPlugin; 26 | 27 | import java.util.ArrayList; 28 | import java.util.Collections; 29 | import java.util.List; 30 | 31 | /** 32 | * The base class of the Modular Framework. If this library is shaded into your plugin, use {@link ShadedModularFramework}. 33 | */ 34 | public class ModularFramework extends JavaPlugin { 35 | 36 | /** 37 | * Internal list of Module Controllers. 38 | */ 39 | private static final List controllerList = new ArrayList<>(); 40 | 41 | /** 42 | * Register a new Module Controller. 43 | * 44 | * @param plugin The plugin object to register with. 45 | * @param The plugin type. 46 | * @return The newly registered BukkitModuleController. 47 | */ 48 | public static BukkitModuleController registerModuleController(T plugin) { 49 | BukkitModuleController controller = new BukkitModuleController<>(plugin); 50 | controllerList.add(controller); 51 | return controller; 52 | } 53 | 54 | /** 55 | * Gets an immutable list of all {@link BukkitModuleController}s. 56 | * 57 | * @return The immutable list 58 | */ 59 | public static List getModuleControllers() { 60 | return Collections.unmodifiableList(controllerList); 61 | } 62 | 63 | /** 64 | * Unregisters a {@link BukkitModuleController}. 65 | * 66 | * @param controller The controller to unregister 67 | */ 68 | public static void unregisterModuleController(BukkitModuleController controller) { 69 | controller.disableModules(); 70 | controllerList.remove(controller); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /modularframework-spigot/src/main/java/com/me4502/modularframework/ShadedModularFramework.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import org.bukkit.plugin.java.JavaPlugin; 26 | 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.util.ArrayList; 29 | import java.util.Collections; 30 | import java.util.List; 31 | 32 | /** 33 | * Wrapper class to call main functions from when used via Maven/Gradle shading. 34 | */ 35 | public class ShadedModularFramework { 36 | 37 | /** 38 | * Internal list of Module Controllers. 39 | */ 40 | private static List controllerList = new ArrayList<>(); 41 | 42 | /** 43 | * Register a new Module Controller. 44 | * 45 | * @param plugin The plugin object to register with. 46 | * @param The plugin type. 47 | * @return The newly registered BukkitModuleController. 48 | */ 49 | public static BukkitModuleController registerModuleController(T plugin) { 50 | //If a real copy is installed, use that over this one. 51 | try { 52 | Class clazz = Class.forName("com.me4502.modularframework.ModularFramework"); 53 | return (BukkitModuleController) clazz.getMethod("registerModuleController").invoke(null, plugin); 54 | } catch (ClassNotFoundException | NoSuchMethodException ignored) { 55 | } catch (InvocationTargetException | IllegalAccessException e) { 56 | e.printStackTrace(); 57 | } 58 | 59 | BukkitModuleController controller = new BukkitModuleController<>(plugin); 60 | controllerList.add(controller); 61 | return controller; 62 | } 63 | 64 | /** 65 | * Gets an immutable list of all {@link BukkitModuleController}s. 66 | * 67 | * @return The immutable list 68 | */ 69 | public static List getModuleControllers() { 70 | return Collections.unmodifiableList(controllerList); 71 | } 72 | 73 | /** 74 | * Unregisters a {@link BukkitModuleController}. 75 | * 76 | * @param controller The controller to unregister 77 | */ 78 | public static void unregisterModuleController(BukkitModuleController controller) { 79 | controller.disableModules(); 80 | controllerList.remove(controller); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /modularframework-spigot/src/main/java/com/me4502/modularframework/module/BukkitModuleWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module; 24 | 25 | import com.google.inject.Guice; 26 | import com.google.inject.Injector; 27 | import com.me4502.modularframework.BukkitModuleController; 28 | import com.me4502.modularframework.exception.ModuleNotInstantiatedException; 29 | import com.me4502.modularframework.module.guice.ModuleInjector; 30 | import org.bukkit.Bukkit; 31 | import org.bukkit.event.Listener; 32 | import org.bukkit.plugin.java.JavaPlugin; 33 | 34 | import java.io.IOException; 35 | import java.lang.reflect.InvocationTargetException; 36 | import java.lang.reflect.Method; 37 | import java.util.Arrays; 38 | import java.util.List; 39 | import java.util.Optional; 40 | 41 | public class BukkitModuleWrapper extends ModuleWrapper { 42 | 43 | private T module; 44 | private boolean enabled = false; 45 | private ModuleInjector injectorModule; 46 | 47 | public BukkitModuleWrapper(BukkitModuleController owner, String moduleClassName) { 48 | super(owner, moduleClassName); 49 | } 50 | 51 | @Deprecated 52 | public BukkitModuleWrapper(BukkitModuleController owner, Class moduleClass) { 53 | super(owner, moduleClass); 54 | } 55 | 56 | public void setInjectorModule(ModuleInjector injectorModule) { 57 | this.injectorModule = injectorModule; 58 | } 59 | 60 | public ModuleInjector getInjectorModule() { 61 | return this.injectorModule != null ? this.injectorModule : new ModuleInjector(this); 62 | } 63 | 64 | public boolean isEnabled() { 65 | return this.enabled; 66 | } 67 | 68 | public void enableModule() throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException { 69 | Injector injector = Guice.createInjector(this.getInjectorModule()); 70 | this.module = injector.getInstance(getModuleClass()); 71 | 72 | if(getAnnotation().eventListener()) 73 | Bukkit.getPluginManager().registerEvents((Listener) this.module, (JavaPlugin) this.getOwner().getPlugin()); 74 | 75 | if(!getAnnotation().onEnable().isEmpty()) { 76 | Method meth = this.module.getClass().getMethod(getAnnotation().onEnable()); 77 | meth.invoke(this.module); 78 | } 79 | 80 | this.enabled = true; 81 | } 82 | 83 | public void disableModule() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { 84 | 85 | if(!getAnnotation().onDisable().isEmpty()) { 86 | Method meth = this.module.getClass().getMethod(getAnnotation().onDisable()); 87 | meth.invoke(this.module); 88 | } 89 | 90 | this.module = null; 91 | this.enabled = false; 92 | } 93 | 94 | /** 95 | * Gets the module object, if it exists. 96 | * 97 | * @return The module object 98 | */ 99 | public Optional getModule() { 100 | return Optional.ofNullable(this.module); 101 | } 102 | 103 | public T getModuleUnchecked() throws ModuleNotInstantiatedException { 104 | if(this.module == null) 105 | throw new ModuleNotInstantiatedException(); 106 | return this.module; 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /modularframework-spigot/src/main/java/com/me4502/modularframework/module/guice/ModuleInjector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module.guice; 24 | 25 | import com.google.inject.AbstractModule; 26 | import com.google.inject.Provider; 27 | import com.me4502.modularframework.module.BukkitModuleWrapper; 28 | 29 | public class ModuleInjector extends AbstractModule { 30 | 31 | private static BukkitModuleWrapper moduleWrapper; 32 | 33 | public ModuleInjector(BukkitModuleWrapper moduleWrapper) { 34 | ModuleInjector.moduleWrapper = moduleWrapper; 35 | } 36 | 37 | @Override 38 | protected void configure() { 39 | bind(Object.class).annotatedWith(ModuleContainer.class).toProvider(ContainerProvider.class); 40 | } 41 | 42 | private static class ContainerProvider implements Provider { 43 | @Override 44 | public Object get() { 45 | return moduleWrapper.getOwner().getPlugin(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /modularframework-sponge/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.spongepowered.plugin' version '0.8.1' 3 | } 4 | 5 | repositories { 6 | maven { url "http://repo.spongepowered.org/maven/" } 7 | } 8 | 9 | dependencies { 10 | compile project(':modularframework-core') 11 | compileOnly 'org.spongepowered:spongeapi:7.0.0-SNAPSHOT' 12 | } 13 | 14 | sponge { 15 | plugin { 16 | id = 'modularframework' 17 | } 18 | } 19 | 20 | publishing { 21 | publications { 22 | MyPublication(MavenPublication) { 23 | from components.java 24 | artifact sourceJar 25 | artifact javadocJar 26 | groupId 'com.me4502' 27 | artifactId 'modularframework-sponge' 28 | version versionNumber 29 | } 30 | } 31 | } 32 | 33 | bintray { 34 | user = System.getProperty('BINTRAY_USER') 35 | key = System.getProperty('BINTRAY_KEY') 36 | publications = ['MyPublication'] 37 | pkg { 38 | repo = 'maven' 39 | name = 'modularframework-sponge' 40 | licenses = ['MIT'] 41 | vcsUrl = 'https://github.com/me4502/ModularFramework.git' 42 | publicDownloadNumbers = true 43 | version { 44 | name = 'modularframework-sponge-' + versionNumber 45 | desc = name 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/ModularFramework.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import org.spongepowered.api.Game; 26 | 27 | import java.util.ArrayList; 28 | import java.util.Collections; 29 | import java.util.List; 30 | 31 | /** 32 | * The base class of the Modular Framework. If this library is shaded into your plugin, use {@link ShadedModularFramework}. 33 | */ 34 | public class ModularFramework { 35 | 36 | /** 37 | * Internal list of Module Controllers. 38 | */ 39 | private static final List controllerList = new ArrayList<>(); 40 | 41 | /** 42 | * Register a new Module Controller. 43 | * 44 | * @param plugin The plugin object to register with. 45 | * @param game The game. 46 | * @param The plugin type. 47 | * @return The newly registered SpongeModuleController. 48 | */ 49 | public static SpongeModuleController registerModuleController(T plugin, Game game) { 50 | SpongeModuleController controller = new SpongeModuleController<>(plugin, game); 51 | controllerList.add(controller); 52 | return controller; 53 | } 54 | 55 | /** 56 | * Gets an immutable list of all {@link SpongeModuleController}s. 57 | * 58 | * @return The immutable list 59 | */ 60 | public static List getModuleControllers() { 61 | return Collections.unmodifiableList(controllerList); 62 | } 63 | 64 | /** 65 | * Unregisters a {@link SpongeModuleController}. 66 | * 67 | * @param controller The controller to unregister 68 | */ 69 | public static void unregisterModuleController(SpongeModuleController controller) { 70 | controller.disableModules(); 71 | controllerList.remove(controller); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/ShadedModularFramework.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import org.spongepowered.api.Game; 26 | 27 | import java.lang.reflect.InvocationTargetException; 28 | import java.util.ArrayList; 29 | import java.util.Collections; 30 | import java.util.List; 31 | 32 | /** 33 | * Wrapper class to call main functions from when used via Maven/Gradle shading. 34 | */ 35 | public class ShadedModularFramework { 36 | 37 | /** 38 | * Internal list of Module Controllers. 39 | */ 40 | private static List controllerList = new ArrayList<>(); 41 | 42 | /** 43 | * Register a new Module Controller. 44 | * 45 | * @param plugin The plugin object to register with. 46 | * @param game The game. 47 | * @param The plugin type. 48 | * @return The newly registered SpongeModuleController. 49 | */ 50 | public static SpongeModuleController registerModuleController(T plugin, Game game) { 51 | //If a real copy is installed, use that over this one. 52 | try { 53 | Class clazz = Class.forName("com.me4502.modularframework.ModularFramework"); 54 | return (SpongeModuleController) clazz.getMethod("registerModuleController").invoke(null, plugin, game); 55 | } catch (ClassNotFoundException | NoSuchMethodException ignored) { 56 | } catch (InvocationTargetException | IllegalAccessException e) { 57 | e.printStackTrace(); 58 | } 59 | 60 | SpongeModuleController controller = new SpongeModuleController<>(plugin, game); 61 | controllerList.add(controller); 62 | return controller; 63 | } 64 | 65 | /** 66 | * Gets an immutable list of all {@link SpongeModuleController}s. 67 | * 68 | * @return The immutable list 69 | */ 70 | public static List getModuleControllers() { 71 | return Collections.unmodifiableList(controllerList); 72 | } 73 | 74 | /** 75 | * Unregisters a {@link SpongeModuleController}. 76 | * 77 | * @param controller The controller to unregister 78 | */ 79 | public static void unregisterModuleController(SpongeModuleController controller) { 80 | controller.disableModules(); 81 | controllerList.remove(controller); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/SpongeModuleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework; 24 | 25 | import com.me4502.modularframework.module.Module; 26 | import com.me4502.modularframework.module.SpongeModuleWrapper; 27 | import ninja.leaping.configurate.ConfigurationOptions; 28 | import org.spongepowered.api.Game; 29 | import org.spongepowered.api.GameState; 30 | import org.spongepowered.api.plugin.PluginContainer; 31 | 32 | import java.io.File; 33 | import java.util.LinkedHashSet; 34 | import java.util.Set; 35 | 36 | /** 37 | * A per-plugin class for managing registered modules. 38 | */ 39 | public class SpongeModuleController extends ModuleController { 40 | 41 | private final Game game; 42 | 43 | private PluginContainer pluginContainer; 44 | private File configurationDirectory; 45 | private ConfigurationOptions configurationOptions; 46 | private boolean overrideConfigurationNode = false; 47 | 48 | /** 49 | * Constructs a new SpongeModuleController. 50 | * 51 | * @param plugin The plugin instance. 52 | */ 53 | SpongeModuleController(T plugin, Game game) { 54 | super(plugin); 55 | this.game = game; 56 | } 57 | 58 | public void setPluginContainer(PluginContainer pluginContainer) { 59 | this.pluginContainer = pluginContainer; 60 | } 61 | 62 | public PluginContainer getPluginContainer() { 63 | return this.pluginContainer; 64 | } 65 | 66 | public void setConfigurationDirectory(File configurationDirectory) { 67 | this.configurationDirectory = configurationDirectory; 68 | } 69 | 70 | public File getConfigurationDirectory() { 71 | return this.configurationDirectory; 72 | } 73 | 74 | public void setConfigurationOptions(ConfigurationOptions configurationOptions) { 75 | this.configurationOptions = configurationOptions; 76 | } 77 | 78 | public ConfigurationOptions getConfigurationOptions() { 79 | return this.configurationOptions == null ? ConfigurationOptions.defaults() : this.configurationOptions; 80 | } 81 | 82 | public boolean isOverrideConfigurationNode() { 83 | return this.overrideConfigurationNode; 84 | } 85 | 86 | public void setOverrideConfigurationNode(boolean overrideConfigurationNode) { 87 | this.overrideConfigurationNode = overrideConfigurationNode; 88 | } 89 | 90 | public Game getGame() { 91 | return this.game; 92 | } 93 | 94 | /** 95 | * Register a class to be a module. 96 | * @param clazz The class containing the module. 97 | */ 98 | @Override 99 | @Deprecated 100 | public void registerModule(Class clazz) { 101 | if(!clazz.isAnnotationPresent(Module.class)) 102 | throw new IllegalArgumentException("Passed class is not a Module!"); 103 | 104 | moduleSet.add(new SpongeModuleWrapper<>(this, clazz)); 105 | } 106 | 107 | /** 108 | * Register a class to be a module. 109 | * @param className The name of the class to use. 110 | */ 111 | @Override 112 | public void registerModule(String className) { 113 | moduleSet.add(new SpongeModuleWrapper(this, className)); 114 | } 115 | 116 | /** 117 | * Register a class to be a module with a load state. 118 | * @param className The name of the class to use. 119 | * @param loadState The state to load the module. 120 | */ 121 | public void registerModule(String className, GameState loadState) { 122 | moduleSet.add(new SpongeModuleWrapper(this, className, loadState)); 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/module/SpongeModuleWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module; 24 | 25 | import com.google.inject.Guice; 26 | import com.google.inject.Injector; 27 | import com.me4502.modularframework.SpongeModuleController; 28 | import com.me4502.modularframework.exception.ModuleNotInstantiatedException; 29 | import com.me4502.modularframework.module.guice.ModuleConfiguration; 30 | import com.me4502.modularframework.module.guice.ModuleInjector; 31 | import ninja.leaping.configurate.ConfigurationNode; 32 | import ninja.leaping.configurate.commented.CommentedConfigurationNode; 33 | import ninja.leaping.configurate.hocon.HoconConfigurationLoader; 34 | import ninja.leaping.configurate.loader.ConfigurationLoader; 35 | import org.spongepowered.api.GameState; 36 | 37 | import java.io.File; 38 | import java.io.IOException; 39 | import java.lang.reflect.Field; 40 | import java.lang.reflect.InvocationTargetException; 41 | import java.lang.reflect.Method; 42 | import java.util.Arrays; 43 | import java.util.List; 44 | import java.util.Optional; 45 | 46 | /** 47 | * Wraps a {@link Module} in a tangible object. 48 | */ 49 | public class SpongeModuleWrapper extends ModuleWrapper { 50 | 51 | private T module; 52 | private GameState loadState; 53 | private ModuleInjector injectorModule; 54 | 55 | private boolean enabled = false; 56 | 57 | public SpongeModuleWrapper(SpongeModuleController owner, String moduleClassName) { 58 | this(owner, moduleClassName, GameState.SERVER_STARTED); 59 | } 60 | 61 | public SpongeModuleWrapper(SpongeModuleController owner, String moduleClassName, GameState loadState) { 62 | super(owner, moduleClassName); 63 | this.loadState = loadState; 64 | } 65 | 66 | @Deprecated 67 | public SpongeModuleWrapper(SpongeModuleController owner, Class moduleClass) { 68 | super(owner, moduleClass); 69 | } 70 | 71 | public void setInjectorModule(ModuleInjector injectorModule) { 72 | this.injectorModule = injectorModule; 73 | } 74 | 75 | public ModuleInjector getInjectorModule() { 76 | return this.injectorModule != null ? this.injectorModule : new ModuleInjector(this); 77 | } 78 | 79 | public GameState getLoadState() { 80 | return this.loadState; 81 | } 82 | 83 | public boolean isEnabled() { 84 | return this.enabled; 85 | } 86 | 87 | public void enableModule() throws IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IOException { 88 | Injector injector = Guice.createInjector(this.getInjectorModule()); 89 | this.module = injector.getInstance(getModuleClass()); 90 | 91 | if(getAnnotation().eventListener()) { 92 | ((SpongeModuleController) this.getOwner()).getGame().getEventManager().registerListeners(((SpongeModuleController) this.getOwner()).getPlugin(), this.module); 93 | } 94 | 95 | if(!getAnnotation().onEnable().isEmpty()) { 96 | Method meth = this.module.getClass().getMethod(getAnnotation().onEnable()); 97 | meth.invoke(this.module); 98 | } 99 | 100 | loadAndSaveConfiguration(); 101 | 102 | this.enabled = true; 103 | } 104 | 105 | public void disableModule() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, IOException { 106 | 107 | if (getAnnotation().eventListener()) { 108 | ((SpongeModuleController) this.getOwner()).getGame().getEventManager().unregisterListeners(this.module); 109 | } 110 | 111 | if(!getAnnotation().onDisable().isEmpty()) { 112 | Method meth = this.module.getClass().getMethod(getAnnotation().onDisable()); 113 | meth.invoke(this.module); 114 | } 115 | 116 | loadAndSaveConfiguration(); 117 | 118 | this.module = null; 119 | this.enabled = false; 120 | } 121 | 122 | private void loadAndSaveConfiguration() throws ClassNotFoundException, IllegalAccessException, IOException { 123 | for(Field field : module.getClass().getFields()) { 124 | if(field.isAnnotationPresent(ModuleConfiguration.class)) { 125 | File legacyConfig = new File(((SpongeModuleController) this.getOwner()).getConfigurationDirectory(), getName() + ".conf"); 126 | File config = new File(((SpongeModuleController) this.getOwner()).getConfigurationDirectory(), getId() + ".conf"); 127 | if(!getAnnotation().name().equals(getAnnotation().id()) && legacyConfig.exists()) { 128 | legacyConfig.renameTo(config); 129 | } 130 | if(!config.exists()) 131 | config.createNewFile(); 132 | ConfigurationLoader configLoader = HoconConfigurationLoader.builder().setFile(config).build(); 133 | if(((SpongeModuleController) this.getOwner()).isOverrideConfigurationNode()) { 134 | field.set(module, configLoader.load(((SpongeModuleController) this.getOwner()).getConfigurationOptions())); 135 | } 136 | configLoader.save((ConfigurationNode) field.get(module)); 137 | } 138 | } 139 | } 140 | 141 | /** 142 | * Gets the module object, if it exists. 143 | * 144 | * @return The module object 145 | */ 146 | public Optional getModule() { 147 | return Optional.ofNullable(this.module); 148 | } 149 | 150 | public T getModuleUnchecked() throws ModuleNotInstantiatedException { 151 | if(this.module == null) 152 | throw new ModuleNotInstantiatedException(); 153 | return this.module; 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/module/guice/ModuleConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module.guice; 24 | 25 | import com.google.inject.BindingAnnotation; 26 | 27 | import java.lang.annotation.ElementType; 28 | import java.lang.annotation.Retention; 29 | import java.lang.annotation.RetentionPolicy; 30 | import java.lang.annotation.Target; 31 | 32 | @BindingAnnotation 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target({ElementType.PARAMETER, ElementType.FIELD}) 35 | public @interface ModuleConfiguration { 36 | } 37 | -------------------------------------------------------------------------------- /modularframework-sponge/src/main/java/com/me4502/modularframework/module/guice/ModuleInjector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Me4502 (Madeline Miller) 3 | * Copyright (c) Contributors 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | package com.me4502.modularframework.module.guice; 24 | 25 | import com.google.inject.AbstractModule; 26 | import com.google.inject.Provider; 27 | import com.me4502.modularframework.SpongeModuleController; 28 | import com.me4502.modularframework.module.SpongeModuleWrapper; 29 | import ninja.leaping.configurate.ConfigurationNode; 30 | import ninja.leaping.configurate.commented.CommentedConfigurationNode; 31 | import ninja.leaping.configurate.hocon.HoconConfigurationLoader; 32 | import ninja.leaping.configurate.loader.ConfigurationLoader; 33 | import org.spongepowered.api.plugin.PluginContainer; 34 | 35 | import java.io.File; 36 | import java.io.IOException; 37 | 38 | public class ModuleInjector extends AbstractModule { 39 | 40 | private static SpongeModuleWrapper moduleWrapper; 41 | 42 | public ModuleInjector(SpongeModuleWrapper moduleWrapper) { 43 | ModuleInjector.moduleWrapper = moduleWrapper; 44 | } 45 | 46 | @Override 47 | protected void configure() { 48 | bind(PluginContainer.class).annotatedWith(ModuleContainer.class).toProvider(ContainerProvider.class); 49 | bind(ConfigurationNode.class).annotatedWith(ModuleConfiguration.class).toProvider(ConfigurationProvider.class); 50 | } 51 | 52 | private static class ContainerProvider implements Provider { 53 | 54 | @Override 55 | public PluginContainer get() { 56 | return ((SpongeModuleController) moduleWrapper.getOwner()).getPluginContainer(); 57 | } 58 | } 59 | 60 | private static class ConfigurationProvider implements Provider { 61 | 62 | @Override 63 | public ConfigurationNode get() { 64 | ConfigurationNode configNode = null; 65 | 66 | if(((SpongeModuleController) moduleWrapper.getOwner()).getConfigurationDirectory() != null) { 67 | try { 68 | File legacyConfig = new File(((SpongeModuleController) moduleWrapper.getOwner()).getConfigurationDirectory(), moduleWrapper.getName() + ".conf"); 69 | File config = new File(((SpongeModuleController) moduleWrapper.getOwner()).getConfigurationDirectory(), moduleWrapper.getId()+ ".conf"); 70 | if(!moduleWrapper.getName().equals(moduleWrapper.getId()) && legacyConfig.exists()) { 71 | legacyConfig.renameTo(config); 72 | } 73 | if(!config.exists()) 74 | config.createNewFile(); 75 | ConfigurationLoader configLoader = HoconConfigurationLoader.builder().setFile(config).build(); 76 | configNode = configLoader.load(((SpongeModuleController) moduleWrapper.getOwner()).getConfigurationOptions()); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | } 81 | 82 | return configNode; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'modularframework' 2 | 3 | include 'modularframework-core', 'modularframework-sponge', 'modularframework-spigot' 4 | 5 | --------------------------------------------------------------------------------