Add support for /give syntax to /firm buttons
This commit is contained in:
@@ -7,17 +7,22 @@
|
|||||||
|
|
||||||
package moe.nea.firmament.features.inventory.buttons
|
package moe.nea.firmament.features.inventory.buttons
|
||||||
|
|
||||||
|
import com.mojang.brigadier.StringReader
|
||||||
import me.shedaniel.math.Dimension
|
import me.shedaniel.math.Dimension
|
||||||
import me.shedaniel.math.Point
|
import me.shedaniel.math.Point
|
||||||
import me.shedaniel.math.Rectangle
|
import me.shedaniel.math.Rectangle
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import net.minecraft.client.gui.DrawContext
|
import net.minecraft.client.gui.DrawContext
|
||||||
|
import net.minecraft.command.CommandRegistryAccess
|
||||||
|
import net.minecraft.command.argument.ItemStackArgumentType
|
||||||
import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
|
import net.minecraft.resource.featuretoggle.FeatureFlags
|
||||||
import net.minecraft.util.Identifier
|
import net.minecraft.util.Identifier
|
||||||
import moe.nea.firmament.repo.ItemCache.asItemStack
|
import moe.nea.firmament.repo.ItemCache.asItemStack
|
||||||
import moe.nea.firmament.repo.RepoManager
|
import moe.nea.firmament.repo.RepoManager
|
||||||
import moe.nea.firmament.util.MC
|
import moe.nea.firmament.util.MC
|
||||||
import moe.nea.firmament.util.SkyblockId
|
import moe.nea.firmament.util.SkyblockId
|
||||||
|
import moe.nea.firmament.util.memoize
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class InventoryButton(
|
data class InventoryButton(
|
||||||
@@ -29,9 +34,27 @@ data class InventoryButton(
|
|||||||
var command: String? = "",
|
var command: String? = "",
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
|
val itemStackParser by lazy {
|
||||||
|
ItemStackArgumentType.itemStack(CommandRegistryAccess.of(MC.defaultRegistries,
|
||||||
|
FeatureFlags.VANILLA_FEATURES))
|
||||||
|
}
|
||||||
val dimensions = Dimension(18, 18)
|
val dimensions = Dimension(18, 18)
|
||||||
fun getItemForName(icon: String): ItemStack {
|
val getItemForName = ::getItemForName0.memoize(1024)
|
||||||
return RepoManager.getNEUItem(SkyblockId(icon)).asItemStack(idHint = SkyblockId(icon))
|
fun getItemForName0(icon: String): ItemStack {
|
||||||
|
val repoItem = RepoManager.getNEUItem(SkyblockId(icon))
|
||||||
|
var itemStack = repoItem.asItemStack(idHint = SkyblockId(icon))
|
||||||
|
if (repoItem == null) {
|
||||||
|
val giveSyntaxItem = if (icon.startsWith("/give") || icon.startsWith("give"))
|
||||||
|
icon.split(" ", limit = 3).getOrNull(2) ?: icon
|
||||||
|
else icon
|
||||||
|
val componentItem =
|
||||||
|
runCatching {
|
||||||
|
itemStackParser.parse(StringReader(giveSyntaxItem)).createStack(1, false)
|
||||||
|
}.getOrNull()
|
||||||
|
if (componentItem != null)
|
||||||
|
itemStack = componentItem
|
||||||
|
}
|
||||||
|
return itemStack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
|
|||||||
<ItemStack value="@getItemIcon"/>
|
<ItemStack value="@getItemIcon"/>
|
||||||
<Text text="Icon"/>
|
<Text text="Icon"/>
|
||||||
</Row>
|
</Row>
|
||||||
<Hover lines="Put your display item in here.;Can be any SkyBlock item id.">
|
<Hover lines="Put your display item in here.;Can be any SkyBlock item id.;;Alternatively you can paste in a /give command">
|
||||||
<TextField value="@icon" width="180"/>
|
<TextField value="@icon" width="180"/>
|
||||||
</Hover>
|
</Hover>
|
||||||
<Text text="Command"/>
|
<Text text="Command"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user