This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
forked from rospino74/BanList
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
203 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/it/bteitalia/datalist/handlers/PlaytimeRequestHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021 Build The Earth Italia | ||
* This file (PlaytimeRequestHandler.kt) and its related project (DataList) are governed by the Apache 2.0 license. | ||
* You may not use them except in compliance with the License which can be found at: | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
package it.bteitalia.datalist.handlers | ||
|
||
import com.google.gson.JsonObject | ||
import com.sun.net.httpserver.HttpExchange | ||
import it.bteitalia.datalist.DataList | ||
import it.bteitalia.datalist.listers.PlaytimeLister | ||
import it.bteitalia.datalist.server.RequestHandler | ||
import org.bukkit.Bukkit | ||
|
||
@Suppress("unused") | ||
internal class PlaytimeRequestHandler : RequestHandler() { | ||
override fun onIncomingRequest(httpExchange: HttpExchange) { | ||
val out = JsonObject() | ||
|
||
//json di player online | ||
out.add("playtime", PlaytimeLister().getJSON()) | ||
|
||
//invio i dati | ||
flushData(out.toString()) | ||
} | ||
|
||
companion object { | ||
init { | ||
Bukkit.getPluginManager().registerEvents(PlaytimeLister(), DataList.getInstance()) | ||
} | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/main/kotlin/it/bteitalia/datalist/listers/PlaytimeLister.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright (c) 2021 Build The Earth Italia | ||
* This file (PlaytimeLister.kt) and its related project (DataList) are governed by the Apache 2.0 license. | ||
* You may not use them except in compliance with the License which can be found at: | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
package it.bteitalia.datalist.listers | ||
|
||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParser | ||
import it.bteitalia.datalist.DataList | ||
import org.bukkit.Bukkit | ||
import org.bukkit.Statistic | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.EventPriority | ||
import org.bukkit.event.Listener | ||
import org.bukkit.event.player.PlayerJoinEvent | ||
import org.bukkit.event.player.PlayerQuitEvent | ||
import java.io.FileReader | ||
import java.io.FileWriter | ||
import java.util.* | ||
import kotlin.collections.HashMap | ||
|
||
|
||
@Suppress("unused") | ||
internal class PlaytimeLister : Listener { | ||
fun getJSON(): JsonArray { | ||
//oggetto di out | ||
val out = JsonArray() | ||
|
||
for (player in Bukkit.getOfflinePlayers()) { | ||
val playtime: Int = | ||
if (DataList.getInstance().version <= 1.13) | ||
player.player?.getStatistic(Statistic.PLAY_ONE_TICK) | ||
else { | ||
val statistic = Statistic.valueOf("PLAY_ONE_MINUTE") | ||
player.player?.getStatistic(statistic) | ||
} ?: getPlaytimeFromList(player.uniqueId) | ||
|
||
// Se playtime è 0 ignoro | ||
if (playtime <= 0) | ||
continue | ||
|
||
// Creo oggetto | ||
val jsonPlayerEntry = JsonObject() | ||
jsonPlayerEntry.addProperty("name", player.name) | ||
jsonPlayerEntry.addProperty("ticks", playtime) | ||
|
||
out.add(jsonPlayerEntry) | ||
} | ||
|
||
//ritorno l'array | ||
return out | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
fun onPlayerLogout(evt: PlayerQuitEvent) { | ||
val playtime: Int? = | ||
if (DataList.getInstance().version <= 1.12) | ||
evt.player?.getStatistic(Statistic.PLAY_ONE_TICK) | ||
else { | ||
val statistic = Statistic.valueOf("PLAY_ONE_MINUTE") | ||
evt.player?.getStatistic(statistic) | ||
} | ||
|
||
PlaytimeLister.playtime[evt.player.uniqueId] = playtime ?: return | ||
|
||
updateFile() | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
fun onPlayerLogin(evt: PlayerJoinEvent) { | ||
val playtime: Int? = | ||
if (DataList.getInstance().version <= 1.12) | ||
evt.player?.getStatistic(Statistic.PLAY_ONE_TICK) | ||
else { | ||
// val statistic = Statistic::class.java.enumConstants.find { it.name == "PLAY_ONE_MINUTE" } | ||
val statistic = Statistic.valueOf("PLAY_ONE_MINUTE") | ||
evt.player?.getStatistic(statistic) | ||
} | ||
|
||
PlaytimeLister.playtime[evt.player.uniqueId] = playtime ?: return | ||
|
||
updateFile() | ||
} | ||
|
||
private fun getPlaytimeFromList(uuid: UUID): Int { | ||
return playtime[uuid] ?: 0 | ||
} | ||
|
||
private fun updateFile() { | ||
val file = DataList.getInstance().dataFolder.resolve("playtime.json") | ||
|
||
// Controllo che esista | ||
if (!file.exists()) | ||
file.createNewFile() | ||
|
||
// salvo nel file nell'hashmap | ||
val toSave = JsonObject() | ||
|
||
playtime.forEach { (uuid, value) -> | ||
toSave.addProperty(uuid.toString(), value) | ||
} | ||
|
||
val writer = FileWriter(file) | ||
writer.write(toSave.toString()) | ||
writer.close() | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
private val playtime = HashMap<UUID, Int>() | ||
|
||
init { | ||
val file = DataList.getInstance().dataFolder.resolve("playtime.json") | ||
|
||
// Controllo che esista | ||
if (!file.exists()) | ||
file.createNewFile() | ||
|
||
// Leggo il file nell'hashmap | ||
try { | ||
val parser = JsonParser().parse(FileReader(file)) | ||
|
||
parser.asJsonObject.entrySet().forEach { (key, value) -> | ||
val uuid = UUID.fromString(key) | ||
val ticks = value.asInt | ||
|
||
playtime[uuid] = ticks | ||
} | ||
} catch (e: Exception) { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ output: | |
onlinePlayers: "/online" | ||
permissions: "/permissions" | ||
points: "/points" | ||
playtime: "/playtime" | ||
port: 80 |