add more block protection hooks

This commit is contained in:
yrsegal@gmail.com 2022-07-24 20:30:58 -04:00
parent 54f0007315
commit 96e4569b24
4 changed files with 90 additions and 75 deletions

View file

@ -3,6 +3,7 @@ package at.petrak.hexcasting.common.casting.operators.spells
import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.particles.ParticleTypes
@ -58,27 +59,34 @@ object OpDestroyWater : SpellOperator {
val fluid = ctx.world.getFluidState(here)
if (fluid != Fluids.EMPTY.defaultFluidState()) {
val blockstate = ctx.world.getBlockState(here)
val material = blockstate.material
val success =
if (blockstate.block is BucketPickup && !(blockstate.block as BucketPickup).pickupBlock(
ctx.world,
here,
blockstate
).isEmpty
) {
true
} else if (blockstate.block is LiquidBlock) {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3)
true
} else if (material == Material.WATER_PLANT || material == Material.REPLACEABLE_WATER_PLANT) {
val blockentity: BlockEntity? =
if (blockstate.hasBlockEntity()) ctx.world.getBlockEntity(here) else null
Block.dropResources(blockstate, ctx.world, here, blockentity)
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3)
true
} else {
false
}
if (IXplatAbstractions.INSTANCE.isBreakingAllowed(
ctx.world,
here,
blockstate,
ctx.caster
)
) {
val material = blockstate.material
val success =
if (blockstate.block is BucketPickup && !(blockstate.block as BucketPickup).pickupBlock(
ctx.world,
here,
blockstate
).isEmpty
) {
true
} else if (blockstate.block is LiquidBlock) {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3)
true
} else if (material == Material.WATER_PLANT || material == Material.REPLACEABLE_WATER_PLANT) {
val blockentity: BlockEntity? =
if (blockstate.hasBlockEntity()) ctx.world.getBlockEntity(here) else null
Block.dropResources(blockstate, ctx.world, here, blockentity)
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3)
true
} else {
false
}
if (success) {
ctx.world.sendParticles(
@ -98,6 +106,7 @@ object OpDestroyWater : SpellOperator {
}
}
}
}
}
}

View file

@ -1,14 +1,11 @@
package at.petrak.hexcasting.common.casting.operators.spells
import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.getChecked
import at.petrak.hexcasting.api.spell.ParticleSpray
import at.petrak.hexcasting.api.spell.RenderedSpell
import at.petrak.hexcasting.api.spell.SpellDatum
import at.petrak.hexcasting.api.spell.SpellOperator
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.mishaps.MishapBadBlock
import at.petrak.hexcasting.common.misc.AkashicTreeGrower
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.tags.BlockTags
import net.minecraft.world.phys.Vec3
@ -37,7 +34,9 @@ object OpEdifySapling : SpellOperator {
private data class Spell(val pos: BlockPos) : RenderedSpell {
override fun cast(ctx: CastingContext) {
if (!ctx.world.mayInteract(ctx.caster, pos))
val blockstate = ctx.world.getBlockState(pos)
if (!ctx.world.mayInteract(ctx.caster, pos) ||
!IXplatAbstractions.INSTANCE.isBreakingAllowed(ctx.world, pos, blockstate, ctx.caster))
return
val bs = ctx.world.getBlockState(pos)

View file

@ -4,6 +4,7 @@ import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.ktxt.UseOnContext
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.particles.ParticleTypes
@ -56,53 +57,56 @@ object OpExtinguish : SpellOperator {
) {
// never seen this pos in my life
val blockstate = ctx.world.getBlockState(here)
val success =
when (blockstate.block) {
is BaseFireBlock -> {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3); true
if (IXplatAbstractions.INSTANCE.isBreakingAllowed(ctx.world, here, blockstate, ctx.caster)) {
val success =
when (blockstate.block) {
is BaseFireBlock -> {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3); true
}
is CampfireBlock -> {
if (blockstate.getValue(CampfireBlock.LIT)) { // check if campfire is lit before putting it out
val wilson =
Items.WOODEN_SHOVEL // summon shovel from the ether to do our bidding
val hereVec = Vec3.atCenterOf(here)
wilson.useOn(
UseOnContext(
ctx.world,
null,
InteractionHand.MAIN_HAND,
ItemStack(wilson),
BlockHitResult(hereVec, Direction.UP, here, false)
)
); true
} else false
}
is AbstractCandleBlock -> {
if (blockstate.getValue(AbstractCandleBlock.LIT)) { // same check for candles
AbstractCandleBlock.extinguish(null, blockstate, ctx.world, here); true
} else false
}
is NetherPortalBlock -> {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3); true
}
else -> false
}
is CampfireBlock -> {
if (blockstate.getValue(CampfireBlock.LIT)) { // check if campfire is lit before putting it out
val wilson = Items.WOODEN_SHOVEL // summon shovel from the ether to do our bidding
val hereVec = Vec3.atCenterOf(here)
wilson.useOn(
UseOnContext(
ctx.world,
null,
InteractionHand.MAIN_HAND,
ItemStack(wilson),
BlockHitResult(hereVec, Direction.UP, here, false)
)
); true
} else false
}
is AbstractCandleBlock -> {
if (blockstate.getValue(AbstractCandleBlock.LIT)) { // same check for candles
AbstractCandleBlock.extinguish(null, blockstate, ctx.world, here); true
} else false
}
is NetherPortalBlock -> {
ctx.world.setBlock(here, Blocks.AIR.defaultBlockState(), 3); true
}
else -> false
}
if (success) {
ctx.world.sendParticles(
ParticleTypes.SMOKE,
here.x + 0.5 + Math.random() * 0.4 - 0.2,
here.y + 0.5 + Math.random() * 0.4 - 0.2,
here.z + 0.5 + Math.random() * 0.4 - 0.2,
2,
0.0,
0.05,
0.0,
0.0
)
successes++
}
for (dir in Direction.values()) {
todo.add(here.relative(dir))
if (success) {
ctx.world.sendParticles(
ParticleTypes.SMOKE,
here.x + 0.5 + Math.random() * 0.4 - 0.2,
here.y + 0.5 + Math.random() * 0.4 - 0.2,
here.z + 0.5 + Math.random() * 0.4 - 0.2,
2,
0.0,
0.05,
0.0,
0.0
)
successes++
}
for (dir in Direction.values()) {
todo.add(here.relative(dir))
}
}
}
}

View file

@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.misc.ManaConstants
import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.ktxt.UseOnContext
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.InteractionHand
@ -33,11 +34,13 @@ object OpIgnite : SpellOperator {
private data class Spell(val target: Vec3) : RenderedSpell {
override fun cast(ctx: CastingContext) {
val pos = BlockPos(target)
if (!ctx.world.mayInteract(ctx.caster, pos))
return
// steal petra code that steals bucket code
val maxwell = Items.FIRE_CHARGE
if (!ctx.world.mayInteract(ctx.caster, pos) || !IXplatAbstractions.INSTANCE.isPlacingAllowed(ctx.world, pos, ItemStack(maxwell), ctx.caster))
return
if (maxwell is FireChargeItem) {
// help
maxwell.useOn(
@ -45,7 +48,7 @@ object OpIgnite : SpellOperator {
ctx.world,
null,
InteractionHand.MAIN_HAND,
ItemStack(maxwell.asItem()),
ItemStack(maxwell),
BlockHitResult(target, Direction.UP, pos, false)
)
)