From cf745b35b94e751c6c6f0661e29a7b9bac2e08f7 Mon Sep 17 00:00:00 2001 From: Kalle Struik Date: Tue, 26 Oct 2021 11:27:06 +0200 Subject: [PATCH] Add a bunch of config stuff. --- build.gradle.kts | 6 +-- .../kotlin/nl/kallestruik/darena/DArena.kt | 9 ++-- .../nl/kallestruik/darena/arenas/Arena.kt | 8 ++-- .../kallestruik/darena/arenas/ArenaConfig.kt | 8 +++- .../kallestruik/darena/arenas/ArenaSession.kt | 1 + .../exceptions/MaterialNotFoundException.kt | 6 +++ .../darena/types/arena/ArenaCheckpoint.kt | 31 ++++++++++++++ .../darena/types/arena/ArenaEnchantment.kt | 27 ++++++++++++ .../darena/types/arena/ArenaItem.kt | 36 ++++++++++++++++ .../darena/types/arena/ArenaLoadout.kt | 42 +++++++++++++++++++ .../darena/types/arena/ArenaLocation.kt | 20 +++++++++ .../darena/types/arena/ArenaPoints.kt | 5 +++ .../{arenas => types/arena}/ArenaSpawn.kt | 6 ++- .../kallestruik/darena/util/ConfigHelper.kt | 7 ++++ src/main/resources/template/arena.yml | 32 +++++++++++++- 15 files changed, 228 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/nl/kallestruik/darena/exceptions/MaterialNotFoundException.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaCheckpoint.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaEnchantment.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaItem.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLoadout.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLocation.kt create mode 100644 src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaPoints.kt rename src/main/kotlin/nl/kallestruik/darena/{arenas => types/arena}/ArenaSpawn.kt (86%) diff --git a/build.gradle.kts b/build.gradle.kts index 966adb9..9dff59e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,8 +18,8 @@ repositories { dependencies { implementation("co.aikar:acf-paper:0.5.0-SNAPSHOT") - compileOnly("com.destroystokyo.paper:paper:1.16.5-R0.1-SNAPSHOT") - compileOnly(kotlin("stdlib-jdk8")) + compileOnly("com.destroystokyo.paper:paper-api:1.16.5-R0.1-SNAPSHOT") + compileOnly(kotlin("stdlib")) } tasks.compileJava { @@ -41,4 +41,4 @@ tasks.processResources { tasks.withType() { kotlinOptions.jvmTarget = "13" -} \ No newline at end of file +} diff --git a/src/main/kotlin/nl/kallestruik/darena/DArena.kt b/src/main/kotlin/nl/kallestruik/darena/DArena.kt index 9e3e411..533a843 100644 --- a/src/main/kotlin/nl/kallestruik/darena/DArena.kt +++ b/src/main/kotlin/nl/kallestruik/darena/DArena.kt @@ -6,13 +6,12 @@ import nl.kallestruik.darena.managers.TeamManager import org.bukkit.plugin.java.JavaPlugin import java.io.File -class DArena: JavaPlugin() { +class DArena : JavaPlugin() { private lateinit var configManager: ConfigManager private lateinit var pointsManager: PointsManager private lateinit var teamManager: TeamManager - - //TODO: + // TODO: // Thinking: // - Datastructures for arenas // - Datastructures for teams (Everyone is always in a team even when solo) @@ -35,10 +34,8 @@ class DArena: JavaPlugin() { teamManager = TeamManager(File(dataFolder, "teams.yml")) teamManager.load() pointsManager = PointsManager(teamManager, File(dataFolder, "points.yml")) - } override fun onDisable() { - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/nl/kallestruik/darena/arenas/Arena.kt b/src/main/kotlin/nl/kallestruik/darena/arenas/Arena.kt index 07cf631..5ce3580 100644 --- a/src/main/kotlin/nl/kallestruik/darena/arenas/Arena.kt +++ b/src/main/kotlin/nl/kallestruik/darena/arenas/Arena.kt @@ -2,6 +2,8 @@ package nl.kallestruik.darena.arenas import nl.kallestruik.darena.arenas.world.ArenaWorld import nl.kallestruik.darena.util.ArenaUtil +import org.bukkit.potion.PotionEffect +import org.bukkit.potion.PotionEffectType class Arena( private val config: ArenaConfig, @@ -39,13 +41,13 @@ class Arena( // Spawn all the players session.participants.forEachIndexed { index, player -> - //TODO: Freeze players in place (for in arena countdown) (if countdown is 0 dont freeze them) + // TODO: Freeze players in place (for in arena countdown) (if countdown is 0 dont freeze them) session.spawns[index % session.spawns.size].spawn(world, player) } - //TODO: + // TODO: } fun reset() { world.reset() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaConfig.kt b/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaConfig.kt index 51f95aa..9411942 100644 --- a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaConfig.kt +++ b/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaConfig.kt @@ -1,12 +1,15 @@ package nl.kallestruik.darena.arenas +import nl.kallestruik.darena.types.arena.ArenaLoadout +import nl.kallestruik.darena.types.arena.ArenaSpawn import nl.kallestruik.darena.util.ConfigHelper import java.io.File data class ArenaConfig( var name: String = "[Missing name]", var spawns: List = emptyList(), - var spectatorSpawn: ArenaSpawn = ArenaSpawn("default", 0.0, 100.0, 0.0, 0.0F, 0.0F), + var spectatorSpawn: ArenaSpawn = ArenaSpawn("default", 0.0, 100.0, 0.0, 0.0F, 0.0F, "default"), + var loadouts: List = emptyList(), ) { companion object { @@ -23,6 +26,9 @@ data class ArenaConfig( if (config.contains("spectatorSpawn")) arenaConfig.spectatorSpawn = ArenaSpawn.load(config.getConfigurationSection("spectatorSpawn")!!) + if (config.contains("loadouts")) + arenaConfig.loadouts = ArenaLoadout.loadList(config.getConfigurationSection("loadouts")!!) + return arenaConfig } } diff --git a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSession.kt b/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSession.kt index 953a559..e5bbd94 100644 --- a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSession.kt +++ b/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSession.kt @@ -1,5 +1,6 @@ package nl.kallestruik.darena.arenas +import nl.kallestruik.darena.types.arena.ArenaSpawn import org.bukkit.entity.Player data class ArenaSession( diff --git a/src/main/kotlin/nl/kallestruik/darena/exceptions/MaterialNotFoundException.kt b/src/main/kotlin/nl/kallestruik/darena/exceptions/MaterialNotFoundException.kt new file mode 100644 index 0000000..e65aad7 --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/exceptions/MaterialNotFoundException.kt @@ -0,0 +1,6 @@ +package nl.kallestruik.darena.exceptions + +class MaterialNotFoundException( + message: String? = null, + cause: Throwable? = null +): Exception(message, cause) \ No newline at end of file diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaCheckpoint.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaCheckpoint.kt new file mode 100644 index 0000000..1ba206d --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaCheckpoint.kt @@ -0,0 +1,31 @@ +package nl.kallestruik.darena.types.arena + +import org.bukkit.configuration.ConfigurationSection +import javax.naming.ConfigurationException + +data class ArenaCheckpoint( + val label: String, + val lower: ArenaLocation, + val upper: ArenaLocation, + val spawn: String, +) { + companion object { + fun loadList(section: ConfigurationSection): List { + val list = mutableListOf() + for (key in section.getKeys(false)) { + list.add(load(section.getConfigurationSection(key)!!)) + } + + return list + } + + fun load(section: ConfigurationSection): ArenaCheckpoint { + return ArenaCheckpoint( + section.name, + ArenaLocation.load(section.getConfigurationSection("lower")!!), + ArenaLocation.load(section.getConfigurationSection("upper")!!), + section.getString("spawn") ?: throw ConfigurationException("Could not find required field spawn in '${section.currentPath}'") + ) + } + } +} diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaEnchantment.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaEnchantment.kt new file mode 100644 index 0000000..b1c693b --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaEnchantment.kt @@ -0,0 +1,27 @@ +package nl.kallestruik.darena.types.arena + +import org.bukkit.configuration.ConfigurationSection +import org.bukkit.configuration.InvalidConfigurationException + +data class ArenaEnchantment( + val name: String, + val level: Int, +) { + companion object { + fun loadList(section: ConfigurationSection): List { + val list = mutableListOf() + for (key in section.getKeys(false)) { + list.add(load(section.getConfigurationSection(key)!!)) + } + + return list + } + + fun load(section: ConfigurationSection): ArenaEnchantment { + return ArenaEnchantment( + section.getString("name") ?: throw InvalidConfigurationException("Could not find required field name in section '${section.currentPath}'"), + section.getInt("level", 1) + ) + } + } +} diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaItem.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaItem.kt new file mode 100644 index 0000000..c95d574 --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaItem.kt @@ -0,0 +1,36 @@ +package nl.kallestruik.darena.types.arena + +import nl.kallestruik.darena.util.ConfigHelper +import org.bukkit.Material +import org.bukkit.configuration.ConfigurationSection + +data class ArenaItem( + val material: Material = Material.AIR, + val amount: Int = 1, + val enchantments: List = listOf(), + val unbreakable: Boolean = false, +) { + companion object { + fun loadList(section: ConfigurationSection): List { + val list = mutableListOf() + for (key in section.getKeys(false)) { + list.add(load(section.getConfigurationSection(key)!!)) + } + + return list + } + + fun load(section: ConfigurationSection): ArenaItem { + val enchantments = section.getConfigurationSection("enchantments")?.let { + ArenaEnchantment.loadList(it) + } ?: listOf() + + return ArenaItem( + ConfigHelper.matchMaterial(section.getString("material")!!), + section.getInt("amount", 1), + enchantments, + section.getBoolean("unbreakable", false) + ) + } + } +} diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLoadout.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLoadout.kt new file mode 100644 index 0000000..0a9367a --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLoadout.kt @@ -0,0 +1,42 @@ +package nl.kallestruik.darena.types.arena + +import org.bukkit.configuration.ConfigurationSection + +data class ArenaLoadout( + val name: String, + val hotbar: List, + val armor: List, + val offhand: ArenaItem, +) { + companion object { + fun loadList(section: ConfigurationSection): List { + val list = mutableListOf() + for (key in section.getKeys(false)) { + list.add(load(section.getConfigurationSection(key)!!)) + } + + return list + } + + fun load(section: ConfigurationSection): ArenaLoadout { + val hotbar = section.getConfigurationSection("hotbar")?.let { + ArenaItem.loadList(it) + }?: listOf() + + val armor = section.getConfigurationSection("armor")?.let { + ArenaItem.loadList(it) + }?: listOf() + + val offhand = section.getConfigurationSection("offhand")?.let { + ArenaItem.load(it) + }?: ArenaItem() + + return ArenaLoadout( + section.name, + hotbar, + armor, + offhand, + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLocation.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLocation.kt new file mode 100644 index 0000000..5d202f8 --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaLocation.kt @@ -0,0 +1,20 @@ +package nl.kallestruik.darena.types.arena + +import org.bukkit.configuration.ConfigurationSection + +data class ArenaLocation( + val x: Int, + val y: Int, + val z: Int +) { + companion object { + + fun load(section: ConfigurationSection): ArenaLocation { + return ArenaLocation( + section.getInt("x", 0), + section.getInt("y", 0), + section.getInt("z", 0), + ) + } + } +} diff --git a/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaPoints.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaPoints.kt new file mode 100644 index 0000000..37f0544 --- /dev/null +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaPoints.kt @@ -0,0 +1,5 @@ +package nl.kallestruik.darena.types.arena + +data class ArenaPoints( + val kill: Int, +) diff --git a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSpawn.kt b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaSpawn.kt similarity index 86% rename from src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSpawn.kt rename to src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaSpawn.kt index f88b91e..1a8a139 100644 --- a/src/main/kotlin/nl/kallestruik/darena/arenas/ArenaSpawn.kt +++ b/src/main/kotlin/nl/kallestruik/darena/types/arena/ArenaSpawn.kt @@ -1,4 +1,4 @@ -package nl.kallestruik.darena.arenas +package nl.kallestruik.darena.types.arena import nl.kallestruik.darena.arenas.world.ArenaWorld import org.bukkit.Location @@ -12,6 +12,7 @@ data class ArenaSpawn( val z: Double, val yaw: Float, val pitch: Float, + val loadout: String ) { fun spawn(world: ArenaWorld, player: Player) { player.teleport(Location(world.world, x, y, z, yaw, pitch)) @@ -35,7 +36,8 @@ data class ArenaSpawn( section.getDouble("y"), section.getDouble("z"), section.getDouble("yaw").toFloat(), - section.getDouble("pitch").toFloat() + section.getDouble("pitch").toFloat(), + section.getString("loadout")!! ) } } diff --git a/src/main/kotlin/nl/kallestruik/darena/util/ConfigHelper.kt b/src/main/kotlin/nl/kallestruik/darena/util/ConfigHelper.kt index 41079a3..6db9097 100644 --- a/src/main/kotlin/nl/kallestruik/darena/util/ConfigHelper.kt +++ b/src/main/kotlin/nl/kallestruik/darena/util/ConfigHelper.kt @@ -1,5 +1,7 @@ package nl.kallestruik.darena.util +import nl.kallestruik.darena.exceptions.MaterialNotFoundException +import org.bukkit.Material import org.bukkit.configuration.file.YamlConfiguration import java.io.* @@ -34,4 +36,9 @@ object ConfigHelper { e.printStackTrace() } } + + @Throws(MaterialNotFoundException::class) + fun matchMaterial(material: String): Material { + return Material.matchMaterial(material) ?: throw MaterialNotFoundException("There is not material with the name '$material'") + } } \ No newline at end of file diff --git a/src/main/resources/template/arena.yml b/src/main/resources/template/arena.yml index d569c27..45f45ff 100644 --- a/src/main/resources/template/arena.yml +++ b/src/main/resources/template/arena.yml @@ -8,27 +8,57 @@ # z: 0 # yaw: 0 # pitch: 0 +# loadout: "default" # label2: # x: 10 # y: 100 # z: 0 # yaw: 0 # pitch: 0 +# loadout: "default" # label3: # x: 10 # y: 100 # z: 10 # yaw: 0 # pitch: 0 +# loadout: "default" # label4: # x: 0 # y: 100 # z: 10 # yaw: 0 # pitch: 0 +# loadout: "default" +# loadouts: +# default: +# hotbar: +# weapon: +# material: iron_axe +# amount: 1 +# food: +# material: cooked_beef +# amount: 10 +# armor: +# helmet: +# material: golden_helmet +# amount: 1 +# elytra: +# material: elytra +# amount: 1 +# leggings: +# material: iron_leggings +# amount: 1 +# boots: +# material: diamond_boots +# amount: 1 +# offhand: +# material: shield +# amount: 1 #specatorSpawn: # x: 0 # y: 150 # z: 0 # yaw: 0 -# pitch: 0 \ No newline at end of file +# pitch: 0 +# loadout: "none" \ No newline at end of file