place block is a bit safer now

This commit is contained in:
yrsegal@gmail.com 2022-04-23 13:26:22 -04:00
parent 6547047d11
commit 7dabb7ef09

View file

@ -48,39 +48,41 @@ object OpPlaceBlock : SpellOperator {
val placeeSlot = ctx.getOperativeSlot { it.item is BlockItem }
if (placeeSlot != null) {
val placeeStack = ctx.caster.inventory.getItem(placeeSlot).copy()
val placee = placeeStack.item as BlockItem
if (ctx.withdrawItem(placee, 1, false)) {
// https://github.com/VazkiiMods/Psi/blob/master/src/main/java/vazkii/psi/common/spell/trick/block/PieceTrickPlaceBlock.java#L143
val evt = BlockEvent.EntityPlaceEvent(
BlockSnapshot.create(ctx.world.dimension(), ctx.world, pos),
ctx.world.getBlockState(pos.above()),
ctx.caster
)
MinecraftForge.EVENT_BUS.post(evt)
// we temporarily give the player the stack, place it using mc code, then give them the old stack back.
val oldStack = ctx.caster.getItemInHand(ctx.castingHand)
val spoofedStack = placeeStack.copy()
spoofedStack.count = 1
ctx.caster.setItemInHand(ctx.castingHand, spoofedStack)
val blockHit = BlockHitResult(
Vec3.ZERO, ctx.caster.direction, pos, false
)
val itemUseCtx = UseOnContext(ctx.caster, ctx.castingHand, blockHit)
val res = spoofedStack.useOn(itemUseCtx)
ctx.caster.setItemInHand(ctx.castingHand, oldStack)
if (res != InteractionResult.FAIL) {
ctx.withdrawItem(placee, 1, true)
ctx.world.playSound(
ctx.caster,
vec.x, vec.y, vec.z, bstate.soundType.placeSound, SoundSource.BLOCKS, 1.0f,
1.0f + (Math.random() * 0.5 - 0.25).toFloat()
if (!placeeStack.isEmpty) {
val placee = placeeStack.item as BlockItem
if (ctx.withdrawItem(placee, 1, false)) {
// https://github.com/VazkiiMods/Psi/blob/master/src/main/java/vazkii/psi/common/spell/trick/block/PieceTrickPlaceBlock.java#L143
val evt = BlockEvent.EntityPlaceEvent(
BlockSnapshot.create(ctx.world.dimension(), ctx.world, pos),
ctx.world.getBlockState(pos.above()),
ctx.caster
)
val particle = BlockParticleOption(ParticleTypes.BLOCK, bstate)
ctx.world.sendParticles(particle, vec.x, vec.y, vec.z, 4, 0.1, 0.2, 0.1, 0.1)
MinecraftForge.EVENT_BUS.post(evt)
// we temporarily give the player the stack, place it using mc code, then give them the old stack back.
val oldStack = ctx.caster.getItemInHand(ctx.castingHand)
val spoofedStack = placeeStack.copy()
spoofedStack.count = 1
ctx.caster.setItemInHand(ctx.castingHand, spoofedStack)
val blockHit = BlockHitResult(
Vec3.ZERO, ctx.caster.direction, pos, false
)
val itemUseCtx = UseOnContext(ctx.caster, ctx.castingHand, blockHit)
val res = spoofedStack.useOn(itemUseCtx)
ctx.caster.setItemInHand(ctx.castingHand, oldStack)
if (res != InteractionResult.FAIL) {
ctx.withdrawItem(placee, 1, true)
ctx.world.playSound(
ctx.caster,
vec.x, vec.y, vec.z, bstate.soundType.placeSound, SoundSource.BLOCKS, 1.0f,
1.0f + (Math.random() * 0.5 - 0.25).toFloat()
)
val particle = BlockParticleOption(ParticleTypes.BLOCK, bstate)
ctx.world.sendParticles(particle, vec.x, vec.y, vec.z, 4, 0.1, 0.2, 0.1, 0.1)
}
}
}
}