add deprecated
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package cc.maxmc.servux.dataproviders
|
||||
|
||||
import kotlin.math.max
|
||||
|
||||
abstract class DataProviderBase protected constructor(
|
||||
override val name: String,
|
||||
override val networkChannel: String,
|
||||
override val protocolVersion: Int,
|
||||
override val description: String,
|
||||
) : IDataProvider {
|
||||
override var isEnabled = false
|
||||
override var tickRate = 40
|
||||
protected set(tickRate) {
|
||||
field = max(tickRate, 1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package cc.maxmc.servux.dataproviders
|
||||
|
||||
interface IDataProvider {
|
||||
/**
|
||||
* Returns the simple name for this data provider.
|
||||
* This should preferably be a lower case alphanumeric string with no
|
||||
* other special characters than '-' and '_'.
|
||||
* This name will be used in the enable/disable commands as the argument
|
||||
* and also as the config file key/identifier.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
val name: String
|
||||
|
||||
/**
|
||||
* Returns the description of this data provider.
|
||||
* Used in the command to list the available providers and to check the status
|
||||
* of a given provider.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
val description: String
|
||||
|
||||
/**
|
||||
* Returns the network channel name used by this data provider to listen
|
||||
* for incoming data requests and to respond and send the requested data.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
val networkChannel: String
|
||||
|
||||
/**
|
||||
* Returns the current protocol version this provider supports
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
val protocolVersion: Int
|
||||
/**
|
||||
* Returns true if this data provider is currently enabled.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/**
|
||||
* Enables or disables this data provider
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
var isEnabled: Boolean
|
||||
|
||||
/**
|
||||
* Returns whether or not this data provider should get ticked to periodically send some data,
|
||||
* or if it's only listening for incoming requests and responds to them directly.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
fun shouldTick(): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the interval in game ticks that this data provider should be ticked at
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
val tickRate: Int
|
||||
|
||||
/**
|
||||
* Called at the given tick rate
|
||||
*
|
||||
* @param tickCounter The current server tick (since last server start)
|
||||
*/
|
||||
fun tick(tickCounter: Int) {}
|
||||
/**
|
||||
* Returns the network packet handler used for this data provider.
|
||||
* @return
|
||||
*/
|
||||
// IPluginChannelHandler getPacketHandler();
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
package cc.maxmc.servux.dataproviders
|
||||
|
||||
// native NBT
|
||||
// World Structure
|
||||
import cc.maxmc.servux.network.packet.StructureDataPacketHandler
|
||||
import cc.maxmc.servux.util.*
|
||||
import io.netty.buffer.Unpooled
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet
|
||||
import it.unimi.dsi.fastutil.longs.LongSet
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraft.nbt.NBTTagList
|
||||
import net.minecraft.world.level.ChunkCoordIntPair
|
||||
import net.minecraft.world.level.levelgen.feature.StructureGenerator
|
||||
import net.minecraft.world.level.levelgen.structure.StructureStart
|
||||
import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.Chunk
|
||||
import org.bukkit.World
|
||||
import org.bukkit.entity.Player
|
||||
import taboolib.platform.BukkitPlugin
|
||||
import java.util.*
|
||||
import kotlin.math.abs
|
||||
|
||||
class StructureDataProvider : DataProviderBase("structure_bounding_boxes",
|
||||
StructureDataPacketHandler.CHANNEL,
|
||||
StructureDataPacketHandler.PROTOCOL_VERSION,
|
||||
"Structure Bounding Boxes data for structures such as Witch Huts, Ocean Monuments, Nether Fortresses etc.") {
|
||||
|
||||
companion object {
|
||||
private const val timeout = 30 * 20
|
||||
private const val updateInterval = 40
|
||||
}
|
||||
|
||||
private val registeredPlayers = HashMap<UUID, PlayerDimensionPosition>()
|
||||
private val timeouts = HashMap<UUID, MutableMap<ChunkPos, Timeout>>()
|
||||
val metadata: NBTTagCompound = NBTTagCompound()
|
||||
|
||||
init {
|
||||
metadata.putString("id", StructureDataPacketHandler.CHANNEL)
|
||||
metadata.putInt("timeout", timeout)
|
||||
metadata.putInt("version", StructureDataPacketHandler.PROTOCOL_VERSION)
|
||||
}
|
||||
|
||||
private var retainDistance = 0
|
||||
|
||||
override fun shouldTick(): Boolean = true
|
||||
|
||||
override fun tick(tickCounter: Int) {
|
||||
if (tickCounter % updateInterval == 0) {
|
||||
if (registeredPlayers.isNotEmpty()) {
|
||||
// System.out.printf("=======================\n");
|
||||
// System.out.printf("tick: %d - %s\n", tickCounter, this.isEnabled());
|
||||
retainDistance = Bukkit.getViewDistance() + 2
|
||||
val uuidIter = registeredPlayers.keys.iterator()
|
||||
while (uuidIter.hasNext()) {
|
||||
val uuid = uuidIter.next()
|
||||
val player: Player = Bukkit.getPlayer(uuid) ?: return run {
|
||||
timeouts.remove(uuid)
|
||||
uuidIter.remove()
|
||||
}
|
||||
this.checkForDimensionChange(player)
|
||||
this.refreshTrackedChunks(player, tickCounter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForDimensionChange(player: Player) {
|
||||
val uuid: UUID = player.uniqueId
|
||||
val playerPos: PlayerDimensionPosition? = registeredPlayers[uuid]
|
||||
if (playerPos == null || playerPos.dimensionChanged(player)) {
|
||||
timeouts.remove(uuid)
|
||||
registeredPlayers.computeIfAbsent(uuid) {
|
||||
PlayerDimensionPosition(player)
|
||||
}.setPosition(player)
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshTrackedChunks(player: Player, tickCounter: Int) {
|
||||
val uuid: UUID = player.uniqueId
|
||||
val map: MutableMap<ChunkPos, Timeout>? = timeouts[uuid]
|
||||
if (map != null) {
|
||||
// System.out.printf("refreshTrackedChunks: timeouts: %d\n", map.size());
|
||||
this.sendAndRefreshExpiredStructures(player, map, tickCounter)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendAndRefreshExpiredStructures(
|
||||
player: Player,
|
||||
map: MutableMap<ChunkPos, Timeout>,
|
||||
tickCounter: Int,
|
||||
) {
|
||||
val positionsToUpdate: MutableSet<ChunkPos> = HashSet()
|
||||
map.forEach { (key, timeout) ->
|
||||
if (timeout.needsUpdate(tickCounter, Companion.timeout)) {
|
||||
positionsToUpdate.add(key)
|
||||
}
|
||||
}
|
||||
if (positionsToUpdate.isNotEmpty()) {
|
||||
val world = player.world
|
||||
val center = player.location.chunk
|
||||
val references: MutableMap<StructureGenerator<*>, LongSet> = HashMap()
|
||||
for (pos in positionsToUpdate) {
|
||||
if (this.isOutOfRange(pos, center)) {
|
||||
map.remove(pos)
|
||||
} else {
|
||||
this.getStructureReferencesFromChunk(pos.x, pos.z, world, references)
|
||||
val timeout = map[pos]
|
||||
timeout?.lastSync = tickCounter
|
||||
}
|
||||
}
|
||||
|
||||
// System.out.printf("sendAndRefreshExpiredStructures: positionsToUpdate: %d -> references: %d, to: %d\n", positionsToUpdate.size(), references.size(), this.timeout);
|
||||
if (references.isNotEmpty()) {
|
||||
this.sendStructures(player, references, tickCounter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isOutOfRange(pos: ChunkPos, center: Chunk): Boolean {
|
||||
val chunkRadius = retainDistance
|
||||
return abs(pos.x - center.x) > chunkRadius ||
|
||||
abs(pos.z - center.z) > chunkRadius
|
||||
}
|
||||
|
||||
private fun getStructureReferencesFromChunk(
|
||||
chunkX: Int,
|
||||
chunkZ: Int,
|
||||
world: World,
|
||||
references: MutableMap<StructureGenerator<*>, LongSet>,
|
||||
) {
|
||||
if (!world.isChunkLoaded(chunkX, chunkZ)) {
|
||||
return
|
||||
}
|
||||
val nmsWorld = NMS.INSTANCE.getMinecraftServer().allLevels.findLast { it.serverLevelData.levelName == world.name }!!
|
||||
val chunk = nmsWorld.getChunk(chunkX, chunkZ)
|
||||
chunk.allReferences.forEach { (feature, startChunks) ->
|
||||
if (!startChunks.isEmpty() && feature != StructureGenerator.MINESHAFT) {
|
||||
references.merge(feature, startChunks) { oldSet, entrySet ->
|
||||
val newSet = LongOpenHashSet(oldSet)
|
||||
newSet.addAll(entrySet)
|
||||
return@merge newSet
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendStructures(
|
||||
player: Player,
|
||||
references: Map<StructureGenerator<*>, LongSet>,
|
||||
tickCounter: Int,
|
||||
) {
|
||||
val world: World = player.world
|
||||
val starts: Map<ChunkPos, StructureStart<*>> = this.getStructureStartsFromReferences(world, references)
|
||||
if (starts.isNotEmpty()) {
|
||||
this.addOrRefreshTimeouts(player.uniqueId, references, tickCounter)
|
||||
val structureList: NBTTagList = getStructureList(starts, world)
|
||||
// System.out.printf("sendStructures: starts: %d -> structureList: %d. refs: %s\n", starts.size(), structureList.size(), references.keySet());
|
||||
val tag = NBTTagCompound()
|
||||
tag.put("Structures", structureList)
|
||||
sendPacketTypeAndCompound(StructureDataPacketHandler.CHANNEL,
|
||||
StructureDataPacketHandler.PACKET_S2C_STRUCTURE_DATA,
|
||||
tag,
|
||||
player)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getStructureStartsFromReferences(
|
||||
world: World,
|
||||
references: Map<StructureGenerator<*>, LongSet>,
|
||||
): Map<ChunkPos, StructureStart<*>> {
|
||||
val starts = HashMap<ChunkPos, StructureStart<*>>()
|
||||
references.forEach { (feature, startChunks) ->
|
||||
val iter = startChunks.iterator()
|
||||
while (iter.hasNext()) {
|
||||
val pos = ChunkPos(iter.nextLong())
|
||||
if (!world.isChunkLoaded(pos.x, pos.z)) {
|
||||
continue
|
||||
}
|
||||
val nmsWorld = NMS.INSTANCE.getMinecraftServer().allLevels.findLast { it.serverLevelData.levelName == world.name }!!
|
||||
val chunk = nmsWorld.getChunk(pos.x, pos.z)
|
||||
val start: StructureStart<*> = chunk.getStartForFeature(feature) ?: return@forEach
|
||||
starts[pos] = start
|
||||
}
|
||||
}
|
||||
|
||||
// System.out.printf("getStructureStartsFromReferences: references: %d -> starts: %d\n", references.size(), starts.size());
|
||||
return starts
|
||||
}
|
||||
|
||||
private fun addOrRefreshTimeouts(uuid: UUID, references: Map<StructureGenerator<*>, LongSet>, tickCounter: Int) {
|
||||
// System.out.printf("addOrRefreshTimeouts: references: %d\n", references.size());
|
||||
val map: MutableMap<ChunkPos, Timeout> = timeouts.computeIfAbsent(uuid
|
||||
) { HashMap() }
|
||||
for (chunks: LongSet in references.values) {
|
||||
for (chunkPosLong in chunks) {
|
||||
val pos = ChunkPos(chunkPosLong)
|
||||
map.computeIfAbsent(pos) {
|
||||
Timeout(tickCounter)
|
||||
}.lastSync = tickCounter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getStructureList(structures: Map<ChunkPos, StructureStart<*>>, world: World): NBTTagList {
|
||||
val list = NBTTagList()
|
||||
structures.forEach { (pos, value) ->
|
||||
list.add(value.createTag(StructurePieceSerializationContext.fromLevel(NMS.INSTANCE.getMinecraftServer().allLevels.findLast { it.serverLevelData.levelName == world.name }!!),
|
||||
ChunkCoordIntPair(pos.x, pos.z)))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
fun register(player: Player): Boolean {
|
||||
// System.out.printf("register\n");
|
||||
var registered = false
|
||||
val uuid: UUID = player.uniqueId
|
||||
if (!registeredPlayers.containsKey(uuid)) {
|
||||
val bytebuf = Unpooled.buffer()
|
||||
sendPacketTypeAndCompound(StructureDataPacketHandler.CHANNEL,
|
||||
StructureDataPacketHandler.PACKET_S2C_METADATA,
|
||||
metadata,
|
||||
player)
|
||||
registeredPlayers[uuid] = PlayerDimensionPosition(player)
|
||||
val tickCounter: Int = Bukkit.getServer().ticksPerAmbientSpawns
|
||||
this.initialSyncStructuresToPlayerWithinRange(player,
|
||||
Bukkit.getViewDistance(),
|
||||
tickCounter)
|
||||
registered = true
|
||||
}
|
||||
return registered
|
||||
}
|
||||
|
||||
private fun initialSyncStructuresToPlayerWithinRange(
|
||||
player: Player,
|
||||
chunkRadius: Int,
|
||||
tickCounter: Int,
|
||||
) {
|
||||
val uuid: UUID = player.uniqueId
|
||||
val center = ChunkPos(player.location.chunk)
|
||||
val references: Map<StructureGenerator<*>, LongSet> =
|
||||
this.getStructureReferencesWithinRange(player.world, center, chunkRadius)
|
||||
timeouts.remove(uuid)
|
||||
registeredPlayers.computeIfAbsent(uuid) { u: UUID? ->
|
||||
PlayerDimensionPosition(player)
|
||||
}.setPosition(player)
|
||||
|
||||
// System.out.printf("initialSyncStructuresToPlayerWithinRange: references: %d\n", references.size());
|
||||
sendStructures(player, references, tickCounter)
|
||||
}
|
||||
|
||||
private fun getStructureReferencesWithinRange(
|
||||
world: World,
|
||||
center: ChunkPos,
|
||||
chunkRadius: Int,
|
||||
): Map<StructureGenerator<*>, LongSet> {
|
||||
val references = HashMap<StructureGenerator<*>, LongSet>()
|
||||
for (cx in center.x - chunkRadius..center.x + chunkRadius) {
|
||||
for (cz in center.z - chunkRadius..center.z + chunkRadius) {
|
||||
getStructureReferencesFromChunk(cx, cz, world, references)
|
||||
}
|
||||
}
|
||||
|
||||
// System.out.printf("getStructureReferencesWithinRange: references: %d\n", references.size());
|
||||
return references
|
||||
}
|
||||
|
||||
private fun sendPacketTypeAndCompound(
|
||||
channel: String,
|
||||
packetType: Int,
|
||||
data: NBTTagCompound,
|
||||
player: Player,
|
||||
) {
|
||||
val buf = Unpooled.buffer()
|
||||
buf.writeVarInt(packetType)
|
||||
buf.writeNBT(data)
|
||||
buf.readBytes(ByteArray(buf.capacity()))
|
||||
player.sendPluginMessage(BukkitPlugin.getInstance(), channel, buf.array())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user