-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code to make conjuring blocks easier
- Loading branch information
1 parent
a64ca2b
commit 980ebeb
Showing
15 changed files
with
220 additions
and
163 deletions.
There are no files selected for viewing
39 changes: 2 additions & 37 deletions
39
common/src/main/java/miyucomics/hexical/blocks/ConjuredBouncyBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
64 changes: 6 additions & 58 deletions
64
common/src/main/java/miyucomics/hexical/blocks/ConjuredBouncyBlockEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
common/src/main/java/miyucomics/hexical/blocks/ConjuredSlipperyBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
15 changes: 15 additions & 0 deletions
15
common/src/main/java/miyucomics/hexical/blocks/ConjuredSlipperyBlockEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
common/src/main/java/miyucomics/hexical/casting/patterns/spells/OpConjureBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
common/src/main/java/miyucomics/hexical/casting/patterns/spells/OpConjureBouncyBlock.kt
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
common/src/main/java/miyucomics/hexical/generics/GenericConjuredBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
common/src/main/java/miyucomics/hexical/generics/GenericConjuredBlockEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.