From a85ec4c68eabfee5332c8a3f599a2620b746d38f Mon Sep 17 00:00:00 2001 From: Bixilon Date: Thu, 7 Oct 2021 23:28:22 +0200 Subject: [PATCH] fix some bugs, inventory enhancements --- .../minosoft/data/entities/entities/Entity.kt | 2 +- .../minosoft/data/inventory/InventorySlots.kt | 3 +++ .../minosoft/data/player/LocalPlayerEntity.kt | 4 +++ .../minosoft/data/registries/items/Item.kt | 2 ++ .../items/armor/DyeableArmorItem.kt | 25 ++++++++++++++++++ .../registries/other/containers/Container.kt | 4 +++ .../other/containers/PlayerInventory.kt | 26 +++++++++++++++++++ .../gui/hud/elements/hotbar/HotbarElement.kt | 2 +- .../hotbar/HotbarExperienceBarElement.kt | 4 +++ .../hotbar/HotbarProtectionElement.kt | 11 +++++--- 10 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/items/armor/DyeableArmorItem.kt diff --git a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt index 467dd623e..c3ad2f9c9 100644 --- a/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt +++ b/src/main/java/de/bixilon/minosoft/data/entities/entities/Entity.kt @@ -70,7 +70,7 @@ abstract class Entity( var rotation: EntityRotation, ) : PhysicsEntity { protected val random = Random - val equipment: MutableMap = mutableMapOf() + open val equipment: MutableMap = mutableMapOf() val activeStatusEffects: MutableMap = synchronizedMapOf() val attributes: MutableMap> = synchronizedMapOf() diff --git a/src/main/java/de/bixilon/minosoft/data/inventory/InventorySlots.kt b/src/main/java/de/bixilon/minosoft/data/inventory/InventorySlots.kt index f44cb84df..e4d4377c8 100644 --- a/src/main/java/de/bixilon/minosoft/data/inventory/InventorySlots.kt +++ b/src/main/java/de/bixilon/minosoft/data/inventory/InventorySlots.kt @@ -32,6 +32,9 @@ class InventorySlots { companion object : ValuesEnum { override val VALUES = values() override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + + + val ARMOR_SLOTS = arrayOf(FEET, LEGS, CHEST, HEAD) } } } diff --git a/src/main/java/de/bixilon/minosoft/data/player/LocalPlayerEntity.kt b/src/main/java/de/bixilon/minosoft/data/player/LocalPlayerEntity.kt index 1e4fdda80..0fd664a8f 100644 --- a/src/main/java/de/bixilon/minosoft/data/player/LocalPlayerEntity.kt +++ b/src/main/java/de/bixilon/minosoft/data/player/LocalPlayerEntity.kt @@ -22,6 +22,7 @@ import de.bixilon.minosoft.data.entities.EntityRotation import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity import de.bixilon.minosoft.data.entities.entities.player.RemotePlayerEntity import de.bixilon.minosoft.data.inventory.InventorySlots +import de.bixilon.minosoft.data.inventory.ItemStack import de.bixilon.minosoft.data.physics.PhysicsConstants import de.bixilon.minosoft.data.registries.AABB import de.bixilon.minosoft.data.registries.blocks.DefaultBlocks @@ -201,6 +202,9 @@ class LocalPlayerEntity( val reachDistance: Double get() = (gamemode == Gamemodes.CREATIVE).decide(5.0, 4.5) + override val equipment: MutableMap + get() = inventory.equipment + private fun sendMovementPackets() { if (Minosoft.config.config.game.camera.disableMovementSending) { return diff --git a/src/main/java/de/bixilon/minosoft/data/registries/items/Item.kt b/src/main/java/de/bixilon/minosoft/data/registries/items/Item.kt index 906be5177..5d6da29a0 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/items/Item.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/items/Item.kt @@ -20,6 +20,7 @@ import de.bixilon.minosoft.data.registries.blocks.BlockState import de.bixilon.minosoft.data.registries.blocks.BlockUsages import de.bixilon.minosoft.data.registries.inventory.CreativeModeTab import de.bixilon.minosoft.data.registries.items.armor.ArmorItem +import de.bixilon.minosoft.data.registries.items.armor.DyeableArmorItem import de.bixilon.minosoft.data.registries.items.armor.HorseArmorItem import de.bixilon.minosoft.data.registries.items.tools.* import de.bixilon.minosoft.data.registries.registries.Registries @@ -76,6 +77,7 @@ open class Item( "ShovelItem" -> ShovelItem(resourceLocation, registries, data) "PickaxeItem" -> PickaxeItem(resourceLocation, registries, data) "HoeItem" -> HoeItem(resourceLocation, registries, data) + "DyeableArmorItem" -> DyeableArmorItem(resourceLocation, registries, data) // "Item" -> Item(resourceLocation, data) // else -> TODO("Can not find item class: ${data["class"].asString}") else -> Item(resourceLocation, registries, data) diff --git a/src/main/java/de/bixilon/minosoft/data/registries/items/armor/DyeableArmorItem.kt b/src/main/java/de/bixilon/minosoft/data/registries/items/armor/DyeableArmorItem.kt new file mode 100644 index 000000000..f8bc6c0f5 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/items/armor/DyeableArmorItem.kt @@ -0,0 +1,25 @@ +/* + * Minosoft + * Copyright (C) 2021 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. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.items.armor + +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.data.registries.registries.Registries + +open class DyeableArmorItem( + resourceLocation: ResourceLocation, + registries: Registries, + data: Map, +) : ArmorItem(resourceLocation, registries, data) { + // ToDo +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/containers/Container.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/containers/Container.kt index ffb73f0ba..1628bd4ac 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/containers/Container.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/containers/Container.kt @@ -34,4 +34,8 @@ open class Container( } } } + + operator fun get(slotId: Int): ItemStack? { + return slots[slotId] + } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/other/containers/PlayerInventory.kt b/src/main/java/de/bixilon/minosoft/data/registries/other/containers/PlayerInventory.kt index 9690e9555..3247bf7fc 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/other/containers/PlayerInventory.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/other/containers/PlayerInventory.kt @@ -13,10 +13,12 @@ package de.bixilon.minosoft.data.registries.other.containers +import de.bixilon.minosoft.data.inventory.InventorySlots import de.bixilon.minosoft.data.inventory.ItemStack import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.util.KUtil.toResourceLocation +// https://c4k3.github.io/wiki.vg/images/1/13/Inventory-slots.png class PlayerInventory(connection: PlayConnection) : Container( connection = connection, ContainerType( @@ -24,7 +26,31 @@ class PlayerInventory(connection: PlayConnection) : Container( ), ) { + val equipment: MutableMap + get() { + val equipment: MutableMap = mutableMapOf() + + for (slot in InventorySlots.EquipmentSlots.ARMOR_SLOTS) { + equipment[slot] = this[slot] ?: continue + } + + return equipment + } + fun getHotbarSlot(hotbarSlot: Int = connection.player.selectedHotbarSlot): ItemStack? { + check(hotbarSlot in 0..9) { "Hotbar slot out of bounds!" } return slots[hotbarSlot + 36] // ToDo } + + operator fun get(slot: InventorySlots.EquipmentSlots): ItemStack? { + return this[when (slot) { + InventorySlots.EquipmentSlots.HEAD -> 5 + InventorySlots.EquipmentSlots.CHEST -> 6 + InventorySlots.EquipmentSlots.LEGS -> 7 + InventorySlots.EquipmentSlots.FEET -> 8 + + InventorySlots.EquipmentSlots.MAIN_HAND -> connection.player.selectedHotbarSlot + 36 + InventorySlots.EquipmentSlots.OFF_HAND -> 45 + }] + } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt index 78a8b0cc0..1fdf3350c 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarElement.kt @@ -32,7 +32,7 @@ class HotbarElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { private val hunger = HotbarHungerElement(hudRenderer) private val protection = HotbarProtectionElement(hudRenderer) - private val topLeft = RowLayout(hudRenderer, HorizontalAlignments.LEFT, 1) // contains health, armor, etc + private val topLeft = RowLayout(hudRenderer, HorizontalAlignments.LEFT, 1) // contains health, protection, etc private val topRight = RowLayout(hudRenderer, HorizontalAlignments.RIGHT, 1) // contains hunger, air private var renderElements = setOf( diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarExperienceBarElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarExperienceBarElement.kt index 179576476..0b3fb72db 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarExperienceBarElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarExperienceBarElement.kt @@ -86,6 +86,10 @@ class HotbarExperienceBarElement(hudRenderer: HUDRenderer) : Element(hudRenderer } } + override fun onParentChange() { + silentApply() + } + companion object { private val SIZE = Vec2i(182, 5) } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt index e5a1014d3..fbe8030b4 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/hotbar/HotbarProtectionElement.kt @@ -40,8 +40,8 @@ class HotbarProtectionElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { for (i in 0 until 10) { val atlasElement = when { - protectionLeft <= 0.0f -> emptyProtection - protectionLeft < 1.0f -> halfProtection + protectionLeft < 1.0f -> emptyProtection + protectionLeft < 2.0f -> halfProtection else -> fullProtection } @@ -49,12 +49,16 @@ class HotbarProtectionElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { image.render(offset + Vec2i(i * ARMOR_SIZE.x, 0), z, consumer) - protectionLeft -= 1.0f + protectionLeft -= 2.0f } return 1 } + override fun onParentChange() { + silentApply() + } + override fun silentApply() { val protection = hudRenderer.connection.player.protectionLevel // ToDo: Check for equipment change @@ -67,6 +71,7 @@ class HotbarProtectionElement(hudRenderer: HUDRenderer) : Element(hudRenderer) { } else { SIZE } + this.protection = protection cacheUpToDate = false }