From fca511beb6026c8117b412014d2fcd4ba1c8f757 Mon Sep 17 00:00:00 2001 From: DE_DZ_TBH Date: Thu, 18 Jul 2019 23:11:12 +0800 Subject: [PATCH] v0.1.1-alpha - improved DEMagicaModStuff - corrected version in mod file - applied kotlin language adapeter - added Config explosionDoAffectSelf --- README.md | 4 ++++ build.gradle | 2 +- src/main/kotlin/com/dedztbh/demagica/DEMagica.kt | 6 +++--- .../kotlin/com/dedztbh/demagica/global/Config.kt | 4 ++-- .../com/dedztbh/demagica/global/DEMagicaStuff.kt | 15 ++++++++++----- .../com/dedztbh/demagica/items/ItemMagicStick.kt | 4 +++- .../demagica/projectile/MagicBallHeavy.kt | 6 ++++-- .../demagica/projectile/MagicBallKatyusha.kt | 4 +++- .../com/dedztbh/demagica/projectile/MagicBomb.kt | 4 +++- .../com/dedztbh/demagica/proxy/CommonProxy.kt | 16 +++++++++------- .../kotlin/com/dedztbh/demagica/util/Helper.kt | 5 ++++- 11 files changed, 46 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 6a9b543..eab243f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ Useful Keybindings: - Z - teleport to y=350 in creative - B - binocular +Config: +- explosionDoAffectSelf: Explosion Do Affect Creator + + Localizations: - 简体中文 - English \ No newline at end of file diff --git a/build.gradle b/build.gradle index a6d8a0e..b8efdc1 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ allOpen { annotation("com.dedztbh.demagica.util.Open") } -version = "0.1-alpha" +version = "0.1.1-alpha" group = "com.dedztbh.demagica" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "demagica" diff --git a/src/main/kotlin/com/dedztbh/demagica/DEMagica.kt b/src/main/kotlin/com/dedztbh/demagica/DEMagica.kt index d9b5c38..eb536c7 100644 --- a/src/main/kotlin/com/dedztbh/demagica/DEMagica.kt +++ b/src/main/kotlin/com/dedztbh/demagica/DEMagica.kt @@ -12,12 +12,12 @@ import net.minecraftforge.fml.common.network.NetworkRegistry import org.apache.logging.log4j.Logger -@Mod(modid = DEMagica.MODID, name = DEMagica.NAME, version = DEMagica.VERSION) +@Mod(modid = DEMagica.MODID, name = DEMagica.NAME, version = DEMagica.VERSION, modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter") class DEMagica { companion object { const val MODID = "demagica" const val NAME = "DEMagica" - const val VERSION = "1.0" + const val VERSION = "0.1.1-alpha" @JvmStatic lateinit var logger: Logger @@ -48,7 +48,7 @@ class DEMagica { @Mod.EventHandler fun postInit(e: FMLPostInitializationEvent) { proxy.postInit(e) - logger.debug("Dalao:${Config.dalaofy}") + logger.debug("explosionDoAffectSelf:${Config.explosionDoAffectSelf}") } } diff --git a/src/main/kotlin/com/dedztbh/demagica/global/Config.kt b/src/main/kotlin/com/dedztbh/demagica/global/Config.kt index 84294cc..2a96329 100644 --- a/src/main/kotlin/com/dedztbh/demagica/global/Config.kt +++ b/src/main/kotlin/com/dedztbh/demagica/global/Config.kt @@ -7,7 +7,7 @@ import org.apache.logging.log4j.Level object Config { // This values below you can access elsewhere in your mod: - var dalaofy = true + var explosionDoAffectSelf = false private val CATEGORY_GENERAL = "general" private val CATEGORY_DIMENSIONS = "dimensions" @@ -31,7 +31,7 @@ object Config { private fun initGeneralConfig(cfg: Configuration) { cfg.addCustomCategoryComment(CATEGORY_GENERAL, "General configuration") - dalaofy = cfg.getBoolean("dalaofy", CATEGORY_GENERAL, dalaofy, "To Dalaofy") + explosionDoAffectSelf = cfg.getBoolean("explosionDoAffectSelf", CATEGORY_GENERAL, explosionDoAffectSelf, "Explosion Do Affect Creator") } private fun initDimensionConfig(cfg: Configuration) { diff --git a/src/main/kotlin/com/dedztbh/demagica/global/DEMagicaStuff.kt b/src/main/kotlin/com/dedztbh/demagica/global/DEMagicaStuff.kt index 6a6c1c0..373f594 100644 --- a/src/main/kotlin/com/dedztbh/demagica/global/DEMagicaStuff.kt +++ b/src/main/kotlin/com/dedztbh/demagica/global/DEMagicaStuff.kt @@ -4,6 +4,7 @@ import com.dedztbh.demagica.util.Open import net.minecraft.tileentity.TileEntity import net.minecraftforge.fml.relauncher.Side import net.minecraftforge.fml.relauncher.SideOnly +import kotlin.reflect.KClass /** * Created by DEDZTBH on 2019-07-15. @@ -25,20 +26,24 @@ typealias IDEMagicaItem = IDEMagicaStuff class DEMagicaModStuff { @Suppress("UNCHECKED_CAST") fun stuffOf(clazz: Class, vararg otherClazz: Class, newInstance: Boolean = false): List = - this::class.java.declaredMethods.filter { func -> + this::class.java.declaredFields.filter { field -> setOf(clazz, *otherClazz).all { - it.isAssignableFrom(func.returnType) + it.isAssignableFrom(field.type) } }.map { (if (newInstance) - it.returnType.newInstance() + it.type.newInstance() else - it.invoke(this)) as T + it.get(this)) as T } + fun stuffOf(clazz: KClass, vararg otherClazz: KClass, newInstance: Boolean = false): List = + stuffOf(clazz.java, *otherClazz.map { it.java }.toTypedArray(), newInstance = newInstance) + + @SideOnly(Side.CLIENT) fun initModels() { - this.stuffOf(IDEMagicaStuff::class.java).forEach { + this.stuffOf(IDEMagicaStuff::class).forEach { it.initModel() } } diff --git a/src/main/kotlin/com/dedztbh/demagica/items/ItemMagicStick.kt b/src/main/kotlin/com/dedztbh/demagica/items/ItemMagicStick.kt index 09b125c..f29cc80 100644 --- a/src/main/kotlin/com/dedztbh/demagica/items/ItemMagicStick.kt +++ b/src/main/kotlin/com/dedztbh/demagica/items/ItemMagicStick.kt @@ -2,12 +2,14 @@ package com.dedztbh.demagica.items import com.dedztbh.demagica.DEMagica import com.dedztbh.demagica.blocks.BlockMagic +import com.dedztbh.demagica.global.Config import com.dedztbh.demagica.global.IDEMagicaItem import com.dedztbh.demagica.global.ModItems import com.dedztbh.demagica.global.ServerTickOS import com.dedztbh.demagica.util.TickTaskManager import com.dedztbh.demagica.util.isLocal import com.dedztbh.demagica.util.nextPitch +import com.dedztbh.demagica.util.onlyIfNot import net.minecraft.client.renderer.block.model.ModelResourceLocation import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.player.EntityPlayer @@ -83,7 +85,7 @@ class ItemMagicStick : ItemSword(ToolMaterial.GOLD), IDEMagicaItem { taskManager.runTask(20, false) { target.apply { - world.createExplosion(attacker, posX, posY, posZ, 5f, true) + world.createExplosion(attacker onlyIfNot Config.explosionDoAffectSelf, posX, posY, posZ, 5f, true) world.playSound(attacker as? EntityPlayer, posX, posY, posZ, SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 10f, Random.nextPitch()) } } diff --git a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallHeavy.kt b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallHeavy.kt index 75d3f19..ba760f3 100644 --- a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallHeavy.kt +++ b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallHeavy.kt @@ -1,6 +1,8 @@ package com.dedztbh.demagica.projectile +import com.dedztbh.demagica.global.Config import com.dedztbh.demagica.util.isLocal +import com.dedztbh.demagica.util.onlyIfNot import net.minecraft.entity.player.EntityPlayer import net.minecraft.util.math.RayTraceResult import net.minecraft.world.World @@ -11,7 +13,7 @@ import net.minecraft.world.World * Project DEMagica */ -open class MagicBallHeavy : MagicBall { +class MagicBallHeavy : MagicBall { constructor(worldIn: World) : super(worldIn) @@ -26,7 +28,7 @@ open class MagicBallHeavy : MagicBall { override fun onHit(raytraceResultIn: RayTraceResult) { super.onHit(raytraceResultIn) if (world.isLocal) { - world.newExplosion(this, posX, posY, posZ, 1f, false, false) + world.newExplosion(thrower onlyIfNot Config.explosionDoAffectSelf, posX, posY, posZ, 1f, false, false) setDead() } } diff --git a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallKatyusha.kt b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallKatyusha.kt index adf3456..394c918 100644 --- a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallKatyusha.kt +++ b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBallKatyusha.kt @@ -1,6 +1,8 @@ package com.dedztbh.demagica.projectile +import com.dedztbh.demagica.global.Config import com.dedztbh.demagica.util.isLocal +import com.dedztbh.demagica.util.onlyIfNot import net.minecraft.entity.player.EntityPlayer import net.minecraft.init.SoundEvents import net.minecraft.util.SoundEvent @@ -24,7 +26,7 @@ open class MagicBallKatyusha : MagicBall { override fun onHit(raytraceResultIn: RayTraceResult) { if (world.isLocal) { - world.newExplosion(this, posX, posY, posZ, 3f, true, true) + world.newExplosion(thrower onlyIfNot Config.explosionDoAffectSelf, posX, posY, posZ, 3f, true, true) setDead() } } diff --git a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBomb.kt b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBomb.kt index 9714137..973eec1 100644 --- a/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBomb.kt +++ b/src/main/kotlin/com/dedztbh/demagica/projectile/MagicBomb.kt @@ -1,6 +1,8 @@ package com.dedztbh.demagica.projectile +import com.dedztbh.demagica.global.Config import com.dedztbh.demagica.util.isLocal +import com.dedztbh.demagica.util.onlyIfNot import net.minecraft.entity.player.EntityPlayer import net.minecraft.util.math.RayTraceResult import net.minecraft.world.World @@ -22,7 +24,7 @@ class MagicBomb : MagicBall { override fun onHit(raytraceResultIn: RayTraceResult) { super.onHit(raytraceResultIn) if (world.isLocal) { - world.newExplosion(this, posX, posY, posZ, 6f, true, true) + world.newExplosion(thrower onlyIfNot Config.explosionDoAffectSelf, posX, posY, posZ, 6f, true, true) setDead() } } diff --git a/src/main/kotlin/com/dedztbh/demagica/proxy/CommonProxy.kt b/src/main/kotlin/com/dedztbh/demagica/proxy/CommonProxy.kt index 37ece9c..cf64b08 100644 --- a/src/main/kotlin/com/dedztbh/demagica/proxy/CommonProxy.kt +++ b/src/main/kotlin/com/dedztbh/demagica/proxy/CommonProxy.kt @@ -6,6 +6,7 @@ import com.dedztbh.demagica.global.IDEMagicaBlock import com.dedztbh.demagica.global.ModBlocks import com.dedztbh.demagica.global.ModItems import com.dedztbh.demagica.projectile.* +import com.dedztbh.demagica.util.Open import com.dedztbh.demagica.util.then import net.minecraft.block.Block import net.minecraft.item.Item @@ -25,7 +26,8 @@ import java.io.File @Mod.EventBusSubscriber -open class CommonProxy { +@Open +class CommonProxy { companion object { // Config instance @JvmStatic @@ -34,7 +36,7 @@ open class CommonProxy { @JvmStatic @SubscribeEvent fun registerBlocks(event: RegistryEvent.Register) { - ModBlocks.stuffOf(Block::class.java, IDEMagicaBlock::class.java, newInstance = true).forEach { + ModBlocks.stuffOf(Block::class, IDEMagicaBlock::class, newInstance = true).forEach { event.registry.register(it) it.hasTileEntity(it.defaultState) then { it as IDEMagicaBlock @@ -46,17 +48,17 @@ open class CommonProxy { @JvmStatic @SubscribeEvent fun registerItems(event: RegistryEvent.Register) { - ModItems.stuffOf(Item::class.java, newInstance = true).forEach { + ModItems.stuffOf(Item::class, newInstance = true).forEach { event.registry.register(it) } - ModBlocks.stuffOf(Block::class.java).forEach { + ModBlocks.stuffOf(Block::class).forEach { event.registry.register(ItemBlock(it).setRegistryName(ModBlocks.blockMagic.registryName)) } } } - open fun preInit(e: FMLPreInitializationEvent) { + fun preInit(e: FMLPreInitializationEvent) { val directory = e.modConfigurationDirectory config = Configuration(File(directory.path, "demagica.cfg")) Config.readConfig() @@ -72,9 +74,9 @@ open class CommonProxy { RenderingRegistry.registerEntityRenderingHandler(MagicBallKatyusha::class.java, MagicBallRenderFactory()) } - open fun init(e: FMLInitializationEvent) {} + fun init(e: FMLInitializationEvent) {} - open fun postInit(e: FMLPostInitializationEvent) { + fun postInit(e: FMLPostInitializationEvent) { if (config.hasChanged()) { config.save() } diff --git a/src/main/kotlin/com/dedztbh/demagica/util/Helper.kt b/src/main/kotlin/com/dedztbh/demagica/util/Helper.kt index 2b3802c..39375a6 100644 --- a/src/main/kotlin/com/dedztbh/demagica/util/Helper.kt +++ b/src/main/kotlin/com/dedztbh/demagica/util/Helper.kt @@ -51,4 +51,7 @@ fun Int.toBool(): Boolean = this != 0 infix fun > T.orIfSmaller(b: T) = if (compareTo(b) > 0) b else this -fun nil(): T? = null \ No newline at end of file +fun nil(): T? = null + +infix fun T.onlyIf(cond: Boolean): T? = if (cond) this else null +infix fun T.onlyIfNot(cond: Boolean): T? = onlyIf(!cond) \ No newline at end of file