From 34e6332c8b58a62fed7e22388dec8b4f5d595799 Mon Sep 17 00:00:00 2001 From: lafkpages Date: Fri, 5 Jul 2024 14:24:05 +0200 Subject: [PATCH] feat: initial commit with basic Javalin server --- .gitattributes | 9 + build.gradle | 102 +++++----- gradle.properties | 8 +- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew | 2 +- gradlew.bat | 184 +++++++++--------- src/main/java/com/replmc/ReplCraft.java | 59 ++++++ src/main/java/com/replmc/WebsocketServer.java | 20 ++ .../java/com/replmc/mixin/ExampleMixin.java | 15 ++ src/main/resources/assets/replcraft/icon.png | Bin 0 -> 634 bytes src/main/resources/fabric.mod.json | 14 +- src/main/resources/replcraft.mixins.json | 11 ++ 12 files changed, 271 insertions(+), 155 deletions(-) create mode 100644 .gitattributes create mode 100644 src/main/java/com/replmc/ReplCraft.java create mode 100644 src/main/java/com/replmc/WebsocketServer.java create mode 100644 src/main/java/com/replmc/mixin/ExampleMixin.java create mode 100644 src/main/resources/assets/replcraft/icon.png create mode 100644 src/main/resources/replcraft.mixins.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..097f9f9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux start script should use lf +/gradlew text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf + diff --git a/build.gradle b/build.gradle index 8446969..524d6bf 100644 --- a/build.gradle +++ b/build.gradle @@ -1,88 +1,90 @@ plugins { - id 'fabric-loom' version '1.6-SNAPSHOT' - id 'maven-publish' + id 'fabric-loom' version '1.7-SNAPSHOT' + id 'maven-publish' } version = project.mod_version group = project.maven_group base { - archivesName = project.archives_base_name + archivesName = project.archives_base_name } repositories { - // Add repositories to retrieve artifacts from in here. - // You should only use this when depending on other mods because - // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. - // See https://docs.gradle.org/current/userguide/declaring_repositories.html - // for more information about repositories. + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. + mavenCentral() } loom { splitEnvironmentSourceSets() - mods { - "modid" { - sourceSet sourceSets.main - sourceSet sourceSets.client - } - } + mods { + "replcraft" { + sourceSet sourceSets.main + sourceSet sourceSets.client + } + } } dependencies { - // To change the versions see the gradle.properties file - minecraft "com.mojang:minecraft:${project.minecraft_version}" - mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" - modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - - // Fabric API. This is technically optional, but you probably want it anyway. - modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + // Fabric API. This is technically optional, but you probably want it anyway. + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + implementation "io.javalin:javalin:6.1.3" } processResources { - inputs.property "version", project.version + inputs.property "version", project.version - filesMatching("fabric.mod.json") { - expand "version": project.version - } + filesMatching("fabric.mod.json") { + expand "version": project.version + } } tasks.withType(JavaCompile).configureEach { - it.options.release = 21 + it.options.release = 21 } java { - // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task - // if it is present. - // If you remove this line, sources will not be generated. - withSourcesJar() + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() - sourceCompatibility = JavaVersion.VERSION_21 - targetCompatibility = JavaVersion.VERSION_21 + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 } jar { - from("LICENSE") { - rename { "${it}_${project.base.archivesName.get()}"} - } + from("LICENSE") { + rename { "${it}_${project.base.archivesName.get()}" } + } } // configure the maven publication publishing { - publications { - create("mavenJava", MavenPublication) { - artifactId = project.archives_base_name - from components.java - } - } - - // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. - repositories { - // Add repositories to publish to here. - // Notice: This block does NOT have the same function as the block in the top level. - // The repositories here will be used for publishing your artifact, not for - // retrieving dependencies. - } + publications { + create("mavenJava", MavenPublication) { + artifactId = project.archives_base_name + from components.java + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index c129da3..fc894a7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,13 +5,13 @@ org.gradle.parallel=true # Fabric Properties # check these on https://fabricmc.net/develop minecraft_version=1.21 -yarn_mappings=1.21+build.1 +yarn_mappings=1.21+build.7 loader_version=0.15.11 # Mod Properties mod_version=1.0.0 -maven_group=com.example -archives_base_name=modid +maven_group=com.replmc +archives_base_name=replcraft # Dependencies -fabric_version=0.100.1+1.21 \ No newline at end of file +fabric_version=0.100.4+1.21 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b82aa23..a441313 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index 1aa94a4..b740cf1 100755 --- a/gradlew +++ b/gradlew @@ -55,7 +55,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. diff --git a/gradlew.bat b/gradlew.bat index 7101f8e..25da30d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,92 +1,92 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/com/replmc/ReplCraft.java b/src/main/java/com/replmc/ReplCraft.java new file mode 100644 index 0000000..9ac6566 --- /dev/null +++ b/src/main/java/com/replmc/ReplCraft.java @@ -0,0 +1,59 @@ +package com.replmc; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.fabricmc.fabric.api.event.player.UseBlockCallback; +import net.minecraft.block.Blocks; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReplCraft implements ModInitializer { + // This logger is used to write text to the console and the log file. + // It is considered best practice to use your mod id as the logger's name. + // That way, it's clear which mod wrote info, warnings, and errors. + public static final String MOD_ID = "replcraft"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + + private WebsocketServer websocketServer = null; + + @Override + public void onInitialize() { + // This code runs as soon as Minecraft is in a mod-load-ready state. + // However, some things (like resources) may still be uninitialized. + // Proceed with mild caution. + + LOGGER.info("Hello Fabric world!"); + + CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> dispatcher.register(CommandManager.literal("replcraft").executes(context -> { + ServerCommandSource source = context.getSource(); + + ServerPlayerEntity player = source.getPlayerOrThrow(); + + player.getWorld().setBlockState(player.getBlockPos(), Blocks.IRON_BLOCK.getDefaultState()); + + source.sendFeedback(() -> Text.literal("Hello from /replcraft, " + player.getName().getString() + "!"), false); + return 1; + })))); + + websocketServer = new WebsocketServer(); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + LOGGER.info("Cleanup"); + + websocketServer.shutdown(); + })); + + UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> { + LOGGER.info("Block interacted by" + player.getName()); + + return ActionResult.PASS; + }); + } + + +} diff --git a/src/main/java/com/replmc/WebsocketServer.java b/src/main/java/com/replmc/WebsocketServer.java new file mode 100644 index 0000000..ada6246 --- /dev/null +++ b/src/main/java/com/replmc/WebsocketServer.java @@ -0,0 +1,20 @@ +package com.replmc; + + +import io.javalin.Javalin; + +public class WebsocketServer { + private final Javalin app; + + public WebsocketServer() { + app = Javalin.create(); + + app.get("/", ctx -> ctx.result("Hello from ReplCraft")); + + app.start(4680); + } + + public void shutdown() { + this.app.stop(); + } +} diff --git a/src/main/java/com/replmc/mixin/ExampleMixin.java b/src/main/java/com/replmc/mixin/ExampleMixin.java new file mode 100644 index 0000000..d635cf3 --- /dev/null +++ b/src/main/java/com/replmc/mixin/ExampleMixin.java @@ -0,0 +1,15 @@ +package com.replmc.mixin; + +import net.minecraft.server.MinecraftServer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(MinecraftServer.class) +public class ExampleMixin { + @Inject(at = @At("HEAD"), method = "loadWorld") + private void init(CallbackInfo info) { + // This code is injected into the start of MinecraftServer.loadWorld()V + } +} \ No newline at end of file diff --git a/src/main/resources/assets/replcraft/icon.png b/src/main/resources/assets/replcraft/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..0f1651e693d86fbdae3f8ac2e9b2d5ba0678d927 GIT binary patch literal 634 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7xzrU~=?yaSW-5dwX{w?;!`7){j#p z6*4@;6MD4R93BO#h)Nt{&|q<#q@cxcluc!gL(pL{i$ikX>!eDb%-{We-}_r(uWwiX z+QY}b_5lp3`+o?VLiGV&haY_D3_J}y6Xq1Y zVJKs;Zjg20?OA)UkHNh``GDX9jsg~kgEKW^*d{QhFi1K;r~|bP2Yw4UEfCOP;c7%s zFTx6Lt8U;3JfYvXzJ%lIx-CG2N(+&uu}Ql{FTvG z0xT?yjt&C2sGoTWhsuQ~I;aS6x-j9RUN3up$TRZg>-MI@>~|O>81x#rA298}Lv@ua z1{jF&usRkLdF;d@dK;{_$db~ZT;H6#HeHt53u6)kC!KGCdk?)n?GH; b{-6Dj>!DAg4=;KH(-ecJtDnm{r-UW|jONMm literal 0 HcmV?d00001 diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1429df8..9ef9018 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,8 +1,8 @@ { "schemaVersion": 1, - "id": "modid", + "id": "replcraft", "version": "${version}", - "name": "Example mod", + "name": "ReplCraft", "description": "This is an example description! Tell everyone what your mod is about!", "authors": [ "Me!" @@ -12,20 +12,20 @@ "sources": "https://github.com/FabricMC/fabric-example-mod" }, "license": "CC0-1.0", - "icon": "assets/modid/icon.png", + "icon": "assets/replcraft/icon.png", "environment": "*", "entrypoints": { "main": [ - "com.example.ExampleMod" + "com.replmc.ReplCraft" ], "client": [ - "com.example.ExampleModClient" + "com.replmc.ReplCraftClient" ] }, "mixins": [ - "modid.mixins.json", + "replcraft.mixins.json", { - "config": "modid.client.mixins.json", + "config": "replcraft.client.mixins.json", "environment": "client" } ], diff --git a/src/main/resources/replcraft.mixins.json b/src/main/resources/replcraft.mixins.json new file mode 100644 index 0000000..4ef2c15 --- /dev/null +++ b/src/main/resources/replcraft.mixins.json @@ -0,0 +1,11 @@ +{ + "required": true, + "package": "com.replmc.mixin", + "compatibilityLevel": "JAVA_21", + "mixins": [ + "ExampleMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file