From 29839f8c6bdba0757f641afd7f4b6a84daad49bb Mon Sep 17 00:00:00 2001 From: LartyHD Date: Sat, 18 May 2019 18:15:44 +0200 Subject: [PATCH] Finished Setup GUI!!!! --- .../main/kotlin/de/astride/gungame/GunGame.kt | 10 +- .../de/astride/gungame/commands/GunGame.kt | 165 +++++++++--------- .../de/astride/gungame/functions/API.kt | 4 +- .../astride/gungame/services/ConfigService.kt | 25 +-- .../kotlin/de/astride/gungame/setup/Events.kt | 44 ++--- .../de/astride/gungame/setup/Inventories.kt | 21 ++- 6 files changed, 132 insertions(+), 137 deletions(-) diff --git a/spigot/src/main/kotlin/de/astride/gungame/GunGame.kt b/spigot/src/main/kotlin/de/astride/gungame/GunGame.kt index 109efd7..9e1bdf0 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/GunGame.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/GunGame.kt @@ -19,12 +19,9 @@ import de.astride.gungame.shop.ShopListener import de.astride.gungame.stats.Actions import net.darkdevelopers.darkbedrock.darkness.general.functions.performCraftPluginUpdater import net.darkdevelopers.darkbedrock.darkness.spigot.events.listener.EventsListener -import net.darkdevelopers.darkbedrock.darkness.spigot.functions.JsonArray import net.darkdevelopers.darkbedrock.darkness.spigot.functions.loadBukkitWorld import net.darkdevelopers.darkbedrock.darkness.spigot.functions.setup -import net.darkdevelopers.darkbedrock.darkness.spigot.functions.toJsonObject import net.darkdevelopers.darkbedrock.darkness.spigot.location.toBukkitLocation -import net.darkdevelopers.darkbedrock.darkness.spigot.location.toMap import net.darkdevelopers.darkbedrock.darkness.spigot.plugin.DarkPlugin import net.darkdevelopers.darkbedrock.darkness.spigot.utils.Items import net.darkdevelopers.darkbedrock.darkness.spigot.utils.map.setupWorldBorder @@ -110,11 +107,8 @@ class GunGame : DarkPlugin() { override fun onDisable(): Unit = onDisable { logUnregister("setup events") { Events.reset() } - logSave("shops") { - val shops = configService.shops - val list = shops.locations.map { it.toMap().toJsonObject() } - shops.save(jsonElement = JsonArray(list)) - } + logSave("shops") { configService.shops.save() } + logSave("maps") { configService.maps.save() } @Suppress("LABEL_NAME_CLASH") if (isSetup) { diff --git a/spigot/src/main/kotlin/de/astride/gungame/commands/GunGame.kt b/spigot/src/main/kotlin/de/astride/gungame/commands/GunGame.kt index d13856f..79f6070 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/commands/GunGame.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/commands/GunGame.kt @@ -1,7 +1,6 @@ package de.astride.gungame.commands import de.astride.gungame.functions.configService -import de.astride.gungame.functions.edit import de.astride.gungame.functions.messages import de.astride.gungame.functions.replace import de.astride.gungame.setup.* @@ -18,6 +17,7 @@ import net.darkdevelopers.darkbedrock.darkness.spigot.location.toBukkitLocation import net.darkdevelopers.darkbedrock.darkness.spigot.location.toLocation import net.darkdevelopers.darkbedrock.darkness.spigot.location.vector.Vector3D import net.darkdevelopers.darkbedrock.darkness.spigot.location.vector.copy +import net.darkdevelopers.darkbedrock.darkness.spigot.location.vector.toVector3D import net.darkdevelopers.darkbedrock.darkness.spigot.messages.Colors.* import net.darkdevelopers.darkbedrock.darkness.spigot.region.Region import net.darkdevelopers.darkbedrock.darkness.spigot.utils.AnvilGUI @@ -41,7 +41,7 @@ import java.io.File /** * @author Lars Artmann | LartyHD * Created by Lars Artmann | LartyHD on 04.04.2019 18:33. - * Current Version: 1.0 (04.04.2019 - 07.05.2019) + * Current Version: 1.0 (04.04.2019 - 18.05.2019) */ class GunGame(javaPlugin: JavaPlugin) : Command( javaPlugin, @@ -49,23 +49,21 @@ class GunGame(javaPlugin: JavaPlugin) : Command( permission = config.permission, usage = "save actions/kits/shops/maps [.json]" + "|load messages [.json]" + - "|set map name [.json]" + - "|set map world [.json]" + - "|set map worldboarder damage buffer/amount [.json]" + - "|set map worldboarder warning time/distance [.json]" + - "|set map worldboarder size [.json]" + - "|set map worldboarder center [-o] [.json]" + - "|set map spawn/hologram [-o] [.json]" + - "|set map region pos1/pos2 [-o] [.json]" + +// "|set map worldboarder damage buffer/amount [.json]" + +// "|set map worldboarder warning time/distance [.json]" + +// "|set map worldboarder size [.json]" + +// "|set map worldboarder center [-o] [.json]" + "|add shop [-o] [.json]" + "|add map [-o] [.json]" + "|setup help/all/shops/maps" + - "|setup shops/maps delete " + + "|setup shops delete " + "|setup shops teleport " + + "|setup shops movehere " + "|setup shops edit " + + "|setup maps delete [region/hologram]" + "|setup maps edit name/world " + - "|setup maps edit hologram/spawn " + - "|setup maps edit region pos1/pos2 " + + "|setup maps edit hologram/spawn" +//" " + + "|setup maps edit region pos1/pos2" +//" " + "|setup reload", minLength = 2, maxLength = 8, @@ -83,13 +81,14 @@ class GunGame(javaPlugin: JavaPlugin) : Command( val toLowerCase = args[1].toLowerCase() val maps = configService.maps + val shops = configService.shops @Suppress("IMPLICIT_CAST_TO_ANY") val configData = if (!args.last().endsWith(".json")) when (args[0].toLowerCase()) { "save" -> when (toLowerCase) { "actions" -> configService.actions.configData "kits" -> configService.kits.configData - "shops" -> configService.shops.configData - "maps" -> configService.maps.configData + "shops" -> shops.configData + "maps" -> maps.configData else -> failed = true } "load" -> when (toLowerCase) { @@ -98,8 +97,8 @@ class GunGame(javaPlugin: JavaPlugin) : Command( } "set" -> if (toLowerCase == "map") maps.bukkitGsonConfig.configData else failed = true "add" -> when (toLowerCase) { - "shop" -> configService.shops.configData - "map" -> configService.maps.configData + "shop" -> shops.configData + "map" -> maps.configData else -> failed = true } else -> failed = true @@ -128,8 +127,8 @@ class GunGame(javaPlugin: JavaPlugin) : Command( when (toLowerCase) { "actions" -> configService.actions.save(configData = configData) "kits" -> configService.kits.save(configData = configData) - "shops" -> configService.shops.save(configData = configData) - "maps" -> configService.maps.save(configData = configData) + "shops" -> shops.save(configData = configData) + "maps" -> maps.save(configData = configData) else -> { sendUseMessage(sender) return @@ -148,13 +147,11 @@ class GunGame(javaPlugin: JavaPlugin) : Command( return } when (args[3].toLowerCase()) { - "name" -> isSizeOrHigher(5, sender) { maps.setNameAndSave(id, args[4], configData) } - "world" -> isSizeOrHigher(5, sender) { maps.setWorldAndSave(id, args[4], configData) } "worldboarder" -> isSizeOrHigher(5, sender) { when (args[4].toLowerCase()) { "center" -> sender.isPlayer { - val location = roundLocation(args, it.location) - maps.setWorldBoarderCenterAndSave(id, location, configData) + val location = it.location.round(args) + maps.setWorldBoarderCenterAndSave(id, location.toBukkitLocation(), configData) } "size" -> isSizeOrHigher(6, sender) { val value = args[5].toDoubleOrNull() @@ -199,33 +196,15 @@ class GunGame(javaPlugin: JavaPlugin) : Command( else -> sendUseMessage(sender) } } - "spawn" -> sender.isPlayer { - val location = roundLocation(args, it.location) - maps.setSpawnAndSave(id, location, configData) - } - "hologram" -> sender.isPlayer { - val location = roundLocation(args, it.location) - maps.setHologramAndSave(id, location, configData) - } - "region" -> sender.isPlayer { - val location = roundLocation(args, it.location) - if (args.size >= 5) when { - args[4].equals("pos1", true) -> - maps.setRegionPos1AndSave(id, location, configData) - args[4].equals("pos2", true) -> - maps.setRegionPos2AndSave(id, location, configData) - else -> sendUseMessage(sender) - } else sendUseMessage(sender) - } else -> sendUseMessage(sender) } } else sendUseMessage(sender) "add" -> sender.isPlayer { player -> - val location = roundLocation(args, player.location) + val location = player.location.round(args) when (toLowerCase) { "shop" -> { - configService.shops.addAndSave(location, configData) - if (configData == configService.shops.configData) configService.shops.locations += location.toLocation() + if (configData == shops.configData) shops.locations += location + shops.save(configData) } "map" -> when (args.size) { 2 -> { @@ -240,13 +219,13 @@ class GunGame(javaPlugin: JavaPlugin) : Command( 3 -> { val gameMap: GameMap = DataGameMap( args[2], - location.toLocation(), + location, null, null, null ) - configService.maps.addAndSave(gameMap, configData) - if (configData == configService.maps.configData) configService.maps.maps += gameMap + maps.addAndSave(gameMap, configData) + if (configData == maps.configData) maps.maps += gameMap } else -> sendUseMessage(sender) } @@ -286,22 +265,30 @@ class GunGame(javaPlugin: JavaPlugin) : Command( val gameMap = maps[id] player.editID = id when (args[1].toLowerCase()) { - "delete" -> { - configService.maps.maps -= gameMap - Bukkit.getConsoleSender().execute("$commandName save maps") - } + "delete" -> if (args.size == 4) when (args[3].toLowerCase()) { + "region" -> gameMap.editTo(maps, id, region = null) + "hologram" -> gameMap.editTo(maps, id, hologram = null) + else -> sendUseMessage(sender) + } else configService.maps.maps -= gameMap "edit" -> when (args.size) { 3 -> player.openInventory(Setup.generateMapsEdit(gameMap)) - 4 -> if (args[3].toLowerCase() == "name" || args[3].toLowerCase() == "world") { - AnvilGUI(javaPlugin, player).apply { - setSlot( - AnvilGUI.AnvilSlot.INPUT_LEFT, - ItemBuilder(Material.PAPER).setName("Value").build() - ) - }.open("GunGame Setup Maps Edit ${args[3]}") - player.editType = args[3] - player.anvilType = "setup-maps-edit-4" - } else sendUseMessage(sender) + 4 -> when (args[3].toLowerCase()) { + "name", "world" -> { + AnvilGUI(javaPlugin, player).apply { + setSlot( + AnvilGUI.AnvilSlot.INPUT_LEFT, + ItemBuilder(Material.PAPER).setName("Value").build() + ) + }.open("GunGame Setup Maps Edit ${args[3]}") + player.editType = args[3] + player.anvilType = "setup-maps-edit-4" + } + "hologram" -> gameMap.editTo( + maps, id, hologram = player.location.toLocation().round() + ) + "spawn" -> gameMap.editTo(maps, id, spawn = player.location.toLocation().round()) + else -> sendUseMessage(sender) + } 5 -> { val arg4 = args[4] when (args[3].toLowerCase()) { @@ -321,19 +308,26 @@ class GunGame(javaPlugin: JavaPlugin) : Command( ) gameMap.setupWorldBorder() } - "hologram" -> player.openInventory( - Setup.generateMapsEditLocation(gameMap, player, "hologram") - ) - "spawn" -> player.openInventory( - Setup.generateMapsEditLocation(gameMap, player, "spawn") - ) -// "region" -> when (arg4.toLowerCase()) { -// "pos1" -> { -// } -// "pos2" -> { -// } -// else -> sendUseMessage(sender) -// } + "region" -> { + val region = + gameMap.region ?: Region.of("generated", 0.toVector3D(), 0.toVector3D()) + + val worldName = player.world.name + val playerVector = player.location.toLocation().vector + when (arg4.toLowerCase()) { + "pos1" -> gameMap.editTo( + maps, + id, + region = Region.of(worldName, playerVector.round(), region.max) + ) + "pos2" -> gameMap.editTo( + maps, + id, + region = Region.of(worldName, region.min, playerVector.round()) + ) + else -> sendUseMessage(sender) + } + } else -> sendUseMessage(sender) } } @@ -360,6 +354,7 @@ class GunGame(javaPlugin: JavaPlugin) : Command( Bukkit.getConsoleSender().execute("$commandName save shops") } "teleport" -> player.teleport(location.toBukkitLocation()) + "movehere" -> shopLocations.editTo(id, player.location.toLocation().round()) "edit" -> when (args.size) { 3 -> player.openInventory(Setup.generateLocationEdit(location)) 4 -> { @@ -441,14 +436,20 @@ class GunGame(javaPlugin: JavaPlugin) : Command( private inline fun Array.isSizeOrHigher(size: Int, sender: CommandSender, block: () -> Unit): Unit = if (this.size >= size) block() else sendUseMessage(sender) - private fun roundLocation(args: Array, location: Location): Location = - if (args.isNotEmpty() && - (args.last().equals("-o", true) || - (args.last().endsWith(".json") && - args.size >= 2 && - args.dropLast(1).last().equals("-o", true))) - ) location - else location.edit(x = location.blockX + 0.5, z = location.blockZ + 0.5) + private fun Location.round(args: Array): ReadOnlyLocation = toLocation().round(args) + + private fun ReadOnlyLocation.round(args: Array): ReadOnlyLocation = if (args.isNotEmpty() && + (args.last().equals("-o", true) || + (args.last().endsWith(".json") && + args.size >= 2 && + args.dropLast(1).last().equals("-o", true))) + ) this else round() + + //TODO darkness + private fun ReadOnlyLocation.round(): ReadOnlyLocation = copy(vector = vector.round()) + + //TODO darkness + private fun Vector3D.round(): Vector3D = copy(x = x.toInt() + 0.5, z = z.toInt() + 0.5) private fun generatePath(input: String): ConfigData { val path = input.split('/') diff --git a/spigot/src/main/kotlin/de/astride/gungame/functions/API.kt b/spigot/src/main/kotlin/de/astride/gungame/functions/API.kt index d3e4102..f49e9af 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/functions/API.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/functions/API.kt @@ -32,7 +32,7 @@ fun String.replace(key: String, value: Any): String = replace("@$key@", value.to * * Current Version: 1.0 (06.05.2019 - 06.05.2019) */ -fun Location.edit( +fun Location.copy( world: World? = null, x: Double? = null, y: Double? = null, @@ -46,4 +46,4 @@ fun Location.edit( z ?: this.z, yaw ?: this.yaw, pitch ?: this.pitch -) \ No newline at end of file +) diff --git a/spigot/src/main/kotlin/de/astride/gungame/services/ConfigService.kt b/spigot/src/main/kotlin/de/astride/gungame/services/ConfigService.kt index bbc949a..36f836d 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/services/ConfigService.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/services/ConfigService.kt @@ -4,6 +4,7 @@ import com.google.gson.* import de.astride.data.ItemStackSerializer import de.astride.gungame.functions.AllowTeams import de.astride.gungame.functions.allActions +import de.astride.gungame.functions.configService import de.astride.gungame.functions.messages import de.astride.gungame.kits.DefaultKits import de.astride.gungame.stats.Action @@ -22,6 +23,7 @@ import net.darkdevelopers.darkbedrock.darkness.general.functions.asString import net.darkdevelopers.darkbedrock.darkness.spigot.configs.gson.BukkitGsonConfig import net.darkdevelopers.darkbedrock.darkness.spigot.functions.* import net.darkdevelopers.darkbedrock.darkness.spigot.location.toLocation +import net.darkdevelopers.darkbedrock.darkness.spigot.location.toMap import net.darkdevelopers.darkbedrock.darkness.spigot.messages.Colors.SECONDARY import net.darkdevelopers.darkbedrock.darkness.spigot.messages.Colors.TEXT import net.darkdevelopers.darkbedrock.darkness.spigot.messages.SpigotGsonMessages @@ -531,8 +533,10 @@ class ConfigService(private val directory: File) { jsonArray: JsonArray = loadAs(configData) ?: JsonArray() ): Unit = set(id, value, configData, jsonArray, "worldBoarder", "damage", "amount") - fun save(configData: ConfigData = this.configData, jsonElement: JsonElement = rawMaps): Unit = - GsonService.save(configData, jsonElement) + fun save( + configData: ConfigData = this.configData, + jsonElement: JsonElement = JsonArray(configService.maps.maps.map { it.toMap().toJsonObject() }) + ): Unit = GsonService.save(configData, jsonElement) private fun set( id: Int, @@ -577,7 +581,6 @@ class ConfigService(private val directory: File) { /* Main */ val configData = ConfigData(directory, config.files.shops) private val jsonArray = GsonService.load(configData) as? JsonArray ?: JsonArray() - val bukkitGsonConfig = BukkitGsonConfig(configData) /* Values */ val locations = jsonArray.mapNotNull { @@ -585,20 +588,10 @@ class ConfigService(private val directory: File) { jsonObject.toMap().toLocation() }.toMutableList() - fun addAndSave( - location: Location, + fun save( configData: ConfigData = this.configData, - jsonArray: JsonArray = loadAs(configData) ?: JsonArray() - ) { - add(location, jsonArray) - save(configData, jsonArray) - } - - fun add(location: Location, jsonArray: JsonArray = loadAs(configData) ?: JsonArray()): Unit = - jsonArray.add(JsonObject().apply { bukkitGsonConfig.setLocation(location, this) }) - - fun save(configData: ConfigData = this.configData, jsonElement: JsonElement = this.jsonArray): Unit = - GsonService.save(configData, jsonElement) + jsonElement: JsonElement = JsonArray(locations.map { it.toMap().toJsonObject() }) + ): Unit = GsonService.save(configData, jsonElement) } diff --git a/spigot/src/main/kotlin/de/astride/gungame/setup/Events.kt b/spigot/src/main/kotlin/de/astride/gungame/setup/Events.kt index bd82bc0..f20710d 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/setup/Events.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/setup/Events.kt @@ -92,12 +92,14 @@ object Events : EventsTemplate() { //open shops edit gui Setup.shops.listenTop(plugin, onlyCheckName = true, acceptSlot = { it in 19..25 }) { event -> val id = getID(event.currentItem) ?: return@listenTop - val type = if (event.isShiftClick) - if (event.isRightClick) { - event.whoClicked.closeInventory() - "delete" - } else "teleport" - else "edit" + event.whoClicked.closeInventory() + val type = when { + event.isShiftClick && event.isLeftClick -> "teleport" + event.isShiftClick && event.isRightClick -> "delete" + event.isLeftClick -> "edit" + event.isRightClick -> "movehere" + else -> return@listenTop + } event.whoClicked.execute("$commandName setup shops $type $id") }.add() @@ -125,20 +127,10 @@ object Events : EventsTemplate() { whoClicked.execute("$commandName setup shops edit $id $type") }.add() - //open maps edit gui - Setup.maps.listenTop(plugin, onlyCheckName = true, acceptSlot = { it in 19..25 }) { event -> - val id = getID(event.currentItem) ?: return@listenTop - val type = if (event.isShiftClick && event.isRightClick) { - event.whoClicked.closeInventory() - "delete" - } else "edit" - event.whoClicked.execute("$commandName setup maps $type $id") - }.add() - //open "maps edit name" gui Setup.mapsInventoryEditName.listenTop( plugin, - acceptSlot = { it == 20 || it == 40 } + acceptSlot = { it == 21 } ) { event -> val id = event.whoClicked.editID ?: return@listenTop event.whoClicked.execute("$commandName setup maps edit $id name") @@ -147,12 +139,24 @@ object Events : EventsTemplate() { //maps edit hologram/spawn ## Setup.mapsInventoryEditName.listenTop( plugin, - acceptSlot = { it == 22 || it == 24 } + acceptSlot = { it in 19 until 26 step 2 } ) { event -> val player: HumanEntity = event.whoClicked ?: return@listenTop - val type = event.slot.toType() ?: return@listenTop val id = player.editID ?: return@listenTop - event.whoClicked.execute("$commandName setup maps edit $id $type") + player.closeInventory() + val type = when (event.slot) { + 19 -> if (event.isShiftClick && event.isRightClick) { + player.execute("$commandName setup maps delete $id region") + return@listenTop + } else "region ${if (event.isLeftClick) "pos1" else "pos2"}" + 23 -> "spawn" + 25 -> if (event.isShiftClick && event.isRightClick) { + player.execute("$commandName setup maps delete $id hologram") + return@listenTop + } else "hologram" + else -> return@listenTop + } + player.execute("$commandName setup maps edit $id $type") }.add() //shops edit type to value diff --git a/spigot/src/main/kotlin/de/astride/gungame/setup/Inventories.kt b/spigot/src/main/kotlin/de/astride/gungame/setup/Inventories.kt index 76ebe13..d61b0ee 100644 --- a/spigot/src/main/kotlin/de/astride/gungame/setup/Inventories.kt +++ b/spigot/src/main/kotlin/de/astride/gungame/setup/Inventories.kt @@ -133,7 +133,9 @@ object Setup { "$TEXT Yaw: $IMPORTANT${location.yawOr0}", "$TEXT Pitch: $IMPORTANT${location.pitchOr0}", "", - "${GREEN}Klicken zum editieren", + "${GREEN}Links klicken zum editieren", + "${GREEN}Rechts klicken um es hier her zu verschieben", + "", "${GREEN}Shift links klicken zum telportieren", "${RED}Shift rechts klicken zum löschen", "" @@ -150,9 +152,9 @@ object Setup { val region = gameMap.region addAll( if (region == null) listOf( - "Region is null", + "${TEXT}Region ist nicht gesetzt", "", - "${GREEN}Klicken zum erstellen", + "${GREEN}Kicken zum erstellen (hier)", "" ) else listOf( "${TEXT}World: $IMPORTANT${region.world}", @@ -167,7 +169,9 @@ object Setup { "$TEXT Y: $IMPORTANT${region.max.y}", "$TEXT Z: $IMPORTANT${region.max.z}", "", - "${GREEN}Klicken zum editieren", + "${GREEN}Links klicken um min hier her zu verschieben", + "${GREEN}Rechts klicken um max hier her zu verschieben", + "", "${RED}Shift rechts klicken zum löschen", "" ) @@ -191,8 +195,7 @@ object Setup { "$TEXT Y: $IMPORTANT${gameMap.spawn.y}", "$TEXT Z: $IMPORTANT${gameMap.spawn.z}", "", - "${GREEN}Klicken zum editieren", - "${GREEN}Shift links klicken zum telportieren", + "${GREEN}Klicken um es hier her zu verschieben", "" ).build() ).setItem( @@ -202,7 +205,7 @@ object Setup { val hologram = gameMap.hologram addAll( if (hologram == null) listOf( - "Hologram is null", + "${TEXT}Hologram ist nicht gesetzt", "", "${GREEN}Klicken zum erstellen", "" @@ -213,8 +216,8 @@ object Setup { "$TEXT Y: $IMPORTANT${hologram.y}", "$TEXT Z: $IMPORTANT${hologram.z}", "", - "${GREEN}Klicken zum editieren", - "${GREEN}Shift links klicken zum telportieren", + "${GREEN}Klicken um es hier her zu verschieben", + "", "${RED}Shift rechts klicken zum löschen", "" )