Pair programming with @Nanokie on a world swapper plugin :D

This commit is contained in:
Alex Zenla 2023-03-30 21:30:21 -07:00
parent 53bdc3a4cb
commit b7ce799593
Signed by: alex
GPG Key ID: C0780728420EBFE5
3 changed files with 54 additions and 1 deletions

View File

@ -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()

View File

@ -9,6 +9,7 @@ object ChaosModules {
KillRandomPlayer(plugin),
TntAllPlayers(plugin),
MegaTnt(plugin),
PlayerSwap(plugin)
PlayerSwap(plugin),
WorldSwapper(plugin)
).shuffled()
}

View File

@ -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
}
}
}
}
}