Skip to content

Commit

Permalink
feat: implement configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarium150 committed Mar 5, 2022
1 parent 7570baa commit 61bda72
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 52 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loaderVersion = 0.13.3
fabricVersion = 0.42.0+1.16
loomVersion = 0.7-SNAPSHOT
# Mod Properties
modVersion = 1.4.0-beta.1
modVersion = 1.4.0
mavenGroup = io.github.samarium150
archivesBaseName = structures_compass
# Kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.github.samarium150.minecraft.mod.structures_compass

import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig
import io.github.samarium150.minecraft.mod.structures_compass.init.CommandRegistry
import io.github.samarium150.minecraft.mod.structures_compass.init.ItemRegistry
import io.github.samarium150.minecraft.mod.structures_compass.network.StructuresCompassServerNetwork
Expand All @@ -26,5 +27,6 @@ object StructuresCompass: ModInitializer {
CommandRegistry.init()
ItemRegistry.init()
StructuresCompassServerNetwork.init()
StructuresCompassConfig.load()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ package io.github.samarium150.minecraft.mod.structures_compass.client.gui.hud
import io.github.samarium150.minecraft.mod.structures_compass.client.util.drawConfiguredStringOnHud
import io.github.samarium150.minecraft.mod.structures_compass.client.util.getLocalizedDimensionName
import io.github.samarium150.minecraft.mod.structures_compass.client.util.getLocalizedStructureName
import io.github.samarium150.minecraft.mod.structures_compass.config.ClientConfig
import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig
import io.github.samarium150.minecraft.mod.structures_compass.init.ItemRegistry
import io.github.samarium150.minecraft.mod.structures_compass.util.*
import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.client.font.TextRenderer
import net.minecraft.client.gui.screen.ChatScreen
import net.minecraft.client.resource.language.I18n
import net.minecraft.client.util.math.MatrixStack

Expand All @@ -33,8 +36,12 @@ object StructuresCompassHud {
private val textRenderer: TextRenderer
get() = minecraftClient.textRenderer

private val config: ClientConfig
get() = StructuresCompassConfig.configData.client

fun render(matrixStack: MatrixStack) {
if (minecraftClient.currentScreen == null) {
if (minecraftClient.currentScreen == null ||
(config.displayWithChatOpen && minecraftClient.currentScreen is ChatScreen)) {
val player = minecraftClient.player
if (player != null) {
val itemStack = if (player.mainHandStack.item == ItemRegistry.STRUCTURES_COMPASS) {
Expand Down Expand Up @@ -78,50 +85,53 @@ object StructuresCompassHud {
)
relLineOffset++

// TODO: Add if statement for checking information level
textRenderer.drawConfiguredStringOnHud(
matrixStack, I18n.translate("${prefix}hud_pos") +
" [${position.x}, ${if (position.y == 0) "X" else "${position.y}"}, ${position.z}]",
5, 5, 0x4AFF4A, ++relLineOffset
)
if (config.HudInfoLevel == 3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, I18n.translate("${prefix}hud_pos") +
" [${position.x}, ${if (position.y == 0) "X" else "${position.y}"}, ${position.z}]",
5, 5, 0x4AFF4A, ++relLineOffset
)

if (player.world.registryKey.value == dimension) {
val distanceVector = position.getDistanceVector(player)
val x = distanceVector.x
val y = distanceVector.y
val z = distanceVector.z
val distance = distanceVector.getLength()
if (x > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_east")} $x",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (x < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_west")} ${-x}",
5, 5, 0xFFFFFF, ++relLineOffset
)
if (z > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_south")} $z",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (z < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_north")} ${-z}",
5, 5, 0xFFFFFF, ++relLineOffset
)
if (y > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_up")} $y",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (y < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_down")} ${-y}",
5, 5, 0xFFFFFF, ++relLineOffset
)
if (relLineOffset > 6 || distance > 0.5) {
if (config.HudInfoLevel == 3) {
if (x > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_east")} $x",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (x < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_west")} ${-x}",
5, 5, 0xFFFFFF, ++relLineOffset
)
if (z > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_south")} $z",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (z < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_north")} ${-z}",
5, 5, 0xFFFFFF, ++relLineOffset
)
if (y > 0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_up")} $y",
5, 5, 0xFFFFFF, ++relLineOffset
)
else if (y < -0.3)
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_down")} ${-y}",
5, 5, 0xFFFFFF, ++relLineOffset
)
}
if ((config.HudInfoLevel == 3 && relLineOffset > 6) ||
(config.HudInfoLevel >= 3 && distance > config.closedEnough)) {
relLineOffset++
textRenderer.drawConfiguredStringOnHud(
matrixStack, "${I18n.translate("${prefix}hud_distance")} %.3f".format(distance),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package io.github.samarium150.minecraft.mod.structures_compass.client.util

import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import io.github.samarium150.minecraft.mod.structures_compass.config.HudSide
import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig
import io.github.samarium150.minecraft.mod.structures_compass.util.Rect
import io.github.samarium150.minecraft.mod.structures_compass.util.minecraftClient
import net.fabricmc.api.EnvType
Expand All @@ -34,24 +36,22 @@ fun TextRenderer.drawConfiguredStringOnHud(
string: String, xOffset: Int, yOffset: Int,
color: Int, relLineOffset: Int
) {
// TODO: replace 1 with StructuresCompassConfig.overlayLineOffset
val configuredYOffset = yOffset + (relLineOffset + 1) * 9
// TODO: replace ture with StructuresCompassConfig.hudPosition
if (true)
val clientConfig = StructuresCompassConfig.configData.client
val configuredYOffset = yOffset + (relLineOffset + clientConfig.overlayLineOffset) * 9
if (clientConfig.HudPosition == HudSide.RIGHT)
draw(
matrixStack,
string,
(xOffset + 7 - 5).toFloat(), // TODO: replace 7 with StructuresCompassConfig.xOffset
(configuredYOffset + 16 - 14).toFloat(), // TODO: replace 16 with StructuresCompassConfig.yOffset
(minecraftClient.window.scaledWidth - getWidth(string) - xOffset - clientConfig.xOffset + 5).toFloat(),
(configuredYOffset + clientConfig.yOffset - 14).toFloat(),
color
)
else
draw(
matrixStack,
string,
// TODO: replace 7 with StructuresCompassConfig.xOffset
(minecraftClient.window.scaledWidth - getWidth(string) - xOffset - 7 + 5).toFloat(),
(configuredYOffset + 0 - 14).toFloat(), // TODO: replace 0 with StructuresCompassConfig.yOffset
(xOffset + clientConfig.xOffset - 5).toFloat(),
(configuredYOffset + clientConfig.yOffset - 14).toFloat(),
color
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

data class ClientConfig(
val HudInfoLevel: Int = 3,
val HudPosition: HudSide = HudSide.LEFT,
val displayWithChatOpen: Boolean = true,
val xOffset: Int = 7,
val yOffset: Int = 16,
val overlayLineOffset: Int = 1,
val closedEnough: Double = 0.3
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

data class CommonConfig(
val filterMode: FilterMode = FilterMode.BLACKLIST,
val filterList: MutableList<String> = mutableListOf(),
val maxDistance: Double = 5000.0,
val radius: Int = 64,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

data class ConfigComments(
val _filterMode: String = "The mode of the filter, either blacklist or whitelist, default is blacklist",
val _filter: String = "A list of structures that the compass will not search in blacklist mode " +
"or will only search in whitelist mode, specified by resource location, supporting regex",
val _maxDistance: String = "The pseudo maximum searching radius. " +
"If the distance to the structure exceeds this value, HUD would display 'Not Found'",
val _radius: String = "The real maximum searching radius used by the underlying method (no idea how it works.)" +
"If you still couldn't find a structure with a big enough MaxSearchRadius, increase this one." +
"If you think searching makes the server slow, decrease this one.",
val _HudInfoLevel: String = "HUD information detail level. 0: Nothing." +
"1+: Structure and Dimension name." +
"2+: Distance to the structure." +
"3: Position of the structure and distance in x/y/z axis.",
val _HudPosition: String = "The side of the information HUD. Either LEFT or RIGHT.",
val _displayWithChatOpen: String = "Displays the compass information HUD even while chat is open.",
val _xOffset: String = "The X offset for information rendered on the HUD. (default:7)",
val _yOffset: String = "The Y offset for information rendered on the HUD. (default:16)",
val _overlayLineOffset: String = "The line offset for information rendered on the HUD. (default:1)",
val _closedEnough: String = "The X/Y/Z-distance won't be shown " +
"if the distance is smaller than the value. (default:0.3)"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

enum class FilterMode {
BLACKLIST, WHITELIST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

enum class HudSide {
LEFT, RIGHT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2022 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
package io.github.samarium150.minecraft.mod.structures_compass.config

import com.google.gson.GsonBuilder
import com.google.gson.JsonIOException
import io.github.samarium150.minecraft.mod.structures_compass.data.StructuresCompassData
import io.github.samarium150.minecraft.mod.structures_compass.util.MOD_ID
import net.fabricmc.loader.api.FabricLoader
import java.io.IOException
import java.nio.file.Files

object StructuresCompassConfig {

private val configFilePath
get() = FabricLoader.getInstance().configDir.resolve("$MOD_ID.json")

private val gson = GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create()

data class ConfigData(
val comments: ConfigComments = ConfigComments(),
val common: CommonConfig,
val client: ClientConfig
)

var configData = ConfigData(common = CommonConfig(), client = ClientConfig())

@Throws(SecurityException::class, IOException::class, JsonIOException::class)
fun load() {
if (Files.exists(configFilePath))
Files.newBufferedReader(configFilePath).use { reader ->
configData = gson.fromJson(reader, ConfigData::class.java)
}
save()
StructuresCompassData.init()
}

@Throws(JsonIOException::class, IOException::class)
private fun save() {
Files.newBufferedWriter(configFilePath).use { writer ->
gson.toJson(configData, writer)
}
}
}
Loading

0 comments on commit 61bda72

Please sign in to comment.