Skip to content

Commit

Permalink
Changed how conjured blocks work fundamentally
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Feb 22, 2024
1 parent 8d0db26 commit 06f8782
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package miyucomics.hexical.blocks

import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.common.blocks.BlockConjured
import miyucomics.hexical.registry.HexicalBlocks
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks
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.entity.player.PlayerEntity
import net.minecraft.sound.BlockSoundGroup
import net.minecraft.sound.SoundCategory
import net.minecraft.sound.SoundEvents
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3i
import net.minecraft.world.BlockView
import net.minecraft.world.World
import net.minecraft.world.WorldAccess

class AdvancedConjuredBlock : BlockConjured(Settings.of(Material.ORGANIC_PRODUCT).nonOpaque().dropsNothing().breakInstantly().luminance { _ -> 2 }.mapColor(MapColor.CLEAR).suffocates { _, _, _ -> false }.blockVision { _, _, _ -> false }.allowsSpawning { _, _, _, _ -> false }.sounds(BlockSoundGroup.AMETHYST_CLUSTER)) {
private val offsets: List<Vec3i> = listOf(
Vec3i(-1, 0, 0), Vec3i(1, 0, 0),
Vec3i(0, -1, 0), Vec3i(0, 1, 0),
Vec3i(0, 0, -1), Vec3i(0, 0, 1),
)

override fun onBreak(world: World, position: BlockPos, state: BlockState, player: PlayerEntity) {
world.playSound(position.x.toDouble(), position.y.toDouble(), position.z.toDouble(), SoundEvents.BLOCK_AMETHYST_BLOCK_BREAK, SoundCategory.BLOCKS, 1f, 1f, true)
val tile = world.getBlockEntity(position)
if (tile !is AdvancedConjuredBlockEntity)
return
if (!tile.volatile)
return
world.setBlockState(position, Blocks.AIR.defaultState)
for (offset in offsets) {
val positionToTest = position.add(offset);
val otherState = world.getBlockState(positionToTest)
val block = otherState.block
if (block == HexicalBlocks.ADVANCED_CONJURED_BLOCK)
block.onBreak(world, positionToTest, otherState, player)
}
}

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 createBlockEntity(pos: BlockPos, state: BlockState): BlockEntity {
return AdvancedConjuredBlockEntity(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 AdvancedConjuredBlockEntity)
tile.walkParticle(entity)
}

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

fun setProperty(world: WorldAccess, pos: BlockPos, property: String) {
val blockEntity = world.getBlockEntity(pos)
if (blockEntity is AdvancedConjuredBlockEntity)
blockEntity.setProperty(property)
}

fun setColor(world: WorldAccess, pos: BlockPos, colorizer: FrozenColorizer) {
val blockEntity = world.getBlockEntity(pos)
if (blockEntity is AdvancedConjuredBlockEntity)
blockEntity.setColorizer(colorizer)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package miyucomics.hexical.blocks

import at.petrak.hexcasting.api.block.HexBlockEntity
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.iota.DoubleIota
import at.petrak.hexcasting.api.spell.iota.Vec3Iota
import at.petrak.hexcasting.api.utils.vecFromNBT
import at.petrak.hexcasting.common.particles.ConjureParticleOptions
import miyucomics.hexical.registry.HexicalBlocks
import net.minecraft.block.BlockState
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.*

class AdvancedConjuredBlockEntity(pos: BlockPos?, state: BlockState?) : HexBlockEntity(HexicalBlocks.ADVANCED_CONJURED_BLOCK_ENTITY, pos, state) {
private val random = Random()
private val colorizerTag: String = "colorizer"
private val bouncyTag: String = "bouncy"
private val slipperyTag: String = "slippery"
private val volatileTag: String = "volatile"
private var colorizer: FrozenColorizer = FrozenColorizer.DEFAULT.get()
var bouncy: Boolean = false
var slippery: Boolean = false
var volatile: Boolean = false

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(colorizerTag, colorizer.serializeToNBT())
tag.putBoolean(bouncyTag, this.bouncy)
tag.putBoolean(slipperyTag, this.slippery)
tag.putBoolean(volatileTag, this.volatile)
}

override fun loadModData(tag: NbtCompound) {
this.colorizer = FrozenColorizer.fromNBT(tag.getCompound(colorizerTag))
this.bouncy = tag.getBoolean(bouncyTag)
this.slippery = tag.getBoolean(slipperyTag)
this.volatile = tag.getBoolean(volatileTag)
}

fun setProperty(property: String) {
when (property) {
"bouncy" -> this.bouncy = true
"slippery" -> this.slippery = true
"volatile" -> this.volatile = true
}
this.sync()
}

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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 miyucomics.hexical.blocks.AdvancedConjuredBlock
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d

class OpConfigureBlock(private val property: String) : 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(pos, property), MediaConstants.DUST_UNIT, listOf(ParticleSpray.cloud(Vec3d.ofCenter(pos), 1.0)))
}

private data class Spell(val pos: BlockPos, val property: String) : RenderedSpell {
override fun cast(ctx: CastingContext) {
AdvancedConjuredBlock.setProperty(ctx.world, pos, property)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ 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 miyucomics.hexical.blocks.AdvancedConjuredBlock
import miyucomics.hexical.registry.HexicalBlocks
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Vec3d

class OpConjureBlock(private val state: BlockState) : SpellAction {
class OpConjureAdvancedBlock : 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)))
return Triple(Spell(pos), MediaConstants.DUST_UNIT, listOf(ParticleSpray.cloud(Vec3d.ofCenter(pos), 1.0)))
}

private data class Spell(val state: BlockState, val pos: BlockPos) : RenderedSpell {
private data class Spell(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))
ctx.world.setBlockState(pos, HexicalBlocks.ADVANCED_CONJURED_BLOCK.defaultState, 5)
AdvancedConjuredBlock.setColor(ctx.world, pos, IXplatAbstractions.INSTANCE.getColorizer(ctx.caster))
}
}
}
Loading

0 comments on commit 06f8782

Please sign in to comment.