add more block protection hooks

Port of 96e4569b24
This commit is contained in:
yrsegal@gmail.com 2022-07-24 21:45:46 -04:00
parent 8f6bddb25c
commit 1424cff844
6 changed files with 107 additions and 89 deletions

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.api.spell.iota.Iota
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.core.particles.ParticleTypes
@ -60,43 +61,51 @@ object OpDestroyWater : SpellAction {
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 (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
if (IXplatAbstractions.INSTANCE.isBreakingAllowed(
ctx.world,
here,
blockstate,
ctx.caster
)
successes++
for (dir in Direction.values()) {
todo.add(here.relative(dir))
) {
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(
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

@ -9,6 +9,7 @@ import at.petrak.hexcasting.api.spell.getBlockPos
import at.petrak.hexcasting.api.spell.iota.Iota
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
@ -34,7 +35,9 @@ object OpEdifySapling : SpellAction {
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

@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.spell.*
import at.petrak.hexcasting.api.spell.casting.CastingContext
import at.petrak.hexcasting.api.spell.iota.Iota
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
@ -55,53 +56,56 @@ object OpExtinguish : SpellAction {
) {
// 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

@ -9,6 +9,7 @@ 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.ktxt.UseOnContext
import at.petrak.hexcasting.xplat.IXplatAbstractions
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.InteractionHand
@ -36,12 +37,13 @@ object OpIgnite : SpellAction {
private data class Spell(val pos: BlockPos) : RenderedSpell {
override fun cast(ctx: CastingContext) {
// TODO should we do these checks in the action part of the spell
if (!ctx.world.mayInteract(ctx.caster, pos))
return
// steal petra code that steals bucket code
val maxwell = Items.FIRE_CHARGE
// TODO should we do these checks in the action part of the spell
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(

View file

@ -97,7 +97,7 @@ tasks.withType(JavaCompile) {
source(project(":Common").sourceSets.main.allSource)
}
compileKotlin {
source(project(":Common").sourceSets.main.allSource)
source(project(":Common").sourceSets.main.kotlin)
}
sourcesJar {

View file

@ -155,10 +155,10 @@ tasks.withType(JavaCompile) {
source(project(":Common").sourceSets.main.allSource)
}
compileKotlin {
source(project(":Common").sourceSets.main.allSource)
source(project(":Common").sourceSets.main.kotlin)
}
compileTestKotlin {
source(project(":Common").sourceSets.main.allSource)
source(project(":Common").sourceSets.main.kotlin)
}