Skip to content

Commit

Permalink
fix: force load enclosure
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 16, 2024
1 parent 7cce461 commit e03eba9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.zly2006.enclosure

import com.github.zly2006.enclosure.command.CONSOLE
import com.github.zly2006.enclosure.command.MAX_CHUNK_LEVEL
import com.github.zly2006.enclosure.command.Session
import com.github.zly2006.enclosure.gui.EnclosureScreenHandler
import com.github.zly2006.enclosure.network.play.SyncPermissionS2CPacket
Expand Down Expand Up @@ -425,10 +426,7 @@ open class EnclosureArea : PersistentState, EnclosureView {
ServerMain.getAllEnclosures(world).markDirty()
if (ticket != null) {
toBlockBox().streamChunkPos().forEach {
val level = world.chunkManager.ticketManager.simulationDistanceTracker.getLevel(it.toLong())
if (level > ticket!!.level) {
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, ticket!!.level, it)
}
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, MAX_CHUNK_LEVEL - ticket!!.level, it)
}
}
super.markDirty()
Expand Down
20 changes: 12 additions & 8 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package com.github.zly2006.enclosure
import com.github.zly2006.enclosure.ServerMain.enclosures
import com.github.zly2006.enclosure.ServerMain.getAllEnclosures
import com.github.zly2006.enclosure.access.ChunkAccess
import com.github.zly2006.enclosure.utils.Serializable2Text
import com.github.zly2006.enclosure.command.MAX_CHUNK_LEVEL
import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings.NameHover
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtList
import net.minecraft.nbt.NbtString
import net.minecraft.registry.RegistryWrapper
import net.minecraft.server.world.ChunkLevelType
import net.minecraft.server.world.ChunkLevels
import net.minecraft.server.world.ChunkTicketType
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.Text
Expand Down Expand Up @@ -114,8 +116,8 @@ class EnclosureList(world: ServerWorld, private val isRoot: Boolean) : Persisten
if (area.ticket != null) {
area.ticket!!.remainingTicks--
if (area.ticket!!.remainingTicks <= 0) {
area.ticket = null
removeTicket(area, world)
area.ticket = null
world.server.playerManager.broadcast(
Text.literal("Force loading for ").append(area.serialize(NameHover, null)).append(" expired."), false
)
Expand All @@ -130,13 +132,15 @@ class EnclosureList(world: ServerWorld, private val isRoot: Boolean) : Persisten
area.toBlockBox().streamChunkPos().forEach {
val chunk = world.getChunkAsView(it.x, it.z)
if (chunk is ChunkAccess) {
val targetLevel = chunk.cache.mapNotNull { it.ticket }.minOfOrNull { it.level }
world.chunkManager.ticketManager.ticketsByPosition.remove(it.toLong())
if (targetLevel != null) {
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, targetLevel, it)
val targetLevel = chunk.cache
.filter { it.ticket != null && it != area }
.minOfOrNull { it.ticket!!.level }
?: ChunkLevels.getLevelFromType(ChunkLevelType.FULL) // chunk border level
if (targetLevel > area.ticket!!.level) {
world.chunkManager.removeTicket(ChunkTicketType.FORCED, it, MAX_CHUNK_LEVEL - area.ticket!!.level, it)
}
} else {
world.chunkManager.removeTicket(ChunkTicketType.FORCED, it, area.ticket!!.level, it)
world.chunkManager.removeTicket(ChunkTicketType.FORCED, it, MAX_CHUNK_LEVEL - area.ticket!!.level, it)
}
}
}
Expand All @@ -146,7 +150,7 @@ class EnclosureList(world: ServerWorld, private val isRoot: Boolean) : Persisten
if (area.ticket != null) {
if (area.ticket!!.remainingTicks > 0) {
area.toBlockBox().streamChunkPos().forEach {
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, area.ticket!!.level, it)
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, MAX_CHUNK_LEVEL - area.ticket!!.level, it)
}
// todo: message
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import net.minecraft.server.world.ChunkLevelType
import net.minecraft.server.world.ChunkLevels
import net.minecraft.text.Text

const val MAX_CHUNK_LEVEL = 33 // ChunkLevels.getLevelFromType(ChunkLevelType.FULL)

fun BuilderScope<*>.registerForceLoad() {
fun forceLoad(source: ServerCommandSource, area: Enclosure, ticks: Int, level: Int) {
val ticket = EnclosureArea.ForceLoadTicket(
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/enclosure/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"enclosure.about.author": "Mod author: zly2006",
"enclosure.about.click_to_open": "Click to open URL",
"enclosure.about.copyright": "Copyright (c) 2022-2023, zly2006 & contributors",
"enclosure.about.copyright": "Copyright (c) 2022-2024, zly2006 & contributors",
"enclosure.about.source": "Source: https://github.com/zly2006/Enclosure",
"enclosure.about.team_page": "Team homepage: https://www.starlight.cool/",
"enclosure.about.version.client": "Client version: ",
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/enclosure/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"enclosure.about.author": "Mod作者: zly2006, 贡献者: Disy920",
"enclosure.about.author": "Mod作者: zly2006",
"enclosure.about.click_to_open": "点击打开URL",
"enclosure.about.copyright": "版权所有 (c) 2022-2023, zly2006 和贡献者们",
"enclosure.about.copyright": "版权所有 (c) 2022-2024, zly2006 和贡献者们",
"enclosure.about.source": "源码: https://github.com/zly2006/Enclosure",
"enclosure.about.team_page": "团队官网: https://www.starlight.cool/",
"enclosure.about.version.client": "客户端版本: ",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/enclosure.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ accessible field net/minecraft/server/world/ChunkTicketManager simulationDistanc
accessible field net/minecraft/server/world/ServerChunkManager ticketManager Lnet/minecraft/server/world/ChunkTicketManager;
accessible method net/minecraft/world/SimulationDistanceLevelPropagator getLevel (J)I
accessible field net/minecraft/server/world/ChunkTicketManager ticketsByPosition Lit/unimi/dsi/fastutil/longs/Long2ObjectOpenHashMap;
accessible method net/minecraft/server/world/ChunkTicketManager getTicketSet (J)Lnet/minecraft/util/collection/SortedArraySet;
accessible method net/minecraft/server/world/ChunkTicketManager removeTicket (JLnet/minecraft/server/world/ChunkTicket;)V

0 comments on commit e03eba9

Please sign in to comment.