add deprecated

This commit is contained in:
2022-03-13 08:16:35 +08:00
parent 2e9d684622
commit 660dd4f029
18 changed files with 231 additions and 42 deletions
+8
View File
@@ -0,0 +1,8 @@
package cc.maxmc.servux.util
import org.bukkit.Chunk
data class ChunkPos(var x: Int, var z: Int) {
constructor(pos: Long) : this(pos.toInt(), (pos shr 32).toInt())
constructor(chunk: Chunk) : this(chunk.x, chunk.z)
}
+330
View File
@@ -0,0 +1,330 @@
package cc.maxmc.servux.util
import com.google.gson.*
import org.bukkit.util.Vector
import taboolib.common.platform.function.info
import taboolib.common.platform.function.warning
import taboolib.platform.BukkitPlugin
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
object JsonUtils {
val GSON = GsonBuilder().setPrettyPrinting().create()
fun getNestedObject(parent: JsonObject, key: String?, create: Boolean): JsonObject? {
return if (!parent.has(key) || !parent[key].isJsonObject) {
if (!create) {
return null
}
val obj = JsonObject()
parent.add(key, obj)
obj
} else {
parent[key].asJsonObject
}
}
fun hasBoolean(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asBoolean
return true
} catch (_: Exception) { }
}
return false
}
fun hasInteger(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asInt
return true
} catch (e: Exception) {
}
}
return false
}
fun hasLong(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asLong
return true
} catch (_: Exception) {
}
}
return false
}
fun hasFloat(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asFloat
return true
} catch (_: Exception) {
}
}
return false
}
fun hasDouble(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asDouble
return true
} catch (_: Exception) { }
}
return false
}
fun hasString(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
if (el != null && el.isJsonPrimitive) {
try {
el.asString
return true
} catch (_: Exception) { }
}
return false
}
fun hasObject(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
return el != null && el.isJsonObject
}
fun hasArray(obj: JsonObject, name: String?): Boolean {
val el = obj[name]
return el != null && el.isJsonArray
}
fun getBooleanOrDefault(obj: JsonObject, name: String?, defaultValue: Boolean): Boolean {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asBoolean
} catch (e: Exception) {
}
}
return defaultValue
}
fun getIntegerOrDefault(obj: JsonObject, name: String?, defaultValue: Int): Int {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asInt
} catch (e: Exception) {
}
}
return defaultValue
}
fun getLongOrDefault(obj: JsonObject, name: String?, defaultValue: Long): Long {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asLong
} catch (e: Exception) {
}
}
return defaultValue
}
fun getFloatOrDefault(obj: JsonObject, name: String?, defaultValue: Float): Float {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asFloat
} catch (e: Exception) {
}
}
return defaultValue
}
fun getDoubleOrDefault(obj: JsonObject, name: String?, defaultValue: Double): Double {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asDouble
} catch (e: Exception) {
}
}
return defaultValue
}
fun getStringOrDefault(obj: JsonObject, name: String?, defaultValue: String?): String? {
if (obj.has(name) && obj[name].isJsonPrimitive) {
try {
return obj[name].asString
} catch (e: Exception) {
}
}
return defaultValue
}
fun getBoolean(obj: JsonObject, name: String?): Boolean {
return getBooleanOrDefault(obj, name, false)
}
fun getInteger(obj: JsonObject, name: String?): Int {
return getIntegerOrDefault(obj, name, 0)
}
fun getLong(obj: JsonObject, name: String?): Long {
return getLongOrDefault(obj, name, 0)
}
fun getFloat(obj: JsonObject, name: String?): Float {
return getFloatOrDefault(obj, name, 0f)
}
fun getDouble(obj: JsonObject, name: String?): Double {
return getDoubleOrDefault(obj, name, 0.0)
}
fun getString(obj: JsonObject, name: String?): String? {
return getStringOrDefault(obj, name, null)
}
fun hasBlockPos(obj: JsonObject, name: String?): Boolean {
return blockPosFromJson(obj, name) != null
}
fun blockPosToJson(pos: Vector): JsonArray {
val arr = JsonArray()
arr.add(pos.x)
arr.add(pos.y)
arr.add(pos.z)
return arr
}
fun blockPosFromJson(obj: JsonObject, name: String?): Vector? {
if (hasArray(obj, name)) {
val arr = obj.getAsJsonArray(name)
if (arr.size() == 3) {
try {
return Vector(arr[0].asInt, arr[1].asInt, arr[2].asInt)
} catch (ignored: Exception) {
}
}
}
return null
}
fun hasVec3d(obj: JsonObject, name: String?): Boolean {
return vec3dFromJson(obj, name) != null
}
fun vec3dToJson(vec: Vector): JsonArray {
val arr = JsonArray()
arr.add(vec.x)
arr.add(vec.y)
arr.add(vec.z)
return arr
}
fun vec3dFromJson(obj: JsonObject, name: String?): Vector? {
if (hasArray(obj, name)) {
val arr = obj.getAsJsonArray(name)
if (arr.size() == 3) {
try {
return Vector(arr[0].asDouble, arr[1].asDouble, arr[2].asDouble)
} catch (e: Exception) {
}
}
}
return null
}
// https://stackoverflow.com/questions/29786197/gson-jsonobject-copy-value-affected-others-jsonobject-instance
fun deepCopy(jsonObject: JsonObject): JsonObject {
val result = JsonObject()
for ((key, value) in jsonObject.entrySet()) {
result.add(key, deepCopy(value))
}
return result
}
fun deepCopy(jsonArray: JsonArray): JsonArray {
val result = JsonArray()
for (e in jsonArray) {
result.add(deepCopy(e))
}
return result
}
fun deepCopy(jsonElement: JsonElement): JsonElement {
return if (jsonElement.isJsonPrimitive || jsonElement.isJsonNull) {
jsonElement // these are immutable anyway
} else if (jsonElement.isJsonObject) {
deepCopy(jsonElement.asJsonObject)
} else if (jsonElement.isJsonArray) {
deepCopy(jsonElement.asJsonArray)
} else {
throw UnsupportedOperationException("Unsupported element: $jsonElement")
}
}
fun parseJsonFromString(str: String?): JsonElement? {
try {
val parser = JsonParser()
return parser.parse(str)
} catch (e: Exception) {
}
return null
}
fun parseJsonFile(file: File?): JsonElement? {
if (file != null && file.exists() && file.isFile && file.canRead()) {
val fileName = file.absolutePath
try {
val parser = JsonParser()
val reader = FileReader(file)
val element = parser.parse(reader)
reader.close()
return element
} catch (e: Exception) {
warning("Failed to parse the JSON file '" + fileName + "'" + e.message)
}
}
return null
}
/**
* Converts the given JsonElement tree into its string representation.
* If **compact** is true, then it's written in one line without spaces or line breaks.
*
* @param element
* @param compact
* @return
*/
fun jsonToString(element: JsonElement?, compact: Boolean): String {
val gson = if (compact) Gson() else GSON
return gson.toJson(element)
}
fun writeJsonToFile(root: JsonElement?, file: File): Boolean {
return writeJsonToFile(GSON, root, file)
}
fun writeJsonToFile(gson: Gson, root: JsonElement?, file: File): Boolean {
var writer: FileWriter? = null
try {
writer = FileWriter(file)
writer.write(gson.toJson(root))
writer.close()
return true
} catch (e: IOException) {
warning("Failed to write JSON data to file '" + file.absolutePath + "'" + e.message)
} finally {
try {
writer?.close()
} catch (e: Exception) {
warning("Failed to close JSON file" + e.message)
}
}
return false
}
}
+12
View File
@@ -0,0 +1,12 @@
package cc.maxmc.servux.util
import net.minecraft.server.MinecraftServer
import taboolib.module.nms.nmsProxy
abstract class NMS {
abstract fun getMinecraftServer(): MinecraftServer
companion object {
val INSTANCE = nmsProxy<NMS>()
}
}
+13
View File
@@ -0,0 +1,13 @@
package cc.maxmc.servux.util
import net.minecraft.server.MinecraftServer
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.v1_18_R1.CraftServer
class NMSImpl: NMS() {
override fun getMinecraftServer(): MinecraftServer {
val cServer = Bukkit.getServer() as CraftServer
return cServer.server
}
}
+12
View File
@@ -0,0 +1,12 @@
package cc.maxmc.servux.util
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket
import taboolib.common.platform.event.SubscribeEvent
import taboolib.module.nms.PacketSendEvent
class PacketListener {
@SubscribeEvent
fun onPacket(e: PacketSendEvent) {
if(e.packet.source is ClientboundLevelChunkWithLightPacket)
}
}
+31
View File
@@ -0,0 +1,31 @@
package cc.maxmc.servux.util
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufOutputStream
import io.netty.handler.codec.EncoderException
import net.minecraft.nbt.NBTCompressedStreamTools
import net.minecraft.nbt.NBTTagCompound
import java.io.IOException
fun ByteBuf.writeVarInt(value: Int): ByteBuf {
var value = value
while (value and -128 != 0) {
this.writeByte(value and 127 or 128)
value = value ushr 7
}
this.writeByte(value)
return this
}
fun ByteBuf.writeNBT(compound: NBTTagCompound?): ByteBuf {
if (compound == null) {
this.writeByte(0)
} else {
try {
NBTCompressedStreamTools.write(compound, ByteBufOutputStream(this))
} catch (var3: IOException) {
throw EncoderException(var3)
}
}
return this
}
@@ -0,0 +1,34 @@
package cc.maxmc.servux.util
import org.bukkit.World
import org.bukkit.entity.Player
import org.bukkit.util.BlockVector
import org.bukkit.util.Vector
import kotlin.math.abs
class PlayerDimensionPosition(player: Player) {
lateinit var world: World
lateinit var pos: BlockVector
init {
setPosition(player)
}
fun dimensionChanged(player: Player): Boolean {
return world != player.world
}
fun needsUpdate(player: Player, distanceThreshold: Int): Boolean {
if (player.world != world) {
return true
}
val pos: Vector = player.location.toVector().toBlockVector()
return abs(pos.x - this.pos.x) > distanceThreshold || abs(pos.y - this.pos.y) > distanceThreshold || abs(
pos.z - this.pos.z) > distanceThreshold
}
fun setPosition(player: Player) {
world = player.world
pos = player.location.toVector().toBlockVector()
}
}
+9
View File
@@ -0,0 +1,9 @@
package cc.maxmc.servux.util
class Timeout(var lastSync: Int) {
fun needsUpdate(currentTick: Int, timeout: Int): Boolean {
return currentTick - lastSync >= timeout
}
}