From 4d233480feca9f7d4416a3a9a3dda477f2a9c18f Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sun, 15 Sep 2019 21:23:10 +0200 Subject: [PATCH] Added Spiral class - Spiral class to create spirals around a coordinate --- .gitignore | 6 ++ build.gradle | 49 ++++++++++++++++ gradle.properties | 1 + settings.gradle | 2 + .../net/trivernis/chunkmaster/Chunkmaster.kt | 18 ++++++ .../net/trivernis/chunkmaster/lib/Spiral.kt | 56 +++++++++++++++++++ src/main/resources/plugin.yml | 12 ++++ 7 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 settings.gradle create mode 100644 src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt create mode 100644 src/main/kotlin/net/trivernis/chunkmaster/lib/Spiral.kt create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a8c18f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +gradle +.gradle +.idea +build +out +gradlew* \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..fc7f334 --- /dev/null +++ b/build.gradle @@ -0,0 +1,49 @@ + +plugins { + id 'idea' + id 'org.jetbrains.kotlin.jvm' version '1.3.50' + id 'com.github.johnrengelman.shadow' version '2.0.4' +} + +idea { + module { + downloadJavadoc = true + downloadSources = true + } +} + +group "net.trivernis" +version "1.0-SNAPSHOT" + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() + maven { + url "https://hub.spigotmc.org/nexus/content/repositories/snapshots" + } + maven { + url 'https://papermc.io/repo/repository/maven-public/' + } +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + testCompile group: 'junit', name: 'junit', version: '4.12' + compileOnly "org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT" +} + +jar { + from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } +} +compileKotlin { + kotlinOptions { + jvmTarget = "1.8" + } +} +compileTestKotlin { + kotlinOptions { + jvmTarget = "1.8" + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..29e08e8 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..e2cead2 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'chunkmaster' + diff --git a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt new file mode 100644 index 0000000..ed0b0cf --- /dev/null +++ b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt @@ -0,0 +1,18 @@ +package net.trivernis.chunkmaster + +import net.trivernis.chunkmaster.lib.Spiral +import org.bukkit.plugin.java.JavaPlugin + +class Chunkmaster: JavaPlugin() { + override fun onEnable() { + configure() + } + + override fun onDisable() { + + } + + private fun configure() { + config.options().copyDefaults(true) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/Spiral.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/Spiral.kt new file mode 100644 index 0000000..4484896 --- /dev/null +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/Spiral.kt @@ -0,0 +1,56 @@ +package net.trivernis.chunkmaster.lib + +import kotlin.math.abs + +class Spiral(private val center: Pair, private val start: Pair) { + var currentPos = start + var direction = 0 + var count = 0 + + /** + * Returns the next value in the spiral + */ + fun next(): Pair { + if (count == 1) { // because of the center behaviour + count ++ + return currentPos + } + if (currentPos == center) { // the center has to be handled exclusively + currentPos = Pair(center.first, center.second + 1) + count ++ + return center + } else { + val distances = getDistances(center, currentPos) + if (abs(distances.first) == abs(distances.second)) { + direction = (direction + 1)%5 + } + } + when(direction) { + 0 -> { + currentPos = Pair(currentPos.first + 1, currentPos.second) + } + 1 -> { + currentPos = Pair(currentPos.first, currentPos.second - 1) + } + 2 -> { + currentPos = Pair(currentPos.first - 1, currentPos.second) + } + 3 -> { + currentPos = Pair(currentPos.first, currentPos.second + 1) + } + 4 -> { + currentPos = Pair(currentPos.first, currentPos.second + 1) + direction = 0 + } + } + count ++ + return currentPos + } + + /** + * Returns the distances between 2 coordinates + */ + private fun getDistances(pos1: Pair, pos2: Pair): Pair { + return Pair(pos2.first - pos1.first, pos2.second - pos1.second) + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..9c5d9dc --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,12 @@ +main: net.trivernis.chunkmaster.Chunkmaster +name: Chunkmaster +version: '1.0 SNAPSHOT' +description: Chunk commands plugin. +author: Trivernis +website: trivernis.net +commands: + +permissions: + chunkmaster.*: + description: Wildcard permission + default: op \ No newline at end of file