Skip to content

Commit

Permalink
1.3.0 Beta Fabric 1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
eth0net authored Jul 31, 2022
2 parents 78199ec + 715d58b commit 9e84b6a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ Enchant Menu adds a simple menu for modifying item enchantments.
- Place an enchantable item in and select your enchantments
- Keys can be changed in Minecraft controls settings

## Current Features
## Features
- Select enchantments from the list of possible enchantments
- Select the level of enchantment from 1 to 10
- Supports custom enchantments
- Toggle to surpass vanilla level limits
- Toggle to allow incompatible enchantments
- Toggle to allow treasure-only enchantments
- Configuration of menu features

## Planned Features
- Enchantment search box

## Gallery
Expand Down
Binary file modified assets/enchant-menu.aseprite
Binary file not shown.
6 changes: 6 additions & 0 deletions changelogs/1.3.0-beta+fabric-1.19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Now with search!

**Added**:
- Search box to filter enchantments by name

**Full Changelog**: https://github.com/eth0net/enchant-menu/compare/v1.2.1+fabric-1.19...v1.3.0-beta+fabric-1.19
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ completeConfigVersionId=zaC01d1T
# Mod Properties
mavenGroup=com.github.eth0net
modId=enchant-menu
modVersion=1.2.1+fabric-1.19
modVersionName=1.2.1 Fabric 1.19
modVersionType=release
modVersion=1.3.0-beta+fabric-1.19
modVersionName=1.3.0 Beta Fabric 1.19
modVersionType=Beta
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.fabricmc.api.Environment
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
import net.minecraft.client.gui.screen.ingame.HandledScreen
import net.minecraft.client.gui.widget.TextFieldWidget
import net.minecraft.client.gui.widget.TexturedButtonWidget
import net.minecraft.client.render.DiffuseLighting
import net.minecraft.client.render.GameRenderer
Expand All @@ -19,6 +20,7 @@ import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.item.ItemStack
import net.minecraft.text.Text
import org.lwjgl.glfw.GLFW
import kotlin.math.roundToInt

@Environment(EnvType.CLIENT)
Expand All @@ -31,6 +33,9 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
private var stack = ItemStack.EMPTY
private var ticks = 0

private var searchBox: TextFieldWidget? = null
private var focusSearchBox = false

private val maxRows = 5
private val canScroll get() = handler.enchantments.size > maxRows
private val maxScrollOffset get() = if (canScroll) handler.enchantments.size - maxRows else 0
Expand Down Expand Up @@ -62,6 +67,12 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
val y = (height - backgroundHeight) / 2
val clickX = mouseX.roundToInt() - (x + 44)

focusSearchBox = searchBox?.isMouseOver(mouseX, mouseY) ?: false
if (!focusSearchBox) {
focused = null
searchBox?.setTextFieldFocused(false)
}

for (i in 0 until maxRows) {
val index = i + scrollOffset
val clickY = mouseY.roundToInt() - (y + 19 + 12 * i)
Expand All @@ -80,16 +91,28 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
}

override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
if (focusSearchBox && keyCode == GLFW.GLFW_KEY_ESCAPE) {
focusSearchBox = false
focused = null
searchBox?.setTextFieldFocused(false)
return true
}

if (focusSearchBox && keyCode != GLFW.GLFW_KEY_BACKSPACE) return false

if (KeyBindings.ToggleMenu.matchesKey(keyCode, scanCode)) close()
if (KeyBindings.IncrementLevel.matchesKey(keyCode, scanCode)) onIncrementLevelClick()
if (KeyBindings.DecrementLevel.matchesKey(keyCode, scanCode)) onDecrementLevelClick()
if (KeyBindings.ToggleIncompatible.matchesKey(keyCode, scanCode)) onToggleIncompatibleClick()
if (KeyBindings.ToggleLevel.matchesKey(keyCode, scanCode)) onToggleLevelClick()
if (KeyBindings.ToggleTreasure.matchesKey(keyCode, scanCode)) onToggleTreasureClick()

return super.keyPressed(keyCode, scanCode, modifiers)
}

override fun drawBackground(matrices: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
clearChildren()

DiffuseLighting.disableGuiDepthLighting()
RenderSystem.setShader(GameRenderer::getPositionTexShader)
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f)
Expand All @@ -106,7 +129,6 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
textRenderer.drawTrimmed(Text.literal(level), x + 23 - level.length * 3, y + 21, level.length * 6, 0xFFFFFF)

// level change buttons
clearChildren()
if (handler.level < EnchantMenuConfig.Levels.maximum) addDrawableChild(TexturedButtonWidget(
x + 31, y + 18, 8, 13, 149, 166, texture
) { onIncrementLevelClick() })
Expand All @@ -133,6 +155,14 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
x + 178, y + 5, 11, 11, 190, if (handler.treasureUnlocked) 177 else 166, texture
) { onToggleTreasureClick() })

// search box
searchBox = TextFieldWidget(textRenderer, x + 78, y + 6, 71, 9, Text.translatable("enchant-menu.title"))
searchBox?.setChangedListener { handler.search = it }
searchBox?.text = handler.search
addSelectableChild(searchBox)
addDrawableChild(searchBox)
if (focusSearchBox) setInitialFocus(searchBox)

// enchantments list, from scroll offset to max rows
for (i in 0 until maxRows) {
val index = i + scrollOffset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class EnchantMenuScreenHandler(

internal var enchantments: List<Triple<Enchantment, Int, Boolean>> = listOf()

internal var search = ""
set(value) {
field = value
onContentChanged(inventory)
}

internal var level = EnchantMenuConfig.Levels.default
set(value) {
field = if (value < EnchantMenuConfig.Levels.minimum) {
Expand Down Expand Up @@ -164,7 +170,10 @@ class EnchantMenuScreenHandler(
}

private fun Enchantment.acceptableStack(stack: ItemStack): Boolean {
return isAcceptableItem(stack) && (!isTreasure || treasureUnlocked || stack.hasEnchantment(this))
val acceptable = isAcceptableItem(stack)
val allowed = !isTreasure || treasureUnlocked || stack.hasEnchantment(this)
val inSearch = getName(level).string.lowercase().contains(search.lowercase())
return acceptable && allowed && inSearch
}

private val ItemStack.acceptableEnchantments get() = Registry.ENCHANTMENT.filter { it.acceptableStack(this) }
Expand All @@ -187,10 +196,12 @@ class EnchantMenuScreenHandler(

internal fun incrementLevel() {
if (level < EnchantMenuConfig.Levels.maximum) ++level
onContentChanged(inventory)
}

internal fun decrementLevel() {
if (level > EnchantMenuConfig.Levels.minimum) --level
onContentChanged(inventory)
}

internal fun toggleIncompatible() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9e84b6a

Please sign in to comment.