Skip to content

Commit b4b5029

Browse files
committed
Add OpReadBlockIndexed and OpWriteBlockIndexed
1 parent b3d3595 commit b4b5029

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package gay.`object`.hexdebug.casting.actions
2+
3+
import at.petrak.hexcasting.api.casting.castables.ConstMediaAction
4+
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
5+
import at.petrak.hexcasting.api.casting.getBlockPos
6+
import at.petrak.hexcasting.api.casting.getInt
7+
import at.petrak.hexcasting.api.casting.iota.Iota
8+
import at.petrak.hexcasting.api.casting.iota.IotaType
9+
import at.petrak.hexcasting.api.casting.mishaps.MishapBadBlock
10+
import at.petrak.hexcasting.api.utils.downcast
11+
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes
12+
import gay.`object`.hexdebug.utils.findDataHolder
13+
import net.minecraft.nbt.CompoundTag
14+
import net.minecraft.nbt.ListTag
15+
16+
// temporary implementation of FallingColors/HexMod#412 since there's no 1.20 gloop
17+
object OpReadBlockIndexed : ConstMediaAction {
18+
override val argc = 2
19+
20+
override fun execute(
21+
args: List<Iota>,
22+
env: CastingEnvironment,
23+
): List<Iota> {
24+
val pos = args.getBlockPos(0, argc)
25+
val index = args.getInt(1, argc)
26+
27+
env.assertPosInRange(pos)
28+
29+
val datumHolder = findDataHolder(env.world, pos)
30+
?: throw MishapBadBlock.of(pos, "iota.read")
31+
32+
val tag = datumHolder.readIotaTag()
33+
?: throw MishapBadBlock.of(pos, "iota.read")
34+
35+
if (tag.getString(HexIotaTypes.KEY_TYPE) != "hexcasting:list")
36+
throw MishapBadBlock.of(pos, "iota.read")
37+
38+
val item = try {
39+
tag.get(HexIotaTypes.KEY_DATA)
40+
?.downcast(ListTag.TYPE)
41+
?.getOrNull(index)
42+
?.downcast(CompoundTag.TYPE)
43+
?: throw MishapBadBlock.of(pos, "iota.read")
44+
} catch (e: IllegalArgumentException) {
45+
throw MishapBadBlock.of(pos, "iota.read")
46+
}
47+
48+
val datum = IotaType.deserialize(item, env.world)
49+
return listOf(datum)
50+
}
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package gay.`object`.hexdebug.casting.actions
2+
3+
import at.petrak.hexcasting.api.addldata.ADIotaHolder
4+
import at.petrak.hexcasting.api.casting.ParticleSpray
5+
import at.petrak.hexcasting.api.casting.RenderedSpell
6+
import at.petrak.hexcasting.api.casting.castables.SpellAction
7+
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
8+
import at.petrak.hexcasting.api.casting.getBlockPos
9+
import at.petrak.hexcasting.api.casting.getInt
10+
import at.petrak.hexcasting.api.casting.iota.Iota
11+
import at.petrak.hexcasting.api.casting.iota.ListIota
12+
import at.petrak.hexcasting.api.casting.mishaps.MishapBadBlock
13+
import at.petrak.hexcasting.api.casting.mishaps.MishapOthersName
14+
import gay.`object`.hexdebug.utils.findDataHolder
15+
import net.minecraft.world.phys.Vec3
16+
17+
// temporary implementation of FallingColors/HexMod#412 since there's no 1.20 gloop
18+
object OpWriteBlockIndexed : SpellAction {
19+
override val argc = 3
20+
21+
override fun execute(
22+
args: List<Iota>,
23+
env: CastingEnvironment,
24+
): SpellAction.Result {
25+
val pos = args.getBlockPos(0, argc)
26+
val index = args.getInt(1, argc)
27+
val datum = args[2]
28+
29+
env.assertPosInRange(pos)
30+
31+
val datumHolder = findDataHolder(env.world, pos)
32+
?: throw MishapBadBlock.of(pos, "iota.write")
33+
34+
val list = (datumHolder.readIota(env.world) as? ListIota)
35+
?.list
36+
?.toMutableList()
37+
?: throw MishapBadBlock.of(pos, "iota.read")
38+
39+
if (index > list.lastIndex)
40+
throw MishapBadBlock.of(pos, "iota.write")
41+
42+
list[index] = datum
43+
44+
val listIota = ListIota(list)
45+
if (!datumHolder.writeIota(listIota, true))
46+
throw MishapBadBlock.of(pos, "iota.write")
47+
48+
val trueName = MishapOthersName.getTrueNameFromDatum(listIota, null)
49+
if (trueName != null)
50+
throw MishapOthersName(trueName)
51+
52+
return SpellAction.Result(
53+
Spell(listIota, datumHolder),
54+
0,
55+
listOf(ParticleSpray(pos.center, Vec3(1.0, 0.0, 0.0), 0.25, 3.14, 40))
56+
)
57+
}
58+
59+
private data class Spell(val datum: Iota, val datumHolder: ADIotaHolder) : RenderedSpell {
60+
override fun cast(env: CastingEnvironment) {
61+
datumHolder.writeIota(datum, false)
62+
}
63+
}
64+
}

Common/src/main/kotlin/gay/object/hexdebug/registry/HexDebugActions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ object HexDebugActions : HexDebugRegistrar<ActionRegistryEntry>(HexRegistries.AC
2626
// temporary implementation of FallingColors/HexMod#412 since there's no 1.20 gloop
2727
val BLOCK_READ = make("block_read", HexDir.EAST, "aqqqqqeawqwaw", OpReadBlock)
2828
val BLOCK_WRITE = make("block_write", HexDir.EAST, "deeeeeqdwewewewdw", OpWriteBlock)
29+
val BLOCK_READ_INDEXED = make("block_read_indexed", HexDir.EAST, "aqqqqqedwewewewdw", OpReadBlockIndexed)
30+
val BLOCK_WRITE_INDEXED = make("block_write_indexed", HexDir.EAST, "deeeeeqawqwaw", OpWriteBlockIndexed)
2931

3032
private fun make(name: String, startDir: HexDir, signature: String, action: Action) =
3133
make(name, startDir, signature) { action }

0 commit comments

Comments
 (0)