Skip to content

Commit

Permalink
Improved handling of offline player data
Browse files Browse the repository at this point in the history
  • Loading branch information
freundTech committed Apr 13, 2019
1 parent 6d41566 commit 395d63b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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.OfflinePlayer
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
import java.nio.file.Files
Expand Down Expand Up @@ -37,6 +38,10 @@ class PlayerInfo(uuid: UUID) {
userConfig.save(configPath.toFile())
}

fun join() {
joinedAt = currentTime()
}

fun hasTimeRemaining(): Boolean {
val now = currentTime()
if (this.firstJoin < now - plugin.waitTime) {
Expand All @@ -53,6 +58,9 @@ val playerCache = HashMap<UUID, PlayerInfo>()
val Player.oneSlotServer: PlayerInfo
get() = playerCache.getOrPut(uniqueId) { PlayerInfo(uniqueId) }

val OfflinePlayer.oneSlotServer: PlayerInfo
get() = playerCache.getOrPut(uniqueId) { PlayerInfo(uniqueId) }

fun Player.setSpectator() {
this.gameMode = GameMode.SPECTATOR
this.isSleepingIgnored = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UnspectateCommand(private val plugin: OneSlotServer) : CommandExecutor {
else -> return false
}

target.oneSlotServer.joinedAt = currentTime()
target.oneSlotServer.join()
if (!target.oneSlotServer.hasTimeRemaining()) {
val waitLeft = plugin.waitTime - (currentTime() - target.oneSlotServer.firstJoin)
sender.sendMessage("You have no time left on this server. Please wait ${waitLeft.format()}.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SetTimeLeftCommand(private val plugin: OneSlotServer) : CommandExecutor {
sender.sendMessage("Player $playerName is isn't known to this server")
return false
}
PlayerInfo(player.uniqueId).also { it.timeLeft = time }.save()
player.oneSlotServer.also { it.timeLeft = time }.save()
sender.sendMessage("Set time left to $time for $playerName")
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PlayerListener(private val plugin: OneSlotServer) : Listener {
val playerInfo = event.player.oneSlotServer
val activePlayer = plugin.activePlayer

event.player.oneSlotServer.joinedAt = currentTime()
event.player.oneSlotServer.join()

if (!playerInfo.hasTimeRemaining()) {
val waitLeft = plugin.waitTime - (now - playerInfo.firstJoin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ServerListPingListener(private val plugin: OneSlotServer) : Listener{
event.maxPlayers = 1

event.removeAll { player -> Boolean
player.uniqueId != plugin.activePlayer?.player?.uniqueId
player.uniqueId != plugin.activePlayer?.uniqueId
}

plugin.activePlayer?.let {
Expand Down

0 comments on commit 395d63b

Please sign in to comment.