Skip to content

Commit

Permalink
Fully implement blacklist screen, remove now-useless break toggle, ad…
Browse files Browse the repository at this point in the history
…d shield info, and fix config (Closes #3, and closes #5)
  • Loading branch information
NiChrosia committed Aug 16, 2021
1 parent 342ab13 commit 9613adc
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 162 deletions.
88 changes: 25 additions & 63 deletions src/main/kotlin/nichrosia/nobreak/content/NBEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.client.MinecraftClient
import net.minecraft.item.ToolItem
import net.minecraft.item.ShieldItem
import net.minecraft.text.TranslatableText
import net.minecraft.util.ActionResult
import nichrosia.nobreak.content.type.Content
Expand All @@ -17,106 +17,68 @@ import nichrosia.nobreak.util.MessageUtilities.onOrOff
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) ||
stack.isEmpty
) return@Before true
if (NBSettings.breakingAllowedFor(stack, playerEntity)) return@Before true

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

return@Before false
}

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

val stack = player.getStackInHand(hand)

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.SUCCESS

println("item is at 1 durability and not blacklisted")
if (stack.isEmpty) return@Before ActionResult.PASS
if (NBSettings.breakingAllowedFor(stack, player)) return@Before ActionResult.SUCCESS

player.inform(TranslatableText("text.nobreak.tool_break_prevented", NBKeyBinds.toggleToolBreakage.boundKeyLocalizedText))
player.inform(TranslatableText("text.nobreak.tool_break_prevented", NBKeyBinds.toggleCurrentItemBlacklist.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")
if (stack.isEmpty) return@Before ActionResult.PASS
if (NBSettings.breakingAllowedFor(stack, player)) return@Before ActionResult.PASS

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

return@Before ActionResult.FAIL
}

ClientTickEvents.END_CLIENT_TICK.register {
it.player?.let { player ->
it.player?.let tick@{ player ->
run {
var hasToggled = false

while(NBKeyBinds.toggleToolBreakage.wasPressed() && !hasToggled) {
NBSettings.allowBreakage = !NBSettings.allowBreakage
if (NBKeyBinds.toggleCurrentItemBlacklist.wasPressed()) {
player.mainHandStack.item.let { item ->
if (!item.isDamageable) return@tick

player.inform(
TranslatableText(
"text.nobreak.toggled_tool_breakage",
onOrOff(NBSettings.allowBreakage)
)
)
if (item is ShieldItem) {
player.inform(TranslatableText("text.nobreak.shields_not_supported"))

hasToggled = true
}
}
return@tick
}

run {
var hasToggled = false
val currentBlacklist = NBSettings.blacklist.copy()

while(NBKeyBinds.toggleCurrentItemBlacklist.wasPressed() && !hasToggled) {
player.mainHandStack.item.let { item ->
if (NBSettings.toolBlacklist.contains(item)) {
NBSettings.toolBlacklist.remove(item)
NBSettings.blacklist = if (NBSettings.blacklist.items(player).contains(item)) {
NBSettings.blacklist.copy(items = { currentBlacklist.items(player) - item })
} else {
NBSettings.toolBlacklist.add(item)
NBSettings.blacklist.copy(items = { currentBlacklist.items(player) + item })
}
}

player.inform(
TranslatableText(
"text.nobreak.toggled_item_blacklist",
onOrOff(NBSettings.toolBlacklist.contains(player.mainHandStack.item)),
player.mainHandStack.item.name
)
)

hasToggled = true
player.inform(TranslatableText(
"text.nobreak.toggled_item_blacklist",
onOrOff(NBSettings.blacklist.items(player).contains(player.mainHandStack.item)),
player.mainHandStack.item.name))
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/kotlin/nichrosia/nobreak/content/NBKeyBinds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@ import nichrosia.nobreak.content.type.Content
import org.lwjgl.glfw.GLFW

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

override fun load() {
toggleToolBreakage = KeyBindingHelper.registerKeyBinding(KeyBinding(
"key.nobreak.toggle_tool_breakage",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_Y,
"category.nobreak.keybindings"
))

toggleCurrentItemBlacklist = KeyBindingHelper.registerKeyBinding(KeyBinding(
"key.nobreak.toggle_current_item_blacklist",
InputUtil.Type.KEYSYM,
Expand Down
31 changes: 14 additions & 17 deletions src/main/kotlin/nichrosia/nobreak/content/NBSettings.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package nichrosia.nobreak.content

import net.fabricmc.loader.api.FabricLoader
import net.minecraft.item.Item
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry
import nichrosia.nobreak.gui.screen.description.PresetScreenDescription
import nichrosia.nobreak.util.DataStreams
import nichrosia.nobreak.util.DataStreams.bool
import nichrosia.nobreak.util.DataStreams.readItemArr
import nichrosia.nobreak.util.DataStreams.writeItemArr
import java.io.*
import nichrosia.nobreak.util.DataStreams.str
import java.io.DataInputStream
import java.io.DataOutputStream
import java.io.File
import kotlin.io.path.pathString

object NBSettings {
private val configDir = File(FabricLoader.getInstance().configDir.pathString + "/nobreak")
private val configFile = File(configDir.path + "/config.dat")

val toolBlacklist = mutableListOf<Item>()
lateinit var blacklist: PresetScreenDescription.BlacklistPreset

var allowBreakage = false
var blacklist = PresetScreenDescription.BlacklistPreset.empty
var notifyUser = true

fun isBlacklisted(itemStack: ItemStack): Boolean {
return toolBlacklist.contains(itemStack.item)
fun breakingAllowedFor(itemStack: ItemStack, player: PlayerEntity): Boolean {
return (blacklist.items(player).contains(itemStack.item) ||
blacklist.isBreakingAllowed(itemStack)) &&
if (itemStack.hasEnchantments()) blacklist.isEnchanted(itemStack) else true
}

private fun createFiles() {
Expand All @@ -35,20 +36,16 @@ object NBSettings {

val write = DataOutputStream(configFile.outputStream())

write.bool(allowBreakage)
write.bool(notifyUser)

write.writeItemArr(toolBlacklist)
write.str(blacklist.items(null).joinToString("|") { Registry.ITEM.getId(it).toString() })
}

fun load() {
if (!configFile.exists()) return

val read = DataInputStream(configFile.inputStream())

allowBreakage = read.bool()
notifyUser = read.bool()

toolBlacklist.addAll(read.readItemArr())
blacklist.addItems(read.str().split("|").map { Registry.ITEM.get(Identifier(it)) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ object NBClothConfigSettings {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import io.github.cottonmc.cotton.gui.client.CottonClientScreen
import nichrosia.nobreak.gui.screen.description.BlacklistScreenDescription
import nichrosia.nobreak.gui.screen.description.PresetScreenDescription

open class PresetScreen(from: BlacklistScreenDescription, guiDescription: GuiDescription = PresetScreenDescription(from)) : CottonClientScreen(guiDescription)
open class PresetScreen(guiDescription: GuiDescription = PresetScreenDescription()) : CottonClientScreen(guiDescription)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.cottonmc.cotton.gui.widget.*
import io.github.cottonmc.cotton.gui.widget.data.Axis
import io.github.cottonmc.cotton.gui.widget.data.Insets
import net.minecraft.client.MinecraftClient
import net.minecraft.enchantment.Enchantments
import net.minecraft.item.ItemStack
import net.minecraft.text.TranslatableText
import nichrosia.nobreak.content.NBSettings
Expand All @@ -13,6 +14,7 @@ import nichrosia.nobreak.gui.screen.PresetScreen
@Suppress("MemberVisibilityCanBePrivate", "LeakingThis")
open class BlacklistScreenDescription : LightweightGuiDescription() {
open val elementsPerRow = 5
open val rowsPerColumn = 10
open val blacklistedItemBox: WBoxOpen

init {
Expand All @@ -30,7 +32,7 @@ open class BlacklistScreenDescription : LightweightGuiDescription() {

val clearButton = WButton(TranslatableText("button.nobreak.reset_blacklist")).apply {
onClick = Runnable {
NBSettings.toolBlacklist.clear()
NBSettings.blacklist = PresetScreenDescription.BlacklistPreset.empty
blacklistedItemBox.clearChildren()

populateBlacklistBox()
Expand All @@ -41,7 +43,7 @@ open class BlacklistScreenDescription : LightweightGuiDescription() {

val presetButton = WButton(TranslatableText("button.nobreak.open_presets")).apply {
onClick = Runnable {
MinecraftClient.getInstance().setScreen(PresetScreen(this@BlacklistScreenDescription))
MinecraftClient.getInstance().setScreen(PresetScreen())
}
}

Expand All @@ -51,17 +53,25 @@ open class BlacklistScreenDescription : LightweightGuiDescription() {
open fun populateBlacklistBox() {
var rowBox = WBox(Axis.HORIZONTAL)

NBSettings.toolBlacklist.forEachIndexed { index, item ->
rowBox.add(WItem(ItemStack(item)))
run forEach@{
NBSettings.blacklist.items(MinecraftClient.getInstance().player).forEachIndexed { index, item ->
val stack = ItemStack(item)
if (NBSettings.blacklist.isEnchanted(stack)) stack.addEnchantment(Enchantments.MENDING, 1)

if ((index + 1) % elementsPerRow == 0) {
blacklistedItemBox.add(rowBox)
rowBox.add(WItem(stack))

rowBox = WBox(Axis.HORIZONTAL)
if ((index + 1) % elementsPerRow == 0) {
blacklistedItemBox.add(rowBox)

rowBox = WBox(Axis.HORIZONTAL)

val row = (index + 1) / elementsPerRow
if (row >= rowsPerColumn) return@forEach
}
}
}

if (NBSettings.toolBlacklist.size % elementsPerRow != 0) blacklistedItemBox.add(rowBox)
if (NBSettings.blacklist.items(MinecraftClient.getInstance().player).size % elementsPerRow != 0) blacklistedItemBox.add(rowBox)
}

open class WBoxOpen(axis: Axis) : WBox(axis) {
Expand Down
Loading

0 comments on commit 9613adc

Please sign in to comment.