Skip to content

Commit

Permalink
Modularized specklikes
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Oct 4, 2024
1 parent ef76a53 commit d5b2a82
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 174 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
- fixed error in documentation: Racer's Purification can work on nonplayers
- fixed error in documentation: Telepathy Reflection does not return null ever
- made code much better
- used lambdas rather than larger switch statements
- specklikes are now modularized
- merged Arch Lamp Storage spells into the Arch Lamps page
- removed cost from Reweave Mesh
- removed limit on grimoires
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import at.petrak.hexcasting.api.spell.getVec3
import at.petrak.hexcasting.api.spell.iota.EntityIota
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.xplat.IXplatAbstractions
import miyucomics.hexical.entities.MeshEntity
import miyucomics.hexical.entities.specklikes.MeshEntity
import miyucomics.hexical.inits.HexicalAdvancements

class OpConjureMesh : ConstMediaAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import at.petrak.hexcasting.api.spell.iota.PatternIota
import at.petrak.hexcasting.api.spell.math.HexDir
import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.xplat.IXplatAbstractions
import miyucomics.hexical.entities.SpeckEntity
import miyucomics.hexical.entities.specklikes.SpeckEntity
import miyucomics.hexical.inits.HexicalAdvancements
import net.minecraft.command.argument.EntityAnchorArgumentType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.getEntity
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.api.spell.mishaps.MishapBadEntity
import miyucomics.hexical.entities.SpeckEntity
import miyucomics.hexical.entities.specklikes.SpeckEntity

class OpIotaSpeck : ConstMediaAction {
override val argc = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import at.petrak.hexcasting.api.spell.getEntity
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.api.spell.iota.ListIota
import at.petrak.hexcasting.api.spell.mishaps.MishapBadEntity
import miyucomics.hexical.entities.MeshEntity
import miyucomics.hexical.entities.specklikes.MeshEntity

class OpReadMesh : ConstMediaAction {
override val argc = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.api.spell.iota.Vec3Iota
import at.petrak.hexcasting.api.spell.mishaps.MishapBadEntity
import at.petrak.hexcasting.api.spell.mishaps.MishapInvalidIota
import miyucomics.hexical.entities.MeshEntity
import miyucomics.hexical.entities.specklikes.MeshEntity
import net.minecraft.util.math.Vec3f

class OpWeaveMesh : ConstMediaAction {
Expand Down
125 changes: 0 additions & 125 deletions src/main/java/miyucomics/hexical/entities/MeshEntity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package miyucomics.hexical.entities
package miyucomics.hexical.entities.specklikes

import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.api.spell.iota.PatternIota
import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.api.utils.putCompound
import miyucomics.hexical.inits.HexicalEntities
import miyucomics.hexical.interfaces.Specklike
import miyucomics.hexical.utils.RenderUtils
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityDimensions
import net.minecraft.entity.EntityPose
Expand All @@ -17,18 +12,11 @@ import net.minecraft.entity.data.TrackedData
import net.minecraft.entity.data.TrackedDataHandlerRegistry
import net.minecraft.nbt.NbtCompound
import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket
import net.minecraft.text.Text
import net.minecraft.util.math.Vec2f
import net.minecraft.world.World

class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(entityType, world), Specklike {
constructor(world: World) : this(HexicalEntities.SPECK_ENTITY, world)

abstract class BaseSpecklike(entityType: EntityType<out BaseSpecklike>, world: World) : Entity(entityType, world), Specklike {
private var lifespan = -1

var clientIsText = false
var clientText: Text = Text.empty()
var clientVerts: List<Vec2f> = listOf()
var clientPigment: FrozenColorizer = FrozenColorizer.DEFAULT.get()
var clientSize = 1f
var clientThickness = 1f
Expand All @@ -43,7 +31,6 @@ class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(en
}

override fun readCustomDataFromNbt(nbt: NbtCompound) {
dataTracker.set(displayDataTracker, nbt.getCompound("display"))
dataTracker.set(pigmentDataTracker, nbt.getCompound("pigment"))
dataTracker.set(rollDataTracker, nbt.getFloat("roll"))
dataTracker.set(sizeDataTracker, nbt.getFloat("size"))
Expand All @@ -52,25 +39,13 @@ class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(en
}

override fun writeCustomDataToNbt(nbt: NbtCompound) {
nbt.putCompound("display", dataTracker.get(displayDataTracker))
nbt.putCompound("pigment", dataTracker.get(pigmentDataTracker))
nbt.putFloat("roll", dataTracker.get(rollDataTracker))
nbt.putFloat("size", dataTracker.get(sizeDataTracker))
nbt.putFloat("thickness", dataTracker.get(thicknessDataTracker))
nbt.putInt("lifespan", lifespan)
}

fun setIota(iota: Iota) {
if (iota is PatternIota) {
dataTracker.set(displayDataTracker, iota.pattern.serializeToNBT())
} else {
val compound = NbtCompound()
val text = iota.display()
compound.putString("text", Text.Serializer.toJson(Text.of(text.string.removePrefix("\"").removeSuffix("\"")).getWithStyle(text.style)[0]))
dataTracker.set(displayDataTracker, compound)
}
}

override fun setLifespan(lifespan: Int) {
this.lifespan = lifespan
}
Expand All @@ -83,7 +58,6 @@ class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(en
override fun createSpawnPacket() = EntitySpawnS2CPacket(this)

override fun initDataTracker() {
dataTracker.startTracking(displayDataTracker, NbtCompound())
dataTracker.startTracking(pigmentDataTracker, NbtCompound())
dataTracker.startTracking(rollDataTracker, 0f)
dataTracker.startTracking(sizeDataTracker, 1f)
Expand All @@ -92,16 +66,6 @@ class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(en

override fun onTrackedDataSet(data: TrackedData<*>) {
when (data) {
displayDataTracker -> {
val raw = dataTracker.get(displayDataTracker)
if (raw.contains("text")) {
this.clientIsText = true
this.clientText = Text.Serializer.fromJson(raw.getString("text"))!!
} else {
this.clientIsText = false
this.clientVerts = RenderUtils.getNormalizedStrokes(HexPattern.fromNBT(raw))
}
}
pigmentDataTracker -> this.clientPigment = FrozenColorizer.fromNBT(dataTracker.get(pigmentDataTracker))
sizeDataTracker -> this.clientSize = dataTracker.get(sizeDataTracker)
rollDataTracker -> this.clientRoll = dataTracker.get(rollDataTracker)
Expand All @@ -111,7 +75,6 @@ class SpeckEntity(entityType: EntityType<SpeckEntity>, world: World) : Entity(en
}

companion object {
private val displayDataTracker: TrackedData<NbtCompound> = DataTracker.registerData(SpeckEntity::class.java, TrackedDataHandlerRegistry.NBT_COMPOUND)
private val pigmentDataTracker: TrackedData<NbtCompound> = DataTracker.registerData(SpeckEntity::class.java, TrackedDataHandlerRegistry.NBT_COMPOUND)
private val sizeDataTracker: TrackedData<Float> = DataTracker.registerData(SpeckEntity::class.java, TrackedDataHandlerRegistry.FLOAT)
private val thicknessDataTracker: TrackedData<Float> = DataTracker.registerData(SpeckEntity::class.java, TrackedDataHandlerRegistry.FLOAT)
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/miyucomics/hexical/entities/specklikes/MeshEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package miyucomics.hexical.entities.specklikes

import at.petrak.hexcasting.api.spell.iota.Vec3Iota
import at.petrak.hexcasting.api.utils.putCompound
import at.petrak.hexcasting.api.utils.putList
import miyucomics.hexical.inits.HexicalEntities
import net.minecraft.entity.EntityType
import net.minecraft.entity.data.DataTracker
import net.minecraft.entity.data.TrackedData
import net.minecraft.entity.data.TrackedDataHandlerRegistry
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtFloat
import net.minecraft.nbt.NbtList
import net.minecraft.util.math.Vec3d
import net.minecraft.util.math.Vec3f
import net.minecraft.world.World

@OptIn(ExperimentalStdlibApi::class)
open class MeshEntity(entityType: EntityType<MeshEntity>, world: World) : BaseSpecklike(entityType, world) {
constructor(world: World) : this(HexicalEntities.MESH_ENTITY, world)

var clientVertices: MutableList<Vec3f> = mutableListOf()

fun getShape(): List<Vec3Iota> {
val list = dataTracker.get(shapeDataTracker).getList("shape", NbtElement.FLOAT_TYPE.toInt())
val deserializedVertices = mutableListOf<Vec3Iota>()
for (i in 0..<(list.size / 3))
deserializedVertices.add(Vec3Iota(Vec3d(list.getFloat(3 * i).toDouble(), list.getFloat(3 * i + 1).toDouble(), list.getFloat(3 * i + 2).toDouble())))
return deserializedVertices
}

fun setShape(shape: List<Vec3f>) {
val compound = NbtCompound()
val list = NbtList()
for (vertex in shape) {
list.add(NbtFloat.of(vertex.x))
list.add(NbtFloat.of(vertex.y))
list.add(NbtFloat.of(vertex.z))
}
compound.putList("shape", list)
this.dataTracker.set(shapeDataTracker, compound)
}

override fun readCustomDataFromNbt(nbt: NbtCompound) {
super.readCustomDataFromNbt(nbt)
dataTracker.set(shapeDataTracker, nbt.getCompound("shape"))
}

override fun writeCustomDataToNbt(nbt: NbtCompound) {
super.writeCustomDataToNbt(nbt)
nbt.putCompound("shape", dataTracker.get(shapeDataTracker))
}

override fun initDataTracker() {
super.initDataTracker()
dataTracker.startTracking(shapeDataTracker, NbtCompound())
}

override fun onTrackedDataSet(data: TrackedData<*>) {
super.onTrackedDataSet(data)
if (data == shapeDataTracker) {
val list = this.dataTracker.get(shapeDataTracker).getList("shape", NbtElement.FLOAT_TYPE.toInt())
this.clientVertices = mutableListOf()
for (i in 0..<(list.size / 3))
clientVertices.add(Vec3f(list.getFloat(3 * i), list.getFloat(3 * i + 1), list.getFloat(3 * i + 2)))
}
}

companion object {
private val shapeDataTracker: TrackedData<NbtCompound> = DataTracker.registerData(MeshEntity::class.java, TrackedDataHandlerRegistry.NBT_COMPOUND)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package miyucomics.hexical.entities
package miyucomics.hexical.entities.specklikes

import at.petrak.hexcasting.api.HexAPI.modLoc
import at.petrak.hexcasting.api.misc.FrozenColorizer
Expand Down
Loading

0 comments on commit d5b2a82

Please sign in to comment.