diff --git a/src/main/kotlin/me/imtoggle/itemcounter/command/ModCommand.kt b/src/main/kotlin/me/imtoggle/itemcounter/command/ModCommand.kt index 83c4a3a..2f884f1 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/command/ModCommand.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/command/ModCommand.kt @@ -5,6 +5,7 @@ import cc.polyfrost.oneconfig.utils.commands.annotations.SubCommand import cc.polyfrost.oneconfig.utils.dsl.mc import me.imtoggle.itemcounter.ItemCounter import me.imtoggle.itemcounter.config.ModConfig +import me.imtoggle.itemcounter.util.addItem import me.imtoggle.itemcounter.util.notify import net.minecraft.item.Item @@ -19,8 +20,7 @@ class ModCommand { return } val id = Item.getIdFromItem(stack.item) - ModConfig.addItem(arrayListOf(id, stack.metadata)) - ModConfig.save() + if (addItem(arrayListOf(id, stack.metadata))) ModConfig.save() } } \ No newline at end of file diff --git a/src/main/kotlin/me/imtoggle/itemcounter/config/MainRenderer.kt b/src/main/kotlin/me/imtoggle/itemcounter/config/MainRenderer.kt index 04811ca..4758c6c 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/config/MainRenderer.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/config/MainRenderer.kt @@ -43,9 +43,7 @@ object MainRenderer : BasicOption(null, null, "", "", "General", "", 2) { renderX = x.toFloat() + 64 + i % 4 * 224 renderY = y.toFloat() + i / 4 * 96 + 16 if (ItemRenderer.shouldCheck) { - ModConfig.itemInfos.add(dragging!!.itemEntry.itemInfo) - ModConfig.entries.add(index, dragging!!.itemEntry) - addQueue[index] = dragging!! + dragging!!.onAdd(index) } } if (index < elements.size) { diff --git a/src/main/kotlin/me/imtoggle/itemcounter/config/ModConfig.kt b/src/main/kotlin/me/imtoggle/itemcounter/config/ModConfig.kt index 3b5bd20..7eb60f5 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/config/ModConfig.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/config/ModConfig.kt @@ -9,27 +9,12 @@ import cc.polyfrost.oneconfig.config.elements.OptionPage import me.imtoggle.itemcounter.ItemCounter import me.imtoggle.itemcounter.element.ItemElement import me.imtoggle.itemcounter.hud.ItemCounterHud -import me.imtoggle.itemcounter.util.notify import net.minecraft.item.Item import net.minecraft.item.ItemStack import java.lang.reflect.Field object ModConfig : Config(Mod(ItemCounter.NAME, ModType.UTIL_QOL), "${ItemCounter.MODID}.json") { - fun addItem(id: ArrayList) { - val stack = ItemStack(Item.getItemById(id[0]) ?: return) - stack.itemDamage = id[1] - val info = "${id[0]} ${id[1]}" - if (itemInfos.contains(info)) { - notify("You can't add the same item twice") - return - } - val entry = ItemEntry(itemInfo = info, id = id[0]) - MainRenderer.elements.add(ItemElement(entry, stack)) - entries.add(entry) - itemInfos.add(info) - } - @CustomOption var entries = ArrayList() @@ -61,7 +46,7 @@ object ModConfig : Config(Mod(ItemCounter.NAME, ModType.UTIL_QOL), "${ItemCounte i.id = key[0] val stack = ItemStack(Item.getItemById(key[0]) ?: continue) stack.itemDamage = key[1] - itemInfos.add(i.itemInfo) + itemInfos.add(stack.displayName) MainRenderer.elements.add(ItemElement(i, stack)) } } diff --git a/src/main/kotlin/me/imtoggle/itemcounter/element/ItemElement.kt b/src/main/kotlin/me/imtoggle/itemcounter/element/ItemElement.kt index 8d9ec55..03b5b71 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/element/ItemElement.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/element/ItemElement.kt @@ -30,12 +30,13 @@ class ItemElement(val itemEntry: ItemEntry, val itemStack: ItemStack) { } fun draw(vg: Long, x: Float, y: Float, inputHandler: InputHandler) { - if (animationX is DummyAnimation) { + val doAnimation = dragging != null && dragging != this + if (animationX is DummyAnimation || !doAnimation) { animationX = EaseOutQuart(0f, 0f, x, false) } else if (x != animationX.end) { animationX = EaseOutQuart(400f, animationX.end, x, false) } - if (animationY is DummyAnimation) { + if (animationY is DummyAnimation || !doAnimation) { animationY = EaseOutQuart(0f, 0f, y, false) } else if (y != animationY.end) { animationY = EaseOutQuart(400f, animationY.end, y, false) @@ -69,8 +70,14 @@ class ItemElement(val itemEntry: ItemEntry, val itemStack: ItemStack) { fun onRemove() { ModConfig.entries.remove(itemEntry) - ModConfig.itemInfos.remove(itemEntry.itemInfo) + ModConfig.itemInfos.remove(itemStack.displayName) MainRenderer.removeQueue.add(this) } + fun onAdd(index: Int) { + ModConfig.entries.add(index, itemEntry) + ModConfig.itemInfos.add(itemStack.displayName) + MainRenderer.addQueue[index] = this + } + } \ No newline at end of file diff --git a/src/main/kotlin/me/imtoggle/itemcounter/hud/ItemCounterHud.kt b/src/main/kotlin/me/imtoggle/itemcounter/hud/ItemCounterHud.kt index 26ca60f..9a92d88 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/hud/ItemCounterHud.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/hud/ItemCounterHud.kt @@ -13,7 +13,6 @@ import me.imtoggle.itemcounter.element.ItemElement import me.imtoggle.itemcounter.util.exampleItems import net.minecraft.client.renderer.RenderHelper import net.minecraft.client.renderer.GlStateManager as GL -import net.minecraft.inventory.IInventory import net.minecraft.item.ItemStack @@ -99,15 +98,12 @@ class ItemCounterHud : BasicHud(true, 1920f - 400, 1080f - 21) { } val itemList = mc.thePlayer.inventory.mainInventory.toMutableList() return itemList.filter { - it?.item == element.itemStack.item && (element.itemEntry.ignoreMetaData || it?.metadata == element.itemStack.metadata) + it?.item == element.itemStack.item && (element.itemEntry.ignoreMetaData || ItemStack(it.item, 1, it.metadata).displayName == element.itemStack.displayName) }.sumOf { it.stackSize } } - private val IInventory.itemStackList: List - get() = (0..26).map { index -> getStackInSlot(index) } - private fun draw(x: Float, y: Float, scale: Float, example: Boolean) { val itemAmountMap: Map = shownItems.associateWith { getItemAmount(it) } val iconSize = 16f diff --git a/src/main/kotlin/me/imtoggle/itemcounter/util/ItemRenderer.kt b/src/main/kotlin/me/imtoggle/itemcounter/util/ItemRenderer.kt index f644f93..070452c 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/util/ItemRenderer.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/util/ItemRenderer.kt @@ -10,7 +10,7 @@ import cc.polyfrost.oneconfig.renderer.font.Fonts import cc.polyfrost.oneconfig.utils.InputHandler import cc.polyfrost.oneconfig.utils.dsl.* import me.imtoggle.itemcounter.config.MainRenderer.renderText -import net.minecraft.client.renderer.GlStateManager +import net.minecraft.client.renderer.GlStateManager as GL import net.minecraft.client.renderer.RenderHelper import net.minecraft.item.ItemStack import net.minecraftforge.client.event.GuiScreenEvent @@ -55,10 +55,10 @@ object ItemRenderer { val oneUIScale = OneConfigGui.getScaleFactor() * oneConfigGui.animationScaleFactor val rawX = ((UResolution.windowWidth - 800 * oneUIScale) / 2f).toInt() val rawY = ((UResolution.windowHeight - 768 * oneUIScale) / 2f).toInt() - GlStateManager.pushMatrix() + GL.pushMatrix() GL11.glEnable(GL11.GL_SCISSOR_TEST) GL11.glScissor(rawX, rawY, (1024 * oneUIScale).toInt(), (696 * oneUIScale).toInt()) - GlStateManager.scale(unscaleMC * oneUIScale, unscaleMC * oneUIScale, 1.0) + GL.scale(unscaleMC * oneUIScale, unscaleMC * oneUIScale, 1.0) for (info in renderInfos) { val (itemStack, x, y) = info ?: continue renderItem(x, y, itemStack) @@ -68,39 +68,48 @@ object ItemRenderer { val inputHandler = InputHandler() nanoVG { if (dragging != null) { + translate(inputHandler.mouseX() * (1 - oneUIScale), inputHandler.mouseY() * (1 - oneUIScale)) + scale(oneUIScale, oneUIScale) dragging!!.draw(this.instance, inputHandler.mouseX() + offsetX, inputHandler.mouseY() + offsetY, inputHandler) } } if (dragging != null) { - renderItem(dragging!!.animationX.get() + 56, dragging!!.animationY.get() + 16, dragging!!.itemStack) + GL.pushMatrix() + GL.scale(1 / oneUIScale, 1 / oneUIScale, 1f) + GL.translate(inputHandler.mouseX() * (1 - oneUIScale), inputHandler.mouseY() * (1 - oneUIScale), 0f) + GL.scale(oneUIScale, oneUIScale, 1f) + renderItem(inputHandler.mouseX() + offsetX + 56, inputHandler.mouseY() + offsetY + 16, dragging!!.itemStack) + GL.popMatrix() } nanoVG { if (renderText.isNotEmpty()) { val textWidth = getTextWidth(renderText, 14f, Fonts.MEDIUM) - drawRoundedRect(inputHandler.mouseX(), inputHandler.mouseY() - 46, textWidth + 32, 14f + 32, 10f, Colors.GRAY_800) - drawText(renderText, inputHandler.mouseX() + 16, inputHandler.mouseY() - 23, Colors.WHITE_90, 14f, Fonts.MEDIUM) + translate(inputHandler.mouseX(), inputHandler.mouseY()) + scale(oneUIScale, oneUIScale) + drawRoundedRect(0, -46, textWidth + 32, 14f + 32, 10f, Colors.GRAY_800) + drawText(renderText, 16, -23, Colors.WHITE_90, 14f, Fonts.MEDIUM) renderText = "" } } - GlStateManager.popMatrix() + GL.popMatrix() } fun renderItem(x: Float, y: Float, itemStack: ItemStack) { - GlStateManager.pushMatrix() - GlStateManager.enableRescaleNormal() - GlStateManager.enableBlend() - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0) - GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f) + GL.pushMatrix() + GL.enableRescaleNormal() + GL.enableBlend() + GL.tryBlendFuncSeparate(770, 771, 1, 0) + GL.color(1.0f, 1.0f, 1.0f, 1.0f) RenderHelper.enableGUIStandardItemLighting() val itemRenderer = mc.renderItem - GlStateManager.translate(x, y, 0f) - GlStateManager.scale(32 / 16f, 32 / 16f, 1f) + GL.translate(x, y, 0f) + GL.scale(32 / 16f, 32 / 16f, 1f) itemRenderer.renderItemAndEffectIntoGUI(itemStack, 0, 0) RenderHelper.disableStandardItemLighting() - GlStateManager.disableBlend() - GlStateManager.disableRescaleNormal() - GlStateManager.enableAlpha() - GlStateManager.popMatrix() + GL.disableBlend() + GL.disableRescaleNormal() + GL.enableAlpha() + GL.popMatrix() } } \ No newline at end of file diff --git a/src/main/kotlin/me/imtoggle/itemcounter/util/Util.kt b/src/main/kotlin/me/imtoggle/itemcounter/util/Util.kt index eabcdbe..bd24348 100644 --- a/src/main/kotlin/me/imtoggle/itemcounter/util/Util.kt +++ b/src/main/kotlin/me/imtoggle/itemcounter/util/Util.kt @@ -5,6 +5,8 @@ import cc.polyfrost.oneconfig.utils.InputHandler import cc.polyfrost.oneconfig.utils.Notifications import me.imtoggle.itemcounter.ItemCounter import me.imtoggle.itemcounter.config.ItemEntry +import me.imtoggle.itemcounter.config.MainRenderer +import me.imtoggle.itemcounter.config.ModConfig import me.imtoggle.itemcounter.element.ItemElement import net.minecraft.item.Item import net.minecraft.item.ItemStack @@ -14,10 +16,11 @@ val MINUS = SVG("/assets/minus.svg") var dragging: ItemElement? = null val exampleItems = arrayListOf( - ItemElement(ItemEntry(), ItemStack(Item.getItemById(425), 8, 1)), - ItemElement(ItemEntry(), ItemStack(Item.getItemById(420), 9)), - ItemElement(ItemEntry(), ItemStack(Item.getItemById(383), 6, 65)), - ItemElement(ItemEntry(), ItemStack(Item.getItemById(367), 4, 0)) + ItemElement(ItemEntry(), ItemStack(Item.getItemById(35), 64)), + ItemElement(ItemEntry(), ItemStack(Item.getItemById(262), 16)), + ItemElement(ItemEntry(), ItemStack(Item.getItemById(326), 1)), + ItemElement(ItemEntry(), ItemStack(Item.getItemById(327), 1)), + ItemElement(ItemEntry(), ItemStack(Item.getItemById(373), 28, 16421)) ) fun isHovered(inputHandler: InputHandler, x: Float, y: Float, width: Int, height: Int): Boolean { @@ -26,4 +29,18 @@ fun isHovered(inputHandler: InputHandler, x: Float, y: Float, width: Int, height fun notify(string: String) { Notifications.INSTANCE.send(ItemCounter.NAME, string) +} + +fun addItem(id: ArrayList): Boolean { + val stack = ItemStack(Item.getItemById(id[0]) ?: return false) + stack.itemDamage = id[1] + if (ModConfig.itemInfos.contains(stack.displayName)) { + notify("You can't add the same item twice") + return false + } + val entry = ItemEntry(itemInfo = "${id[0]} ${id[1]}", id = id[0]) + MainRenderer.elements.add(ItemElement(entry, stack)) + ModConfig.entries.add(entry) + ModConfig.itemInfos.add(stack.displayName) + return true } \ No newline at end of file