2021-02-28 22:56:25 +00:00
|
|
|
package net.shadowfacts.phycon.block.inserter
|
|
|
|
|
|
|
|
import com.mojang.blaze3d.systems.RenderSystem
|
|
|
|
import net.minecraft.client.gui.screen.ingame.HandledScreen
|
|
|
|
import net.minecraft.client.gui.widget.TextFieldWidget
|
|
|
|
import net.minecraft.client.util.math.MatrixStack
|
|
|
|
import net.minecraft.entity.player.PlayerInventory
|
|
|
|
import net.minecraft.screen.slot.Slot
|
|
|
|
import net.minecraft.screen.slot.SlotActionType
|
|
|
|
import net.minecraft.text.LiteralText
|
|
|
|
import net.minecraft.text.Text
|
|
|
|
import net.minecraft.util.Identifier
|
|
|
|
import net.shadowfacts.phycon.PhysicalConnectivity
|
2021-03-03 03:32:30 +00:00
|
|
|
import net.shadowfacts.phycon.networking.C2SConfigureDevice
|
2021-02-28 22:56:25 +00:00
|
|
|
import java.lang.NumberFormatException
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
class InserterScreen(
|
|
|
|
handler: InserterScreenHandler,
|
|
|
|
playerInv: PlayerInventory,
|
|
|
|
title: Text,
|
|
|
|
): HandledScreen<InserterScreenHandler>(
|
|
|
|
handler,
|
|
|
|
playerInv,
|
|
|
|
title
|
|
|
|
) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val BACKGROUND = Identifier(PhysicalConnectivity.MODID, "textures/gui/inserter.png")
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var amountField: TextFieldWidget
|
|
|
|
|
|
|
|
init {
|
|
|
|
backgroundWidth = 176
|
|
|
|
backgroundHeight = 133
|
|
|
|
playerInventoryTitleY = backgroundHeight - 94
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
amountField = TextFieldWidget(textRenderer, x + 57, y + 24, 80, 9, LiteralText("Amount"))
|
|
|
|
amountField.text = handler.inserter.amountToExtract.toString()
|
|
|
|
amountField.setHasBorder(false)
|
|
|
|
amountField.isVisible = true
|
|
|
|
amountField.setSelected(true)
|
|
|
|
amountField.setEditableColor(0xffffff)
|
|
|
|
amountField.setTextPredicate {
|
|
|
|
if (it.isEmpty()) {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
val value = Integer.parseInt(it)
|
|
|
|
value in 1..64
|
|
|
|
} catch (e: NumberFormatException) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addChild(amountField)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun amountUpdated() {
|
|
|
|
if (amountField.text.isNotEmpty()) {
|
|
|
|
handler.inserter.amountToExtract = Integer.parseInt(amountField.text)
|
2021-03-03 03:32:30 +00:00
|
|
|
client!!.player!!.networkHandler.sendPacket(C2SConfigureDevice(handler.inserter))
|
2021-02-28 22:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun tick() {
|
|
|
|
super.tick()
|
|
|
|
amountField.tick()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun drawBackground(matrixStack: MatrixStack, delta: Float, mouseX: Int, mouseY: Int) {
|
2021-03-01 03:21:08 +00:00
|
|
|
renderBackground(matrixStack)
|
|
|
|
|
2021-02-28 22:56:25 +00:00
|
|
|
RenderSystem.color4f(1f, 1f, 1f, 1f)
|
|
|
|
client!!.textureManager.bindTexture(BACKGROUND)
|
|
|
|
val x = (width - backgroundWidth) / 2
|
|
|
|
val y = (height - backgroundHeight) / 2
|
|
|
|
drawTexture(matrixStack, x, y, 0, 0, backgroundWidth, backgroundHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun render(matrixStack: MatrixStack, mouseX: Int, mouseY: Int, delta: Float) {
|
|
|
|
super.render(matrixStack, mouseX, mouseY, delta)
|
|
|
|
|
|
|
|
amountField.render(matrixStack, mouseX, mouseY, delta)
|
|
|
|
|
|
|
|
drawMouseoverTooltip(matrixStack, mouseX, mouseY)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onMouseClick(slot: Slot?, invSlot: Int, clickData: Int, slotActionType: SlotActionType?) {
|
|
|
|
super.onMouseClick(slot, invSlot, clickData, slotActionType)
|
|
|
|
|
|
|
|
amountField.setSelected(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun charTyped(c: Char, i: Int): Boolean {
|
|
|
|
val oldText = amountField.text
|
|
|
|
if (amountField.charTyped(c, i)) {
|
|
|
|
if (oldText != amountField.text) {
|
|
|
|
amountUpdated()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return super.charTyped(c, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun keyPressed(i: Int, j: Int, k: Int): Boolean {
|
|
|
|
val oldText = amountField.text
|
|
|
|
if (amountField.keyPressed(i, j, k)) {
|
|
|
|
if (oldText != amountField.text) {
|
|
|
|
amountUpdated()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return super.keyPressed(i, j, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|