fix: Fix position of inventory buttons during preset importing

This commit is contained in:
Linnea Gräf
2024-11-17 18:33:29 +01:00
parent e00c21b2ad
commit 6045077025
3 changed files with 198 additions and 166 deletions

View File

@@ -29,7 +29,6 @@ public abstract class ReferenceCustomModelsPatch {
private void addFirmamentReferencedModels(
BlockStatesLoader.BlockStateDefinition definition, CallbackInfo ci
) {
inputs.keySet().stream().filter(it->it.toString().contains("firm")).forEach(System.out::println);
BakeExtraModelsEvent.Companion.publish(new BakeExtraModelsEvent(
(modelIdentifier, identifier) -> addTopLevelModel(modelIdentifier, new ItemModel(identifier))));

View File

@@ -1,5 +1,3 @@
package moe.nea.firmament.features.inventory.buttons
import io.github.notenoughupdates.moulconfig.common.IItemStack
@@ -18,6 +16,7 @@ import moe.nea.firmament.util.ClipboardUtils
import moe.nea.firmament.util.FragmentGuiScreen
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.MoulConfigUtils
import moe.nea.firmament.util.tr
class InventoryButtonEditor(
val lastGuiRect: Rectangle,
@@ -63,7 +62,7 @@ class InventoryButtonEditor(
val t = ClipboardUtils.getTextContents()
val newButtons = InventoryButtonTemplates.loadTemplate(t)
if (newButtons != null)
buttons = newButtons.toMutableList()
buttons = moveButtons(newButtons.map { it.copy(command = it.command?.removePrefix("/")) })
}
.position(lastGuiRect.minX + 10, lastGuiRect.minY + 35)
.width(lastGuiRect.width - 20)
@@ -79,6 +78,42 @@ class InventoryButtonEditor(
)
}
private fun moveButtons(buttons: List<InventoryButton>): MutableList<InventoryButton> {
val newButtons: MutableList<InventoryButton> = ArrayList(buttons.size)
val movedButtons = mutableListOf<InventoryButton>()
for (button in buttons) {
if ((!button.anchorBottom && !button.anchorRight && button.x > 0 && button.y > 0)) {
MC.sendChat(tr("firmament.inventory-buttons.button-moved",
"One of your imported buttons intersects with the inventory and has been moved to the top left."))
movedButtons.add(button.copy(
x = 0,
y = -InventoryButton.dimensions.width,
anchorRight = false,
anchorBottom = false
))
} else {
newButtons.add(button)
}
}
var i = 0
val zeroRect = Rectangle(0, 0, 1, 1)
for (movedButton in movedButtons) {
fun getPosition(button: InventoryButton, index: Int) =
button.copy(x = (index % 10) * InventoryButton.dimensions.width,
y = (index / 10) * -InventoryButton.dimensions.height,
anchorRight = false, anchorBottom = false)
while (true) {
val newPos = getPosition(movedButton, i++)
val newBounds = newPos.getBounds(zeroRect)
if (newButtons.none { it.getBounds(zeroRect).intersects(newBounds) }) {
newButtons.add(newPos)
break
}
}
}
return newButtons
}
override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {
super.render(context, mouseX, mouseY, delta)
context.matrices.push()

View File

@@ -1,11 +1,9 @@
package moe.nea.firmament.features.inventory.buttons
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import net.minecraft.text.Text
import moe.nea.firmament.Firmament
import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.TemplateUtil
@@ -17,13 +15,13 @@ object InventoryButtonTemplates {
fun loadTemplate(t: String): List<InventoryButton>? {
val buttons = TemplateUtil.maybeDecodeTemplate<List<String>>(legacyPrefix, t) ?: return null
return buttons.mapNotNull {
try {
ErrorUtil.catch<InventoryButton?>("Could not import button") {
Firmament.json.decodeFromString<InventoryButton>(it).also {
if (it.icon?.startsWith("extra:") == true || it.command?.any { it.isLowerCase() } == true) {
if (it.icon?.startsWith("extra:") == true) {
MC.sendChat(Text.translatable("firmament.inventory-buttons.import-failed"))
}
}
} catch (e: Exception) {
}.or {
null
}
}