Skip to content

Commit 1383385

Browse files
committed
[NetworkThrottleModule] Migrate to new Feature API
1 parent 9608986 commit 1383385

File tree

6 files changed

+307
-313
lines changed

6 files changed

+307
-313
lines changed

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

Lines changed: 8 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -3,242 +3,37 @@ package io.github.rothes.esu.bukkit.module
33
import io.github.rothes.esu.bukkit.module.networkthrottle.ChunkDataThrottle
44
import io.github.rothes.esu.bukkit.module.networkthrottle.DynamicChunkSendRate
55
import io.github.rothes.esu.bukkit.module.networkthrottle.HighLatencyAdjust
6-
import io.github.rothes.esu.bukkit.plugin
7-
import io.github.rothes.esu.bukkit.util.ServerCompatibility
86
import io.github.rothes.esu.core.configuration.ConfigLoader
97
import io.github.rothes.esu.core.configuration.ConfigurationPart
10-
import io.github.rothes.esu.core.configuration.data.MessageData
11-
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
12-
import io.github.rothes.esu.core.configuration.meta.Comment
13-
import io.github.rothes.esu.core.configuration.meta.RemovedNode
14-
import io.github.rothes.esu.core.configuration.meta.RenamedFrom
15-
import io.github.rothes.esu.core.configuration.serializer.MapSerializer.DefaultedLinkedHashMap
168
import io.github.rothes.esu.core.module.configuration.BaseModuleConfiguration
17-
import io.github.rothes.esu.core.util.version.Version
18-
import io.github.rothes.esu.lib.configurate.objectmapping.meta.PostProcess
19-
import io.github.rothes.esu.lib.packetevents.util.SpigotConversionUtil
20-
import org.bukkit.Material
21-
import java.time.Duration
229
import java.util.*
23-
import kotlin.time.toJavaDuration
2410

25-
object NetworkThrottleModule: BukkitModule<NetworkThrottleModule.ModuleConfig, NetworkThrottleModule.ModuleLang>() {
11+
object NetworkThrottleModule: BukkitModule<BaseModuleConfiguration, NetworkThrottleModule.ModuleLang>() {
12+
13+
init {
14+
registerFeature(ChunkDataThrottle)
15+
registerFeature(DynamicChunkSendRate)
16+
registerFeature(HighLatencyAdjust)
17+
}
2618

2719
lateinit var data: ModuleData
2820
private val dataPath = moduleFolder.resolve("data.yml")
2921

3022
override fun onEnable() {
3123
data = ConfigLoader.load(dataPath)
32-
HighLatencyAdjust.onEnable()
33-
ChunkDataThrottle.onEnable()
34-
DynamicChunkSendRate.enable()
3524
}
3625

3726
override fun onDisable() {
3827
super.onDisable()
39-
ChunkDataThrottle.onDisable()
40-
HighLatencyAdjust.onDisable()
41-
DynamicChunkSendRate.disable()
4228
ConfigLoader.save(dataPath, data)
4329
}
4430

45-
override fun onReload() {
46-
super.onReload()
47-
if (enabled) {
48-
if (config.dynamicChunkSendRate.enabled) {
49-
DynamicChunkSendRate.enable()
50-
} else {
51-
DynamicChunkSendRate.disable()
52-
}
53-
if (config.chunkDataThrottle.enabled) {
54-
ChunkDataThrottle.onReload()
55-
}
56-
}
57-
}
58-
5931

6032
data class ModuleData(
6133
val originalViewDistance: MutableMap<UUID, Int> = linkedMapOf(),
6234
)
6335

64-
data class ModuleConfig(
65-
@Comment("""
66-
Helps to reduce chunk upload bandwidth. Plugin will compress invisible blocks in chunk data packet.
67-
If necessary, we send a full chunk data again.
68-
This can save about 50% bandwidth usage in overworld and 30% in nether averagely.
69-
Make sure you have enabled network-compression on proxy or this server.
70-
""")
71-
val chunkDataThrottle: ChunkDataThrottle = ChunkDataThrottle(),
72-
@Comment("Enable DynamicChunkSendRate. Make sure you have velocity mode on, and installed ESU on velocity.")
73-
val dynamicChunkSendRate: DynamicChunkSendRate = DynamicChunkSendRate(),
74-
@Comment("""
75-
Adjust the settings the players with high latency to lower value.
76-
So they won't affect average quality of all players.
77-
""")
78-
val highLatencyAdjust: HighLatencyAdjust = HighLatencyAdjust(),
79-
): BaseModuleConfiguration() {
80-
81-
data class ChunkDataThrottle(
82-
val enabled: Boolean = true,
83-
@Comment("""
84-
Plugin will resent complete original chunk data if resent block amount exceeds this value.
85-
Set it to -1 will never resent chunk but keep updating nearby blocks,
86-
0 to always resent original chunks.
87-
Set this to a large value can prevent constantly sending block update packets.
88-
Original chunk is not with anti-xray functionality. It is recommended to leave this value -1 .
89-
""",
90-
overrideOld = [
91-
"""
92-
Plugin will resent whole chunk data if resent block amount exceeds this value.
93-
Set it to -1 will never resent chunk but keep updating nearby blocks,
94-
0 to always resent chunks.
95-
"""
96-
])
97-
val thresholdToResentWholeChunk: Int = -1,
98-
@Comment("""
99-
We updates the nearby blocks when a player digs a block immediately.
100-
If this is enabled, we will check if the block is in the interaction range
101-
of the player with a rough calculation.
102-
""")
103-
val updateOnLegalInteractOnly: Boolean = true,
104-
@Comment("How many distance of blocks to update from the center when necessary.")
105-
val updateDistance: Int = 2,
106-
@Comment("""
107-
The bedrock level(minimal height) is never visible unless you are in void.
108-
We would skip the check, and if you don't like it you can enable it.
109-
""")
110-
val minimalHeightInvisibleCheck: Boolean = false,
111-
@Comment("""
112-
Same with minimal-height but it's for nether roof. For out-of-the-box, it's true by default.
113-
It's highly recommend to set it to FALSE if you don't allow players to get above there.
114-
""",
115-
overrideOld = ["Same with minimal-height but it's for nether roof."]
116-
)
117-
val netherRoofInvisibleCheck: Boolean = true,
118-
@Comment("""
119-
If a non-occluding block is surrounded by occluding blocks, the center block is invisible.
120-
But should we consider all surrounded blocks invisible to this block face?
121-
Unless the player joins the game with their eye in the non-occluding block,
122-
they will never naturally see those surrounded blocks.
123-
This step takes extra ~0.02ms, so it's not enabled by default.
124-
Enable this could help with saving bandwidth in nether, as there's many single-block lava.
125-
""")
126-
val detectInvisibleSingleBlock: Boolean = false,
127-
@Comment("""
128-
Detect lava pool, and consider lava blocks which being covered invisible.
129-
This step takes extra ~0.03ms, so it's not enabled by default.
130-
It also makes the plugin detect nearby blocks everytime player moves.
131-
Enable this could help with saving bandwidth, especially in nether.
132-
""")
133-
val detectLavaPool: Boolean = false,
134-
@RenamedFrom("single-valued-section-block-list")
135-
@Comment("""
136-
This feature doesn't support running along with any other anti-xray plugins.
137-
You must use the anti-xray here we provide.
138-
139-
We will send non-visible blocks to one of the random block in this list.
140-
If you don't like to anti-xray, you can set the list to 'bedrock'.
141-
""",
142-
overrideOld = ["Plugin will convert chunks with all non-visible blocks to single-valued palette format,\nThis could save a lot of bandwidth. And since we are conflicting with anti-xray things,\nyou can use this for some kind of substitution.\nWe choose a random block from the list and make it of a 16*16*16 chunk section."]
143-
)
144-
val antiXrayRandomBlockList: DefaultedLinkedHashMap<String, MutableList<Material>> = DefaultedLinkedHashMap<String, MutableList<Material>>(
145-
mutableListOf(Material.BEDROCK)
146-
).apply {
147-
put("world", buildList {
148-
val cavesUpdate = ServerCompatibility.serverVersion >= Version.fromString("1.17")
149-
add(Material.COAL_ORE)
150-
if (cavesUpdate) add(Material.COPPER_ORE)
151-
addAll(listOf(Material.IRON_ORE, Material.GOLD_ORE,
152-
Material.EMERALD_ORE, Material.DIAMOND_ORE, Material.REDSTONE_ORE, Material.LAPIS_ORE))
153-
154-
if (cavesUpdate) addAll(listOf(Material.DEEPSLATE_COAL_ORE, Material.DEEPSLATE_COPPER_ORE,
155-
Material.DEEPSLATE_IRON_ORE, Material.DEEPSLATE_GOLD_ORE, Material.DEEPSLATE_EMERALD_ORE,
156-
Material.DEEPSLATE_DIAMOND_ORE, Material.DEEPSLATE_REDSTONE_ORE, Material.DEEPSLATE_LAPIS_ORE))
157-
}.toMutableList())
158-
put("world_nether", buildList {
159-
add(Material.NETHER_QUARTZ_ORE)
160-
if (ServerCompatibility.serverVersion >= Version.fromString("1.16")) {
161-
add(Material.NETHER_GOLD_ORE)
162-
add(Material.ANCIENT_DEBRIS)
163-
}
164-
}.toMutableList())
165-
put("world_the_end", mutableListOf(Material.END_STONE))
166-
},
167-
@Comment("""
168-
If enabled, we add a extra block type to chunk section palettes for the random block.
169-
This will greatly enhance anti-xray capabilities while giving only few bytes of additional bandwidth.
170-
""")
171-
val enhancedAntiXray: Boolean = true,
172-
@Comment("""
173-
Put any blocks you don't want to hide, so they are ignored while processing.
174-
For example, you can add any ores to it, so there's no anti-xray effect.
175-
WARNING: This significantly reduces compression badly. Please make sure you really have to do this.
176-
""")
177-
val nonInvisibleBlocksOverrides: Set<Material> = setOf(),
178-
) {
179-
180-
@RemovedNode
181-
val rebuildPaletteMappings: Boolean? = null
182-
183-
val antiXrayRandomBlockIds by lazy {
184-
with(antiXrayRandomBlockList) {
185-
DefaultedLinkedHashMap<String, IntArray>((default ?: listOf(Material.BEDROCK)).map { it.globalId }.toIntArray()).also {
186-
it.putAll(entries.map { it.key to it.value.map { it.globalId }.toIntArray() })
187-
}
188-
}
189-
}
190-
191-
private val Material.globalId
192-
get() = if (!this.isBlock) error("Material $this is not a block type!") else SpigotConversionUtil.fromBukkitBlockData(createBlockData()).globalId
193-
194-
@PostProcess
195-
private fun postProcess() {
196-
fun checkEmptyBlockList(key: String, list: MutableList<Material>) {
197-
if (list.isEmpty()) {
198-
list.add(Material.BEDROCK)
199-
plugin.warn("[ChunkDataThrottle] Anti-xray random block list of '$key' is empty! We have added bedrock to it.")
200-
}
201-
}
202-
antiXrayRandomBlockList.default?.let {
203-
checkEmptyBlockList("default", it)
204-
}
205-
antiXrayRandomBlockList.entries.toList().forEach {
206-
checkEmptyBlockList(it.key, it.value)
207-
}
208-
}
209-
}
210-
211-
data class DynamicChunkSendRate(
212-
val enabled: Boolean = ServerCompatibility.isProxyMode,
213-
)
214-
215-
data class HighLatencyAdjust(
216-
val enabled: Boolean = false,
217-
@Comment("Trigger a adjust when player's ping is greater than or equal this.")
218-
val latencyThreshold: Int = 150,
219-
@Comment("The high ping must keep for the duration to trigger a adjust finally.")
220-
val duration: Duration = kotlin.time.Duration.parse("1m").toJavaDuration(),
221-
@Comment("Plugin detects CLIENT_SETTINGS packets to reset the view distance for players.\n" +
222-
"If true, player must change the client view distance for a reset;\n" +
223-
"If false, any new settings could reset the view distance for the player.")
224-
val newViewDistanceToReset: Boolean = false,
225-
val minViewDistance: Int = 5,
226-
)
227-
}
228-
229-
data class ModuleLang(
230-
val highLatencyAdjust: HighLatencyAdjust = HighLatencyAdjust(),
231-
): ConfigurationPart {
232-
233-
@RemovedNode
234-
val analyser: Unit? = null
235-
236-
data class HighLatencyAdjust(
237-
val adjustedWarning: MessageData = ("<ec><b>Warning: </b><pc>Your network latency seems to be high. \n" +
238-
"To enhance your experience, we have adjusted your view distance. " +
239-
"You can always adjust it yourself in the game options.").message,
240-
)
241-
}
36+
class ModuleLang(): ConfigurationPart
24237

24338
}
24439

0 commit comments

Comments
 (0)