-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cac7baa
commit 2238161
Showing
3 changed files
with
51 additions
and
15 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
nova/src/main/kotlin/xyz/xenondevs/nova/patch/impl/block/EarlyBlockPlaceEventPatch.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,45 @@ | ||
package xyz.xenondevs.nova.patch.impl.block | ||
|
||
import net.minecraft.world.item.BlockItem | ||
import net.minecraft.world.item.context.BlockPlaceContext | ||
import net.minecraft.world.level.block.state.BlockState | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.tree.MethodInsnNode | ||
import xyz.xenondevs.bytebase.jvm.VirtualClassPath | ||
import xyz.xenondevs.bytebase.util.calls | ||
import xyz.xenondevs.bytebase.util.replaceEvery | ||
import xyz.xenondevs.nova.LOGGER | ||
import xyz.xenondevs.nova.patch.MultiTransformer | ||
import xyz.xenondevs.nova.util.reflection.ReflectionUtils | ||
import xyz.xenondevs.nova.util.toNovaPos | ||
import xyz.xenondevs.nova.world.block.logic.place.BlockPlacing | ||
import java.util.logging.Level | ||
|
||
private val BLOCK_ITEM_PLACE_BLOCK = ReflectionUtils.getMethod(BlockItem::class, "placeBlock", BlockPlaceContext::class, BlockState::class) | ||
|
||
internal object EarlyBlockPlaceEventPatch : MultiTransformer(BlockItem::class) { | ||
|
||
override fun transform() { | ||
VirtualClassPath[BlockItem::place].replaceEvery( | ||
0, 0, | ||
{ invokeStatic(::placeBlock) } | ||
) { it.opcode == Opcodes.INVOKEVIRTUAL && (it as MethodInsnNode).calls(BLOCK_ITEM_PLACE_BLOCK) } | ||
dumpAll() | ||
} | ||
|
||
@JvmStatic | ||
fun placeBlock(blockItem: BlockItem, context: BlockPlaceContext, state: BlockState): Boolean { | ||
try { | ||
val pos = context.clickedPos.toNovaPos(context.level.world) | ||
if (!BlockPlacing.handleBlockPlace(pos)) { | ||
context.player?.containerMenu?.sendAllDataToRemote() | ||
return false | ||
} | ||
} catch(t: Throwable) { | ||
LOGGER.log(Level.SEVERE, "An exception occurred while handling early block place event", t) | ||
} | ||
|
||
return BLOCK_ITEM_PLACE_BLOCK.invoke(blockItem, context, state) as Boolean | ||
} | ||
|
||
} |
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