diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt index 1e6a538..3cab850 100644 --- a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt @@ -1,7 +1,11 @@ package gay.pizza.foundation.chaos import org.bukkit.Location +import org.bukkit.Server import org.bukkit.entity.Player fun Location.nearestPlayer(): Player? = world?.players?.minByOrNull { it.location.distance(this) } + +fun Server.randomPlayer(): Player? = + onlinePlayers.randomOrNull() diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt index 2fd69d2..e1cf462 100644 --- a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt @@ -9,6 +9,7 @@ object ChaosModules { KillRandomPlayer(plugin), TntAllPlayers(plugin), MegaTnt(plugin), - PlayerSwap(plugin) + PlayerSwap(plugin), + WorldSwapper(plugin) ).shuffled() } diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/WorldSwapper.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/WorldSwapper.kt new file mode 100644 index 0000000..b611d40 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/WorldSwapper.kt @@ -0,0 +1,48 @@ +package gay.pizza.foundation.chaos.modules + +import gay.pizza.foundation.chaos.randomPlayer +import org.bukkit.ChunkSnapshot +import org.bukkit.plugin.Plugin + +class WorldSwapper(val plugin: Plugin) : ChaosModule { + override fun id(): String = "world-swapper" + override fun name(): String = "World Swapper" + override fun what(): String = "Swaps the world vertically on activation, and un-swaps it on deactivation." + + var snapshot: ChunkSnapshot? = null + + override fun activate() { + val player = plugin.server.randomPlayer() ?: return + val chunk = player.world.getChunkAt(player.location) + val localSnapshot = chunk.chunkSnapshot + + for (x in 0..15) { + for (z in 0..15) { + val heightRange = (chunk.world.minHeight + 1) until chunk.world.maxHeight + for (y in heightRange) { + val targetBlock = chunk.getBlock(x, y, z) + val inverseY = heightRange.random() + val nextBlock = localSnapshot.getBlockData(x, inverseY, z) + targetBlock.setBlockData(nextBlock, true) + } + } + } + snapshot = localSnapshot + } + + override fun deactivate() { + val localSnapshot = snapshot ?: return + val world = plugin.server.getWorld(localSnapshot.worldName) ?: return + val chunk = world.getChunkAt(localSnapshot.x, localSnapshot.z) + + for (x in 0..15) { + for (z in 0..15) { + val heightRange = chunk.world.minHeight + 1 until chunk.world.maxHeight + for (y in heightRange) { + val originalBlock = localSnapshot.getBlockData(x, y, z) + chunk.getBlock(x, y, z).blockData = originalBlock + } + } + } + } +}