move enchanting logic from rendering to container

This commit is contained in:
Bixilon 2023-01-13 12:19:23 +01:00
parent b7a053b83d
commit 1f6cd17fce
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 34 additions and 9 deletions

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.container.types
import de.bixilon.minosoft.data.abilities.Gamemodes
import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.data.container.InventorySynchronizedContainer
import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction
@ -32,6 +33,7 @@ import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.registries.item.MinecraftItems
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerButtonC2SP
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class EnchantingContainer(connection: PlayConnection, type: ContainerType, title: ChatComponent?) : InventorySynchronizedContainer(connection, type, title, RangeSection(ENCHANTING_SLOTS, PlayerInventory.MAIN_SLOTS)) {
@ -42,6 +44,8 @@ class EnchantingContainer(connection: PlayConnection, type: ContainerType, title
var seed = -1
private set
val lapislazuli: Int get() = this[LAPISLAZULI_SLOT]?.item?._count ?: 0
override fun getSlotType(slotId: Int): SlotType? {
return when (slotId) {
0 -> EnchantableSlotType
@ -67,6 +71,33 @@ class EnchantingContainer(connection: PlayConnection, type: ContainerType, title
}
}
fun canEnchant(index: Int): Boolean {
if (costs[index] < 0) {
return false
}
if (connection.player.gamemode == Gamemodes.CREATIVE) {
return true
}
val lapislazuli = this.lapislazuli
if (lapislazuli < index + 1) {
return false
}
return true
}
fun selectEnchantment(index: Int) {
if (index < 0 || index > 2) {
throw IllegalArgumentException("Index out of bounds: $index")
}
if (!canEnchant(index)) {
throw IllegalStateException("Can not enchant $index!")
}
val id = this.id ?: return
connection.network.send(ContainerButtonC2SP(id, index))
}
private object LapislazuliSlot : SlotType {
override fun canPut(container: Container, slot: Int, stack: ItemStack): Boolean {
return stack.item.item.identifier == MinecraftItems.LAPISLAZULI

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* 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.
*
@ -15,7 +15,6 @@ package de.bixilon.minosoft.gui.rendering.gui.gui.screen.container.enchanting
import de.bixilon.kotlinglm.vec2.Vec2i
import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.minosoft.data.abilities.Gamemodes
import de.bixilon.minosoft.data.container.types.EnchantingContainer
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.elements.Element
@ -66,14 +65,10 @@ class EnchantingContainerScreen(guiRenderer: GUIRenderer, container: EnchantingC
override fun forceSilentApply() {
super.forceSilentApply()
var lapisCount = container[EnchantingContainer.LAPISLAZULI_SLOT]?.item?._count ?: 0
if (guiRenderer.connection.player.gamemode == Gamemodes.CREATIVE) {
lapisCount = 64
}
for (index in 0 until EnchantingContainer.ENCHANTING_OPTIONS) {
val card = cards[index]
card.update(lapisCount < index + 1, container.costs[index], container.enchantments[index], container.enchantmentLevels[index])
card.update(!container.canEnchant(index), container.costs[index], container.enchantments[index], container.enchantmentLevels[index])
}
}

View File

@ -30,7 +30,6 @@ import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.AtlasImageElemen
import de.bixilon.minosoft.gui.rendering.gui.elements.text.TextElement
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerButtonC2SP
class EnchantmentButtonElement(
guiRenderer: GUIRenderer,
@ -63,7 +62,7 @@ class EnchantmentButtonElement(
}
override fun submit() {
container.container.id?.let { guiRenderer.connection.network.send(ContainerButtonC2SP(it, index)) }
container.container.selectEnchantment(index)
}
fun update(disabled: Boolean, cost: Int, enchantment: Enchantment?, level: Int) {