Skip to content

Commit

Permalink
✨ more efficient arena reset
Browse files Browse the repository at this point in the history
  • Loading branch information
b8daniel committed Apr 19, 2022
1 parent 8f769d3 commit ae00806
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
- `kill`: cooldown until a player can kill again after a kill
- `timer`: how long the timer should count down until start
- `restart`: time to wait after a game until automatic restart
- `noReplace`: blocks that shouldn't be replaced
- `autostart`: should the game start automatically when enough players joined
- `minimumPlayers`: minimum players to start automatically
- `teams`: list of teams
- `material`: material used to replace blocks (`RED`|`BLUE`), only one team per material
- `displayName`: name used for the team
Expand All @@ -24,6 +21,9 @@
- `z`: z-pos
- `yaw`: yaw (rotation left-right)
- `pitch`: pitch (rotation up-down)
- `noReplace`: blocks that shouldn't be replaced
- `autostart`: should the game start automatically when enough players joined
- `minimumPlayers`: minimum players to start automatically
- `colorRadius`: radius of the color
- `arenaWorldName`: name of the base world to copy for an arena
- `theme`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Paintball : JavaPlugin() {
pm.registerEvents(SnowballHitPlayer(), this)
pm.registerEvents(SnowballDrop(), this)

Game.setupNewArenaWorld(true)
Game.setupNewArenaWorld()
Scores.createAndResetScores()
}

Expand Down
32 changes: 16 additions & 16 deletions src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.*
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.*
import org.bukkit.entity.EntityType
import org.bukkit.entity.Firework
import org.bukkit.entity.Player
import org.bukkit.entity.ThrowableProjectile
import org.bukkit.event.entity.EntityEvent
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemFlag
Expand Down Expand Up @@ -44,7 +47,7 @@ object Game {
return shooter to (shooter.team ?: return null)
}

val projectileItem = ItemStack(Material.EGG)
val projectileItem = ItemStack(if (Paintball.gameConfig.easterMode) Material.EGG else Material.SNOWBALL )
init {
projectileItem.addUnsafeEnchantment(Enchantment.CHANNELING, 1)
projectileItem.itemMeta = projectileItem.itemMeta.let {
Expand All @@ -67,18 +70,15 @@ object Game {

var arenaWorld: World? = null

fun setupNewArenaWorld(deleteOldWorlds: Boolean = false) {
fun setupNewArenaWorld() {
// https://www.spigotmc.org/threads/world-copy-and-load.316248/

thread {
if (deleteOldWorlds) {
val worldFolders =
Bukkit.getWorldContainer().listFiles { file -> Regex("arena-tmp-\\w+").matches(file.name) }
?: return@thread
Bukkit.getLogger().info("deleting ${worldFolders.size} old arena worlds")
worldFolders.forEach {
Bukkit.unloadWorld(it.name, false)
it.deleteRecursively()
if (Paintball.gameConfig.lastArenaName.isNotBlank()) {
val worldFolder = File(Bukkit.getWorldContainer(), Paintball.gameConfig.lastArenaName)
if (worldFolder.exists() && worldFolder.isDirectory) {
Bukkit.getLogger().info("deleting an old arena world")
worldFolder.deleteRecursively()
}
}

Expand All @@ -104,7 +104,6 @@ object Game {

File(to.absolutePath, "uid.dat").delete()

val prevWorld = arenaWorld
Bukkit.getScheduler().runTask(Paintball.INSTANCE, Runnable {
arenaWorld = Bukkit.createWorld(WorldCreator(randomWorldName).generator(EmptyWorldGen()))
arenaWorld?.isAutoSave = false
Expand All @@ -116,10 +115,11 @@ object Game {
it.teleport(spawn)
}
}
if (prevWorld == null) return@Runnable

Bukkit.unloadWorld(prevWorld, false)
prevWorld.worldFolder.deleteRecursively()
if (Paintball.gameConfig.lastArenaName.isNotBlank())
Bukkit.unloadWorld(Paintball.gameConfig.lastArenaName, false)

Paintball.gameConfig.lastArenaName = randomWorldName
})
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ object Game {
}
}

Bukkit.getWorlds().first().getEntitiesByClass(Snowball::class.java).forEach {
Bukkit.getWorlds().first().getEntitiesByClass(ThrowableProjectile::class.java).forEach {
if (!it.item.isSimilar(projectileItem)) return@forEach
it.fizzleOut()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class GameConfig() : ConfigurationSerializable {
var teams = DefaultConfig.teams
var colorRadius = DefaultConfig.colorRadius
var arenaWorldName = DefaultConfig.arenaWorldName
var lastArenaName = DefaultConfig.lastArenaName
var easterMode = DefaultConfig.easterMode

var noReplace = DefaultConfig.noReplace

Expand Down Expand Up @@ -63,30 +65,36 @@ class GameConfig() : ConfigurationSerializable {
if (filteredNoReplace != null) noReplace = filteredNoReplace

(cfg["autostart"] as? Boolean)?.let { autostart = it }
(cfg["easterMode"] as? Boolean)?.let { easterMode = it }
(cfg["minimumPlayers"] as? Int)?.let { minimumPlayers = it }
(cfg["colorRadius"] as? Int)?.let { colorRadius = it }
(cfg["arenaWorldName"] as? String)?.let { arenaWorldName = it }
(cfg["lastArenaName"] as? String)?.let { lastArenaName = it }

}

override fun serialize(): MutableMap<String, Any> {
return mutableMapOf(
"durations" to durations.map { it.key to ConfigDuration(it.value) }.toMap(),
"teams" to teams,
"noReplace" to noReplace.map { it.name },
"autostart" to autostart,
"easterMode" to easterMode,
"minimumPlayers" to minimumPlayers,
"teams" to teams,
"colorRadius" to colorRadius,
"arenaWorldName" to arenaWorldName,
"lastArenaName" to lastArenaName,
)
}


private object DefaultConfig {
const val minimumPlayers = 4
const val autostart = true
const val easterMode = false
const val colorRadius = 3
const val arenaWorldName = "arena"
const val lastArenaName = ""
val noReplace = mutableListOf(
Material.REDSTONE_LAMP, Material.GLOWSTONE, Material.BARREL, Material.BEACON, Material.BEDROCK
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.*
import org.bukkit.entity.Player
import org.bukkit.entity.Sheep
import org.bukkit.entity.Snowball
import org.bukkit.entity.ThrowableProjectile
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.ProjectileHitEvent
Expand Down Expand Up @@ -74,7 +74,7 @@ class SnowballHitPlayer : Listener {
}

companion object {
fun Snowball.fizzleOut() {
fun ThrowableProjectile.fizzleOut() {
this.world.spawnParticle(Particle.FIREWORKS_SPARK, this.location, 2, 0.1, 0.1, 0.1, 0.0)
this.remove()
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ game:
==: de.crightgames.blxckoxymoron.paintball.game.config.ConfigDuration
duration: 30
unit: SECONDS
noReplace:
- REDSTONE_LAMP
- GLOWSTONE
- BARRIER
- BEACON
- BEDROCK
autostart: true
minimumPlayers: 4
teams:
- ==: de.crightgames.blxckoxymoron.paintball.game.config.ConfigTeam
material: BLUE
Expand All @@ -54,6 +46,15 @@ game:
material: RED
displayName: §4Rot§r
spawn: null
noReplace:
- REDSTONE_LAMP
- GLOWSTONE
- BARRIER
- BEACON
- BEDROCK
autostart: true
easterMode: false
minimumPlayers: 4
colorRadius: 3
arenaWorldName: arena
theme:
Expand Down

0 comments on commit ae00806

Please sign in to comment.