This commit is contained in:
Shadowfacts 2016-11-14 10:53:45 -05:00
parent dccdea16ce
commit 144c4468ee
10 changed files with 24 additions and 127 deletions

View File

@ -1,10 +1,9 @@
# Forgelin
**WARNING:** This project is no longer maintained. It has been superseded by Kotlin integration in [ShadowMC](https://github.com/shadowfacts/ShadowMC).
Fork of [Emberwalker's Forgelin](https://github.com/Emberwalker/Forgelin) with some sprinkles on top.
Fork of [Emberwalker's Forgelin](https://github.com/Emberwalker/Forgelin).
## Additions
- Extensions for Minecraft/Forge code. See them in the [extensions package](https://github.com/shadowfacts/Forgelin/tree/master/src/main/kotlin/net/shadowfacts/forgelin/extensions/).
- Shades the Kotlin standard library, runtime, and reflect libraries so you don't have to.
- Provides a Forge `ILanguageAdapter` for using Kotlin `object` classes as your main mod class.
## Usage
```groovy

View File

@ -1,4 +1,4 @@
mod_version = 1.0.5
mod_version = 1.1.0
group = net.shadowfacts
archivesBaseName = Forgelin
@ -6,4 +6,4 @@ mc_version = 1.10.2
mcp_mappings = snapshot_20160802
forge_version = 12.18.1.2045
kotlin_version = 1.0.3
kotlin_version = 1.0.5

View File

@ -9,64 +9,59 @@ import java.lang.reflect.Field
import java.lang.reflect.Method
/**
* Kotlin implementation of FML's ILanguageAdapter.
*
* Use by setting <pre>modLanguageAdapter = "io.drakon.forgelinFR.KotlinAdapter"</pre> in the Mod annotation
* (Forge 1.8-11.14.1.1371 or above required).
*
* @author Arkan <arkan@drakon.io>
* Forge {@link ILanguageAdapter} for Kotlin
* Usage: Set the {@code modLanguageAdapter} field in your {@code @Mod} annotation to {@code net.shadowfacts.forgelin.KotlinAdapter}
* @author shadowfacts
*/
class KotlinAdapter : ILanguageAdapter {
private val log = LogManager.getLogger("ILanguageAdapter/Kotlin")
private val log = LogManager.getLogger("KotlinAdapter")
override fun supportsStatics(): Boolean {
return false
}
override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) {
log.debug("Setting proxy: {}.{} -> {}", target.declaringClass.simpleName, target.name, proxy)
if (proxyTarget.fields.any { x -> x.name.equals("INSTANCE") }) {
log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $proxy")
if (proxyTarget.fields.any { x -> x.name == "INSTANCE" }) {
// Singleton
try {
log.debug("Setting proxy on INSTANCE; singleton target.")
log.debug("Setting proxy on INSTANCE; singleton target")
val obj = proxyTarget.getField("INSTANCE").get(null)
target.set(obj, proxy)
} catch (ex: Exception) {
throw KotlinAdapterException(ex)
} catch (e: Exception) {
throw KotlinAdapterException(e)
}
} else {
//TODO Log?
target.set(proxyTarget, proxy)
}
}
override fun getNewInstance(container: FMLModContainer?, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any? {
log.debug("FML has asked for {} to be constructed...", objectClass.simpleName)
override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any {
log.debug("FML has asked for ${objectClass.simpleName} to be constructed")
try {
// Try looking for an object type
val f = objectClass.getField("INSTANCE")
val obj = f.get(null) ?: throw NullPointerException()
log.debug("Found an object INSTANCE reference in {}, using that. ({})", objectClass.simpleName, obj)
log.debug("Found an object INSTANCE reference in ${objectClass.simpleName}, using that. ${obj}")
return obj
} catch (ex: Exception) {
} catch (e: Exception) {
// Try looking for a class type
log.debug("Failed to get object reference, trying class construction.")
log.debug("Failed to get object reference, trying class construction")
try {
val obj = objectClass.newInstance() ?: throw NullPointerException()
log.debug("Constructed an object from a class type ({}), using that. ({})", objectClass, obj)
log.warn("Hey, you, modder who owns {} - you should be using 'object' instead of 'class' on your @Mod class.", objectClass.simpleName)
log.debug("Constructed an object from a class type ($objectClass), using that. $obj")
return obj
} catch (ex: Exception) {
throw KotlinAdapterException(ex)
} catch (e: Exception) {
throw KotlinAdapterException(e)
}
}
}
override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) {
// Nothing to do; FML's got this covered for Kotlin.
// Nothing to do; FML's got this covered for Kotlin
}
private class KotlinAdapterException(ex: Exception) : RuntimeException("Kotlin adapter error - do not report to Forge!", ex)
private class KotlinAdapterException(e: Exception) : RuntimeException("Kotlin adapter error - do not report to Forge!", e)
}

View File

@ -1,19 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraft.util.EnumFacing
import net.minecraft.util.math.AxisAlignedBB
/**
* @author shadowfacts
*/
fun AxisAlignedBB.rotateFace(side: EnumFacing): AxisAlignedBB {
when (side) {
EnumFacing.DOWN -> return this
EnumFacing.UP -> return AxisAlignedBB(minX, 1 - maxY, minZ, maxX, 1 - minY, maxZ)
EnumFacing.NORTH -> return AxisAlignedBB(minX, minZ, minY, maxX, maxZ, maxY)
EnumFacing.SOUTH -> return AxisAlignedBB(minX, minZ, 1 - maxY, maxX, maxZ, 1 - minY)
EnumFacing.WEST -> return AxisAlignedBB(minY, minZ, minX, maxY, maxZ, maxX)
EnumFacing.EAST -> return AxisAlignedBB(1 - maxY, minZ, minX, 1 - minY, maxZ, maxX)
else -> return this
}
}

View File

@ -1,10 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraftforge.common.config.Configuration
/**
* @author shadowfacts
*/
fun Configuration.getLong(name: String, category: String, defaultInt: Int, defaultLong: Long, minValue: Int, maxValue: Int, comment: String): Long {
return get(category, name, defaultInt, comment, minValue, maxValue).getLong(defaultLong)
}

View File

@ -1,14 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraft.entity.Entity
import net.minecraft.util.math.RayTraceResult
/**
* @author shadowfacts
*/
fun Entity.rayTrace(distance: Double): RayTraceResult? {
val eyePos = getPositionEyes(0f)
val lookVec = getLook(0f)
val vec = eyePos.addVector(lookVec.xCoord * distance, lookVec.yCoord * distance, lookVec.zCoord * distance)
return worldObj.rayTraceBlocks(eyePos, vec, false, false, true)
}

View File

@ -1,15 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraft.item.ItemStack
/**
* @author shadowfacts
*/
fun List<ItemStack>.containsStack(stack: ItemStack): Boolean {
forEach {
if (it.item == stack.item && it.itemDamage == stack.itemDamage) {
return true
}
}
return false
}

View File

@ -1,13 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraft.nbt.NBTBase
import net.minecraft.nbt.NBTTagList
/**
* @author shadowfacts
*/
fun NBTTagList.forEach(action: (NBTBase) -> Unit) {
for (i in 0.until(tagCount())) {
action(get(i))
}
}

View File

@ -1,15 +0,0 @@
package net.shadowfacts.forgelin.extensions
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.util.text.TextComponentString
/**
* @author shadowfacts
*/
fun EntityPlayer.addChatMsg(msg: String) {
addChatComponentMessage(TextComponentString(msg))
}
fun EntityPlayer.addChatMsg(msg: String, vararg params: Any) {
addChatMsg(String.format(msg, params))
}

View File

@ -1,11 +0,0 @@
package net.shadowfacts.forgelin.extensions.client
import net.minecraft.client.settings.GameSettings
import net.minecraft.client.settings.KeyBinding
/**
* @author shadowfacts
*/
fun KeyBinding.getDisplayString(): String {
return GameSettings.getKeyDisplayString(keyCode)
}