add block settings.

Signed-off-by: scorbett123 <50634068+scorbett123@users.noreply.github.com>
This commit is contained in:
scorbett123 2021-03-01 11:27:36 +00:00
parent 380b37358d
commit d7d5263825
11 changed files with 149 additions and 46 deletions

View File

@ -1,33 +0,0 @@
package org.kamiblue.client.command.commands
import net.minecraft.block.BlockAir
import org.kamiblue.client.command.ClientCommand
import org.kamiblue.client.module.modules.player.InventoryManager
import org.kamiblue.client.util.items.block
import org.kamiblue.client.util.items.id
import org.kamiblue.client.util.text.MessageSendHelper
// TODO: Remove once GUI has Block settings
object SetBuildingBlockCommand : ClientCommand(
name = "setbuildingblock",
description = "Set the default building block"
) {
init {
executeSafe {
val heldItem = player.inventory.getCurrentItem()
when {
heldItem.isEmpty -> {
InventoryManager.buildingBlockID = 0
MessageSendHelper.sendChatMessage("Building block has been reset")
}
heldItem.item.block !is BlockAir -> {
InventoryManager.buildingBlockID = heldItem.item.id
MessageSendHelper.sendChatMessage("Building block has been set to ${heldItem.displayName}")
}
else -> {
MessageSendHelper.sendChatMessage("You are not holding a valid block")
}
}
}
}
}

View File

@ -7,6 +7,7 @@ import net.minecraft.util.ResourceLocation
import net.minecraftforge.fml.common.gameevent.TickEvent
import org.kamiblue.client.event.events.RenderOverlayEvent
import org.kamiblue.client.gui.rgui.WindowComponent
import org.kamiblue.client.gui.rgui.windows.BlockPicker
import org.kamiblue.client.gui.rgui.windows.ColorPicker
import org.kamiblue.client.gui.rgui.windows.SettingWindow
import org.kamiblue.client.mixin.extension.listShaders
@ -86,6 +87,8 @@ abstract class AbstractKamiGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
init {
mc = Wrapper.minecraft
windowList.add(ColorPicker)
windowList.add(BlockPicker)
BlockPicker.visible = false
safeListener<TickEvent.ClientTickEvent> { event ->
if (event.phase != TickEvent.Phase.START) return@safeListener
@ -220,7 +223,7 @@ abstract class AbstractKamiGui<S : SettingWindow<*>, E : Any> : GuiScreen() {
private fun updateSettingWindow() {
settingWindow?.let {
if (lastClickedWindow != it && lastClickedWindow != ColorPicker) {
if (lastClickedWindow != it && lastClickedWindow != ColorPicker && lastClickedWindow != BlockPicker) {
it.onClosed()
windowList.remove(it)
settingWindow = null

View File

@ -25,7 +25,7 @@ open class WindowComponent(
// Interactive info
open val draggableHeight get() = height
var lastActiveTime: Long = System.currentTimeMillis(); protected set
open var lastActiveTime: Long = System.currentTimeMillis(); protected set
var preDragMousePos = Vec2f.ZERO; private set
var preDragPos = Vec2f.ZERO; private set
var preDragSize = Vec2f.ZERO; private set

View File

@ -0,0 +1,23 @@
package org.kamiblue.client.gui.rgui.component
import net.minecraft.block.Block
import org.kamiblue.client.gui.rgui.windows.BlockPicker
import org.kamiblue.client.util.graphics.RenderUtils2D
import org.kamiblue.client.util.graphics.VertexHelper
import org.kamiblue.client.util.items.item
import org.kamiblue.client.util.math.Vec2f
class BlockButton(val block: Block, isCurrent: Boolean) : Slider(block.localizedName, if (isCurrent) 1.0 else 0.0, "", { true }, 40f, 40f) {
override fun onClick(mousePos: Vec2f, buttonId: Int) {
super.onClick(mousePos, buttonId)
BlockPicker.notifyAboutClick(block)
}
override fun onRender(vertexHelper: VertexHelper, absolutePos: Vec2f) {
super.onRender(vertexHelper, absolutePos)
RenderUtils2D.drawItem(block.item.defaultInstance, (width - 20).toInt(), height.toInt())
}
}

View File

@ -0,0 +1,32 @@
package org.kamiblue.client.gui.rgui.component
import org.kamiblue.client.gui.rgui.windows.BlockPicker
import org.kamiblue.client.module.modules.client.GuiColors
import org.kamiblue.client.setting.settings.impl.other.BlockSetting
import org.kamiblue.client.util.graphics.VertexHelper
import org.kamiblue.client.util.graphics.font.FontRenderAdapter
import org.kamiblue.client.util.math.Vec2f
class BlockSettingButton(val setting: BlockSetting) : Slider(setting.name, 0.0, setting.description, setting.visibility) {
override fun onRender(vertexHelper: VertexHelper, absolutePos: Vec2f) {
val valueText = setting.value.localizedName
protectedWidth = FontRenderAdapter.getStringWidth(valueText, 0.75f).toDouble()
super.onRender(vertexHelper, absolutePos)
val posX = (renderWidth - protectedWidth - 2.0f).toFloat()
val posY = renderHeight - 2.0f - FontRenderAdapter.getFontHeight(0.75f)
FontRenderAdapter.drawString(valueText, posX, posY, color = GuiColors.text, scale = 0.75f)
}
override fun onRelease(mousePos: Vec2f, buttonId: Int) {
super.onRelease(mousePos, buttonId)
displayBlockPicker()
}
private fun displayBlockPicker() {
BlockPicker.visible = true
BlockPicker.setting = setting
BlockPicker.onDisplayed()
}
}

View File

@ -20,8 +20,10 @@ open class Slider(
name: String,
valueIn: Double,
private val description: String = "",
private val visibility: (() -> Boolean)?
) : InteractiveComponent(name, 0.0f, 0.0f, 40.0f, 10.0f, SettingGroup.NONE) {
private val visibility: (() -> Boolean)?,
width: Float = 40.0f,
height: Float = 10.0f
) : InteractiveComponent(name, 0.0f, 0.0f, width, height, SettingGroup.NONE) {
protected var value = valueIn
set(value) {
if (value != field) {

View File

@ -0,0 +1,32 @@
package org.kamiblue.client.gui.rgui.windows
import net.minecraft.block.Block
import net.minecraft.util.ResourceLocation
import org.kamiblue.client.gui.rgui.component.BlockButton
import org.kamiblue.client.setting.settings.impl.other.BlockSetting
object BlockPicker : ListWindow("Block Picker", 0.0f, 0.0f, 200.0f, 200.0f, SettingGroup.NONE) {
var setting: BlockSetting? = null
override var lastActiveTime: Long = Long.MAX_VALUE // Dodgy thing to create always on top
set(value) {
field = Long.MAX_VALUE
}
val blocks = Block.REGISTRY.keys.asSequence().map { Block.REGISTRY.getObject(ResourceLocation(it.path)) }.distinctBy { it.localizedName }.filter { !it.localizedName.startsWith("tile") }.sortedBy { it.localizedName.toLowerCase() }.toList()
override fun onDisplayed() {
super.onDisplayed()
for (block in blocks) {
this.children.add(BlockButton(block, block == setting?.value))
}
}
fun notifyAboutClick(block: Block) {
setting?.value = block
setting = null
visible = false
}
}

View File

@ -4,6 +4,7 @@ import org.kamiblue.client.gui.rgui.component.*
import org.kamiblue.client.setting.settings.AbstractSetting
import org.kamiblue.client.setting.settings.impl.number.NumberSetting
import org.kamiblue.client.setting.settings.impl.other.BindSetting
import org.kamiblue.client.setting.settings.impl.other.BlockSetting
import org.kamiblue.client.setting.settings.impl.other.ColorSetting
import org.kamiblue.client.setting.settings.impl.primitive.BooleanSetting
import org.kamiblue.client.setting.settings.impl.primitive.EnumSetting
@ -40,6 +41,7 @@ abstract class SettingWindow<T : Any>(
is ColorSetting -> Button(setting.name, { displayColorPicker(setting) }, setting.description, setting.visibility)
is StringSetting -> StringButton(setting)
is BindSetting -> BindButton(setting)
is BlockSetting -> BlockSettingButton(setting)
else -> null
}?.also {
children.add(it)
@ -80,6 +82,7 @@ abstract class SettingWindow<T : Any>(
super.onClosed()
listeningChild = null
ColorPicker.visible = false
BlockPicker.visible = false
}
override fun onKeyInput(keyCode: Int, keyState: Boolean) {

View File

@ -1,6 +1,7 @@
package org.kamiblue.client.module.modules.player
import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.init.Blocks
import net.minecraft.inventory.Slot
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.gameevent.TickEvent
@ -34,7 +35,7 @@ internal object InventoryManager : Module(
private val autoRefill by setting("Auto Refill", true)
private val buildingMode by setting("Building Mode", false, { autoRefill })
var buildingBlockID by setting("Building Block ID", 0, 0..1000, 1, { false })
private val buildingBlock by setting("Building Block", Blocks.AIR)
private val refillThreshold by setting("Refill Threshold", 16, 1..63, 1, { autoRefill })
private val itemSaver by setting("Item Saver", false)
private val duraThreshold by setting("Durability Threshold", 5, 1..50, 1, { itemSaver })
@ -111,10 +112,10 @@ internal object InventoryManager : Module(
}
private fun SafeClientEvent.refillBuildingCheck(): Boolean {
if (!autoRefill || !buildingMode || buildingBlockID == 0) return false
if (!autoRefill || !buildingMode || buildingBlock == Blocks.AIR) return false
val totalCount = player.inventorySlots.countID(buildingBlockID)
val hotbarCount = player.hotbarSlots.countID(buildingBlockID)
val totalCount = player.inventorySlots.countID(buildingBlock.item.id)
val hotbarCount = player.hotbarSlots.countID(buildingBlock.item.id)
return totalCount >= refillThreshold
&& (hotbarCount < refillThreshold
@ -154,7 +155,7 @@ internal object InventoryManager : Module(
}
private fun SafeClientEvent.refillBuilding() {
player.storageSlots.firstID(buildingBlockID)?.let {
player.storageSlots.firstID(buildingBlock.item.id)?.let {
quickMoveSlot(it)
}
}
@ -188,9 +189,9 @@ internal object InventoryManager : Module(
&& itemStack.itemDamage > itemStack.maxDamage * (1.0f - duraThreshold / 100.0f)
private fun SafeClientEvent.getRefillableSlotBuilding(): Slot? {
if (player.storageSlots.firstID(buildingBlockID) == null) return null
if (player.storageSlots.firstID(buildingBlock.item.id) == null) return null
return player.hotbarSlots.firstID(buildingBlockID) {
return player.hotbarSlots.firstID(buildingBlock.item.id) {
it.isStackable && it.count < it.maxStackSize
}
}
@ -198,7 +199,7 @@ internal object InventoryManager : Module(
private fun SafeClientEvent.getRefillableSlot(): Slot? {
return player.hotbarSlots.firstByStack {
!it.isEmpty
&& (!buildingMode || it.item.id != buildingBlockID)
&& (!buildingMode || it.item.id != buildingBlock.item.id)
&& (!autoEject || !ejectList.contains(it.item.registryName.toString()))
&& it.isStackable
&& it.count < (it.maxStackSize / 64.0f * refillThreshold).ceilToInt()
@ -215,7 +216,7 @@ internal object InventoryManager : Module(
private fun SafeClientEvent.getEjectSlot(): Slot? {
return player.inventorySlots.firstByStack {
!it.isEmpty
&& (!buildingMode || it.item.id != buildingBlockID)
&& (!buildingMode || it.item.id != buildingBlock.item.id)
&& ejectList.contains(it.item.registryName.toString())
}
}

View File

@ -1,9 +1,11 @@
package org.kamiblue.client.setting.settings
import net.minecraft.block.Block
import org.kamiblue.client.setting.settings.impl.number.DoubleSetting
import org.kamiblue.client.setting.settings.impl.number.FloatSetting
import org.kamiblue.client.setting.settings.impl.number.IntegerSetting
import org.kamiblue.client.setting.settings.impl.other.BindSetting
import org.kamiblue.client.setting.settings.impl.other.BlockSetting
import org.kamiblue.client.setting.settings.impl.other.ColorSetting
import org.kamiblue.client.setting.settings.impl.primitive.BooleanSetting
import org.kamiblue.client.setting.settings.impl.primitive.EnumSetting
@ -97,6 +99,15 @@ interface SettingRegister<T : Any> {
consumer: (prev: String, input: String) -> String = { _, input -> input },
description: String = ""
) = setting(StringSetting(name, value, visibility, consumer, description))
/** Block Setting */
fun T.setting(
name: String,
value: Block,
visibility: () -> Boolean = { true },
consumer: (prev: Block, input: Block) -> Block = { _, input -> input },
description: String = ""
) = setting(BlockSetting(name, value, visibility, consumer, description))
/* End of setting registering */
/**

View File

@ -0,0 +1,29 @@
package org.kamiblue.client.setting.settings.impl.other
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import net.minecraft.block.Block
import org.kamiblue.client.gui.rgui.windows.BlockPicker
import org.kamiblue.client.setting.settings.MutableSetting
class BlockSetting(
name: String,
value: Block,
visibility: () -> Boolean = { true },
consumer: (prev: Block, input: Block) -> Block = { _, input -> input },
description: String = ""
) : MutableSetting<Block>(name, value, visibility, consumer, description) {
override fun write() = JsonPrimitive(value.translationKey)
override fun read(jsonElement: JsonElement?) {
jsonElement?.asJsonPrimitive?.asString.let {
for (block in BlockPicker.blocks) {
if (block.translationKey == it) {
value = block
break
}
}
}
}
}