mirror of
https://github.com/chylex/Hardcore-Ender-Expansion-2.git
synced 2025-04-11 03:15:44 +02:00
Replace Chorus Flower override with a transformer
This commit is contained in:
parent
efb295e343
commit
43fa25ace3
src/main/java/chylex/hee
game/block
init
system/core
@ -1,78 +0,0 @@
|
||||
package chylex.hee.game.block
|
||||
import chylex.hee.init.ModBlocks
|
||||
import chylex.hee.system.util.FLAG_NONE
|
||||
import chylex.hee.system.util.Facing4
|
||||
import chylex.hee.system.util.getBlock
|
||||
import chylex.hee.system.util.isAir
|
||||
import chylex.hee.system.util.setBlock
|
||||
import chylex.hee.system.util.translationKeyOriginal
|
||||
import net.minecraft.block.BlockChorusFlower
|
||||
import net.minecraft.init.Blocks
|
||||
import net.minecraft.util.math.BlockPos
|
||||
import net.minecraft.world.World
|
||||
import net.minecraftforge.common.MinecraftForge
|
||||
import net.minecraftforge.event.world.BlockEvent.CropGrowEvent
|
||||
import net.minecraftforge.fml.common.eventhandler.EventPriority.LOWEST
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
|
||||
|
||||
class BlockChorusFlowerOverride : BlockChorusFlower(){
|
||||
init{
|
||||
val source = Blocks.CHORUS_FLOWER
|
||||
|
||||
setHardness(source.getBlockHardness(null, null, null)) // UPDATE
|
||||
soundType = source.blockSoundType
|
||||
translationKey = source.translationKeyOriginal
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this)
|
||||
}
|
||||
|
||||
override fun canSurvive(world: World, pos: BlockPos): Boolean{
|
||||
val supportPos = pos.down()
|
||||
val supportBlock = supportPos.getBlock(world)
|
||||
|
||||
if (supportBlock === Blocks.CHORUS_PLANT || supportBlock === ModBlocks.HUMUS){
|
||||
return true
|
||||
}
|
||||
else if (!supportPos.isAir(world)){
|
||||
return false
|
||||
}
|
||||
else{
|
||||
val adjacentPlants = Facing4.map(pos::offset).sumBy {
|
||||
when{
|
||||
it.getBlock(world) === Blocks.CHORUS_PLANT -> 1
|
||||
it.isAir(world) -> 0
|
||||
else -> return false
|
||||
}
|
||||
}
|
||||
|
||||
return adjacentPlants == 1
|
||||
}
|
||||
}
|
||||
|
||||
// Silently replace Humus below the flower with End Stone to bypass super.updateTick checks
|
||||
// UPDATE: replace this with ASM, probably
|
||||
|
||||
@SubscribeEvent(priority = LOWEST)
|
||||
fun onCropGrowPre(e: CropGrowEvent.Pre){
|
||||
if (e.state.block === this){
|
||||
val world = e.world
|
||||
val supportingPos = e.pos.down(2) // the Pre event uses block above the actual plant
|
||||
|
||||
if (supportingPos.getBlock(world) === ModBlocks.HUMUS){
|
||||
supportingPos.setBlock(world, Blocks.END_STONE, FLAG_NONE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent(receiveCanceled = true) // TODO this will still not trigger if Pre is canceled, which is a serious issue but not during dev
|
||||
fun onCropGrowPost(e: CropGrowEvent.Post){
|
||||
if (e.originalState.block === this){
|
||||
val world = e.world
|
||||
val supportingPos = e.pos.down()
|
||||
|
||||
if (supportingPos.getBlock(world) === Blocks.END_STONE){
|
||||
supportingPos.setBlock(world, ModBlocks.HUMUS, FLAG_NONE)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package chylex.hee.init
|
||||
import chylex.hee.HEE
|
||||
import chylex.hee.game.block.BlockAncientCobweb
|
||||
import chylex.hee.game.block.BlockBrewingStandOverride
|
||||
import chylex.hee.game.block.BlockChorusFlowerOverride
|
||||
import chylex.hee.game.block.BlockChorusPlantOverride
|
||||
import chylex.hee.game.block.BlockCorruptedEnergy
|
||||
import chylex.hee.game.block.BlockDarkChest
|
||||
@ -405,7 +404,6 @@ object ModBlocks{
|
||||
with(e.registry){
|
||||
register(BlockEndPortalOverride().apply { override(Blocks.END_PORTAL, newCreativeTab = null) })
|
||||
register(BlockBrewingStandOverride(buildBrewingStand).apply { override(Blocks.BREWING_STAND) })
|
||||
register(BlockChorusFlowerOverride().apply { override(Blocks.CHORUS_FLOWER) })
|
||||
register(BlockChorusPlantOverride().apply { override(Blocks.CHORUS_PLANT) })
|
||||
register(BlockDragonEggOverride().apply { override(Blocks.DRAGON_EGG) } with ::ItemDragonEgg)
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
package chylex.hee.system.core
|
||||
import chylex.hee.system.core.transformers.TransformBlockChorusFlower
|
||||
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin
|
||||
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.Name
|
||||
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.TransformerExclusions
|
||||
@ -6,7 +7,9 @@ import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.TransformerExclusions
|
||||
@Name("Hardcore Ender Expansion Coremod")
|
||||
@TransformerExclusions("chylex.hee.system.core", "kotlin.")
|
||||
class CoremodPlugin : IFMLLoadingPlugin{
|
||||
override fun getASMTransformerClass() = emptyArray<String>()
|
||||
override fun getASMTransformerClass() = arrayOf(
|
||||
TransformBlockChorusFlower::class.java.name
|
||||
)
|
||||
|
||||
override fun getAccessTransformerClass() = null
|
||||
override fun getSetupClass(): String? = null
|
||||
|
@ -0,0 +1,43 @@
|
||||
package chylex.hee.system.core.transformers
|
||||
import chylex.hee.system.core.ICoremodTransformer
|
||||
import chylex.hee.system.core.fieldMatches
|
||||
import chylex.hee.system.core.findMethod
|
||||
import org.objectweb.asm.Opcodes.GETSTATIC
|
||||
import org.objectweb.asm.tree.ClassNode
|
||||
import org.objectweb.asm.tree.FieldInsnNode
|
||||
import org.objectweb.asm.tree.MethodNode
|
||||
|
||||
class TransformBlockChorusFlower : ICoremodTransformer{
|
||||
override val targetClass = "net.minecraft.block.BlockChorusFlower"
|
||||
|
||||
override fun process(node: ClassNode){
|
||||
val updateTick = node.findMethod(
|
||||
"b", "(Lamu;Let;Lawt;Ljava/util/Random;)V",
|
||||
"updateTick", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/state/IBlockState;Ljava/util/Random;)V"
|
||||
)
|
||||
|
||||
val canSurvive = node.findMethod(
|
||||
"b", "(Lamu;Let;)Z",
|
||||
"canSurvive", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)Z"
|
||||
)
|
||||
|
||||
replaceEndStoneGetters(updateTick, 2)
|
||||
replaceEndStoneGetters(canSurvive, 1)
|
||||
}
|
||||
|
||||
private fun replaceEndStoneGetters(method: MethodNode, replacementCount: Int){
|
||||
val iterator = method.instructions.iterator()
|
||||
var replaced = 0
|
||||
|
||||
for(insn in iterator){
|
||||
if (insn.opcode == GETSTATIC && insn.fieldMatches("aox", "bH", "net/minecraft/init/Blocks", "END_STONE")){
|
||||
iterator.set(FieldInsnNode(insn.opcode, "chylex/hee/init/ModBlocks", "HUMUS", "Lchylex/hee/game/block/BlockHumus;"))
|
||||
++replaced
|
||||
}
|
||||
}
|
||||
|
||||
if (replaced != replacementCount){
|
||||
throw IllegalStateException("expected to do $replacementCount instruction replacement(s), did $replaced instead")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user