Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading Scala and fix ScalaLanguageAdapter's code to be Scala's style. #24

Open
wants to merge 18 commits into
base: scala3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
strategy:
matrix:
java: [16-jdk, 17-jdk]
java: [17-jdk]
runs-on: ubuntu-20.04
container:
image: openjdk:${{ matrix.java }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
!/LICENSE
!/README.md
!.github
!.scalafmt.conf
5 changes: 5 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version = "3.7.15"
runner.dialect = scala3
indent.defnSite = 4
indent.caseSite = 4
indent.main = 4
77 changes: 24 additions & 53 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
plugins {
id "java-library"
id "scala"
id "maven-publish"
id 'fabric-loom' version '0.10.30'
id "com.matthewprenger.cursegradle" version "1.4.0"
id 'java-library'
id 'scala'
id 'maven-publish'
id 'fabric-loom' version '1.5-SNAPSHOT'
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

archivesBaseName = project.archives_base_name
group = project.maven_group

version = mod_version
def ENV = System.getenv()
version = version + "+scala." + project.scala_version + (ENV.GITHUB_ACTIONS ? "" : ".local")
version = mod_version + "+scala." + project.scala_version + (System.getenv('GITHUB_ACTIONS') ? "" : ".local")

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

include(api("org.scala-lang:scala3-library_3:${project.scala_version}"))
// Depended on by scala3
include(api("org.scala-lang:scala-library:2.13.6"))
// Scala Dependencies
api(include("org.scala-lang:scala3-library_3:${project.scala_version}"))
// Dependent by Scala3
api(include("org.scala-lang:scala-library:2.13.12"))
}

processResources {
Expand All @@ -40,48 +37,25 @@ java {

publishing {
publications {
maven(MavenPublication) {
from components.java
mavenJava(MavenPublication) {
groupId = project.group
artifactId = project.name.toLowerCase()
version = project.version

from components.java
}
}
repositories {
repositories {
if (ENV.MAVEN_URL) {
maven {
url ENV.MAVEN_URL
credentials {
username ENV.MAVEN_USERNAME
password ENV.MAVEN_PASSWORD
}
repositories {
if (System.getenv('MAVEN_URL')) {
maven {
url System.getenv('MAVEN_URL')
credentials {
username System.getenv('MAVEN_USERNAME')
password System.getenv('MAVEN_PASSWORD')
}
}
}
}
}

curseforge {
if (ENV.CURSEFORGE_API_KEY) {
apiKey = ENV.CURSEFORGE_API_KEY
}

project {
id = '309275'
changelog = 'A changelog can be found at https://github.com/FabricMC/fabric-language-scala/commits/master'
releaseType = 'release'
addGameVersion '1.17.1'
addGameVersion '1.16.5'
addGameVersion '1.15.2'
addGameVersion '1.14.4'
addGameVersion "Fabric"

mainArtifact(file("${project.buildDir}/libs/${base.archivesBaseName}-${version}.jar")) {
displayName = "Fabric Language Scala $version"
}
}

options {
forgeGradleIntegration = false
}
}

// A task to ensure that the version being released has not already been released.
Expand All @@ -95,6 +69,3 @@ task checkVersion {
}
}
}

publish.mustRunAfter checkVersion
project.tasks.curseforge.mustRunAfter checkVersion
14 changes: 8 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.61
loader_version=0.11.7
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.3
loader_version=0.15.7

# Mod Properties
mod_version=2.0.0
mod_version=2.1.0
maven_group=net.fabricmc
archives_base_name=fabric-language-scala
description=Fabric language module for Scala
url=https://github.com/FabricMC/fabric-language-scala/
# Other
scala_version=3.0.2

# Scala
scala_version=3.3.3
6 changes: 4 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-rc-1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@

package net.fabricmc.language.scala


import net.fabricmc.language.scala.ScalaLanguageAdapter.logger
import net.fabricmc.loader.api.{LanguageAdapter, ModContainer}
import org.slf4j.LoggerFactory
import scala.util.{Failure, Success, Try}
import net.fabricmc.loader.api.LanguageAdapterException

class ScalaLanguageAdapter extends LanguageAdapter {

override def create[T](modContainer: ModContainer, s: String, aClass: Class[T]): T = {
try {
val objectClass = Class.forName(s + "$")
val moduleField = objectClass.getField("MODULE$")
val instance = moduleField.get(null).asInstanceOf[T]
if (instance == null) throw new NullPointerException
instance
} catch {
case _: Exception =>
println(s"Unable to find ${aClass.getName}$$MODULE$$")
aClass.getConstructor().newInstance()
override def create[T](
modContainer: ModContainer,
value: String,
typ: Class[T]
): T = {
// Load Object
val tried = Try {
val objectClass = Class.forName(value + "$")
val moduleField = objectClass.getField("MODULE$")
val instance = moduleField.get(null).asInstanceOf[T]
Option(instance).getOrElse(throw new NullPointerException)
}

tried match
case Success(instance) => return instance
case _ =>

logger.warn(
s"Unable to find ${value}.MODULE$$. Maybe it isn't a Scala object?"
)
Krysztal112233 marked this conversation as resolved.
Show resolved Hide resolved

// Try constructing Scala class
Try(typ.getConstructor().newInstance()) match
case Success(value) => value
case Failure(exception) => throw LanguageAdapterException(exception)
}
}
}

object ScalaLanguageAdapter {
private lazy val logger = LoggerFactory.getLogger("ScalaLanguageAdapter")
}
16 changes: 9 additions & 7 deletions src/test/scala/net/fabricmc/language/scala/TestMod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import net.fabricmc.api.ModInitializer
import net.minecraft.block.Blocks

object TestMod extends ModInitializer {

override def onInitialize(): Unit = {
System.out.println("**********************")
System.out.println("Hello from Scala!")
System.out.println("Hello " + Blocks.DIRT)
System.out.println("**********************")
}
override def onInitialize(): Unit = {
val message = s"""
|**********************
|Hello from Scala!
|Hello ${Blocks.DIRT}!
|**********************
|""".stripMargin
println(message)
}
}