diff --git a/Examples/Bukkit/build.gradle b/Examples/Bukkit/build.gradle new file mode 100644 index 0000000..ab1e1b6 --- /dev/null +++ b/Examples/Bukkit/build.gradle @@ -0,0 +1,40 @@ +buildscript { + repositories { + mavenCentral() + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4" + } +} + +group 'br.com.gamemods.kotlinfun.examples' +version '0.2' + +apply plugin: 'kotlin' + +repositories { + mavenCentral() + maven { url = 'https://hub.spigotmc.org/nexus/content/groups/public/' } + maven { url "https://jitpack.io" } +} + +dependencies { + compile 'org.bukkit:bukkit:1.10.2-R0.1-SNAPSHOT' + compile('com.github.GameModsBR.KotlinFun:BukkitPlugin:0.2') +} + +processResources { + inputs.property "version", project.version + + from(sourceSets.main.resources.srcDirs) { + include 'plugin.yml' + + expand 'version':project.version + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'plugin.yml' + } +} diff --git a/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/CustomPlayerData.kt b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/CustomPlayerData.kt new file mode 100644 index 0000000..779841f --- /dev/null +++ b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/CustomPlayerData.kt @@ -0,0 +1,46 @@ +package example.kotlinfun.bukkit + +import br.com.gamemods.kotlinfun.bukkit.* +import br.com.gamemods.kotlinfun.str +import org.bukkit.OfflinePlayer + +/** + * This holds temporary player data, it's not persisted to the disk but can be quickly accessed from the player object, + */ +class CustomPlayerData(player: OfflinePlayer) : PlayerData(player) { + /** + * The super class of this companion object will setup everything automatically, that's why we need to provide + * all that stuff. Please note that this constructor will be improved in the next version. + */ + companion object : AutoDataCompanion(SamplePlugin.instance, "session", CustomPlayerData::class.java, ::CustomPlayerData) + + // We can do whatever we want here + var observingSpawns = false + var observingDeaths = false + var observingDamages = false + set(value) { + field = value + player?.sendMessage("Damage>".gold() + if(value) "You'll now be notified about entity damages".red() else " You'll no longer see messages about damage".green()) + } + + var silly = false + fun tellHim() { + /** + * Notice that we have constructed this class with an OfflinePlayer. + * If the player is online than it will be returned below as a Player object, if it's not then it will be null + */ + player?.sendMessage( + /** + * Notice that we have encapsulated the if..else with parentheses to call str() in the end. + * That's because String.red() and String.green() will return a formatter object called Colorized, + * that would cause an issue here because Colorized is not an string, all we have to do is to call + * .str() or .toString() to transform it, but, we had two calls here, so to avoid repetition I + * wrapped the if statement and casted the result. + * + * An other way to workaround this would be to add an empty string, for example: + * if(silly) "You are silly!".red()+"" else "You are cool :)".green()+"" + */ + ( if(silly) "You are silly!".red() else "You are cool :)".green() ).str() + ) + } +} diff --git a/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/PlayerListener.kt b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/PlayerListener.kt new file mode 100644 index 0000000..07698d1 --- /dev/null +++ b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/PlayerListener.kt @@ -0,0 +1,45 @@ +package example.kotlinfun.bukkit + +import br.com.gamemods.kotlinfun.bukkit.* +import org.bukkit.Material +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.block.Action +import org.bukkit.event.block.BlockPlaceEvent +import org.bukkit.event.player.PlayerInteractEvent + +class PlayerListener(val plugin: SamplePlugin) : Listener { + @EventHandler + fun placeMine(event: BlockPlaceEvent) { + if(event.block.type != Material.WOOD_PLATE) + return + + /** + * You can set metadata to Metadatable directly, no need to create a FixedMetadataValue + */ + event.block.setMetadata("mine", plugin, event.player.name) + + /** + * Notice how the formatted message was created, you can swap the bold() and darkRed() calls if you want, + * the invocation order is not important. + * + * If the red message wasn't red it would inherit the bold and dark red style + */ + event.player.sendMessage("Mine> ".bold().darkRed() + "The bomb has been placed!".red()) + } + + @EventHandler + fun activateMine(event: PlayerInteractEvent) { + if(event.action != Action.PHYSICAL) + return + + val block = event.clickedBlock ?: return + + /** + * It's super easy to get or remove a metadata now + */ + val owner = block.removeMetadata("mine", plugin, String::class.java) ?: return + if(block.world.createExplosion(block.location, 4f)) + event.player.sendMessage("Mine> ".darkRed().bold() + "BOOOM!".underline().red() + " You've stepped on $owner's mine!".red()) + } +} \ No newline at end of file diff --git a/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/SamplePlugin.kt b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/SamplePlugin.kt new file mode 100644 index 0000000..72eaba6 --- /dev/null +++ b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/SamplePlugin.kt @@ -0,0 +1,52 @@ +package example.kotlinfun.bukkit + +import br.com.gamemods.kotlinfun.bukkit.DataCompanion +import br.com.gamemods.kotlinfun.bukkit.PlayerData +import br.com.gamemods.kotlinfun.bukkit.register +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.plugin.java.JavaPlugin + +/** + * Just a simple example + */ +class SamplePlugin : JavaPlugin() { + companion object { + lateinit var instance : SamplePlugin + } + + override fun onEnable() { + instance = this + + /** + * You can register your listeners with a simple call + */ + register(PlayerListener(this), WorldListener()) + + getCommand("silly")?.setExecutor { sender, command, label, strings -> + /** + * We can get and work the player data easily + */ + CustomPlayerData[sender]?.run { silly = !silly ; tellHim() ; true } ?: false + } + + getCommand("ts")?.setExecutor { sender, command, label, strings -> + CustomPlayerData[sender]?.run { observingSpawns = !observingSpawns; true } ?: false + } + + getCommand("tdt")?.setExecutor { sender, command, label, strings -> + CustomPlayerData[sender]?.run { observingDeaths = !observingDeaths; true } ?: false + } + + getCommand("tda")?.setExecutor { sender, command, label, strings -> + CustomPlayerData[sender]?.run { observingDamages = !observingDamages; true } ?: false + } + } +} + +/** + * This will be added in the next version + */ +operator fun DataCompanion.get(sender: CommandSender?): D? { + return get(sender as? Player ?: return null) +} diff --git a/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/WorldListener.kt b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/WorldListener.kt new file mode 100644 index 0000000..bcc9de9 --- /dev/null +++ b/Examples/Bukkit/src/main/kotlin/example/kotlinfun/bukkit/WorldListener.kt @@ -0,0 +1,57 @@ +package example.kotlinfun.bukkit + +import br.com.gamemods.kotlinfun.bukkit.DataCompanion +import br.com.gamemods.kotlinfun.bukkit.PlayerData +import br.com.gamemods.kotlinfun.bukkit.darkGray +import br.com.gamemods.kotlinfun.bukkit.gray +import org.bukkit.Bukkit +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.entity.CreatureSpawnEvent +import org.bukkit.event.entity.EntityDamageEvent +import org.bukkit.event.entity.EntityDeathEvent + +class WorldListener : Listener { + @EventHandler + fun spawn(event: CreatureSpawnEvent) { + val msg = "Spawn> ".darkGray() + "${event.entity.name} has spawned by ${event.spawnReason} in ${event.location}".gray() + + /** + * We can easily iterate through all the session data from the players online + */ + for(data in CustomPlayerData) { + if(data.observingSpawns) { + data.player?.sendMessage(msg) + } + } + } + + @EventHandler + fun death(event: EntityDeathEvent) { + /** + * Notice that we have a string "nothing" inside a string, Kotlin is fun + */ + val msg = "Death> ".darkGray() + "${event.entity.name} has died by ${event.entity.lastDamageCause?.cause?.name ?: "nothing"} in ${event.entity.location}".gray() + for(data in CustomPlayerData) { + if(data.observingDeaths) { + data.player?.sendMessage(msg) + } + } + } + + @EventHandler + fun damage(event: EntityDamageEvent) { + val msg = "Damage> ".darkGray() + "${event.entity.name} has been damaged by ${event.cause} in ${event.entity.location}".gray() + for(data in CustomPlayerData) { + if(data.observingDeaths) { + data.player?.sendMessage(msg) + } + } + } +} + +/** + * This will be included in the next version + */ +operator fun DataCompanion.iterator() : Iterator = + Bukkit.getOnlinePlayers().map { get(it) }.filterNotNull().iterator() diff --git a/Examples/Bukkit/src/main/resources/plugin.yml b/Examples/Bukkit/src/main/resources/plugin.yml new file mode 100644 index 0000000..fb4cf28 --- /dev/null +++ b/Examples/Bukkit/src/main/resources/plugin.yml @@ -0,0 +1,13 @@ +name: KotlinFunExampleBukkit +version: ${version} +main: example.kotlinfun.bukkit.SamplePlugin + +commands: + silly: + description: Tells you if you are silly + ts: + description: Show/Hide where the entities are spawning + tdt: + description: Show/Hide where the entities are dying + tda: + description: Show/Hide where the entities are taking damage diff --git a/Examples/Bungee/build.gradle b/Examples/Bungee/build.gradle new file mode 100644 index 0000000..5fac65d --- /dev/null +++ b/Examples/Bungee/build.gradle @@ -0,0 +1,40 @@ +buildscript { + repositories { + mavenCentral() + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4" + } +} + +group 'br.com.gamemods.kotlinfun.examples' +version '0.2' + +apply plugin: 'kotlin' + +repositories { + mavenCentral() + maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' } + maven { url "https://jitpack.io" } +} + +dependencies { + compile 'net.md-5:bungeecord-api:1.10-SNAPSHOT' + compile('com.github.GameModsBR.KotlinFun:BungeePlugin:0.2') +} + +processResources { + inputs.property "version", project.version + + from(sourceSets.main.resources.srcDirs) { + include 'bungee.yml' + + expand 'version':project.version + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'bungee.yml' + } +} diff --git a/Examples/Bungee/src/main/resources/bungee.yml b/Examples/Bungee/src/main/resources/bungee.yml new file mode 100644 index 0000000..a6d9354 --- /dev/null +++ b/Examples/Bungee/src/main/resources/bungee.yml @@ -0,0 +1,3 @@ +name: KotlinFunExampleBungee +version: ${version} +main: diff --git a/Examples/Spigot/build.gradle b/Examples/Spigot/build.gradle new file mode 100644 index 0000000..2c642a9 --- /dev/null +++ b/Examples/Spigot/build.gradle @@ -0,0 +1,40 @@ +buildscript { + repositories { + mavenCentral() + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4" + } +} + +group 'br.com.gamemods.kotlinfun.examples' +version '0.2' + +apply plugin: 'kotlin' + +repositories { + mavenCentral() + maven { url = 'https://hub.spigotmc.org/nexus/content/groups/public/' } + maven { url "https://jitpack.io" } +} + +dependencies { + compile 'org.spigotmc:spigot-api:1.10.2-R0.1-SNAPSHOT' + compile('com.github.GameModsBR.KotlinFun:SpigotPlugin:0.2') +} + +processResources { + inputs.property "version", project.version + + from(sourceSets.main.resources.srcDirs) { + include 'plugin.yml' + + expand 'version':project.version + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'plugin.yml' + } +} diff --git a/Examples/Spigot/src/main/resources/plugin.yml b/Examples/Spigot/src/main/resources/plugin.yml new file mode 100644 index 0000000..69f55d2 --- /dev/null +++ b/Examples/Spigot/src/main/resources/plugin.yml @@ -0,0 +1,3 @@ +name: KotlinFunExampleSpigot +version: ${version} +main: diff --git a/Examples/build.gradle b/Examples/build.gradle new file mode 100644 index 0000000..d5703c4 --- /dev/null +++ b/Examples/build.gradle @@ -0,0 +1,2 @@ +group 'br.com.gamemods.kotlinfun.examples' +version '0.2' diff --git a/Examples/gradle/wrapper/.gitignore b/Examples/gradle/wrapper/.gitignore new file mode 100644 index 0000000..24daf48 --- /dev/null +++ b/Examples/gradle/wrapper/.gitignore @@ -0,0 +1 @@ +gradle-wrapper.jar diff --git a/Examples/gradle/wrapper/gradle-wrapper.properties b/Examples/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..90ba45e --- /dev/null +++ b/Examples/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Oct 19 20:45:31 BRT 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip diff --git a/Examples/gradlew b/Examples/gradlew new file mode 100644 index 0000000..27309d9 --- /dev/null +++ b/Examples/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/Examples/gradlew.bat b/Examples/gradlew.bat new file mode 100644 index 0000000..832fdb6 --- /dev/null +++ b/Examples/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/Examples/settings.gradle b/Examples/settings.gradle new file mode 100644 index 0000000..e4eddef --- /dev/null +++ b/Examples/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'KotlinFun-Examples' +include 'Bukkit', 'Bungee', 'Spigot' diff --git a/gradle/wrapper/.gitignore b/gradle/wrapper/.gitignore index 7536258..24daf48 100644 --- a/gradle/wrapper/.gitignore +++ b/gradle/wrapper/.gitignore @@ -1 +1 @@ -gradle-wrapper.jar \ No newline at end of file +gradle-wrapper.jar