Skip to content

Commit

Permalink
done!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jun 3, 2024
1 parent 410fea6 commit 3435665
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.imtoggle.quickswitch.mixin;

import cc.polyfrost.oneconfig.config.elements.OptionPage;
import cc.polyfrost.oneconfig.gui.pages.ModConfigPage;
import cc.polyfrost.oneconfig.utils.InputHandler;
import me.imtoggle.quickswitch.Selector;
import me.imtoggle.quickswitch.config.ModConfig;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(value = ModConfigPage.class, remap = false)
public class ModConfigPageMixin {

@Shadow @Final private OptionPage page;

@ModifyVariable(method = "draw", at = @At("STORE"), ordinal = 2)
private int optionY(int value) {
if (this.page.mod == ModConfig.INSTANCE.mod) {
return value + 48;
}
return value;
}

@Inject(method = "drawStatic", at = @At("HEAD"), cancellable = true)
private void drawSelector(long vg, int x, int y, InputHandler inputHandler, CallbackInfoReturnable<Integer> cir) {
if (this.page.mod == ModConfig.INSTANCE.mod) {
Selector.INSTANCE.draw(vg, x + 32, y + 16, inputHandler);
cir.setReturnValue(64);
}
}
}
40 changes: 16 additions & 24 deletions src/main/kotlin/me/imtoggle/quickswitch/ItemTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package me.imtoggle.quickswitch
import net.minecraft.init.*
import net.minecraft.item.*

enum class ItemTypes(val displayName: String, val type: Any, val renderItem: Item) {
Sword("Sword", ItemSword::class.java, Items.diamond_sword),
Block("Block", ItemBlock::class.java, Item.getItemFromBlock(Blocks.wool)),
FishingRod("Fishing Rod", ItemFishingRod::class.java, Items.fishing_rod),
Bow("Bow", ItemBow::class.java, Items.bow),
Shears("Shears", ItemShears::class.java, Items.shears),
Axe("Axe", ItemAxe::class.java, Items.diamond_axe),
Pickaxe("Pickaxe", ItemPickaxe::class.java, Items.diamond_pickaxe),
Shovel("Shovel", ItemSpade::class.java, Items.diamond_shovel),
Hoe("Hoe", ItemHoe::class.java, Items.diamond_hoe),
Egg("Egg", ItemEgg::class.java, Items.egg),
Snowball("Snowball", ItemSnowball::class.java, Items.snowball),
EnderPearl("Ender Pearl", ItemEnderPearl::class.java, Items.ender_pearl),
Bucket("Empty Bucket", Items.bucket, Items.bucket),
WaterBucket("Water Bucket", Items.water_bucket, Items.water_bucket),
LavaBucket("Lava Bucket", Items.lava_bucket, Items.lava_bucket);
enum class ItemTypes(val type: Any, val renderItem: Item) {
Sword(ItemSword::class.java, Items.diamond_sword),
Block(ItemBlock::class.java, Item.getItemFromBlock(Blocks.wool)),
FishingRod(ItemFishingRod::class.java, Items.fishing_rod),
Bow(ItemBow::class.java, Items.bow),
Shears(ItemShears::class.java, Items.shears),
Axe(ItemAxe::class.java, Items.diamond_axe),
Pickaxe(ItemPickaxe::class.java, Items.diamond_pickaxe),
Shovel(ItemSpade::class.java, Items.diamond_shovel),
Hoe(ItemHoe::class.java, Items.diamond_hoe),
Egg(ItemEgg::class.java, Items.egg),
Snowball(ItemSnowball::class.java, Items.snowball),
EnderPearl(ItemEnderPearl::class.java, Items.ender_pearl),
Bucket(Items.bucket, Items.bucket),
WaterBucket(Items.water_bucket, Items.water_bucket),
LavaBucket(Items.lava_bucket, Items.lava_bucket);

companion object {
fun getRenderItem(index: Int): Item {
Expand All @@ -30,14 +30,6 @@ enum class ItemTypes(val displayName: String, val type: Any, val renderItem: Ite
return Item.getItemFromBlock(Blocks.air)
}

fun getType(name: String): Any {
for (i in entries) {
if (i.displayName == name) {
return i.type
}
}
return ItemBlock::class.java
}
}

}
52 changes: 0 additions & 52 deletions src/main/kotlin/me/imtoggle/quickswitch/KeyBindsRenderer.kt

This file was deleted.

93 changes: 93 additions & 0 deletions src/main/kotlin/me/imtoggle/quickswitch/MainRenderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.imtoggle.quickswitch

import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.RawMouseEvent
import cc.polyfrost.oneconfig.gui.elements.BasicButton
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.renderer.asset.SVG
import cc.polyfrost.oneconfig.utils.InputHandler
import cc.polyfrost.oneconfig.utils.color.ColorPalette
import me.imtoggle.quickswitch.config.KeyBindEntry
import me.imtoggle.quickswitch.config.ModConfig
import me.imtoggle.quickswitch.element.ItemTypeElement
import me.imtoggle.quickswitch.element.KeyBindElement

@Suppress("UnstableAPIUsage")
object MainRenderer : BasicOption(null, null, "", "", "", "", 2) {
private val PLUS = SVG("/assets/plus.svg")
private val addButton = BasicButton(32, 32, PLUS, BasicButton.ALIGNMENT_CENTER, ColorPalette.PRIMARY)

var keyElements = ArrayList<KeyBindElement>()

var removeQueue = ArrayList<KeyBindElement>()

var dragging: ItemTypeElement? = null
var currentHeight = 0
var target = -1
var xOffset = 0f
var yOffset = 0f
var checkChange = false

init {
addButton.setClickAction {
val entry = KeyBindEntry()
ModConfig.entries.add(entry)
entry.onAdd()
}
EventManager.INSTANCE.register(this)
}

@Subscribe
private fun onMouse(event: RawMouseEvent) {
if (event.button == 0 && event.state == 0) {
checkChange = true
}
}

override fun getHeight(): Int = currentHeight

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
var renderY = y
for (element in keyElements) {
element.draw(vg, x.toFloat(), renderY.toFloat(), inputHandler)
renderY += 96
}
if (inputHandler.isMouseDown) {
tryDraw(dragging, vg, inputHandler.mouseX() - xOffset, inputHandler.mouseY() - yOffset, inputHandler)
}
if (checkChange) {
checkChange = false
dragging?.let {
for (element in keyElements) {
if (element.hovered) {
element.addItem(it.id, target, false)
}
}
dragging = null
}
inputHandler.stopBlockingInput()
}
for (element in removeQueue) {
keyElements.remove(element)
element.entry.onRemove()
}
removeQueue.clear()
addButton.draw(vg, x.toFloat(), renderY.toFloat(), inputHandler)

currentHeight = renderY - y + 32
}

fun tryDraw(element: ItemTypeElement?, vg: Long, x: Float, y: Float, inputHandler: InputHandler) {
element?.let {
inputHandler.blockAllInput()
it.draw(vg, inputHandler.mouseX() - xOffset, inputHandler.mouseY() - yOffset, inputHandler)
}
}

override fun keyTyped(key: Char, keyCode: Int) {
for (element in keyElements) {
element.keyBindButton.isKeyTyped(keyCode)
}
}
}
16 changes: 15 additions & 1 deletion src/main/kotlin/me/imtoggle/quickswitch/QuickSwitch.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package me.imtoggle.quickswitch

import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.Stage
import cc.polyfrost.oneconfig.events.event.TickEvent
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.utils.dsl.mc
import me.imtoggle.quickswitch.config.ModConfig
import net.minecraft.item.Item
Expand All @@ -15,6 +19,16 @@ object QuickSwitch {
@Mod.EventHandler
fun onInit(event: FMLInitializationEvent) {
ModConfig
EventManager.INSTANCE.register(this)
}

var switchQueue = -1

@Subscribe
fun onTickEnd(e: TickEvent) {
if (e.stage != Stage.END || switchQueue == -1) return
mc.thePlayer.inventory.currentItem = switchQueue
switchQueue = -1
}

fun switchTo(type: Any): Boolean {
Expand All @@ -27,7 +41,7 @@ object QuickSwitch {
itemStack.stackSize
}?.index
index?.let {
mc.thePlayer.inventory.currentItem = it
switchQueue = it
return true
}
return false
Expand Down
40 changes: 3 additions & 37 deletions src/main/kotlin/me/imtoggle/quickswitch/Selector.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
package me.imtoggle.quickswitch

import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.events.EventManager
import cc.polyfrost.oneconfig.events.event.RawMouseEvent
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe
import cc.polyfrost.oneconfig.utils.InputHandler
import me.imtoggle.quickswitch.element.ItemTypeElement

object Selector : BasicOption(null, null, "", "", "", "", 2) {
object Selector {

var elements: Array<ItemTypeElement> = Array(ItemTypes.entries.size) { ItemTypeElement(it, true) }
var elements: Array<ItemTypeElement> = Array(ItemTypes.entries.size) { ItemTypeElement(it, 0) }

var adding: ItemTypeElement? = null

var xOffset = 0f
var yOffset = 0f
var stop = false

init {
EventManager.INSTANCE.register(this)
}

@Subscribe
private fun onMouse(event: RawMouseEvent) {
if (event.button == 0 && event.state == 0) {
adding = null
stop = true
}
}

override fun getHeight(): Int = 48

override fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
fun draw(vg: Long, x: Int, y: Int, inputHandler: InputHandler) {
var renderX = x
for (element in elements) {
element.draw(vg, renderX.toFloat(), y.toFloat(), inputHandler)
renderX += 48
}
if (inputHandler.isMouseDown) {
adding?.let {
inputHandler.blockAllInput()
it.draw(vg, inputHandler.mouseX() - xOffset, inputHandler.mouseY() - yOffset, inputHandler)
}
}
if (stop) {
inputHandler.stopBlockingInput()
stop = false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cc.polyfrost.oneconfig.config.annotations.Exclude
import cc.polyfrost.oneconfig.config.core.OneKeyBind
import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import me.imtoggle.quickswitch.ItemTypes
import me.imtoggle.quickswitch.KeyBindsRenderer
import me.imtoggle.quickswitch.MainRenderer
import me.imtoggle.quickswitch.QuickSwitch
import me.imtoggle.quickswitch.element.KeyBindElement
import java.lang.reflect.Field
Expand All @@ -29,7 +29,9 @@ class KeyBindEntry {
fun onAdd() {
keyBind.setRunnable { run() }
ModConfig.addKeyBind(map, keyBind)
KeyBindsRenderer.keyElements.add(KeyBindElement(this))
val element = KeyBindElement(this)
itemTypes.forEach { element.addItem(it, -1, true) }
MainRenderer.keyElements.add(element)
}

fun onRemove() {
Expand Down
17 changes: 4 additions & 13 deletions src/main/kotlin/me/imtoggle/quickswitch/config/ModConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import cc.polyfrost.oneconfig.config.data.*
import cc.polyfrost.oneconfig.config.elements.BasicOption
import cc.polyfrost.oneconfig.config.elements.OptionPage
import cc.polyfrost.oneconfig.internal.config.core.KeyBindHandler
import me.imtoggle.quickswitch.KeyBindsRenderer
import me.imtoggle.quickswitch.MainRenderer
import me.imtoggle.quickswitch.QuickSwitch
import me.imtoggle.quickswitch.Selector
import me.imtoggle.quickswitch.mixin.KeyBindHandlerAccessor
import java.lang.reflect.Field

object ModConfig : Config(Mod(QuickSwitch.NAME, ModType.UTIL_QOL), "${QuickSwitch.MODID}.json") {

@CustomOption(id = "selector")
var selector = Runnable { }

@CustomOption(id = "renderer")
@CustomOption
var entries = ArrayList<KeyBindEntry>()

fun addKeyBind(map: MutableMap.MutableEntry<Field?, Any?>, keyBind: OneKeyBind) {
Expand All @@ -36,13 +32,8 @@ object ModConfig : Config(Mod(QuickSwitch.NAME, ModType.UTIL_QOL), "${QuickSwitc
page: OptionPage,
mod: Mod,
migrate: Boolean
): BasicOption? {
var option: BasicOption? = null
when (annotation.id) {
"renderer" -> option = KeyBindsRenderer
"selector" -> option = Selector
}

): BasicOption {
val option = MainRenderer
ConfigUtils.getSubCategory(page, "General", "").options.add(option)
return option
}
Expand Down
Loading

0 comments on commit 3435665

Please sign in to comment.