Skip to content

Commit

Permalink
Added more admin commands and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
freundTech committed Apr 9, 2019
1 parent 7aa6239 commit 6d41566
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 97 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ eclipse/

# Gradle wrapper
.gradle

# Passwords
scripts/.private.sh
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ group 'com.freundtech.minecraft'
version '1.0-SNAPSHOT'

repositories {
maven {
url "https://hub.spigotmc.org/nexus/content/groups/public/"
}
maven { url "https://hub.spigotmc.org/nexus/content/groups/public/" }
mavenCentral()
maven { url "https://jitpack.io" }
}

dependencies {
implementation "org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "org.jetbrains.kotlin:kotlin-reflect"
implementation "com.github.kizitonwose.time:time:1.0.2"
testImplementation "org.jetbrains.kotlin:kotlin-test"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit"
}
Expand Down
5 changes: 5 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

source scripts/.private.sh
scp build/libs/oneslotserver-1.0-SNAPSHOT-all.jar [email protected]:plugins
mcrcon -H oneslotserver.fun reload
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.freundtech.minecraft.oneslotserver

import com.freundtech.minecraft.oneslotserver.extension.oneSlotServer
import com.freundtech.minecraft.oneslotserver.handler.*
import com.freundtech.minecraft.oneslotserver.handler.command.*
import com.freundtech.minecraft.oneslotserver.handler.event.AdvancementListener
import com.freundtech.minecraft.oneslotserver.handler.event.PlayerListener
import com.freundtech.minecraft.oneslotserver.handler.event.ServerListPingListener
import com.freundtech.minecraft.oneslotserver.handler.tick.TickHandler
import com.freundtech.minecraft.oneslotserver.util.*
import org.bukkit.GameRule
import com.kizitonwose.time.Interval
import com.kizitonwose.time.Second
import com.kizitonwose.time.seconds
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import java.util.*
Expand All @@ -25,8 +31,8 @@ class OneSlotServer : JavaPlugin() {
saveConfig()
}

val playTime by config.delegate<Time>(PLAY_TIME, 1800)
val pauseTime by config.delegate<Time>(PAUSE_TIME, 86400)
var playTime by config.delegateTime(PLAY_TIME, 1800.seconds)
var waitTime by config.delegateTime(WAIT_TIME, 86400.seconds)

val iconEmpty = ImageIO.read(getResource(ICON_EMPTY))
?: throw MissingResourceException("Couldn't find image $ICON_EMPTY", this::class.qualifiedName, ICON_EMPTY)
Expand All @@ -37,13 +43,16 @@ class OneSlotServer : JavaPlugin() {
override fun onEnable() {
instance = this

server.pluginManager.registerEvents(PlayerListener(), this)
server.pluginManager.registerEvents(ServerListPingListener(), this)
server.pluginManager.registerEvents(AdvancementListener(), this)
server.scheduler.scheduleSyncRepeatingTask(this, TickHandler(), 20, 20)
server.pluginManager.registerEvents(PlayerListener(this), this)
server.pluginManager.registerEvents(ServerListPingListener(this), this)
server.pluginManager.registerEvents(AdvancementListener(this), this)
server.scheduler.scheduleSyncRepeatingTask(this, TickHandler(this), 20, 20)

getCommand(SPECTATE)!!.setExecutor(CommandSpectate())
getCommand(UNSPECTATE)!!.setExecutor(CommandUnspectate())
getCommand(SPECTATE)!!.setExecutor(SpectateCommand(this))
getCommand(UNSPECTATE)!!.setExecutor(UnspectateCommand(this))
getCommand(SET_PLAY_TIME)!!.setExecutor(SetPlayTimeCommand(this))
getCommand(SET_WAIT_TIME)!!.setExecutor(SetWaitTimeCommand(this))
getCommand(SET_TIME_LEFT)!!.setExecutor(SetTimeLeftCommand(this))

val uuid = config.getString(ACTIVE_PLAYER)
if (uuid != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.freundtech.minecraft.oneslotserver.extension

import com.kizitonwose.time.Interval
import com.kizitonwose.time.Millisecond
import com.kizitonwose.time.milliseconds
import org.bukkit.configuration.ConfigurationSection

fun ConfigurationSection.getTime(path: String, default: Interval<*>): Interval<*> {
return this.getLong(path, default.inMilliseconds.longValue).milliseconds
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.freundtech.minecraft.oneslotserver.extension

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import com.freundtech.minecraft.oneslotserver.util.currentTime
import com.freundtech.minecraft.oneslotserver.util.delegate
import com.freundtech.minecraft.oneslotserver.util.getPrivateField
import com.freundtech.minecraft.oneslotserver.util.currentTime
import com.freundtech.minecraft.oneslotserver.util.delegateTime
import com.kizitonwose.time.*
import org.bukkit.GameMode
import org.bukkit.World
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
import org.bukkit.scheduler.BukkitRunnable
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.nio.file.StandardCopyOption.*
import java.util.*
import kotlin.collections.HashMap
Expand All @@ -27,23 +25,26 @@ class PlayerInfo(uuid: UUID) {
private val userConfig = YamlConfiguration.loadConfiguration(configPath.toFile())

var joinedAt = currentTime()
var timeLeft = userConfig.getLong("time_left", plugin.playTime)
var timeLeft = userConfig.getTime("time_left", plugin.playTime)
get() = field - (currentTime() - joinedAt)
var firstJoin by userConfig.delegate("first_join", joinedAt)
set(value) {
field = currentTime() + value - joinedAt
}
var firstJoin by userConfig.delegateTime("first_join", joinedAt)

fun save() {
userConfig.set("time_left", this.timeLeft)
userConfig.set("time_left", this.timeLeft.inMilliseconds.longValue)
userConfig.save(configPath.toFile())
}

fun hasTimeRemaining(): Boolean {
val now = currentTime()
if (this.firstJoin < now - OneSlotServer.instance.pauseTime) {
if (this.firstJoin < now - plugin.waitTime) {
this.firstJoin = now
this.timeLeft = OneSlotServer.instance.playTime
this.timeLeft = plugin.playTime
}

return this.timeLeft > 0
return this.timeLeft > 0.milliseconds
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.freundtech.minecraft.oneslotserver.extension

import com.kizitonwose.time.Interval
import com.kizitonwose.time.hours
import java.text.SimpleDateFormat
import java.util.*

val hoursFormat = SimpleDateFormat("HH:mm").also { it.timeZone = TimeZone.getTimeZone("GMT") }
val minutesFormat = SimpleDateFormat("mm:ss").also { it.timeZone = TimeZone.getTimeZone("GMT") }

fun Interval<*>.format(): String {
val result = StringBuilder()
return if (this > 1.hours) {
"${hoursFormat.format(inMilliseconds.longValue)} hours"
} else {
"${minutesFormat.format(inMilliseconds.longValue)} minutes"
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package com.freundtech.minecraft.oneslotserver.handler
package com.freundtech.minecraft.oneslotserver.handler.command

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.loadFromSharedData
import com.freundtech.minecraft.oneslotserver.extension.oneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.saveToSharedData
import com.freundtech.minecraft.oneslotserver.extension.setSpectator
import com.freundtech.minecraft.oneslotserver.extension.*
import com.freundtech.minecraft.oneslotserver.util.SPECTATE
import com.freundtech.minecraft.oneslotserver.util.currentTime
import com.freundtech.minecraft.oneslotserver.util.format
import com.freundtech.minecraft.oneslotserver.util.hoursFormat
import org.bukkit.GameMode
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

class CommandSpectate : CommandExecutor {
private val plugin = OneSlotServer.instance

class SpectateCommand(private val plugin: OneSlotServer) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
assert(label.equals(SPECTATE, ignoreCase = true))

Expand Down Expand Up @@ -54,9 +46,7 @@ class CommandSpectate : CommandExecutor {
}
}

class CommandUnspectate : CommandExecutor {
private val plugin = OneSlotServer.instance

class UnspectateCommand(private val plugin: OneSlotServer) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
val target = when (args.size) {
0 -> if (sender is Player) sender else run {
Expand All @@ -75,8 +65,8 @@ class CommandUnspectate : CommandExecutor {

target.oneSlotServer.joinedAt = currentTime()
if (!target.oneSlotServer.hasTimeRemaining()) {
val waitLeft = plugin.pauseTime - (currentTime() - target.oneSlotServer.firstJoin)
sender.sendMessage("You have no time left on this server. Please wait ${waitLeft.format(hoursFormat)} more hours.")
val waitLeft = plugin.waitTime - (currentTime() - target.oneSlotServer.firstJoin)
sender.sendMessage("You have no time left on this server. Please wait ${waitLeft.format()}.")
}
else if (!unsetSpectator(target)) {
sender.sendMessage("A player is already playing")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.freundtech.minecraft.oneslotserver.handler.command

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.PlayerInfo
import com.freundtech.minecraft.oneslotserver.extension.oneSlotServer
import com.kizitonwose.time.seconds
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender

class SetPlayTimeCommand(private val plugin: OneSlotServer) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (args.size != 1) {
return false
}
val time = args[0].toLongOrNull()?.seconds ?: return false
plugin.playTime = time
sender.sendMessage("Set play time to $time")
return true
}
}

class SetWaitTimeCommand(private val plugin: OneSlotServer) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (args.size != 1) {
return false
}
val time = args[0].toLongOrNull()?.seconds ?: return false
plugin.waitTime = time
sender.sendMessage("Set wait time to $time")
return true
}
}

class SetTimeLeftCommand(private val plugin: OneSlotServer) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (args.size !in 1..2) {
return false
}
val time = args[0].toLongOrNull()?.seconds ?: return false
if (args.size == 1) {
val player = plugin.activePlayer ?: run {
sender.sendMessage("Nobody is currently playing")
return false
}
player.oneSlotServer.timeLeft = time
sender.sendMessage("Set time left to $time for active player")
} else {
val playerName = args[1]
val player = sender.server.offlinePlayers.firstOrNull { it.name.equals(playerName, ignoreCase = true) } ?: run {
sender.sendMessage("Player $playerName is isn't known to this server")
return false
}
PlayerInfo(player.uniqueId).also { it.timeLeft = time }.save()
sender.sendMessage("Set time left to $time for $playerName")
}
return true
}
}

Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.freundtech.minecraft.oneslotserver.handler
package com.freundtech.minecraft.oneslotserver.handler.event

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import org.bukkit.GameRule.ANNOUNCE_ADVANCEMENTS
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerAdvancementDoneEvent

class AdvancementListener : Listener {
private val plugin = OneSlotServer.instance


class AdvancementListener(private val plugin: OneSlotServer) : Listener {
@EventHandler
fun onPlayerAdvancementDone(event: PlayerAdvancementDoneEvent) {
val showAdvancement = event.player.uniqueId == plugin.activePlayer?.uniqueId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package com.freundtech.minecraft.oneslotserver.handler
package com.freundtech.minecraft.oneslotserver.handler.event

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.loadFromSharedData
import com.freundtech.minecraft.oneslotserver.extension.oneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.saveToSharedData
import com.freundtech.minecraft.oneslotserver.extension.setSpectator
import com.freundtech.minecraft.oneslotserver.extension.*
import com.freundtech.minecraft.oneslotserver.util.*
import java.nio.file.Paths
import java.util.Date

import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.*
import org.bukkit.event.player.PlayerLoginEvent.Result
import java.nio.file.Path

class PlayerListener : Listener {
private val plugin = OneSlotServer.instance

class PlayerListener(private val plugin: OneSlotServer) : Listener {
@EventHandler
fun onPlayerLogin(event: PlayerLoginEvent) {
val now = currentTime()
Expand All @@ -27,18 +19,18 @@ class PlayerListener : Listener {
event.player.oneSlotServer.joinedAt = currentTime()

if (!playerInfo.hasTimeRemaining()) {
val waitLeft = plugin.pauseTime - (now - playerInfo.firstJoin)
val waitLeft = plugin.waitTime - (now - playerInfo.firstJoin)

if (!event.player.hasPermission(PERMISSION_SPECTATE)) {
event.disallow(Result.KICK_OTHER,
"You have no time left on this server. Please wait ${waitLeft.format(hoursFormat)} more hours.")
"You have no time left on this server. Please wait ${waitLeft.format()}.")
}
} else if (activePlayer != null) {
val waitLeft = activePlayer.oneSlotServer.timeLeft

if (!event.player.hasPermission(PERMISSION_SPECTATE)) {
event.disallow(Result.KICK_FULL,
"A person is already playing. Please wait ${waitLeft.format(minutesFormat)} more minutes.")
"A person is already playing. Please wait ${waitLeft.format()}.")
}
} else {
plugin.activePlayer = event.player
Expand All @@ -64,7 +56,7 @@ class PlayerListener : Listener {
event.player.apply {
sendMessage(arrayOf(
"Welcome to the one slot server.",
"You have ${this.oneSlotServer.timeLeft / 60} minutes left to play."
"You have ${oneSlotServer.timeLeft.format()} left to play."
))
}
} else if (event.player.hasPermission(PERMISSION_SPECTATE)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.freundtech.minecraft.oneslotserver.handler
package com.freundtech.minecraft.oneslotserver.handler.event

import com.freundtech.minecraft.oneslotserver.OneSlotServer
import com.freundtech.minecraft.oneslotserver.extension.format
import com.freundtech.minecraft.oneslotserver.extension.oneSlotServer
import com.freundtech.minecraft.oneslotserver.util.currentTime
import com.freundtech.minecraft.oneslotserver.util.format
import com.freundtech.minecraft.oneslotserver.util.minutesFormat
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.server.ServerListPingEvent

class ServerListPingListener : Listener{
private val plugin = OneSlotServer.instance

class ServerListPingListener(private val plugin: OneSlotServer) : Listener{
private var iconEmpty = Bukkit.loadServerIcon(plugin.iconEmpty)
private var iconFull = Bukkit.loadServerIcon(plugin.iconFull)

Expand All @@ -27,7 +23,7 @@ class ServerListPingListener : Listener{
plugin.activePlayer?.let {
val waitLeft = it.oneSlotServer.timeLeft

event.motd = "A player is currently playing. Please wait ${waitLeft.format(minutesFormat)} more minutes."
event.motd = "A player is currently playing. Please wait ${waitLeft.format()}."
event.setServerIcon(iconFull)
} ?: run {
event.motd = "Nobody is playing. You can join the server."
Expand Down
Loading

0 comments on commit 6d41566

Please sign in to comment.