Skip to content

Commit

Permalink
Use properties file for config, add threshold parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ac101m committed Mar 27, 2024
1 parent 0e8b1f3 commit b25d52d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ A server side fabric mod which loads chunks around moving minecarts, enabling th
## Configuration
Configuration can be found in the server config directory.
- `idleTimeoutTicks` - How long minecarts should remain loaded once they stop moving. Defaults to 6000 (5 minutes).
- `idleThreshold` - The threshold, in blocks per tick, above which a cart will be considered moving. Defaults to 0.2
(4 blocks per second - half maximum vanilla cart speed).
- `chunkLoadRadius` - How large an area to load surrounding each minecart. Defaults to 2 for a 3x3 area.
- `ticketDuration` - Duration of chunk tickets created by the mod in ticks. Defaults to 60 (3 seconds).
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'com.ac101m'
version '1.1.0-SNAPSHOT'
version '1.1.0'

repositories {
mavenCentral()
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/ac101m/am/AutonomousMinecarts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.minecraft.predicate.entity.EntityPredicates
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.TypeFilter
import net.minecraft.util.math.ChunkPos
import net.minecraft.util.math.Vec3d
import org.slf4j.LoggerFactory
import java.nio.file.NoSuchFileException
import java.nio.file.Path
Expand All @@ -24,7 +25,7 @@ import kotlin.collections.HashMap
*/
class AutonomousMinecarts(private val environment: ServerEnvironment) {
companion object {
private const val CONFIG_FILE_NAME = "autonomous-minecarts.json"
private const val CONFIG_FILE_NAME = "autonomous-minecarts.properties"
private const val STATE_FILE_NAME = "autonomous-minecarts-tickets.json"

private val log = LoggerFactory.getLogger(FabricServerEnvironment::class.java)
Expand Down Expand Up @@ -124,7 +125,7 @@ class AutonomousMinecarts(private val environment: ServerEnvironment) {
world.collectEntitiesByType(typeFilter, EntityPredicates.VALID_ENTITY, minecarts)

val movingMinecarts = minecarts.filter { cart ->
cart.velocity.length() > 0.01
cart.velocity.length() > config.idleThreshold
}

for (minecart in movingMinecarts) {
Expand Down
51 changes: 40 additions & 11 deletions src/main/kotlin/com/ac101m/am/persistence/Config.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.ac101m.am.persistence

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import java.nio.file.Path
import java.util.*
import kotlin.io.path.inputStream
import kotlin.io.path.outputStream

Expand All @@ -14,33 +13,63 @@ data class Config(
* Ticks after which chunk tickets associated with unmoving or destroyed minecarts are deleted.
* Defaults to 6000 ticks or 5 minutes.
*/
@JsonProperty("idleTimeoutTicks", required = true)
val idleTimeoutTicks: Int = 6000,
var idleTimeoutTicks: Int = 6000,

/**
* The threshold above which a minecart is considered to be moving in blocks per tick.
* Lower values make the mod more sensitive to minecart movement.
*/
var idleThreshold: Double = 0.2,

/**
* Radius around moving minecarts which will be loaded.
* Defaults to 2, for a 3x3 area of entity ticking chunks.
*/
@JsonProperty("chunkLoadRadius", required = true)
val chunkLoadRadius: Int = 2,
var chunkLoadRadius: Int = 2,

/**
* The duration of created chunk tickets.
* Higher values will cause chunk tickets to last longer and be created less frequently.
* A value of 0 means tickets will be created in every tick.
*/
@JsonProperty("ticketDuration", required = true)
val ticketDuration: Int = 60
var ticketDuration: Int = 60
) {
companion object {
private val mapper = ObjectMapper()
private const val IDLE_TIMEOUT_IDENTIFIER = "idleTimeoutTicks"
private const val IDLE_THRESHOLD_IDENTIFIER = "idleThreshold"
private const val CHUNK_LOAD_RADIUS_IDENTIFIER = "chunkLoadRadius"
private const val TICKET_DURATION_IDENTIFIER = "ticketDuration"

fun load(path: Path): Config {
return mapper.readValue(path.inputStream(), Config::class.java)
return Config().also { config ->
val properties = Properties().also { it.load(path.inputStream()) }

properties.getProperty(IDLE_TIMEOUT_IDENTIFIER)?.let { property ->
config.idleTimeoutTicks = property.toInt()
}

properties.getProperty(IDLE_THRESHOLD_IDENTIFIER)?.let { property ->
config.idleThreshold = property.toDouble()
}

properties.getProperty(CHUNK_LOAD_RADIUS_IDENTIFIER)?.let { property ->
config.chunkLoadRadius = property.toInt()
}

properties.getProperty(TICKET_DURATION_IDENTIFIER)?.let { property ->
config.ticketDuration = property.toInt()
}
}
}
}

fun save(path: Path) {
mapper.writerWithDefaultPrettyPrinter().writeValue(path.outputStream(), this)
Properties().also { properties ->
properties.setProperty(IDLE_TIMEOUT_IDENTIFIER, idleTimeoutTicks.toString())
properties.setProperty(IDLE_THRESHOLD_IDENTIFIER, idleThreshold.toString())
properties.setProperty(CHUNK_LOAD_RADIUS_IDENTIFIER, chunkLoadRadius.toString())
properties.setProperty(TICKET_DURATION_IDENTIFIER, ticketDuration.toString())
properties.store(path.outputStream(), "Autonomous minecarts config")
}
}
}
5 changes: 4 additions & 1 deletion src/main/kotlin/com/ac101m/am/persistence/StartupState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import kotlin.io.path.outputStream
*/
data class StartupState(
@JsonProperty("tickets", required = true)
val tickets: List<PersistentMinecartTicket> = ArrayList()
val tickets: List<PersistentMinecartTicket> = ArrayList(),

@JsonProperty("version", required = true)
val version: Int = 0
) {
companion object {
private val mapper = ObjectMapper()
Expand Down

0 comments on commit b25d52d

Please sign in to comment.