Skip to content

Commit

Permalink
screen finish - no to porting
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Jul 10, 2024
1 parent 61d1777 commit 96ae6c0
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/dev/nyon/autodrop/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ data class TriggerConfig(
*
* @param enabled defines whether the archive is enabled.
* @param name is the identifier of the archive.
* @param entries define the entries of the archive. Those are represented by [dev.nyon.autodrop.config.ItemIdentificator].
* @param entries define the entries of the archive. Those are represented by [dev.nyon.autodrop.config.ItemIdentifier].
* @param ignoredSlots define the slots that are ignored by the mod and thus don't get cleared automatically.
*/
@Serializable
data class Archive(
var enabled: Boolean = true,
val name: String,
var entries: MutableList<ItemIdentificator>,
var entries: MutableList<ItemIdentifier>,
var ignoredSlots: MutableList<Int>
)

Expand All @@ -63,6 +63,6 @@ data class Archive(
* @param amount is the amount of the item that is required for the item to be dropped.
*/
@Serializable
data class ItemIdentificator(
data class ItemIdentifier(
var type: @Contextual Item?, var components: @Contextual DataComponentPatch, var amount: Int
)
12 changes: 5 additions & 7 deletions src/main/kotlin/dev/nyon/autodrop/config/ConfigHandler.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.nyon.autodrop.config

import dev.nyon.autodrop.extensions.resourceLocation
import kotlinx.serialization.json.*
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.resources.ResourceLocation

var config: Config = Config()
var currentItems = mutableSetOf<ItemIdentificator>()
var currentItems = mutableSetOf<ItemIdentifier>()
var ignoredSlots = mutableSetOf<Int>()

fun reloadArchiveProperties() {
Expand All @@ -32,11 +32,9 @@ internal fun migrate(
true,
archiveObject["name"]?.jsonPrimitive?.content ?: return null,
archiveObject["items"]?.jsonArray?.map secMap@{ content ->
val resourceLocation = /*? if >=1.21 {*/ ResourceLocation.parse(content.jsonPrimitive.contentOrNull ?: return null) /*?} else {*//* ResourceLocation(content.jsonPrimitive.contentOrNull ?: return null) *//*?}*/
return@secMap ItemIdentificator(
BuiltInRegistries.ITEM.get(resourceLocation),
DataComponentPatch.EMPTY,
1
val resourceLocation = resourceLocation(content.jsonPrimitive.contentOrNull ?: return null)
return@secMap ItemIdentifier(
BuiltInRegistries.ITEM.get(resourceLocation), DataComponentPatch.EMPTY, 1
)
}?.toMutableList() ?: return null,
archiveObject["lockedSlots"]?.jsonArray?.map secMap@{ content ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.nyon.autodrop.config.screen

import dev.isxander.yacl3.dsl.*
import dev.nyon.autodrop.config.config
import dev.nyon.autodrop.config.screen.root.ArchiveScreen
import dev.nyon.autodrop.minecraft
import dev.nyon.konfig.config.saveConfig
import net.minecraft.client.gui.screens.Screen
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.nyon.autodrop.config.screen.create

import dev.nyon.autodrop.config.Archive
import dev.nyon.autodrop.config.config
import dev.nyon.autodrop.extensions.screenComponent
import dev.nyon.konfig.config.saveConfig
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Button
import net.minecraft.client.gui.components.EditBox
import net.minecraft.client.gui.screens.Screen
import dev.nyon.autodrop.minecraft as internalMinecraft

class CreateArchiveScreen(private val parent: Screen?, private val onClose: (Archive) -> Unit) :
Screen(screenComponent("create.title")) {
private val matcher: (String) -> Boolean = { input ->
input.isNotBlank() && config.archives.none { archive -> archive.name == input }
}

private val archiveNameEditBox =
EditBox(internalMinecraft.font, 0, 0, 20, 20, screenComponent("create.empty")).also {
addWidget(it)
it.onClick(10.0, 10.0)
it.setFilter(matcher)
it.cursorPosition = 0
it.setHighlightPos(0)
}

private val doneButton = Button.builder(screenComponent("done")) {
onClose()
}.build().also { addWidget(it) }

override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) {
renderBackground(guiGraphics, mouseX, mouseY, tickDelta)

// render description
guiGraphics.drawCenteredString(
internalMinecraft.font,
screenComponent("create.description"),
internalMinecraft.screen!!.width / 2,
internalMinecraft.screen!!.height / 6,
0xFFFFFF
)

// render edit box
archiveNameEditBox.setPosition(internalMinecraft.screen!!.width / 3, internalMinecraft.screen!!.height / 4)
archiveNameEditBox.width = internalMinecraft.screen!!.width / 3
archiveNameEditBox.render(guiGraphics, mouseX, mouseY, tickDelta)

// render done button
doneButton.setPosition(internalMinecraft.screen!!.width / 3, (internalMinecraft.screen!!.height * .8).toInt())
doneButton.width = internalMinecraft.screen!!.width / 3
doneButton.render(guiGraphics, mouseX, mouseY, tickDelta)
doneButton.active = matcher(archiveNameEditBox.value)
}

override fun shouldCloseOnEsc(): Boolean {
return matcher(archiveNameEditBox.value)
}

override fun onClose() {
val archive = Archive(
true, archiveNameEditBox.value, mutableListOf(), mutableListOf()
)
config.archives.add(archive)
onClose(archive)

internalMinecraft.setScreen(parent)
saveConfig(config)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dev.nyon.autodrop.config.screen.ignored

import com.mojang.blaze3d.systems.RenderSystem
import dev.nyon.autodrop.config.Archive
import dev.nyon.autodrop.config.config
import dev.nyon.autodrop.extensions.resourceLocation
import dev.nyon.autodrop.extensions.screenComponent
import dev.nyon.konfig.config.saveConfig
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Button
import net.minecraft.client.gui.components.EditBox
import net.minecraft.client.gui.screens.Screen
import net.minecraft.client.renderer.GameRenderer
import dev.nyon.autodrop.minecraft as internalMinecraft

private const val IMAGE_SIZE = 254

class IgnoredSlotsScreen(private val archive: Archive, private val parent: Screen?) :
Screen(screenComponent("ignored.title")) {
private val matcher: (String) -> Boolean = { input ->
val list = input.split(',').toMutableList().also { list ->
list.removeIf { s -> s.isEmpty() }
}
list.all { s ->
s.toIntOrNull() in 0 .. 99
}
}

private val ignoredSlotsEditBox =
EditBox(internalMinecraft.font, 0, 0, 20, 20, screenComponent("ignored.empty")).also {
addWidget(it)
it.value = archive.ignoredSlots.joinToString(separator = ",") { input -> input.toString() }
it.setMaxLength(136)
it.setFilter(matcher)
it.onClick(10.0, 10.0)
it.cursorPosition = 0
it.setHighlightPos(0)
}

private val doneButton = Button.builder(screenComponent("done")) {
onClose()
}.build().also { addWidget(it) }

override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) {
renderBackground(guiGraphics, mouseX, mouseY, tickDelta)

// render description
guiGraphics.drawCenteredString(
internalMinecraft.font,
screenComponent("ignored.description"),
internalMinecraft.screen!!.width / 2,
internalMinecraft.screen!!.height / 6,
0xFFFFFF
)

// render edit box
ignoredSlotsEditBox.setPosition(internalMinecraft.screen!!.width / 3, internalMinecraft.screen!!.height / 4)
ignoredSlotsEditBox.width = internalMinecraft.screen!!.width / 3
ignoredSlotsEditBox.render(guiGraphics, mouseX, mouseY, tickDelta)

// render image
val imageLocation = resourceLocation("autodrop:image/inventory-slots.png")
RenderSystem.setShader(GameRenderer::getPositionTexShader)
RenderSystem.setShaderTexture(0, imageLocation)
RenderSystem.enableBlend()
RenderSystem.defaultBlendFunc()
RenderSystem.enableDepthTest()
guiGraphics.blit(
imageLocation,
internalMinecraft.screen!!.width / 2 - IMAGE_SIZE / 2,
(internalMinecraft.screen!!.height * .55).toInt() - IMAGE_SIZE / 2,
0,
0,
IMAGE_SIZE,
IMAGE_SIZE
)
RenderSystem.disableBlend()
RenderSystem.disableDepthTest()

// render done button
doneButton.setPosition(internalMinecraft.screen!!.width / 3, (internalMinecraft.screen!!.height * .8).toInt())
doneButton.width = internalMinecraft.screen!!.width / 3
doneButton.render(guiGraphics, mouseX, mouseY, tickDelta)
doneButton.active = matcher(ignoredSlotsEditBox.value)
}

override fun shouldCloseOnEsc(): Boolean {
return matcher(ignoredSlotsEditBox.value)
}

override fun onClose() {
internalMinecraft.setScreen(parent)
archive.ignoredSlots = ignoredSlotsEditBox.value.split(',').mapNotNull { it.toIntOrNull() }.toMutableList()
saveConfig(config)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package dev.nyon.autodrop.config.screen.modify

import dev.nyon.autodrop.config.ItemIdentifier
import dev.nyon.autodrop.config.config
import dev.nyon.autodrop.config.screen.root.ArchiveScreen
import dev.nyon.autodrop.extensions.DataComponentPatchSerializer
import dev.nyon.autodrop.extensions.resourceLocation
import dev.nyon.autodrop.extensions.screenComponent
import dev.nyon.konfig.config.saveConfig
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.components.Button
import net.minecraft.client.gui.components.EditBox
import net.minecraft.client.gui.screens.Screen
import net.minecraft.core.registries.BuiltInRegistries
import kotlin.time.Duration.Companion.milliseconds
import dev.nyon.autodrop.minecraft as internalMinecraft

class ModifyIdentifierScreen(private val parent: ArchiveScreen, private val itemIdentifier: ItemIdentifier) :
Screen(screenComponent("modify.title")) {
private val matcher: () -> Boolean = {
(itemEditBox.value.isBlank() || BuiltInRegistries.ITEM.getOptional(resourceLocation(itemEditBox.value)).isPresent) && kotlin.runCatching {
DataComponentPatchSerializer.toPatch(
componentsEditBox.value
)
}.isSuccess && amountEditBox.value.toIntOrNull().let { it != null && it in 0 .. 64 }
}

private val lastIndex: Instant = Clock.System.now()
private val itemEditBox: EditBox =
EditBox(internalMinecraft.font, 0, 0, 20, 20, screenComponent("modify.empty")).also {
addWidget(it)
it.onClick(10.0, 10.0)
it.setResponder { input ->
val now = Clock.System.now()
if (now - lastIndex > 500.milliseconds) {
itemListWidget.input = input
itemListWidget.refreshEntries()
}
}
it.value = itemIdentifier.type?.let { item -> BuiltInRegistries.ITEM.getKey(item).toString() } ?: ""
it.cursorPosition = 0
it.setHighlightPos(0)
}

private val componentsEditBox: EditBox =
EditBox(internalMinecraft.font, 0, 0, 20, 20, screenComponent("modify.empty")).also {
addWidget(it)
it.onClick(10.0, 10.0)
it.setMaxLength(300)
it.value = DataComponentPatchSerializer.toString(itemIdentifier.components)
it.cursorPosition = 0
it.setHighlightPos(0)
}

private val amountEditBox: EditBox =
EditBox(internalMinecraft.font, 0, 0, 20, 20, screenComponent("modify.empty")).also {
addWidget(it)
it.onClick(10.0, 10.0)
it.setMaxLength(2)
it.value = itemIdentifier.amount.toString()
it.setFilter { input ->
val int = input.toIntOrNull() ?: return@setFilter false
int in 0 .. 64
}
it.cursorPosition = 0
it.setHighlightPos(0)
}

private val itemListWidget: ModifyItemsWidget = ModifyItemsWidget(itemEditBox.value) item@{
itemIdentifier.type = this@item
itemEditBox.value = BuiltInRegistries.ITEM.getKey(this@item).toString()
}.also {
addWidget(it)
it.refreshEntries()
}

private val doneButton = Button.builder(screenComponent("done")) {
onClose()
}.build().also { addWidget(it) }

override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) {
renderBackground(guiGraphics, mouseX, mouseY, tickDelta)

// render description
guiGraphics.drawCenteredString(
internalMinecraft.font,
screenComponent("modify.item.description"),
internalMinecraft.screen!!.width / 2,
internalMinecraft.screen!!.height / 10,
0xFFFFFF
)

// render item edit box
itemEditBox.setPosition(internalMinecraft.screen!!.width / 3, internalMinecraft.screen!!.height / 8)
itemEditBox.width = internalMinecraft.screen!!.width / 3
itemEditBox.render(guiGraphics, mouseX, mouseY, tickDelta)

// render item list if edit box is selected
itemListWidget.render(guiGraphics, mouseX, mouseY, tickDelta)

// render components text and edit box
guiGraphics.drawCenteredString(
internalMinecraft.font,
screenComponent("modify.components.description"),
internalMinecraft.screen!!.width / 2,
(internalMinecraft.screen!!.height * .4).toInt(),
0xFFFFFF
)

componentsEditBox.setPosition(
internalMinecraft.screen!!.width / 3, (internalMinecraft.screen!!.height * .45).toInt()
)
componentsEditBox.width = internalMinecraft.screen!!.width / 3
componentsEditBox.render(guiGraphics, mouseX, mouseY, tickDelta)

// render amount text and edit box
guiGraphics.drawCenteredString(
internalMinecraft.font,
screenComponent("modify.amount.description"),
internalMinecraft.screen!!.width / 2,
(internalMinecraft.screen!!.height * .575).toInt(),
0xFFFFFF
)

amountEditBox.setPosition(
internalMinecraft.screen!!.width / 3, (internalMinecraft.screen!!.height * .625).toInt()
)
amountEditBox.width = internalMinecraft.screen!!.width / 3
amountEditBox.render(guiGraphics, mouseX, mouseY, tickDelta)

// render done button
doneButton.setPosition(internalMinecraft.screen!!.width / 3, (internalMinecraft.screen!!.height * .8).toInt())
doneButton.width = internalMinecraft.screen!!.width / 3
doneButton.render(guiGraphics, mouseX, mouseY, tickDelta)
doneButton.active = matcher()
}

override fun shouldCloseOnEsc(): Boolean {
return matcher()
}

override fun onClose() {
itemIdentifier.components = DataComponentPatchSerializer.toPatch(componentsEditBox.value)
itemIdentifier.amount = amountEditBox.value.toInt()
internalMinecraft.setScreen(parent)
saveConfig(config)
parent.archiveItemsWidget.refreshEntries()
}
}
Loading

0 comments on commit 96ae6c0

Please sign in to comment.