Skip to content

Commit c1cce9b

Browse files
committed
[EntityUpdateInterval] Support dynamic apply
1 parent 9677e8f commit c1cce9b

5 files changed

Lines changed: 131 additions & 62 deletions

File tree

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/NetworkThrottleModule.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.github.rothes.esu.bukkit.module
33
import io.github.rothes.esu.bukkit.module.networkthrottle.*
44
import io.github.rothes.esu.bukkit.util.ServerCompatibility
55
import io.github.rothes.esu.bukkit.util.version.adapter.nms.NmsRegistryValueSerializers
6-
import io.github.rothes.esu.bukkit.util.version.versioned
76
import io.github.rothes.esu.core.configuration.ConfigLoader
87
import io.github.rothes.esu.core.configuration.ConfigurationPart
98
import io.github.rothes.esu.core.configuration.LoadedConfiguration
@@ -20,7 +19,7 @@ object NetworkThrottleModule: BukkitModule<BaseModuleConfiguration, NetworkThrot
2019
registerFeature(EntityCulling)
2120
if (NmsRegistryValueSerializers.isSupported) {
2221
if (ServerCompatibility.isPaper) registerFeature(EntityTrackingRange)
23-
registerFeature(EntityUpdateInterval::class.java.versioned())
22+
registerFeature(EntityUpdateInterval.INSTANCE)
2423
}
2524
registerFeature(HighLatencyAdjust)
2625
registerFeature(SkipUnnecessaryPackets)

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/networkthrottle/EntityUpdateInterval.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package io.github.rothes.esu.bukkit.module.networkthrottle
2+
3+
import io.github.rothes.esu.bukkit.util.version.Versioned
4+
import io.github.rothes.esu.bukkit.util.version.adapter.nms.LevelEntitiesHandler
5+
import io.github.rothes.esu.bukkit.util.version.versioned
6+
import io.github.rothes.esu.core.command.annotation.ShortPerm
7+
import io.github.rothes.esu.core.configuration.meta.Comment
8+
import io.github.rothes.esu.core.module.CommonFeature
9+
import io.github.rothes.esu.core.module.configuration.BaseFeatureConfiguration
10+
import io.github.rothes.esu.core.user.User
11+
import net.minecraft.world.entity.Entity
12+
import net.minecraft.world.entity.EntityType
13+
import org.bukkit.Bukkit
14+
import org.bukkit.craftbukkit.CraftWorld
15+
import org.incendo.cloud.annotations.Command
16+
17+
abstract class EntityUpdateInterval: CommonFeature<EntityUpdateInterval.FeatureConfig, Unit>() {
18+
19+
companion object {
20+
val INSTANCE by Versioned(EntityUpdateInterval::class.java)
21+
}
22+
23+
init {
24+
registerFeature(ApplyCommand::class.java.versioned())
25+
}
26+
27+
override val name: String = "EntityUpdateInterval"
28+
29+
override fun onReload() {
30+
super.onReload()
31+
if (enabled) applyUpdateInterval()
32+
}
33+
34+
override fun onEnable() {
35+
applyUpdateInterval()
36+
37+
registerCommands(object {
38+
@Command("esu networkThrottle entityUpdateInterval entityType <entityType>")
39+
@ShortPerm
40+
fun getUpdateInterval(sender: User, entityType: EntityType<*>) {
41+
val interval = getCurrentInterval(entityType)
42+
sender.miniMessage("<pc>Current update interval of entity type <pdc>${entityType.toShortString()}</pdc> is <pdc>$interval")
43+
}
44+
})
45+
}
46+
47+
abstract fun getCurrentInterval(entityType: EntityType<*>): Int
48+
abstract fun setCurrentInterval(entityType: EntityType<*>, interval: Int)
49+
50+
protected fun applyUpdateInterval() {
51+
val config = config
52+
for ((type, interval) in config.entityTypeUpdateInterval) {
53+
setCurrentInterval(type, interval)
54+
}
55+
}
56+
57+
data class FeatureConfig(
58+
@Comment("""
59+
Control the position update interval ticks of entity types.
60+
Higher means less entity movement packets (more de-sync), but less network as deal.
61+
""")
62+
val entityTypeUpdateInterval: Map<EntityType<*>, Int> = mapOf(
63+
EntityType.PIG to INSTANCE.getCurrentInterval(EntityType.PIG),
64+
EntityType.ZOMBIE to INSTANCE.getCurrentInterval(EntityType.ZOMBIE),
65+
)
66+
): BaseFeatureConfiguration()
67+
68+
abstract class ApplyCommand : CommonFeature<Unit, Unit>() {
69+
70+
override fun onEnable() {
71+
registerCommands(object {
72+
@Command("esu networkThrottle entityUpdateInterval apply")
73+
@ShortPerm
74+
fun apply(sender: User) {
75+
val entitiesHandler by Versioned(LevelEntitiesHandler::class.java)
76+
77+
var updated = 0
78+
for (level in Bukkit.getWorlds().map { it as CraftWorld }.map { it.handle }) {
79+
for (entity in entitiesHandler.getEntitiesAll(level)) {
80+
if (handleEntity(entity)) updated++
81+
}
82+
}
83+
sender.message("Updated $updated entities")
84+
}
85+
})
86+
}
87+
88+
abstract fun handleEntity(entity: Entity): Boolean
89+
90+
}
91+
92+
}
Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,37 @@
11
package io.github.rothes.esu.bukkit.module.networkthrottle.v17_1
22

33
import io.github.rothes.esu.bukkit.module.networkthrottle.EntityUpdateInterval
4-
import io.github.rothes.esu.core.command.annotation.ShortPerm
5-
import io.github.rothes.esu.core.configuration.meta.Comment
6-
import io.github.rothes.esu.core.module.configuration.BaseFeatureConfiguration
7-
import io.github.rothes.esu.core.user.User
84
import io.github.rothes.esu.core.util.UnsafeUtils.usIntAccessor
5+
import io.github.rothes.esu.core.util.UnsafeUtils.usObjAccessor
6+
import net.minecraft.server.level.ChunkMap
97
import net.minecraft.server.level.ServerEntity
8+
import net.minecraft.world.entity.Entity
109
import net.minecraft.world.entity.EntityType
11-
import org.incendo.cloud.annotations.Command
1210

13-
class EntityUpdateIntervalImpl: EntityUpdateInterval<EntityUpdateIntervalImpl.FeatureConfig, Unit>() {
11+
object EntityUpdateIntervalImpl: EntityUpdateInterval() {
1412

15-
companion object {
16-
val entityTypeAccessor = EntityType::class.java.getDeclaredField("updateInterval").usIntAccessor
17-
val serverEntityAccessor = ServerEntity::class.java.getDeclaredField("updateInterval").usIntAccessor
18-
// val ENTITIES_HANDLER by Versioned(LevelEntitiesHandler::class.java)
13+
private val ENTITY_TYPE_UPDATE_INTERVAL = EntityType::class.java.getDeclaredField("updateInterval").usIntAccessor
14+
15+
override fun getCurrentInterval(entityType: EntityType<*>): Int {
16+
return ENTITY_TYPE_UPDATE_INTERVAL[entityType]
1917
}
2018

21-
override fun onReload() {
22-
super.onReload()
23-
if (enabled) {
24-
applyUpdateInterval()
25-
}
19+
override fun setCurrentInterval(entityType: EntityType<*>, interval: Int) {
20+
ENTITY_TYPE_UPDATE_INTERVAL[entityType] = interval
2621
}
2722

28-
override fun onEnable() {
29-
applyUpdateInterval()
23+
object ApplyCommandImpl : ApplyCommand() {
3024

31-
registerCommands(object {
32-
@Command("esu networkThrottle entityUpdateInterval entityType <entityType>")
33-
@ShortPerm
34-
fun getUpdateInterval(sender: User, entityType: EntityType<*>) {
35-
val interval = entityTypeAccessor[entityType]
36-
sender.miniMessage("<pc>Current update interval of entity type <pdc>${entityType.toShortString()}</pdc> is <pdc>$interval")
37-
}
25+
val TRACKED_ENTITY_SERVER_ENTITY = ChunkMap.TrackedEntity::class.java.getDeclaredField("serverEntity").usObjAccessor
26+
val SERVER_ENTITY_UPDATE_INTERVAL = ServerEntity::class.java.getDeclaredField("updateInterval").usIntAccessor
3827

39-
// TODO: apply
40-
// @Command("esu networkThrottle entityUpdateInterval apply")
41-
// @ShortPerm
42-
// fun apply(sender: User) {
43-
// for (level in Bukkit.getWorlds().map { it as CraftWorld }.map { it.handle }) {
44-
// for (entity in ENTITIES_HANDLER.getEntitiesAll(level)) {
45-
// entity.tracker.serverEntity
46-
// }
47-
// }
48-
// }
49-
})
50-
}
51-
52-
private fun applyUpdateInterval() {
53-
val config = config
54-
for ((type, interval) in config.entityTypeUpdateInterval) {
55-
entityTypeAccessor[type] = interval
28+
override fun handleEntity(entity: Entity): Boolean {
29+
val tracker = entity.tracker ?: return false
30+
val se = TRACKED_ENTITY_SERVER_ENTITY[tracker] as ServerEntity
31+
SERVER_ENTITY_UPDATE_INTERVAL[se] = getCurrentInterval(entity.type)
32+
return true
5633
}
34+
5735
}
5836

59-
data class FeatureConfig(
60-
@Comment("""
61-
Control the position update interval ticks of entity types.
62-
Higher means less entity movement packets (more de-sync), but less network as deal.
63-
""")
64-
val entityTypeUpdateInterval: Map<EntityType<*>, Int> = mapOf(
65-
EntityType.PIG to entityTypeAccessor[EntityType.PIG],
66-
EntityType.ZOMBIE to entityTypeAccessor[EntityType.ZOMBIE],
67-
)
68-
): BaseFeatureConfiguration()
6937
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.rothes.esu.bukkit.module.networkthrottle.v21__paper
2+
3+
import io.github.rothes.esu.bukkit.module.networkthrottle.EntityUpdateInterval
4+
import io.github.rothes.esu.core.util.UnsafeUtils.usIntAccessor
5+
import net.minecraft.server.level.ServerEntity
6+
import net.minecraft.world.entity.Entity
7+
8+
object ApplyCommandImpl : EntityUpdateInterval.ApplyCommand() {
9+
10+
val SERVER_ENTITY_UPDATE_INTERVAL = ServerEntity::class.java.getDeclaredField("updateInterval").usIntAccessor
11+
12+
override fun handleEntity(entity: Entity): Boolean {
13+
val tracker = entity.`moonrise$getTrackedEntity`() ?: return false
14+
val se = tracker.serverEntity
15+
SERVER_ENTITY_UPDATE_INTERVAL[se] = EntityUpdateInterval.INSTANCE.getCurrentInterval(entity.type)
16+
return true
17+
}
18+
19+
}

0 commit comments

Comments
 (0)