1
0
Fork 0

Add map-specific spawn locations for items and players

This commit is contained in:
Moritz Ruth 2020-06-20 18:51:50 +02:00
parent 29b7b43a54
commit b81e6913fc
No known key found for this signature in database
GPG key ID: AFD57E23E753841B
11 changed files with 121 additions and 75 deletions

View file

@ -4,14 +4,14 @@ import de.moritzruth.spigot_ttt.game.AbortCommand
import de.moritzruth.spigot_ttt.game.InfoCommand import de.moritzruth.spigot_ttt.game.InfoCommand
import de.moritzruth.spigot_ttt.game.ReviveCommand import de.moritzruth.spigot_ttt.game.ReviveCommand
import de.moritzruth.spigot_ttt.game.StartCommand import de.moritzruth.spigot_ttt.game.StartCommand
import de.moritzruth.spigot_ttt.game.items.AddItemSpawnCommand import de.moritzruth.spigot_ttt.game.worlds.AddSpawnLocationCommand
import de.moritzruth.spigot_ttt.game.worlds.VotingCommand import de.moritzruth.spigot_ttt.game.worlds.VotingCommand
object CommandManager { object CommandManager {
fun initializeCommands() { fun initializeCommands() {
StartCommand() StartCommand()
AbortCommand() AbortCommand()
AddItemSpawnCommand() AddSpawnLocationCommand()
ReviveCommand() ReviveCommand()
ResourcepackCommand() ResourcepackCommand()
ReloadTTTConfigCommand() ReloadTTTConfigCommand()

View file

@ -16,11 +16,7 @@ import de.moritzruth.spigot_ttt.game.worlds.TTTWorld
import de.moritzruth.spigot_ttt.plugin import de.moritzruth.spigot_ttt.plugin
import de.moritzruth.spigot_ttt.utils.call import de.moritzruth.spigot_ttt.utils.call
import de.moritzruth.spigot_ttt.utils.heartsToHealth import de.moritzruth.spigot_ttt.utils.heartsToHealth
import de.moritzruth.spigot_ttt.utils.teleportToWorldSpawn import org.bukkit.*
import org.bukkit.GameRule
import org.bukkit.Material
import org.bukkit.Sound
import org.bukkit.SoundCategory
import org.bukkit.block.Block import org.bukkit.block.Block
import kotlin.random.Random import kotlin.random.Random
@ -124,6 +120,7 @@ object GameManager {
PlayerManager.createTTTPlayers() PlayerManager.createTTTPlayers()
phase = GamePhase.PREPARING phase = GamePhase.PREPARING
PlayerManager.teleportPlayersToSpawnLocations()
world.run { world.run {
setStorm(Random.nextInt(4) == 1) setStorm(Random.nextInt(4) == 1)
@ -133,7 +130,6 @@ object GameManager {
} }
PlayerManager.tttPlayers.forEach { PlayerManager.tttPlayers.forEach {
it.player.teleportToWorldSpawn()
it.addDefaultClassItems() it.addDefaultClassItems()
it.player.health = heartsToHealth(10.0) it.player.health = heartsToHealth(10.0)
} }
@ -160,8 +156,8 @@ object GameManager {
if (it.role.group.canUseTeamChat) { if (it.role.group.canUseTeamChat) {
it.player.sendMessage( it.player.sendMessage(
"${TTTPlugin.prefix}Schreibe '$TEAM_CHAT_PREFIX' vor deine Nachrichten, um den Team-Chat" + "${TTTPlugin.prefix}${ChatColor.GOLD}Schreibe einen Punkt vor deine Nachrichten, um den " +
"zu verwenden.") "Team-Chat zu verwenden.")
} }
} }

View file

@ -1,31 +0,0 @@
package de.moritzruth.spigot_ttt.game.items
import de.moritzruth.spigot_ttt.COMMAND_RESPONSE_PREFIX
import de.moritzruth.spigot_ttt.plugin
import de.moritzruth.spigot_ttt.utils.EmptyTabCompleter
import org.bukkit.ChatColor
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class AddItemSpawnCommand: CommandExecutor {
init {
plugin.getCommand("additemspawn")?.let {
it.setExecutor(this)
it.tabCompleter = EmptyTabCompleter
}
}
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (sender !is Player) {
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.RED}Dieser Befehl kann nur von Spielern verwendet werden.")
return true
}
ItemSpawner.addItemSpawnLocation(sender.location)
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.GREEN}Ein Waffenspawn wurde an deiner Position hinzugefügt.")
return true
}
}

View file

@ -1,29 +1,9 @@
package de.moritzruth.spigot_ttt.game.items package de.moritzruth.spigot_ttt.game.items
import de.moritzruth.spigot_ttt.game.GameManager import de.moritzruth.spigot_ttt.game.GameManager
import de.moritzruth.spigot_ttt.utils.ConfigurationFile
import de.moritzruth.spigot_ttt.utils.roundToCenter
import org.bukkit.Location
import java.util.* import java.util.*
object ItemSpawner { object ItemSpawner {
private const val CONFIG_PATH = "spawn-locations"
private val spawnLocationsConfig = ConfigurationFile("spawnLocations")
private fun getSpawnLocations(): Set<Location> {
return spawnLocationsConfig.getStringList(CONFIG_PATH).map {
val (x, y, z) = it.split(":").map(String::toDouble)
Location(GameManager.world, x, y, z)
}.toSet()
}
private fun setSpawnLocations(spawnLocations: Set<Location>) {
spawnLocationsConfig.set(CONFIG_PATH, spawnLocations.map {
"${it.x}:${it.y}:${it.z}"
})
}
fun spawnWeapons() { fun spawnWeapons() {
val spawningItems = mutableListOf<TTTItem<*>>() val spawningItems = mutableListOf<TTTItem<*>>()
for (tttItem in ItemManager.ITEMS) { for (tttItem in ItemManager.ITEMS) {
@ -32,7 +12,7 @@ object ItemSpawner {
} }
var itemsIterator = spawningItems.shuffled().iterator() var itemsIterator = spawningItems.shuffled().iterator()
for (location in getSpawnLocations()) { for (location in GameManager.tttWorld!!.spawnLocations.getItemSpawnLocations()) {
if (!itemsIterator.hasNext()) { if (!itemsIterator.hasNext()) {
itemsIterator = spawningItems.shuffled().iterator() itemsIterator = spawningItems.shuffled().iterator()
} }
@ -40,12 +20,4 @@ object ItemSpawner {
ItemManager.dropItem(location, itemsIterator.next()) ItemManager.dropItem(location, itemsIterator.next())
} }
} }
fun addItemSpawnLocation(location: Location) {
val spawnLocations = getSpawnLocations().toMutableSet()
spawnLocations.add(location.roundToCenter())
setSpawnLocations(spawnLocations)
spawnLocationsConfig.save()
}
} }

View file

@ -133,6 +133,16 @@ object PlayerManager {
playersWithoutRole.forEach { tttPlayers.add(TTTPlayer(it, Role.INNOCENT, classesIterator.next())) } playersWithoutRole.forEach { tttPlayers.add(TTTPlayer(it, Role.INNOCENT, classesIterator.next())) }
} }
fun teleportPlayersToSpawnLocations() {
fun createIterator() = GameManager.tttWorld!!.spawnLocations.getPlayerSpawnLocations().shuffled().iterator()
var spawnLocationsIterator = createIterator()
for (tttPlayer in tttPlayers) {
if (!spawnLocationsIterator.hasNext()) spawnLocationsIterator = createIterator()
tttPlayer.player.teleport(spawnLocationsIterator.next())
}
}
class NotEnoughPlayersException( class NotEnoughPlayersException(
val actual: Int, val actual: Int,
val required: Int val required: Int

View file

@ -0,0 +1,46 @@
package de.moritzruth.spigot_ttt.game.worlds
import de.moritzruth.spigot_ttt.COMMAND_RESPONSE_PREFIX
import de.moritzruth.spigot_ttt.game.GameManager
import de.moritzruth.spigot_ttt.plugin
import de.moritzruth.spigot_ttt.utils.createTabCompleter
import org.bukkit.ChatColor
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
class AddSpawnLocationCommand: CommandExecutor {
init {
plugin.getCommand("addspawnlocation")?.let {
it.setExecutor(this)
it.tabCompleter = createTabCompleter { _, index -> when(index) {
0 -> listOf("item", "player")
else -> null
} }
}
}
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (sender !is Player) {
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.RED}Dieser Befehl kann nur von Spielern verwendet werden.")
} else if (args.count() == 0) {
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.RED}Falsche Verwendung.")
} else {
val tttWorld = GameManager.tttWorld
if (tttWorld == null) {
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.RED}Du befindest dich nicht in einer TTT-Welt.")
} else {
when(args[0].toLowerCase()) {
"item" -> tttWorld.spawnLocations.addItemSpawnLocation(sender.location)
"player" -> tttWorld.spawnLocations.addPlayerSpawnLocation(sender.location)
}
sender.sendMessage("$COMMAND_RESPONSE_PREFIX${ChatColor.GREEN}Ein Spawnpunkt wurde an deiner " +
"Position hinzugefügt.")
}
}
return true
}
}

View file

@ -0,0 +1,30 @@
package de.moritzruth.spigot_ttt.game.worlds
import de.moritzruth.spigot_ttt.utils.getSpawnLocations
import de.moritzruth.spigot_ttt.utils.roundToCenter
import de.moritzruth.spigot_ttt.utils.setSpawnLocations
import org.bukkit.Location
class SpawnLocationsManager(private val tttWorld: TTTWorld) {
fun getItemSpawnLocations() = tttWorld.config.getSpawnLocations("spawn-locations.items", tttWorld.world!!, false)
private fun setItemSpawnLocations(spawnLocations: Set<Location>) =
tttWorld.config.setSpawnLocations("spawn-locations.items", spawnLocations, false)
fun getPlayerSpawnLocations() = tttWorld.config.getSpawnLocations("spawn-locations.players", tttWorld.world!!, true)
private fun setPlayerSpawnLocations(spawnLocations: Set<Location>) =
tttWorld.config.setSpawnLocations("spawn-locations.players", spawnLocations, true)
fun addItemSpawnLocation(location: Location) {
val spawnLocations = getItemSpawnLocations().toMutableSet()
spawnLocations.add(location.roundToCenter())
setItemSpawnLocations(spawnLocations)
tttWorld.config.save()
}
fun addPlayerSpawnLocation(location: Location) {
val spawnLocations = getPlayerSpawnLocations().toMutableSet()
spawnLocations.add(location.roundToCenter())
setPlayerSpawnLocations(spawnLocations)
tttWorld.config.save()
}
}

View file

@ -18,6 +18,7 @@ class TTTWorld(private val sourceWorldDir: File) {
private val actualWorldName = "${WORLD_PREFIX}${name}" private val actualWorldName = "${WORLD_PREFIX}${name}"
private val worldDir = plugin.server.worldContainer.resolve("./$actualWorldName") private val worldDir = plugin.server.worldContainer.resolve("./$actualWorldName")
var world: World? = plugin.server.getWorld(actualWorldName); private set var world: World? = plugin.server.getWorld(actualWorldName); private set
val spawnLocations = SpawnLocationsManager(this)
init { init {
if (world != null) unloadWorld() if (world != null) unloadWorld()

View file

@ -0,0 +1,21 @@
package de.moritzruth.spigot_ttt.utils
import org.bukkit.Location
import org.bukkit.World
import org.bukkit.configuration.ConfigurationSection
fun ConfigurationSection.getSpawnLocations(path: String, world: World, withDirection: Boolean): Set<Location> =
getStringList(path).map {
val parts = it.split(":")
val (x, y, z) = parts.map(String::toDouble)
val yaw = if (withDirection) parts[3].toFloat() else 0F
val pitch = if (withDirection) parts[4].toFloat() else 0F
Location(world, x, y, z, yaw, pitch)
}.toSet()
fun ConfigurationSection.setSpawnLocations(path: String, locations: Iterable<Location>, withDirection: Boolean) =
set(path, locations.map { location ->
"${location.x}:${location.y}:${location.z}"
.let { if (withDirection) "${it}:${location.yaw}:${location.pitch}" else it }
})

View file

@ -6,5 +6,6 @@ fun Location.roundToCenter() =
Location(world, Location(world,
roundToHalf(x), roundToHalf(x),
roundToHalf(y), roundToHalf(y),
roundToHalf(z) roundToHalf(z),
yaw, pitch
) )

View file

@ -22,10 +22,10 @@ commands:
permission: ttt.abort permission: ttt.abort
description: Go back to the lobby world and abort the TTT game, if it is running description: Go back to the lobby world and abort the TTT game, if it is running
additemspawn: addspawnlocation:
usage: /additemspawn usage: /addspawnlocation <'item'|'player'>
permission: ttt.additemspawn permission: ttt.addspawnlocation
description: Add an item spawn description: Add an item or player spawn
revive: revive:
usage: /revive [player] ['here'] usage: /revive [player] ['here']