From 61bda722bddaf29a74a84e0cdb603c6e4ad73931 Mon Sep 17 00:00:00 2001 From: Samarium <28302241+Samarium150@users.noreply.github.com> Date: Sat, 5 Mar 2022 21:18:23 +0800 Subject: [PATCH] feat: implement configuration --- gradle.properties | 2 +- .../structures_compass/StructuresCompass.kt | 2 + .../client/gui/hud/StructuresCompassHud.kt | 86 +++++++++++-------- ...ntifierHelper.kt => LocalizationHelper.kt} | 0 .../client/util/RenderHelper.kt | 18 ++-- .../structures_compass/config/ClientConfig.kt | 27 ++++++ .../structures_compass/config/CommonConfig.kt | 24 ++++++ .../config/ConfigComments.kt | 39 +++++++++ .../structures_compass/config/FilterMode.kt | 21 +++++ .../mod/structures_compass/config/HudSide.kt | 21 +++++ .../config/StructuresCompassConfig.kt | 58 +++++++++++++ .../data/StructuresCompassData.kt | 7 +- .../mod/structures_compass/util/General.kt | 14 +++ .../util/ItemStackHelper.kt | 4 +- .../util/StructureHelper.kt | 14 +++ 15 files changed, 285 insertions(+), 52 deletions(-) rename src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/{IdentifierHelper.kt => LocalizationHelper.kt} (100%) create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ClientConfig.kt create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/CommonConfig.kt create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ConfigComments.kt create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/FilterMode.kt create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/HudSide.kt create mode 100644 src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/StructuresCompassConfig.kt diff --git a/gradle.properties b/gradle.properties index e1915d2..e97b4be 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/StructuresCompass.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/StructuresCompass.kt index be39563..18e97e7 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/StructuresCompass.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/StructuresCompass.kt @@ -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 @@ -26,5 +27,6 @@ object StructuresCompass: ModInitializer { CommandRegistry.init() ItemRegistry.init() StructuresCompassServerNetwork.init() + StructuresCompassConfig.load() } } diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/gui/hud/StructuresCompassHud.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/gui/hud/StructuresCompassHud.kt index 6e2b53e..1cf4ca7 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/gui/hud/StructuresCompassHud.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/gui/hud/StructuresCompassHud.kt @@ -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 @@ -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) { @@ -78,12 +85,12 @@ 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) @@ -91,37 +98,40 @@ object StructuresCompassHud { 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), diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/IdentifierHelper.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/LocalizationHelper.kt similarity index 100% rename from src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/IdentifierHelper.kt rename to src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/LocalizationHelper.kt diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/RenderHelper.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/RenderHelper.kt index aaadbaa..1c22797 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/RenderHelper.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/client/util/RenderHelper.kt @@ -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 @@ -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 ) } diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ClientConfig.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ClientConfig.kt new file mode 100644 index 0000000..cc95684 --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ClientConfig.kt @@ -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 . + */ +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 +) diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/CommonConfig.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/CommonConfig.kt new file mode 100644 index 0000000..970cf60 --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/CommonConfig.kt @@ -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 . + */ +package io.github.samarium150.minecraft.mod.structures_compass.config + +data class CommonConfig( + val filterMode: FilterMode = FilterMode.BLACKLIST, + val filterList: MutableList = mutableListOf(), + val maxDistance: Double = 5000.0, + val radius: Int = 64, +) diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ConfigComments.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ConfigComments.kt new file mode 100644 index 0000000..393d7a1 --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/ConfigComments.kt @@ -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 . + */ +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)" +) diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/FilterMode.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/FilterMode.kt new file mode 100644 index 0000000..12021ff --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/FilterMode.kt @@ -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 . + */ +package io.github.samarium150.minecraft.mod.structures_compass.config + +enum class FilterMode { + BLACKLIST, WHITELIST +} diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/HudSide.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/HudSide.kt new file mode 100644 index 0000000..128bad7 --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/HudSide.kt @@ -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 . + */ +package io.github.samarium150.minecraft.mod.structures_compass.config + +enum class HudSide { + LEFT, RIGHT +} diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/StructuresCompassConfig.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/StructuresCompassConfig.kt new file mode 100644 index 0000000..b760107 --- /dev/null +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/config/StructuresCompassConfig.kt @@ -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 . + */ +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) + } + } +} diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/data/StructuresCompassData.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/data/StructuresCompassData.kt index b7eade0..95d8f70 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/data/StructuresCompassData.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/data/StructuresCompassData.kt @@ -14,11 +14,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package io.github.samarium150.minecraft.mod.structures_compass.data import io.github.samarium150.minecraft.mod.structures_compass.util.getDimensions import io.github.samarium150.minecraft.mod.structures_compass.util.getIdentifier +import io.github.samarium150.minecraft.mod.structures_compass.util.isBanned import net.minecraft.server.world.ServerWorld import net.minecraft.util.Identifier import net.minecraft.util.registry.Registry @@ -29,13 +29,14 @@ object StructuresCompassData { val allowedStructures: MutableList> = mutableListOf() val structuresDimensionsMap: MutableMap, List> = mutableMapOf() - init { + fun init() { readAllowedStructures() } private fun readAllowedStructures() { Registry.STRUCTURE_FEATURE.forEach { - if (it.getIdentifier() != null) + val id = it.getIdentifier() + if (id != null && !id.isBanned()) allowedStructures.add(it) } } diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/General.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/General.kt index d5691bb..6592486 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/General.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/General.kt @@ -44,3 +44,17 @@ data class Rect( return this } } + +fun String.convertToRegex(): Regex { + val regex = StringBuilder("^") + for (i in this.indices) { + when (val c = this[i]) { + '*' -> regex.append(".*") + '?' -> regex.append(".") + '.' -> regex.append("\\.") + else -> regex.append(c) + } + } + regex.append("$") + return regex.toString().toRegex() +} diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/ItemStackHelper.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/ItemStackHelper.kt index fdb54b9..5c1b34a 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/ItemStackHelper.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/ItemStackHelper.kt @@ -16,6 +16,7 @@ */ package io.github.samarium150.minecraft.mod.structures_compass.util +import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig import io.github.samarium150.minecraft.mod.structures_compass.item.StructuresCompassItem import net.minecraft.item.ItemStack import net.minecraft.server.network.ServerPlayerEntity @@ -75,11 +76,12 @@ fun ItemStack.removeTag(tag: String): ItemStack { fun ItemStack.search(player: ServerPlayerEntity, structureId: Identifier) { val structure = structureId.getStructureFeature() + val radius = StructuresCompassConfig.configData.common.radius if (structure != null) { val world = player.serverWorld setStructure(structureId) player.sendMessage(TranslatableText("${prefix}msg_searching"), false) - val pos = world.locateStructure(structure, player.blockPos, 10000, getSkip()) + val pos = world.locateStructure(structure, player.blockPos, radius, getSkip()) player.sendMessage(TranslatableText("${prefix}msg_done"), false) if (pos == null) { removeTag(StructuresCompassItem.DIM_TAG) diff --git a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/StructureHelper.kt b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/StructureHelper.kt index 8984410..67526a3 100644 --- a/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/StructureHelper.kt +++ b/src/main/kotlin/io/github/samarium150/minecraft/mod/structures_compass/util/StructureHelper.kt @@ -16,6 +16,8 @@ */ package io.github.samarium150.minecraft.mod.structures_compass.util +import io.github.samarium150.minecraft.mod.structures_compass.config.FilterMode +import io.github.samarium150.minecraft.mod.structures_compass.config.StructuresCompassConfig import io.github.samarium150.minecraft.mod.structures_compass.data.StructuresCompassData import net.minecraft.entity.Entity import net.minecraft.server.world.ServerWorld @@ -64,3 +66,15 @@ fun BlockPos.getDistanceVector(entity: Entity): Vec3d { fun Vec3d.getLength(): Double { return sqrt(this.x * this.x + this.y * this.y + this.z * this.z) } + +fun Identifier.isBanned(): Boolean { + val flag = StructuresCompassConfig.configData.common.filterMode == FilterMode.WHITELIST + val filters = StructuresCompassConfig.configData.common.filterList + for (filter in filters) { + val matching = this.toString().matches(filter.convertToRegex()) + if (flag && matching) return false + else if (matching) + return true + } + return flag +}