Skip to content

Commit

Permalink
Sync search box and add quilt loader
Browse files Browse the repository at this point in the history
  • Loading branch information
eth0net committed Aug 8, 2022
1 parent e69e31d commit 5122743
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ tasks {

modrinth {
token.set(System.getenv("MODRINTH_TOKEN"))
loaders.set(listOf("fabric", "quilt"))
gameVersions.set(listOf("1.19", "1.19.1", "1.19.2"))
projectId.set(base.archivesName)
versionName.set(modVersionName)
Expand Down
7 changes: 7 additions & 0 deletions changelogs/1.4.0-beta.2+fabric-1.19.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Now with permissions and optional config!

**Changed**:
- Added Quilt to list of loaders
- Sync searchbox between client and server

**Full Changelog**: https://github.com/eth0net/enchant-menu/compare/v1.4.0-beta.1+fabric-1.19...v1.4.0-beta.2+fabric-1.19
4 changes: 2 additions & 2 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.4.0-beta.1+fabric-1.19
modVersionName=1.4.0 Beta 1 Fabric 1.19
modVersion=1.4.0-beta.2+fabric-1.19
modVersionName=1.4.0 Beta 2 Fabric 1.19
modVersionType=beta
3 changes: 3 additions & 0 deletions src/main/kotlin/com/github/eth0net/enchantmenu/EnchantMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ object EnchantMenu : ModInitializer {
ServerPlayNetworking.registerGlobalReceiver(MenuChannel) { _, player, _, _, _ ->
player.openHandledScreen(EnchantMenuScreenHandlerFactory)
}
ServerPlayNetworking.registerGlobalReceiver(SearchChannel) { _, player, _, buf, _ ->
(player.currentScreenHandler as? EnchantMenuScreenHandler)?.search = buf.readString()
}
ServerPlayNetworking.registerGlobalReceiver(IncrementLevelChannel) { _, player, _, _, _ ->
(player.currentScreenHandler as? EnchantMenuScreenHandler)?.incrementLevel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play

// search box
searchBox = TextFieldWidget(textRenderer, x + 78, y + 6, 71, 9, Text.translatable("enchant-menu.title"))
searchBox?.setChangedListener { handler.search = it }
searchBox?.setChangedListener { onSearchChanged(it) }
searchBox?.text = handler.search
addSelectableChild(searchBox)
addDrawableChild(searchBox)
Expand Down Expand Up @@ -228,4 +228,9 @@ class EnchantMenuScreen(handler: EnchantMenuScreenHandler, playerInventory: Play
handler.toggleTreasure()
ClientPlayNetworking.send(ToggleTreasureChannel, PacketByteBufs.empty())
}

private fun onSearchChanged(search: String) {
handler.search = search
ClientPlayNetworking.send(SearchChannel, PacketByteBufs.create().writeString(search))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.eth0net.enchantmenu.EnchantMenu.id

internal val MenuChannel = id("toggle_menu_channel")
internal val ConfigSyncChannel = id("config_sync_channel")
internal val SearchChannel = id("search_channel")
internal val DecrementLevelChannel = id("decrement_level_channel")
internal val IncrementLevelChannel = id("increment_level_channel")
internal val ToggleIncompatibleChannel = id("toggle_incompatible_channel")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class EnchantMenuScreenHandler(
if (inventory != this.inventory) return
enchantments = listOf()
val stack = inventory.getStack(0)
if (!stack.isEmpty) enchantments = stack.acceptableEnchantments.map { stack.enchantmentEntry(it) }
if (!stack.isEmpty) enchantments = acceptableEnchantments(stack).map { stack.enchantmentEntry(it) }
sendContentUpdates()
}

Expand Down Expand Up @@ -180,15 +180,12 @@ class EnchantMenuScreenHandler(

private fun PlayerEntity.canEnchant() = !checkPermission || hasPermissionLevel(2)

private fun Enchantment.acceptableStack(stack: ItemStack): Boolean {
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 fun acceptableEnchantments(stack: ItemStack) = Registry.ENCHANTMENT.filter {
val allowed = !it.isTreasure || treasureUnlocked || stack.hasEnchantment(it)
val inSearch = it.getName(level).string.lowercase().contains(search.lowercase())
it.isAcceptableItem(stack) && allowed && inSearch
}

private val ItemStack.acceptableEnchantments get() = Registry.ENCHANTMENT.filter { it.acceptableStack(this) }

private fun ItemStack.enchantmentCompatible(enchantment: Enchantment): Boolean {
return EnchantmentHelper.fromNbt(enchantments).all { it.key.canCombine(enchantment) }
}
Expand Down

0 comments on commit 5122743

Please sign in to comment.