Skip to content

Commit

Permalink
Refactored code to make conjuring blocks easier
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Feb 21, 2024
1 parent a64ca2b commit 980ebeb
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
package miyucomics.hexical.blocks

import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.common.blocks.BlockConjured
import net.minecraft.block.BlockState
import net.minecraft.block.entity.BlockEntity
import net.minecraft.block.entity.BlockEntityTicker
import net.minecraft.block.entity.BlockEntityType
import miyucomics.hexical.generics.GenericConjuredBlock
import net.minecraft.entity.Entity
import net.minecraft.util.math.BlockPos
import net.minecraft.world.BlockView
import net.minecraft.world.World
import net.minecraft.world.WorldAccess

class ConjuredBouncyBlock(properties: Settings?) : BlockConjured(properties) {
override fun createBlockEntity(pPos: BlockPos, pState: BlockState): BlockEntity {
return ConjuredBouncyBlockEntity(pPos, pState)
}

class ConjuredBouncyBlock : GenericConjuredBlock<ConjuredBouncyBlockEntity>(baseMaterial(), ConjuredBouncyBlockEntity::init) {
override fun onEntityLand(world: BlockView, entity: Entity) {
val velocity = entity.velocity
if (velocity.y < 0)
entity.setVelocity(velocity.x, -velocity.y, velocity.z)
}

override fun <T : BlockEntity?> getTicker(pLevel: World, pState: BlockState?, pBlockEntityType: BlockEntityType<T>?): BlockEntityTicker<T>? {
return if (pLevel.isClient) BlockEntityTicker { _, _, _, blockEntity: T -> tick(blockEntity) } else null
}

override fun onSteppedOn(pLevel: World, pPos: BlockPos, pState: BlockState, pEntity: Entity) {
val tile = pLevel.getBlockEntity(pPos)
if (tile is ConjuredBouncyBlockEntity)
tile.walkParticle(pEntity)
}

companion object {
fun <T> tick(blockEntity: T) {
if (blockEntity is ConjuredBouncyBlockEntity)
blockEntity.particleEffect()
}

fun setColor(pLevel: WorldAccess, pPos: BlockPos, colorizer: FrozenColorizer) {
val blockentity = pLevel.getBlockEntity(pPos)
if (blockentity is ConjuredBouncyBlockEntity)
blockentity.setColorizer(colorizer)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,67 +1,15 @@
package miyucomics.hexical.blocks

import at.petrak.hexcasting.api.block.HexBlockEntity
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.common.blocks.BlockConjured
import at.petrak.hexcasting.common.particles.ConjureParticleOptions
import miyucomics.hexical.generics.GenericConjuredBlockEntity
import miyucomics.hexical.registry.HexicalBlocks
import net.minecraft.block.BlockState
import net.minecraft.entity.Entity
import net.minecraft.nbt.NbtCompound
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import java.util.*

class ConjuredBouncyBlockEntity(pos: BlockPos?, state: BlockState?) : HexBlockEntity(HexicalBlocks.CONJURED_BOUNCY_BLOCK_ENTITY, pos, state) {
private var colorizer: FrozenColorizer = FrozenColorizer.DEFAULT.get()

fun walkParticle(entity: Entity) {
for (i in 0..2) {
val color = colorizer.getColor(entity.age.toFloat(), entity.pos.add(Vec3d(RANDOM.nextDouble(), RANDOM.nextDouble(), RANDOM.nextDouble()).multiply(RANDOM.nextDouble() * 3)))
assert(world != null)
world!!.addParticle(
ConjureParticleOptions(color, false),
entity.x + (RANDOM.nextFloat() * 0.6) - 0.3,
getPos().y + (RANDOM.nextFloat() * 0.05) + 0.95,
entity.z + (RANDOM.nextFloat() * 0.6) - 0.3,
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble()
)
}
}

fun particleEffect() {
val color = colorizer.getColor(RANDOM.nextFloat() * 16384, Vec3d(RANDOM.nextDouble(), RANDOM.nextDouble(), RANDOM.nextDouble()).multiply(RANDOM.nextDouble() * 3))
assert(world != null)
if (RANDOM.nextFloat() < 0.2) {
world!!.addParticle(
ConjureParticleOptions(color, false),
getPos().x.toDouble() + RANDOM.nextFloat(),
getPos().y.toDouble() + RANDOM.nextFloat(),
getPos().z.toDouble() + RANDOM.nextFloat(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble()
)
}
}

override fun saveModData(tag: NbtCompound) {
tag.put(TAG_COLORIZER, colorizer.serializeToNBT())
}

override fun loadModData(tag: NbtCompound) {
this.colorizer = FrozenColorizer.fromNBT(tag.getCompound(TAG_COLORIZER))
}

fun setColorizer(colorizer: FrozenColorizer) {
this.colorizer = colorizer
this.sync()
}

class ConjuredBouncyBlockEntity(type: BlockEntityType<ConjuredBouncyBlockEntity>, pos: BlockPos?, state: BlockState?) : GenericConjuredBlockEntity<ConjuredBouncyBlockEntity>(pos, state, type) {
companion object {
private val RANDOM = Random()
const val TAG_COLORIZER: String = "tag_colorizer"
fun init (pos: BlockPos?, state: BlockState?): ConjuredBouncyBlockEntity {
return ConjuredBouncyBlockEntity(HexicalBlocks.CONJURED_BOUNCY_BLOCK_ENTITY, pos, state)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package miyucomics.hexical.blocks

import miyucomics.hexical.generics.GenericConjuredBlock

class ConjuredSlipperyBlock : GenericConjuredBlock<ConjuredSlipperyBlockEntity>(baseMaterial().slipperiness(1f), ConjuredSlipperyBlockEntity::init)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package miyucomics.hexical.blocks

import miyucomics.hexical.generics.GenericConjuredBlockEntity
import miyucomics.hexical.registry.HexicalBlocks
import net.minecraft.block.BlockState
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.util.math.BlockPos

class ConjuredSlipperyBlockEntity(type: BlockEntityType<ConjuredSlipperyBlockEntity>, pos: BlockPos?, state: BlockState?) : GenericConjuredBlockEntity<ConjuredSlipperyBlockEntity>(pos, state, type) {
companion object {
fun init (pos: BlockPos?, state: BlockState?): ConjuredSlipperyBlockEntity {
return ConjuredSlipperyBlockEntity(HexicalBlocks.CONJURED_SLIPPERY_BLOCK_ENTITY, pos, state)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package miyucomics.hexical.casting.patterns.spells

import at.petrak.hexcasting.api.misc.MediaConstants
import at.petrak.hexcasting.api.spell.ParticleSpray
import at.petrak.hexcasting.api.spell.RenderedSpell
import at.petrak.hexcasting.api.spell.SpellAction
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.getBlockPos
import at.petrak.hexcasting.api.spell.iota.Iota
import at.petrak.hexcasting.xplat.IXplatAbstractions
import miyucomics.hexical.generics.GenericConjuredBlock
import net.minecraft.block.BlockState
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d

class OpConjureBlock(private val state: BlockState) : SpellAction {
override val argc = 1

override fun execute(args: List<Iota>, ctx: CastingContext): Triple<RenderedSpell, Int, List<ParticleSpray>>? {
val pos = args.getBlockPos(0, argc)
ctx.assertVecInRange(pos)
return Triple(Spell(state, pos), MediaConstants.DUST_UNIT, listOf(ParticleSpray.cloud(Vec3d.ofCenter(pos), 1.0)))
}

private data class Spell(val state: BlockState, val pos: BlockPos) : RenderedSpell {
override fun cast(ctx: CastingContext) {
if (!ctx.canEditBlockAt(pos))
return
ctx.world.setBlockState(pos, state, 5)
GenericConjuredBlock.setColor(ctx.world, pos, IXplatAbstractions.INSTANCE.getColorizer(ctx.caster))
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package miyucomics.hexical.generics

import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.common.blocks.BlockConjured
import net.minecraft.block.BlockState
import net.minecraft.block.MapColor
import net.minecraft.block.Material
import net.minecraft.block.entity.BlockEntity
import net.minecraft.block.entity.BlockEntityTicker
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.entity.Entity
import net.minecraft.sound.BlockSoundGroup
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.minecraft.world.WorldAccess

abstract class GenericConjuredBlock<T : BlockEntity>(properties: Settings, private val blockEntityProvider: (BlockPos, BlockState) -> T) : BlockConjured(properties) {
override fun createBlockEntity(pos: BlockPos, state: BlockState): BlockEntity {
return blockEntityProvider(pos, state)
}

override fun <T : BlockEntity?> getTicker(world: World, state: BlockState?, type: BlockEntityType<T>?): BlockEntityTicker<T>? {
return if (world.isClient) BlockEntityTicker { _, _, _, blockEntity: T -> tick(blockEntity) } else null
}

override fun onSteppedOn(world: World, pos: BlockPos, state: BlockState, entity: Entity) {
val tile = world.getBlockEntity(pos)
if (tile is GenericConjuredBlockEntity<*>)
tile.walkParticle(entity)
}

companion object {
fun <T> tick(blockEntity: T) {
if (blockEntity is GenericConjuredBlockEntity<*>)
blockEntity.particleEffect()
}

fun setColor(world: WorldAccess, pos: BlockPos, colorizer: FrozenColorizer) {
val blockEntity = world.getBlockEntity(pos)
if (blockEntity is GenericConjuredBlockEntity<*>)
blockEntity.setColorizer(colorizer)
}

fun baseMaterial(): Settings {
return Settings.of(Material.ORGANIC_PRODUCT).nonOpaque().dropsNothing().breakInstantly().luminance { _ -> 2 }.mapColor(MapColor.CLEAR).suffocates { _, _, _ -> false }.blockVision { _, _, _ -> false }.allowsSpawning { _, _, _, _ -> false }.sounds(BlockSoundGroup.AMETHYST_CLUSTER);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package miyucomics.hexical.generics

import at.petrak.hexcasting.api.block.HexBlockEntity
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.common.particles.ConjureParticleOptions
import net.minecraft.block.BlockState
import net.minecraft.block.entity.BlockEntity
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.entity.Entity
import net.minecraft.nbt.NbtCompound
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d
import java.util.*

open class GenericConjuredBlockEntity<T : BlockEntity>(pos: BlockPos?, state: BlockState?, blockEntityType: BlockEntityType<T>) : HexBlockEntity(blockEntityType, pos, state) {
private var colorizer: FrozenColorizer = FrozenColorizer.DEFAULT.get()

fun walkParticle(entity: Entity) {
for (i in 0..2) {
val color = colorizer.getColor(entity.age.toFloat(), entity.pos.add(Vec3d(RANDOM.nextDouble(), RANDOM.nextDouble(), RANDOM.nextDouble()).multiply(
RANDOM.nextDouble() * 3)))
assert(world != null)
world!!.addParticle(
ConjureParticleOptions(color, false),
entity.x + (RANDOM.nextFloat() * 0.6) - 0.3,
getPos().y + (RANDOM.nextFloat() * 0.05) + 0.95,
entity.z + (RANDOM.nextFloat() * 0.6) - 0.3,
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble()
)
}
}

fun particleEffect() {
val color = colorizer.getColor(
RANDOM.nextFloat() * 16384, Vec3d(RANDOM.nextDouble(), RANDOM.nextDouble(), RANDOM.nextDouble()).multiply(
RANDOM.nextDouble() * 3))
assert(world != null)
if (RANDOM.nextFloat() < 0.2) {
world!!.addParticle(
ConjureParticleOptions(color, false),
getPos().x.toDouble() + RANDOM.nextFloat(),
getPos().y.toDouble() + RANDOM.nextFloat(),
getPos().z.toDouble() + RANDOM.nextFloat(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble(),
RANDOM.nextFloat(-0.02f, 0.02f).toDouble()
)
}
}

override fun saveModData(tag: NbtCompound) {
tag.put(TAG_COLORIZER, colorizer.serializeToNBT())
}

override fun loadModData(tag: NbtCompound) {
this.colorizer = FrozenColorizer.fromNBT(tag.getCompound(TAG_COLORIZER))
}

fun setColorizer(colorizer: FrozenColorizer) {
this.colorizer = colorizer
this.sync()
}

companion object {
private val RANDOM = Random()
const val TAG_COLORIZER: String = "tag_colorizer"
}
}
27 changes: 9 additions & 18 deletions common/src/main/java/miyucomics/hexical/registry/HexicalBlocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@ import dev.architectury.registry.registries.DeferredRegister
import miyucomics.hexical.Hexical
import miyucomics.hexical.blocks.ConjuredBouncyBlock
import miyucomics.hexical.blocks.ConjuredBouncyBlockEntity
import net.minecraft.block.AbstractBlock
import miyucomics.hexical.blocks.ConjuredSlipperyBlock
import miyucomics.hexical.blocks.ConjuredSlipperyBlockEntity
import net.minecraft.block.Block
import net.minecraft.block.MapColor
import net.minecraft.block.Material
import net.minecraft.block.entity.BlockEntityType
import net.minecraft.sound.BlockSoundGroup
import net.minecraft.util.registry.Registry

object HexicalBlocks {
private val BLOCKS: DeferredRegister<Block> = DeferredRegister.create(Hexical.MOD_ID, Registry.BLOCK_KEY)
private val BLOCK_ENTITIES: DeferredRegister<BlockEntityType<*>> = DeferredRegister.create(Hexical.MOD_ID, Registry.BLOCK_ENTITY_TYPE_KEY)
val CONJURED_BOUNCY_BLOCK: Block = ConjuredBouncyBlock(
AbstractBlock.Settings
.of(Material.ORGANIC_PRODUCT)
.nonOpaque()
.dropsNothing()
.breakInstantly()
.luminance { _ -> 2 }
.mapColor(MapColor.CLEAR)
.suffocates { _, _, _ -> false }
.blockVision { _, _, _ -> false }
.allowsSpawning { _, _, _, _ -> false }
.sounds(BlockSoundGroup.AMETHYST_CLUSTER)
)
val CONJURED_BOUNCY_BLOCK_ENTITY: BlockEntityType<ConjuredBouncyBlockEntity> = BlockEntityType.Builder.create(::ConjuredBouncyBlockEntity, CONJURED_BOUNCY_BLOCK).build(null)

val CONJURED_BOUNCY_BLOCK: Block = ConjuredBouncyBlock()
val CONJURED_SLIPPERY_BLOCK: Block = ConjuredSlipperyBlock()
val CONJURED_BOUNCY_BLOCK_ENTITY: BlockEntityType<ConjuredBouncyBlockEntity> = BlockEntityType.Builder.create(ConjuredBouncyBlockEntity::init, CONJURED_BOUNCY_BLOCK).build(null)
val CONJURED_SLIPPERY_BLOCK_ENTITY: BlockEntityType<ConjuredSlipperyBlockEntity> = BlockEntityType.Builder.create(ConjuredSlipperyBlockEntity::init, CONJURED_SLIPPERY_BLOCK).build(null)

@JvmStatic
fun init() {
BLOCKS.register("conjured_bouncy_block") { CONJURED_BOUNCY_BLOCK }
BLOCKS.register("conjured_slippery_block") { CONJURED_SLIPPERY_BLOCK }
BLOCK_ENTITIES.register("conjured_bouncy_block") { CONJURED_BOUNCY_BLOCK_ENTITY }
BLOCK_ENTITIES.register("conjured_slippery_block") { CONJURED_SLIPPERY_BLOCK_ENTITY }
BLOCK_ENTITIES.register()
BLOCKS.register()
}
Expand Down
Loading

0 comments on commit 980ebeb

Please sign in to comment.