Skip to content

Commit

Permalink
Add use and attack blacklist, add LibGUI to dependencies, refactor so…
Browse files Browse the repository at this point in the history
…me things
  • Loading branch information
NiChrosia committed Aug 15, 2021
1 parent 26fedf8 commit 4f3f438
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 127 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ group = project.maven_group
repositories {
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases" }
maven {
name = "CottonMC"
url = "https://server.bbkr.space/artifactory/libs-release"
}
}

dependencies {
Expand All @@ -22,7 +26,6 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modImplementation "net.fabricmc:fabric-language-kotlin:${project.fabric_kotlin_version}"

modImplementation("com.terraformersmc:modmenu:${project.mod_menu_version}") {
Expand All @@ -32,6 +35,10 @@ dependencies {
modApi("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
}

modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
}

processResources {
Expand Down
6 changes: 4 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ minecraft_version=1.17.1
yarn_mappings=1.17.1+build.31
loader_version=0.11.6

# Fabric api
# Fabric API
fabric_version=0.37.1+1.17

loom_version = 0.8-SNAPSHOT

# Mod Properties
# Mod properties
mod_version = 2.0.1
maven_group = nichrosia
archives_base_name = nobreak

# External mod properties
cloth_config_version = 5.0.34
mod_menu_version = 2.0.2
libgui_version = 4.1.4+1.17.1

# Kotlin
kotlin_version = 1.5.0
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/nichrosia/nobreak/NoBreak.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nichrosia.nobreak

import net.fabricmc.api.ClientModInitializer
import nichrosia.nobreak.content.*
import nichrosia.nobreak.content.type.Content

@Suppress("MemberVisibilityCanBePrivate")
object NoBreak : ClientModInitializer {
Expand All @@ -11,7 +12,7 @@ object NoBreak : ClientModInitializer {
)

override fun onInitializeClient() {
content.forEach(NBContent::register)
content.forEach(Content::load)

NBSettings.load()
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/kotlin/nichrosia/nobreak/content/NBContent.kt

This file was deleted.

66 changes: 58 additions & 8 deletions src/main/kotlin/nichrosia/nobreak/content/NBEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,77 @@ package nichrosia.nobreak.content

import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
import net.fabricmc.fabric.api.event.player.AttackEntityCallback
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents
import net.fabricmc.fabric.api.event.player.UseBlockCallback
import net.minecraft.item.ToolItem
import net.minecraft.text.TranslatableText
import nichrosia.nobreak.util.MessageUtil.inform
import nichrosia.nobreak.util.MessageUtil.onOrOff
import net.minecraft.util.ActionResult
import nichrosia.nobreak.content.type.Content
import nichrosia.nobreak.util.MessageUtilities.inform
import nichrosia.nobreak.util.MessageUtilities.onOrOff

object NBEvents : NBContent {
override fun register() {
PlayerBlockBreakEvents.BEFORE.register(PlayerBlockBreakEvents.Before { _, playerEntity, _, _, _ ->
object NBEvents : Content {
override fun load() {
PlayerBlockBreakEvents.BEFORE.register Before@ { _, playerEntity, _, _, _ ->
if (NBSettings.allowBreakage) return@Before true

val stack = playerEntity.getStackInHand(playerEntity.activeHand)

if (stack.item !is ToolItem ||
stack.maxDamage - stack.damage != 1 ||
!NBSettings.isBlacklisted(stack)) return@Before true
!NBSettings.isBlacklisted(stack)
) return@Before true

playerEntity.inform(TranslatableText("text.nobreak.tool_break_prevented", NBKeyBinds.toggleToolBreakage.boundKeyLocalizedText))
playerEntity.inform(
TranslatableText(
"text.nobreak.tool_break_prevented",
NBKeyBinds.toggleToolBreakage.boundKeyLocalizedText
)
)

return@Before false
})
}

AttackEntityCallback.EVENT.register Before@ { player, _, hand, _, _ ->
println("attempting to stop tool breakage")

val stack = player.getStackInHand(hand)

if (NBSettings.allowBreakage) return@Before ActionResult.PASS

println("allow breakage is disabled")
println("damage: ${stack.maxDamage - stack.damage}/${stack.maxDamage}")

if (stack.maxDamage - stack.damage != 1 ||
!NBSettings.isBlacklisted(stack)) return@Before ActionResult.SUCCESS

println("item is at 1 durability and not blacklisted")

player.inform(TranslatableText("text.nobreak.tool_break_prevented", NBKeyBinds.toggleToolBreakage.boundKeyLocalizedText))

return@Before ActionResult.FAIL
}

UseBlockCallback.EVENT.register Before@ { player, _, hand, _ ->
val stack = player.getStackInHand(hand)

println("attempting to stop tool breakage")

if (NBSettings.allowBreakage || stack.isEmpty) return@Before ActionResult.PASS

println("allow breakage is disabled")
println("damage: ${stack.maxDamage - stack.damage}/${stack.maxDamage}")

if (stack.maxDamage - stack.damage != 1 ||
!NBSettings.isBlacklisted(stack)) return@Before ActionResult.PASS

println("item is at 1 durability and not blacklisted")

player.inform(TranslatableText("text.nobreak.tool_break_prevented", NBKeyBinds.toggleToolBreakage.boundKeyLocalizedText))

return@Before ActionResult.FAIL
}

ClientTickEvents.END_CLIENT_TICK.register {
it.player?.let { player ->
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/nichrosia/nobreak/content/NBKeyBinds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package nichrosia.nobreak.content
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper
import net.minecraft.client.option.KeyBinding
import net.minecraft.client.util.InputUtil
import nichrosia.nobreak.content.type.Content
import org.lwjgl.glfw.GLFW

object NBKeyBinds : NBContent {
object NBKeyBinds : Content {
lateinit var toggleToolBreakage: KeyBinding
lateinit var toggleCurrentItemBlacklist: KeyBinding

override fun register() {
override fun load() {
toggleToolBreakage = KeyBindingHelper.registerKeyBinding(KeyBinding(
"key.nobreak.toggle_tool_breakage",
InputUtil.Type.KEYSYM,
Expand Down
39 changes: 5 additions & 34 deletions src/main/kotlin/nichrosia/nobreak/content/NBSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package nichrosia.nobreak.content
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.item.ToolItem
import net.minecraft.item.ToolMaterials
import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry
import nichrosia.nobreak.util.DataStreams.readItemArr
import nichrosia.nobreak.util.DataStreams.writeItemArr
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.File
Expand All @@ -16,22 +14,16 @@ object NBSettings {
private val configDir = File(FabricLoader.getInstance().configDir.pathString + "/nobreak")
private val configFile = File(configDir.path + "/config.dat")

val toolBlacklist = mutableListOf<Item>()

var allowBreakage = false
var notifyUser = true

var diamondPlusToolsCanBreak = false
var enchantedToolsCanBreak = false

val toolBlacklist = mutableListOf<Item>()

fun isBlacklisted(itemStack: ItemStack): Boolean {
val material = (itemStack.item as ToolItem).material

return ((material == ToolMaterials.DIAMOND ||
material == ToolMaterials.NETHERITE ||
material.miningLevel >= 3 && !diamondPlusToolsCanBreak) ||
(itemStack.hasEnchantments() && !enchantedToolsCanBreak) ||
toolBlacklist.contains(itemStack.item))
return toolBlacklist.contains(itemStack.item)
}

private fun createFiles() {
Expand Down Expand Up @@ -64,25 +56,4 @@ object NBSettings {

toolBlacklist.addAll(r.readItemArr())
}

private fun DataOutputStream.writeItemArr(arr: Iterable<Item>) {
writeUTF(arr.joinToString(separator = "|") {
Registry.ITEM.getId(it).toString()
})
}

private fun DataInputStream.readItemArr(): MutableList<Item> {
val output = mutableListOf<Item>()
val items = readUTF().split("|")

for (it in items) {
if (!it.contains(":")) continue

val (namespace, path) = it.split(":")
val identity = Identifier(namespace, path)
output.add(Registry.ITEM.get(identity))
}

return output
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/nichrosia/nobreak/content/type/Content.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nichrosia.nobreak.content.type

interface Content {
fun load()
}
56 changes: 56 additions & 0 deletions src/main/kotlin/nichrosia/nobreak/ext/NBClothConfigSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package nichrosia.nobreak.ext

import me.shedaniel.clothconfig2.api.ConfigBuilder
import me.shedaniel.clothconfig2.api.ConfigCategory
import net.minecraft.client.gui.screen.Screen
import net.minecraft.text.TranslatableText
import nichrosia.nobreak.content.NBSettings

object NBClothConfigSettings {
fun build(parent: Screen): Screen {
val builder = ConfigBuilder.create()
.setParentScreen(parent)
.setTitle(TranslatableText("title.nobreak.config"))
.setSavingRunnable {

}

addCategories(builder)

return builder.build()
}

private fun addCategories(builder: ConfigBuilder) {
val general = builder.getOrCreateCategory(TranslatableText("category.nobreak.general"))

addEntries(builder, general)
}

private fun addEntries(builder: ConfigBuilder, general: ConfigCategory) {
val entryBuilder = builder.entryBuilder()

general.addEntry(entryBuilder.startBooleanToggle(
TranslatableText("setting.nobreak.do_break"),
NBSettings.allowBreakage)
.setSaveConsumer { NBSettings.allowBreakage = it }
.build())

general.addEntry(entryBuilder.startBooleanToggle(
TranslatableText("setting.nobreak.do_feedback"),
NBSettings.notifyUser)
.setSaveConsumer { NBSettings.notifyUser = it }
.build())

general.addEntry(entryBuilder.startBooleanToggle(
TranslatableText("setting.nobreak.diamond+_tools_can_break"),
NBSettings.diamondPlusToolsCanBreak)
.setSaveConsumer { NBSettings.diamondPlusToolsCanBreak = it }
.build())

general.addEntry(entryBuilder.startBooleanToggle(
TranslatableText("setting.nobreak.enchanted_tools_can_break"),
NBSettings.enchantedToolsCanBreak)
.setSaveConsumer { NBSettings.enchantedToolsCanBreak = it }
.build())
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package nichrosia.nobreak.integration
package nichrosia.nobreak.ext

import com.terraformersmc.modmenu.api.ConfigScreenFactory
import com.terraformersmc.modmenu.api.ModMenuApi

@Suppress("unused")
object NBModMenuIntegration : ModMenuApi {
object NBModMenuSettings : ModMenuApi {
override fun getModConfigScreenFactory(): ConfigScreenFactory<*> {
return ConfigScreenFactory(NBClothConfigIntegration::build)
return ConfigScreenFactory(NBClothConfigSettings::build)
}
}

This file was deleted.

Loading

0 comments on commit 4f3f438

Please sign in to comment.