Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 16, 2024
1 parent 11ee178 commit 34c0497
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface Serializable2Text {
enum SerializationSettings {
Hover,
Name,
NameHover,
Summarize,
Full,
BarredFull,
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ open class EnclosureArea : PersistentState, EnclosureView {
TrT.of("enclosure.info.created_on") + literalText(SimpleDateFormat().format(Date(createdOn))).gold()
}

SerializationSettings.NameHover -> {
serialize(SerializationSettings.Name, player).gold().hoverText(serialize(SerializationSettings.Hover, player))
}

SerializationSettings.Summarize -> {
val text = serialize(SerializationSettings.Name, player).gold().styled {
it.withClickEvent(ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/enclosure tp $fullName"))
Expand Down
29 changes: 29 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.util.math.BlockPos
import net.minecraft.world.PersistentState
Expand Down Expand Up @@ -104,6 +105,34 @@ class EnclosureList(world: ServerWorld, private val isRoot: Boolean) : Persisten
}
markDirty()
}

fun tickTickets(world: ServerWorld) {
areas.forEach { area ->
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
}
}
}
}

fun initTickets(world: ServerWorld) {
areas.forEach { area ->
if (area.ticket != null) {
if (area.ticket!!.remainingTicks > 0) {
area.toBlockBox().streamChunkPos().forEach {
world.chunkManager.addTicket(ChunkTicketType.FORCED, it, area.ticket!!.level, it)
}
// todo: message
}
}
}
}
}

const val DATA_VERSION_KEY = "data_version"
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/ServerMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents.ServerStarting
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents
import net.fabricmc.fabric.api.event.player.AttackBlockCallback
import net.fabricmc.fabric.api.event.player.UseBlockCallback
Expand Down Expand Up @@ -592,6 +593,12 @@ object ServerMain: ModInitializer {
playerSessions[CONSOLE] = Session(null)
Converter.convert()
runCatching { checkUpdateThread.start() }
enclosures.forEach { (key, list) ->
list.initTickets(it.getWorld(key)!!)
}
}
ServerTickEvents.START_WORLD_TICK.register {
enclosures[it.registryKey]!!.tickTickets(it)
}
ServerLifecycleEvents.SERVER_STOPPING.register {
checkUpdateThread.interrupt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package com.github.zly2006.enclosure.command

import com.github.zly2006.enclosure.Enclosure
import com.github.zly2006.enclosure.EnclosureArea
import com.github.zly2006.enclosure.utils.Serializable2Text.SerializationSettings.Name
import com.github.zly2006.enclosure.utils.checkPermission
import com.mojang.authlib.GameProfile
import me.lucko.fabric.api.permissions.v0.Options
import net.minecraft.server.command.ServerCommandSource
import net.minecraft.server.world.ChunkLevelType
import net.minecraft.server.world.ChunkLevels
import net.minecraft.server.world.ChunkTicketType
import net.minecraft.text.Text

fun BuilderScope<*>.registerForceLoad() {
fun forceLoad(source: ServerCommandSource, area: EnclosureArea, ticks: Int, level: Int) {
if (checkPermission(source, "enclosure.bypass")) {

}
fun forceLoad(source: ServerCommandSource, area: Enclosure, ticks: Int, level: Int) {
val ticket = EnclosureArea.ForceLoadTicket(
source.player?.gameProfile ?: GameProfile(CONSOLE, "Server"),
if (checkPermission(source, "enclosure.bypass")) Int.MAX_VALUE else ticks,
level
)
area.ticket = ticket
area.markDirty()
source.sendMessage(
Text.literal("Force loading ")
.append(area.serialize(Name, null))
.append(" with level $level")
)
}

literal("force-load") {
Expand All @@ -23,7 +35,9 @@ fun BuilderScope<*>.registerForceLoad() {
executes {
val maxTime = Options.get(source, "enclosure.load.max_time", 21600) { it.toInt() }
val land = getEnclosure(this)
ChunkTicketType.FORCED
if (land !is Enclosure) {
error(Text.literal("Only enclosure can be force loaded"), this)
}
forceLoad(source, land, maxTime * 20, level)
}
}
Expand All @@ -33,6 +47,9 @@ fun BuilderScope<*>.registerForceLoad() {
executes {
val maxTime = Options.get(source, "enclosure.load.max_time", 21600) { it.toInt() }
val land = getEnclosure(this)
if (land !is Enclosure) {
error(Text.literal("Only enclosure can be force loaded"), this)
}
forceLoad(source, land, maxTime * 20, level)
}
}
Expand Down

0 comments on commit 34c0497

Please sign in to comment.