Commit ebfdccc0 authored by Linus K.'s avatar Linus K.
Browse files

Add sleepvote

parent 04f7d650
......@@ -6,3 +6,8 @@ depend:
- Kotlin
main: me.linuscde.mcplugins.autosyston.Plugin
commands:
sleepvote:
description: Majority vote to skip the night
usage: /<command>
\ No newline at end of file
package me.linuscde.mcplugins.autosyston
import me.linuscde.mcplugins.autosyston.commands.SleepVote
import me.linuscde.mcplugins.autosyston.listeners.*
import org.bukkit.Bukkit
import org.bukkit.command.TabCompleter
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.logging.Level
......@@ -36,12 +38,14 @@ class Plugin: JavaPlugin() {
logger.log(Level.INFO, "Plugin enabled")
}
fun registerCommands() {}
fun registerCommands() {
getCommand("sleepvote")?.setExecutor(SleepVote())
}
fun registerTabCompleters() {
//for (commandName in arrayOf("a", "b")) {
// getCommand(commandName)?.tabCompleter = getCommand(commandName)?.executor as TabCompleter
//}
for (commandName in arrayOf("sleepvote")) {
getCommand(commandName)?.tabCompleter = getCommand(commandName)?.executor as TabCompleter
}
}
fun registerListeners() {
......
package me.linuscde.mcplugins.autosyston.commands
import me.linuscde.mcplugins.autosyston.Plugin
import org.bukkit.Bukkit
import org.bukkit.World
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
class SleepVote: CommandExecutor, TabCompleter {
companion object {
const val NIGHT_START = 12500L // usually 13000, but voting a bit earlier is probably better
const val DAY_START = 0L // usually 13000, but voting a bit earlier is probably better
const val TIMELAPSE_WORLD_TICKS_PER_SECOND = 1500L
const val VOTE_DURATION = 60*1000 // 1 minute
var voteStartedAt: Long = -1
val voters = arrayListOf<Player>()
private fun checkVotes() {
voters.removeIf { !it.isOnline }
if(voteStartedAt == -1L) return
if(System.currentTimeMillis() - voteStartedAt > VOTE_DURATION) {
Bukkit.broadcastMessage("${Plugin.prefix} §cVote timed out. Not enough people votes to skip the night.")
voteStartedAt = -1
voters.clear()
}else if(voters.size * 1.0 / Bukkit.getOnlinePlayers().size >= 0.5) {
Bukkit.broadcastMessage("${Plugin.prefix} §aAt least 50% have sleep voted (${voters.size}/${Bukkit.getOnlinePlayers().size}).")
Bukkit.broadcastMessage("${Plugin.prefix} §aSkipping the night...")
skipNight()
voteStartedAt = -1
voters.clear()
}
}
fun initScheduler(plugin: org.bukkit.plugin.Plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, { checkVotes() }, 10, 10)
}
private fun skipNight() {
for(world in Bukkit.getWorlds().filter { it.environment == World.Environment.NORMAL }) {
val totalFastForward = (24000 - world.time) + DAY_START
val worldTicksPerTick = TIMELAPSE_WORLD_TICKS_PER_SECOND / 20
val schedulerCount = totalFastForward / worldTicksPerTick
for(i in 0 until schedulerCount) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, {
world.fullTime += worldTicksPerTick
}, 1*i)
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.instance, {
Bukkit.broadcastMessage("${Plugin.prefix} §aThe night was skipped.")
}, 1*schedulerCount)
}
}
}
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if(sender !is Player) {
sender.sendMessage("This command is only for players.")
return true
}
if(sender in voters) {
sender.sendMessage("${Plugin.prefix} §cYou have already voted this round.")
}
if(voteStartedAt == -1L) {
if(sender.world.time < NIGHT_START) {
sender.sendMessage("${Plugin.prefix} §cIt is not night, yet.")
return true
}
voteStartedAt = System.currentTimeMillis()
voters.clear()
voters.add(sender)
Bukkit.broadcastMessage("${Plugin.prefix} §c${sender.name}§a started a sleep vote. Please run §c/sleepvote§a in the next minute if you want the night to be skipped.")
Bukkit.broadcastMessage("${Plugin.prefix} §c${sender.name}§a Run §c/sleepvote§a you want the night to be skipped.")
}else {
voters.add(sender)
Bukkit.broadcastMessage("${Plugin.prefix} §c${sender.name}§a voted to skip the night (${voters.size}/${Bukkit.getOnlinePlayers().size}).")
}
checkVotes()
return true
}
override fun onTabComplete(sender: CommandSender, cmd: Command, label: String, args: Array<out String>) = mutableListOf<String>()
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment