Skip to content

Commit

Permalink
feat: force load enclosure
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 16, 2024
1 parent 34c0497 commit 7cce461
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "fabric-loom" version "1.6-SNAPSHOT" apply false
id "fabric-loom" version "1.7-SNAPSHOT" apply false
id "maven-publish"
id "org.ajoberstar.grgit" version "5.2.2"
id "org.jetbrains.kotlin.jvm" version "2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ rei_version = 9.1.550

# check these on https://fabricmc.net/develop
minecraft_version=1.21
yarn_mappings=1.21+build.1
yarn_mappings=1.21+build.9
loader_version=0.15.11

# Fabric API
fabric_version=0.100.0+1.21
fabric_version=0.100.7+1.21
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
10 changes: 10 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import net.minecraft.nbt.NbtList
import net.minecraft.registry.RegistryWrapper
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ChunkTicketType
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.ClickEvent
import net.minecraft.text.MutableText
Expand Down Expand Up @@ -422,6 +423,15 @@ open class EnclosureArea : PersistentState, EnclosureView {

final override fun markDirty() {
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)
}
}
}
super.markDirty()
}

fun setTeleportPos(teleportPos: Vec3d?, yaw: Float, pitch: Float) {
Expand Down
28 changes: 24 additions & 4 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,15 @@ 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.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.ChunkTicketType
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
import net.minecraft.world.PersistentState
import net.minecraft.world.chunk.ChunkStatus
Expand Down Expand Up @@ -111,12 +114,29 @@ class EnclosureList(world: ServerWorld, private val isRoot: Boolean) : Persisten
if (area.ticket != null) {
area.ticket!!.remainingTicks--
if (area.ticket!!.remainingTicks <= 0) {
area.toBlockBox().streamChunkPos().forEach {
world.chunkManager.removeTicket(ChunkTicketType.FORCED, it, area.ticket!!.level, it)
}
// todo: message
area.ticket = null
removeTicket(area, world)
world.server.playerManager.broadcast(
Text.literal("Force loading for ").append(area.serialize(NameHover, null)).append(" expired."), false
)
// todo: message
}
area.markDirty()
}
}
}

private fun removeTicket(area: EnclosureArea, world: ServerWorld) {
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)
}
} else {
world.chunkManager.removeTicket(ChunkTicketType.FORCED, it, area.ticket!!.level, it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class BuilderScope<T: argT>(var parent: T) {
error(e.text, it)
} catch (e: CommandSyntaxException) {
throw e
} catch (e: Exception) {
} catch (e: Throwable) {
LOGGER.error("Error while executing command: " + it.input, e)
error(TrT.of("enclosure.message.error").append(e.message), it)
error(TrT.of("enclosure.message.error").append("${e.javaClass.simpleName}: ${e.message}"), it)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,37 @@ fun BuilderScope<*>.registerForceLoad() {
)
area.ticket = ticket
area.markDirty()
source.sendMessage(
Text.literal("Force loading ")
.append(area.serialize(Name, null))
.append(" with level $level")
source.sendFeedback(
{
Text.literal("Force loading ")
.append(area.serialize(Name, null))
.append(" with level $level")
}, true
)
}

fun cancelForceLoad(source: ServerCommandSource, area: Enclosure) {
if (area.ticket != null) {
area.ticket!!.remainingTicks = 0
area.markDirty()
source.sendFeedback(
{
Text.literal("Force loading for ")
.append(area.serialize(Name, null))
.append(" canceled")
},
true
)
}
else {
source.sendError(Text.literal("No force loading tickets for ").append(area.serialize(Name, null)))
}
}

literal("force-load") {
argument(landArgument()) {
permission("enclosure.command.force_load")
literal("blocks") {
permission("enclosure.command.force_load")
val level = ChunkLevels.getLevelFromType(ChunkLevelType.BLOCK_TICKING)
executes {
val maxTime = Options.get(source, "enclosure.load.max_time", 21600) { it.toInt() }
Expand All @@ -42,7 +62,6 @@ fun BuilderScope<*>.registerForceLoad() {
}
}
literal("entities") {
permission("enclosure.command.force_load")
val level = ChunkLevels.getLevelFromType(ChunkLevelType.ENTITY_TICKING)
executes {
val maxTime = Options.get(source, "enclosure.load.max_time", 21600) { it.toInt() }
Expand All @@ -53,6 +72,20 @@ fun BuilderScope<*>.registerForceLoad() {
forceLoad(source, land, maxTime * 20, level)
}
}
literal("cancel") {
executes {
val land = getEnclosure(this)
if (land !is Enclosure) {
error(Text.literal("Only enclosure can be force loaded"), this)
}
cancelForceLoad(source, land)
}
}
}
literal("list") {
executes {
TODO()
}
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/enclosure.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ accessible field net/minecraft/server/world/ServerChunkLoadingManager entityTrac
accessible field net/minecraft/server/world/ServerChunkLoadingManager$EntityTracker entry Lnet/minecraft/server/network/EntityTrackerEntry;
accessible field net/minecraft/inventory/DoubleInventory first Lnet/minecraft/inventory/Inventory;
accessible field net/minecraft/inventory/DoubleInventory second Lnet/minecraft/inventory/Inventory;
accessible field net/minecraft/server/world/ChunkTicketManager simulationDistanceTracker Lnet/minecraft/world/SimulationDistanceLevelPropagator;
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;

0 comments on commit 7cce461

Please sign in to comment.