From 1f6cd17fce54f92e5a6a9dd676c81279af9116d4 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 13 Jan 2023 12:19:23 +0100 Subject: [PATCH] move enchanting logic from rendering to container --- .../container/types/EnchantingContainer.kt | 31 +++++++++++++++++++ .../enchanting/EnchantingContainerScreen.kt | 9 ++---- .../enchanting/EnchantmentButtonElement.kt | 3 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/container/types/EnchantingContainer.kt b/src/main/java/de/bixilon/minosoft/data/container/types/EnchantingContainer.kt index d33b8f816..b67d7aec9 100644 --- a/src/main/java/de/bixilon/minosoft/data/container/types/EnchantingContainer.kt +++ b/src/main/java/de/bixilon/minosoft/data/container/types/EnchantingContainer.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantingContainerScreen.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantingContainerScreen.kt index 68a6a03f4..7f92c40dc 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantingContainerScreen.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantingContainerScreen.kt @@ -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]) } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantmentButtonElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantmentButtonElement.kt index 6a5d0e20f..8910680f6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantmentButtonElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/gui/screen/container/enchanting/EnchantmentButtonElement.kt @@ -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) {