Skip to content

Commit

Permalink
Refactored code to be neater
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Feb 23, 2024
1 parent 6ff15fa commit 21077f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AdvancedConjuredBlock : BlockConjured(
val tile = world.getBlockEntity(pos)
if (tile !is AdvancedConjuredBlockEntity)
return
if (tile.bouncy) {
if (tile.properties["bouncy"]!!) {
println("Entity landed with velocity of " + entity.velocity.toString())
val velocity = entity.velocity
if (velocity.y < 0) {
Expand All @@ -61,7 +61,7 @@ class AdvancedConjuredBlock : BlockConjured(
if (tile !is AdvancedConjuredBlockEntity)
return
world.setBlockState(position, Blocks.AIR.defaultState)
if (tile.volatile) {
if (tile.properties["volatile"]!!) {
for (offset in volatileOffsets) {
val positionToTest = position.add(offset);
val otherState = world.getBlockState(positionToTest)
Expand All @@ -76,7 +76,7 @@ class AdvancedConjuredBlock : BlockConjured(
val tile = world.getBlockEntity(pos)
if (tile !is AdvancedConjuredBlockEntity)
return
if (!tile.invisible)
if (!tile.properties["invisible"]!!)
tile.walkParticle(entity)
}

Expand All @@ -92,9 +92,9 @@ class AdvancedConjuredBlock : BlockConjured(
fun <T> tick(world: World, position: BlockPos, state: BlockState, blockEntity: T) {
if (blockEntity !is AdvancedConjuredBlockEntity)
return
if (!blockEntity.invisible)
if (!blockEntity.properties["invisible"]!!)
blockEntity.particleEffect()
if (blockEntity.ephemeral) {
if (blockEntity.properties["ephemeral"]!!) {
blockEntity.lifespan--
if (blockEntity.lifespan <= 0)
HexicalBlocks.ADVANCED_CONJURED_BLOCK.onBreak(world, position, state, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import java.util.*
class AdvancedConjuredBlockEntity(pos: BlockPos?, state: BlockState?) : HexBlockEntity(HexicalBlocks.ADVANCED_CONJURED_BLOCK_ENTITY, pos, state) {
private val random = Random()
private var colorizer: FrozenColorizer = FrozenColorizer.DEFAULT.get()
var bouncy: Boolean = false
var ephemeral: Boolean = false
var invisible: Boolean = false
var volatile: Boolean = false
var properties: MutableMap<String, Boolean> = mutableMapOf(
"bouncy" to false,
"ephemeral" to false,
"invisible" to false,
"volatile" to false
)
var lifespan: Int = 0

fun walkParticle(entity: Entity) {
Expand Down Expand Up @@ -60,33 +62,21 @@ class AdvancedConjuredBlockEntity(pos: BlockPos?, state: BlockState?) : HexBlock

override fun saveModData(tag: NbtCompound) {
tag.put("colorizer", colorizer.serializeToNBT())
tag.putBoolean("bouncy", this.bouncy)
tag.putBoolean("ephemeral", this.ephemeral)
tag.putBoolean("invisible", this.invisible)
tag.putBoolean("volatile", this.volatile)
properties.forEach { (key, value) -> tag.putBoolean(key, value) }
tag.putInt("lifespan", this.lifespan)
}

override fun loadModData(tag: NbtCompound) {
this.colorizer = FrozenColorizer.fromNBT(tag.getCompound("colorizer"))
this.bouncy = tag.getBoolean("bouncy")
this.ephemeral = tag.getBoolean("ephemeral")
this.invisible = tag.getBoolean("invisible")
this.volatile = tag.getBoolean("volatile")
properties.keys.forEach { key -> properties[key] = tag.getBoolean(key) }
this.lifespan = tag.getInt("lifespan")
}

fun setProperty(property: String, args: List<Iota>) {
when (property) {
"bouncy" -> this.bouncy = true
"ephemeral" -> {
this.ephemeral = true
this.lifespan = args.getPositiveInt(0, args.size)
}
"invisible" -> this.invisible = true
"volatile" -> this.volatile = true
}
this.sync()
if (property == "ephemeral")
this.lifespan = args.getPositiveInt(0, args.size)
properties[property] = !properties[property]!!
sync()
}

fun setColorizer(colorizer: FrozenColorizer) {
Expand Down

0 comments on commit 21077f7

Please sign in to comment.