Skip to content

Commit e7fac29

Browse files
committed
Add item iota renderer for splicing table
1 parent 8b2e00c commit e7fac29

File tree

9 files changed

+172
-7
lines changed

9 files changed

+172
-7
lines changed

Common/src/main/java/gay/object/hexdebug/api/client/splicing/SplicingTableIotaRendererProvider.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.client.gui.components.Tooltip;
88
import net.minecraft.network.chat.Component;
99
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
1011

1112
/**
1213
* A factory for {@link SplicingTableIotaRenderer} instances.
@@ -15,8 +16,13 @@
1516
* currently visible, so don't do anything too laggy in here.
1617
*/
1718
public interface SplicingTableIotaRendererProvider {
18-
/** Creates and returns a new {@link SplicingTableIotaRenderer} for the provided iota. */
19-
@NotNull
19+
/**
20+
* Creates and returns a new {@link SplicingTableIotaRenderer} for the provided iota.
21+
* <br>
22+
* May return null if unable to create a renderer for the given iota; in that case, the default
23+
* renderer will be used instead.
24+
*/
25+
@Nullable
2026
SplicingTableIotaRenderer createRenderer(
2127
@NotNull IotaType<?> type,
2228
@NotNull SplicingTableIotaClientView iota,

Common/src/main/kotlin/gay/object/hexdebug/HexDebugClient.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
1111
import gay.`object`.hexdebug.blocks.focusholder.FocusHolderBlock
1212
import gay.`object`.hexdebug.config.HexDebugClientConfig
1313
import gay.`object`.hexdebug.config.HexDebugServerConfig
14+
import gay.`object`.hexdebug.gui.splicing.renderers.ItemRendererProvider
1415
import gay.`object`.hexdebug.gui.splicing.renderers.ListRendererProvider
1516
import gay.`object`.hexdebug.gui.splicing.renderers.PatternRendererProvider
1617
import gay.`object`.hexdebug.gui.splicing.renderers.TextureRendererProvider
@@ -95,8 +96,13 @@ object HexDebugClient {
9596
}
9697

9798
private fun registerSplicingTableIotaRenderers() {
98-
SplicingTableIotaRenderers.register(HexDebug.id("list"), ListRendererProvider.PARSER)
99-
SplicingTableIotaRenderers.register(HexDebug.id("pattern"), PatternRendererProvider.PARSER)
100-
SplicingTableIotaRenderers.register(HexDebug.id("texture"), TextureRendererProvider.PARSER)
99+
for ((name, parser) in arrayOf(
100+
"item" to ItemRendererProvider.PARSER,
101+
"list" to ListRendererProvider.PARSER,
102+
"pattern" to PatternRendererProvider.PARSER,
103+
"texture" to TextureRendererProvider.PARSER,
104+
)) {
105+
SplicingTableIotaRenderers.register(HexDebug.id(name), parser)
106+
}
101107
}
102108
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package gay.`object`.hexdebug.gui.splicing.renderers
2+
3+
import at.petrak.hexcasting.api.casting.iota.IotaType
4+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
5+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
7+
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
8+
import gay.`object`.hexdebug.utils.*
9+
import net.minecraft.client.Minecraft
10+
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
11+
import net.minecraft.core.registries.BuiltInRegistries
12+
import net.minecraft.nbt.CompoundTag
13+
import net.minecraft.util.GsonHelper
14+
import net.minecraft.world.item.ItemStack
15+
16+
class ItemRendererProvider(
17+
val itemPath: NbtPath,
18+
val blockPath: NbtPath?,
19+
val countPath: NbtPath?,
20+
val tagPath: NbtPath?,
21+
val xOffset: Float,
22+
val yOffset: Float,
23+
val scale: Float,
24+
) : SplicingTableIotaRendererProvider {
25+
override fun createRenderer(
26+
type: IotaType<*>,
27+
iota: SplicingTableIotaClientView,
28+
x: Int,
29+
y: Int
30+
): SplicingTableIotaRenderer? {
31+
val data = iota.data ?: return null
32+
33+
val item = itemPath.getResourceLocationOrNull(data)?.let(BuiltInRegistries.ITEM::getOrNull)
34+
?: blockPath?.getResourceLocationOrNull(data)?.let(BuiltInRegistries.BLOCK::getOrNull)
35+
?: return null
36+
37+
val count = countPath?.getIntOrNull(data) ?: 1
38+
39+
val tag = tagPath?.getOrNull(data) as? CompoundTag
40+
41+
val stack = ItemStack(item, count)
42+
stack.tag = tag
43+
44+
return SplicingTableIotaRenderer { guiGraphics, _, _, _ ->
45+
val ps = guiGraphics.pose()
46+
ps.pushPose {
47+
// align to center of iota display
48+
ps.translate(x + (18f / 2f) + xOffset, y + (21f / 2f) + yOffset, 0f)
49+
ps.scale(scale)
50+
// renderItem wants the top left corner, but it'll calculate it after the scale has been applied
51+
ps.translate(-8f, -8f, 0f)
52+
guiGraphics.renderItem(stack, 0, 0)
53+
guiGraphics.renderItemDecorations(Minecraft.getInstance().font, stack, 0, 0)
54+
}
55+
}
56+
}
57+
58+
companion object {
59+
val PARSER = SplicingTableIotaRendererParser<ItemRendererProvider> { _, json, parent ->
60+
ItemRendererProvider(
61+
itemPath = json.getAsNbtPath("itemPath", parent?.itemPath),
62+
blockPath = json.getAsNbtPathOrNull("blockPath", parent?.blockPath),
63+
countPath = json.getAsNbtPathOrNull("countPath", parent?.countPath),
64+
tagPath = json.getAsNbtPathOrNull("tagPath", parent?.tagPath),
65+
xOffset = GsonHelper.getAsFloat(json, "xOffset", 0f),
66+
yOffset = GsonHelper.getAsFloat(json, "yOffset", 0f),
67+
scale = GsonHelper.getAsFloat(json, "scale", 0.75f),
68+
)
69+
}
70+
}
71+
}

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/widgets/BaseIotaButton.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ abstract class BaseIotaButton(x: Int, y: Int) : HexagonButton(
6161
?: return
6262

6363
renderer = provider.createRenderer(iotaType, iotaView, x, y)
64+
?: SplicingTableIotasResourceReloadListener.FALLBACK?.createRenderer(iotaType, iotaView, x, y)
6465
backgroundType = provider.getBackgroundType(iotaType, iotaView)
6566
tooltip = provider.createTooltip(iotaType, iotaView)
6667
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gay.`object`.hexdebug.utils
2+
3+
import com.mojang.blaze3d.vertex.PoseStack
4+
5+
fun PoseStack.pushPose(f: () -> Unit) {
6+
pushPose()
7+
f()
8+
popPose()
9+
}
10+
11+
fun PoseStack.scale(scale: Float) {
12+
scale(scale, scale, scale) // scale
13+
}

Common/src/main/kotlin/gay/object/hexdebug/utils/Extensions.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import at.petrak.hexcasting.api.utils.*
1212
import at.petrak.hexcasting.common.casting.PatternRegistryManifest
1313
import at.petrak.hexcasting.xplat.IXplatAbstractions
1414
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
15+
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
16+
import net.minecraft.core.Registry
17+
import net.minecraft.nbt.NumericTag
18+
import net.minecraft.nbt.StringTag
19+
import net.minecraft.nbt.Tag
1520
import net.minecraft.network.chat.*
21+
import net.minecraft.resources.ResourceLocation
1622
import net.minecraft.world.Container
1723
import net.minecraft.world.InteractionHand
1824
import net.minecraft.world.entity.LivingEntity
@@ -236,3 +242,34 @@ fun <T : Comparable<T>, V : T> BlockEntity.setPropertyIfChanged(property: Proper
236242
level?.setBlockAndUpdate(blockPos, blockState.setValue(property, value))
237243
}
238244
}
245+
246+
// registries
247+
248+
fun <T> Registry<T>.getOrNull(name: ResourceLocation): T? {
249+
if (containsKey(name)) {
250+
return get(name)
251+
}
252+
return null
253+
}
254+
255+
// nbt paths
256+
257+
fun NbtPath.getOrNull(tag: Tag): List<Tag>? {
258+
return try {
259+
get(tag)
260+
} catch (e: Exception) {
261+
null
262+
}
263+
}
264+
265+
fun NbtPath.getIntOrNull(tag: Tag): Int? {
266+
return (getOrNull(tag)?.first() as? NumericTag)?.asInt
267+
}
268+
269+
fun NbtPath.getStringOrNull(tag: Tag): String? {
270+
return (getOrNull(tag)?.first() as? StringTag)?.asString
271+
}
272+
273+
fun NbtPath.getResourceLocationOrNull(tag: Tag): ResourceLocation? {
274+
return getStringOrNull(tag)?.let(ResourceLocation::tryParse)
275+
}

Common/src/main/kotlin/gay/object/hexdebug/utils/GsonHelper.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,36 @@
33
package gay.`object`.hexdebug.utils
44

55
import com.google.gson.JsonObject
6+
import com.google.gson.JsonSyntaxException
7+
import com.mojang.brigadier.StringReader
8+
import com.mojang.brigadier.exceptions.CommandSyntaxException
9+
import net.minecraft.commands.arguments.NbtPathArgument
10+
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
611
import net.minecraft.resources.ResourceLocation
712
import net.minecraft.util.GsonHelper
813

9-
fun JsonObject.getAsResourceLocation(memberName: String, fallback: ResourceLocation?): ResourceLocation {
14+
operator fun JsonObject.contains(memberName: String) = has(memberName)
15+
16+
fun JsonObject.getAsResourceLocation(memberName: String, fallback: ResourceLocation? = null): ResourceLocation {
1017
return try {
1118
ResourceLocation(GsonHelper.getAsString(this, memberName))
1219
} catch (e: Exception) {
1320
fallback ?: throw e
1421
}
1522
}
23+
24+
fun JsonObject.getAsNbtPathOrNull(memberName: String, fallback: NbtPath? = null): NbtPath? {
25+
if (memberName in this) {
26+
return getAsNbtPath(memberName)
27+
}
28+
return fallback
29+
}
30+
31+
fun JsonObject.getAsNbtPath(memberName: String, fallback: NbtPath? = null): NbtPath {
32+
val rawPath = GsonHelper.getAsString(this, memberName)
33+
return try {
34+
NbtPathArgument().parse(StringReader(rawPath))
35+
} catch (e: CommandSyntaxException) {
36+
fallback ?: throw JsonSyntaxException("Invalid $memberName, expected a valid NBT path", e)
37+
}
38+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "hexdebug:item",
3+
"itemPath": "moreiotas:stack_id",
4+
"countPath": "moreiotas:stack_count",
5+
"tagPath": "moreiotas:stack_tag"
6+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"parent": "hexdebug:builtin/type"
2+
"type": "hexdebug:item",
3+
"itemPath": "item",
4+
"blockPath": "block"
35
}

0 commit comments

Comments
 (0)