Skip to content

Commit fbb0de4

Browse files
committed
Fix recursive list iota rendering (close #56)
1 parent cd17d1e commit fbb0de4

File tree

7 files changed

+81
-26
lines changed

7 files changed

+81
-26
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package gay.object.hexdebug.api.client.splicing;
22

3+
import at.petrak.hexcasting.api.casting.iota.IotaType;
34
import com.google.common.collect.Maps;
45
import com.google.gson.JsonObject;
56
import gay.object.hexdebug.HexDebug;
@@ -51,6 +52,29 @@ public static SplicingTableIotaRendererProvider parseProvider(@NotNull JsonObjec
5152
return SplicingTableIotasResourceReloadListener.parseProvider(jsonObject);
5253
}
5354

55+
/**
56+
* Get a previously loaded provider for a given iota type.
57+
* <br>
58+
* Returns the fallback provider if the type is {@code null} or no provider was found for that
59+
* type, or {@code null} if the fallback provider has not yet been loaded.
60+
*/
61+
@Nullable
62+
public static SplicingTableIotaRendererProvider getProvider(@Nullable IotaType<?> iotaType) {
63+
return getProvider(iotaType, true);
64+
}
65+
66+
/**
67+
* Get a previously loaded provider for a given iota type.
68+
* <br>
69+
* Returns the fallback provider if {@code useFallback} is {@code true} and either the type is
70+
* {@code null} or no provider was found for that type, or {@code null} if {@code useFallback}
71+
* is {@code false} or the fallback provider has not yet been loaded.
72+
*/
73+
@Nullable
74+
public static SplicingTableIotaRendererProvider getProvider(@Nullable IotaType<?> iotaType, boolean useFallback) {
75+
return SplicingTableIotasResourceReloadListener.getProvider(iotaType, useFallback);
76+
}
77+
5478
@ApiStatus.Internal
5579
@Nullable
5680
public static SplicingTableIotaRendererParser<?> getParser(@NotNull ResourceLocation parserId) {

Common/src/main/java/gay/object/hexdebug/api/splicing/SplicingTableIotaClientView.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@ public record SplicingTableIotaClientView(
3131
@NotNull Component display,
3232
@NotNull String hexpatternSource,
3333
int index,
34-
int depth
34+
int depth,
35+
boolean isSubIota
3536
) {
3637
@ApiStatus.Internal
3738
public SplicingTableIotaClientView {}
3839

40+
@ApiStatus.Internal
41+
public SplicingTableIotaClientView(
42+
@NotNull CompoundTag tag,
43+
@NotNull Component display,
44+
@NotNull String hexpatternSource,
45+
int index,
46+
int depth
47+
) {
48+
this(tag, display, hexpatternSource, index, depth, false);
49+
}
50+
3951
@ApiStatus.Internal
4052
public SplicingTableIotaClientView(
4153
@NotNull Iota iota,
@@ -56,4 +68,17 @@ public SplicingTableIotaClientView(
5668
public Tag getData() {
5769
return tag.get(HexIotaTypes.KEY_DATA);
5870
}
71+
72+
@ApiStatus.Internal
73+
@NotNull
74+
public static SplicingTableIotaClientView subIota(@NotNull CompoundTag tag) {
75+
return new SplicingTableIotaClientView(
76+
tag,
77+
IotaType.getDisplay(tag),
78+
"",
79+
0,
80+
0,
81+
true
82+
);
83+
}
5984
}

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/SubIota.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import at.petrak.hexcasting.api.casting.iota.IotaType
33
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
44
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererParser
55
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRendererProvider
6+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
67
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
7-
import gay.`object`.hexdebug.resources.splicing.SplicingTableIotasResourceReloadListener
88
import gay.`object`.hexdebug.utils.getAsNbtPath
99
import gay.`object`.hexdebug.utils.getOrNull
1010
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
1111
import net.minecraft.nbt.CompoundTag
12-
import net.minecraft.network.chat.Component
1312

1413
class SubIotaRendererProvider(private val path: NbtPath) : SplicingTableIotaRendererProvider {
1514
override fun createRenderer(
@@ -21,14 +20,8 @@ class SubIotaRendererProvider(private val path: NbtPath) : SplicingTableIotaRend
2120
val data = iota.data ?: return null
2221
val subIotaTag = path.getOrNull(data)?.first() as? CompoundTag ?: return null
2322
val subIotaType = IotaType.getTypeFromTag(subIotaTag) ?: return null
24-
val provider = SplicingTableIotasResourceReloadListener.getProvider(subIotaType) ?: return null
25-
val subIota = SplicingTableIotaClientView(
26-
subIotaTag,
27-
Component.empty(),
28-
"",
29-
0,
30-
0,
31-
)
23+
val provider = SplicingTableIotaRenderers.getProvider(subIotaType) ?: return null
24+
val subIota = SplicingTableIotaClientView.subIota(subIotaTag)
3225
return provider.createRenderer(subIotaType, subIota, x, y)
3326
}
3427

Common/src/main/kotlin/gay/object/hexdebug/gui/splicing/renderers/conditional/IfPathExists.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,33 @@ import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
88
import gay.`object`.hexdebug.utils.getAsIotaRendererProvider
99
import gay.`object`.hexdebug.utils.getAsNbtPath
1010
import net.minecraft.commands.arguments.NbtPathArgument.NbtPath
11+
import net.minecraft.util.GsonHelper
1112

1213
class IfPathExistsRendererProvider(
1314
private val path: NbtPath,
1415
private val providerIf: SplicingTableIotaRendererProvider,
1516
private val providerElse: SplicingTableIotaRendererProvider,
17+
private val allowInSubIota: Boolean,
1618
) : SplicingTableIotaRendererProvider {
1719
override fun createRenderer(
1820
type: IotaType<*>,
1921
iota: SplicingTableIotaClientView,
2022
x: Int,
2123
y: Int
2224
): SplicingTableIotaRenderer? {
23-
val condition = iota.data?.let { path.countMatching(it) > 0 } == true
25+
val condition = (allowInSubIota || !iota.isSubIota)
26+
&& iota.data?.let { path.countMatching(it) > 0 } == true
2427
val provider = if (condition) providerIf else providerElse
2528
return provider.createRenderer(type, iota, x, y)
2629
}
2730

2831
companion object {
29-
val PARSER = SplicingTableIotaRendererParser<IfPathExistsRendererProvider> { _, jsonObject, _ ->
32+
val PARSER = SplicingTableIotaRendererParser<IfPathExistsRendererProvider> { _, jsonObject, parent ->
3033
IfPathExistsRendererProvider(
31-
path = jsonObject.getAsNbtPath("path"),
32-
providerIf = jsonObject.getAsIotaRendererProvider("if"),
33-
providerElse = jsonObject.getAsIotaRendererProvider("else"),
34+
path = jsonObject.getAsNbtPath("path", parent?.path),
35+
providerIf = jsonObject.getAsIotaRendererProvider("if", parent?.providerIf),
36+
providerElse = jsonObject.getAsIotaRendererProvider("else", parent?.providerElse),
37+
allowInSubIota = GsonHelper.getAsBoolean(jsonObject, "allowInSubIota", parent?.allowInSubIota ?: false)
3438
)
3539
}
3640
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
55
import com.mojang.blaze3d.systems.RenderSystem
66
import gay.`object`.hexdebug.HexDebug
77
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderer
8+
import gay.`object`.hexdebug.api.client.splicing.SplicingTableIotaRenderers
89
import gay.`object`.hexdebug.api.splicing.SplicingTableIotaClientView
9-
import gay.`object`.hexdebug.resources.splicing.SplicingTableIotasResourceReloadListener
1010
import net.minecraft.client.gui.GuiGraphics
1111
import net.minecraft.network.chat.Component
1212

@@ -60,11 +60,8 @@ abstract class BaseIotaButton(x: Int, y: Int) : HexagonButton(
6060
active = true
6161

6262
try {
63-
renderer = iotaType
64-
?.let { SplicingTableIotasResourceReloadListener.getProvider(it) }
63+
renderer = SplicingTableIotaRenderers.getProvider(iotaType)
6564
?.createRenderer(iotaType, iotaView, x, y)
66-
?: SplicingTableIotasResourceReloadListener.fallback
67-
?.createRenderer(iotaType, iotaView, x, y)
6865

6966
tooltip = renderer?.createTooltip()
7067
} catch (e: Exception) {

Common/src/main/kotlin/gay/object/hexdebug/resources/splicing/SplicingTableIotasResourceReloadListener.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ private val GSON = GsonBuilder()
3030
object SplicingTableIotasResourceReloadListener :
3131
SimpleJsonResourceReloadListener(GSON, "hexdebug_splicing_iotas")
3232
{
33-
var fallback: RendererProvider? = null
34-
private set
35-
33+
private var fallback: RendererProvider? = null
3634
private val providersByType = mutableMapOf<IotaType<*>, RendererProvider>()
3735

3836
private val objects = mutableMapOf<ResourceLocation, JsonObject>()
@@ -41,7 +39,9 @@ object SplicingTableIotasResourceReloadListener :
4139
private val visitingProviders = linkedSetOf<ResourceLocation>() // use a linked set so the error message is ordered
4240
private val failedProviders = mutableSetOf<ResourceLocation>()
4341

44-
fun getProvider(iotaType: IotaType<*>): RendererProvider? = providersByType[iotaType]
42+
@JvmStatic
43+
fun getProvider(iotaType: IotaType<*>?, useFallback: Boolean = true): RendererProvider? =
44+
providersByType[iotaType] ?: fallback.takeIf { useFallback }
4545

4646
@JvmStatic
4747
fun loadProvider(providerId: ResourceLocation): RendererProvider {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,19 @@ fun JsonObject.getAsNbtPath(memberName: String, fallback: NbtPath? = null): NbtP
3939
}
4040
}
4141

42-
fun JsonObject.getAsIotaRendererProvider(memberName: String): SplicingTableIotaRendererProvider {
42+
fun JsonObject.getAsIotaRendererProviderOrNull(
43+
memberName: String,
44+
fallback: SplicingTableIotaRendererProvider? = null
45+
): SplicingTableIotaRendererProvider? {
46+
if (memberName !in this) return fallback
47+
return getAsIotaRendererProvider(memberName)
48+
}
49+
50+
fun JsonObject.getAsIotaRendererProvider(
51+
memberName: String,
52+
fallback: SplicingTableIotaRendererProvider? = null
53+
): SplicingTableIotaRendererProvider {
54+
if (memberName !in this && fallback != null) return fallback
4355
return when {
4456
GsonHelper.isStringValue(this, memberName) -> {
4557
SplicingTableIotaRenderers.loadProvider(getAsResourceLocation(memberName))

0 commit comments

Comments
 (0)