Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jun 20, 2024
1 parent 0a8eeba commit 387d99e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/me/imtoggle/itemcounter/command/ModCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 1 addition & 16 deletions src/main/kotlin/me/imtoggle/itemcounter/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>) {
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<ItemEntry>()

Expand Down Expand Up @@ -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))
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/me/imtoggle/itemcounter/element/ItemElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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<ItemStack>
get() = (0..26).map { index -> getStackInSlot(index) }

private fun draw(x: Float, y: Float, scale: Float, example: Boolean) {
val itemAmountMap: Map<ItemElement, Int> = shownItems.associateWith { getItemAmount(it) }
val iconSize = 16f
Expand Down
45 changes: 27 additions & 18 deletions src/main/kotlin/me/imtoggle/itemcounter/util/ItemRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}

}
25 changes: 21 additions & 4 deletions src/main/kotlin/me/imtoggle/itemcounter/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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<Int>): 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
}

0 comments on commit 387d99e

Please sign in to comment.