Skip to content

Commit

Permalink
feat: config
Browse files Browse the repository at this point in the history
  • Loading branch information
znepb committed Oct 22, 2024
1 parent 9700a16 commit 66673d4
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 17 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ sourceSets {
}

repositories {
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases/" }
maven {
url "https://squiddev.cc/maven/"
content {
Expand All @@ -62,6 +64,10 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modApi("me.shedaniel.cloth:cloth-config-fabric:${cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
}

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation "net.fabricmc:fabric-language-kotlin:${project.fabric_kotlin_version}"
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.parallel=true
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.16.7
loader_version=0.14.22
fabric_kotlin_version=1.10.8+kotlin.1.9.0

# Mod Properties
Expand All @@ -16,4 +16,5 @@ archives_base_name=roadworks

# Dependencies
fabric_version=0.87.0+1.20.1
cct_version=1.107.0
cct_version=1.107.0
cloth_config_version = 11.1.136
2 changes: 1 addition & 1 deletion src/client/kotlin/me/znepb/roadworks/RoadworksClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object RoadworksClient : ClientModInitializer {

override fun onInitializeClient() {
logger.info("Roadworks Client Init")
ModelLoadingPlugin.register( me.znepb.roadworks.ModelLoader())
ModelLoadingPlugin.register( me.znepb.roadworks.ModelLoader() )

// Item Groups
ItemGroupEvents.modifyEntriesEvent(Registry.itemGroup).register {
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/me/znepb/roadworks/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.znepb.roadworks

import me.shedaniel.autoconfig.ConfigData
import me.shedaniel.autoconfig.annotation.Config
import me.shedaniel.autoconfig.annotation.ConfigEntry

@Config(name = "roadworks")
class Config : ConfigData {
@ConfigEntry.Gui.CollapsibleObject
var trafficCabinet = TrafficCabinetConfiguration()

class TrafficCabinetConfiguration {
var maxDevices: Int = 24
var maxLinkDistance: Double = 48.0
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/me/znepb/roadworks/RoadworksMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package me.znepb.roadworks
import com.google.gson.JsonParser
import com.mojang.serialization.JsonOps
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
import me.shedaniel.autoconfig.AutoConfig
import me.shedaniel.autoconfig.ConfigHolder
import me.shedaniel.autoconfig.serializer.ConfigSerializer
import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer
import me.znepb.roadworks.block.Linkable
import me.znepb.roadworks.block.cabinet.TrafficCabinetBlockEntity
import me.znepb.roadworks.block.sign.SignType
Expand All @@ -27,6 +31,7 @@ object RoadworksMain : ModInitializer {
val logger = LoggerFactory.getLogger(NAMESPACE)

val SIGN_TYPES = Object2ObjectOpenHashMap<Identifier, SignType>()
var CONFIG: Config? = null

fun ModId(id: String): Identifier {
return Identifier(NAMESPACE, id)
Expand All @@ -35,8 +40,11 @@ object RoadworksMain : ModInitializer {
override fun onInitialize() {
logger.info("Roadworks is initalizing")

AutoConfig.register(Config::class.java, ::Toml4jConfigSerializer)
Registry.init()

CONFIG = AutoConfig.getConfigHolder(Config::class.java).config

PlayerBlockBreakEvents.BEFORE.register { world: World, player: PlayerEntity, pos: BlockPos, state: BlockState, blockEntity: BlockEntity? ->
if (blockEntity is Linkable) {
blockEntity.remove()
Expand Down
22 changes: 9 additions & 13 deletions src/main/kotlin/me/znepb/roadworks/item/Linker.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.znepb.roadworks.item

import me.znepb.roadworks.Registry
import me.znepb.roadworks.RoadworksMain
import me.znepb.roadworks.block.Linkable
import me.znepb.roadworks.block.cabinet.TrafficCabinetBlockEntity
import net.minecraft.block.entity.BlockEntity
Expand All @@ -20,11 +21,6 @@ class Linker(settings: Settings) : Item(settings) {
var linking: BlockPos? = null
var linkingWith: BlockEntityType<*>? = null

companion object {
const val MAX_DEVICES = 24
val MAX_DEVICE_DISTANCE = 48.0
}

private fun getCabinet(blockEntity: Linkable, context: ItemUsageContext): TrafficCabinetBlockEntity? {
val be = context.world?.getBlockEntity(blockEntity.getLinkPos())
return if(be is TrafficCabinetBlockEntity) { be } else null
Expand Down Expand Up @@ -61,9 +57,9 @@ class Linker(settings: Settings) : Item(settings) {
}

private fun linkCabinet(cabinet: TrafficCabinetBlockEntity, context: ItemUsageContext): ActionResult {
if(cabinet.getTotalDevices() > MAX_DEVICES) {
if(cabinet.getTotalDevices() > RoadworksMain.CONFIG!!.trafficCabinet.maxDevices) {
// Too many devices connected to this box!
context.player?.sendMessage(Text.literal("There are too many devices connected to this box! Max is $MAX_DEVICES"), true)
context.player?.sendMessage(Text.literal("There are too many devices connected to this box! Max is ${RoadworksMain.CONFIG!!.trafficCabinet.maxDevices}"), true)
return ActionResult.SUCCESS
}

Expand All @@ -83,9 +79,9 @@ class Linker(settings: Settings) : Item(settings) {
return
}

if(!linkedFrom.pos.isWithinDistance(block.pos, MAX_DEVICE_DISTANCE)) {
if(!linkedFrom.pos.isWithinDistance(block.pos, RoadworksMain.CONFIG!!.trafficCabinet.maxLinkDistance)) {
// Device is too far!
context.player?.sendMessage(Text.literal("This device is too far! Max distance is $MAX_DEVICE_DISTANCE blocks"), true)
context.player?.sendMessage(Text.literal("This device is too far! Max distance is ${RoadworksMain.CONFIG!!.trafficCabinet.maxLinkDistance} blocks"), true)
return
}

Expand Down Expand Up @@ -133,9 +129,9 @@ class Linker(settings: Settings) : Item(settings) {
return
}

if(cabinet.getTotalDevices() >= MAX_DEVICES) {
if(cabinet.getTotalDevices() >= RoadworksMain.CONFIG!!.trafficCabinet.maxDevices) {
// Too many devices connected to this box!
context.player?.sendMessage(Text.literal("There are too many devices connected to this box! Max is $MAX_DEVICES"), true)
context.player?.sendMessage(Text.literal("There are too many devices connected to this box! Max is ${RoadworksMain.CONFIG!!.trafficCabinet.maxDevices}"), true)
return
}

Expand All @@ -147,9 +143,9 @@ class Linker(settings: Settings) : Item(settings) {
return
}

if(!linkedFrom.pos.isWithinDistance(cabinet.pos, MAX_DEVICE_DISTANCE)) {
if(!linkedFrom.pos.isWithinDistance(cabinet.pos, RoadworksMain.CONFIG!!.trafficCabinet.maxLinkDistance)) {
// Device is too far!
context.player?.sendMessage(Text.literal("This device is too far! Max distance is $MAX_DEVICE_DISTANCE blocks"), true)
context.player?.sendMessage(Text.literal("This device is too far! Max distance is ${RoadworksMain.CONFIG!!.trafficCabinet.maxLinkDistance} blocks"), true)
return
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/roadworks/blockstates/sign.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "roadworks:block/sign"
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/assets/roadworks/models/item/sign.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"parent": "roadworks:block/sign"
}
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"minecraft": "~1.20.1",
"java": ">=17",
"fabric-api": "*",
"fabric-language-kotlin": ">=1.9.0"
"fabric-language-kotlin": ">=1.9.0",
"cloth-config": ">=11.1.136"
}
}

0 comments on commit 66673d4

Please sign in to comment.