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/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b01da52 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,37 @@ +# Automatically build the project and run any configured tests for every push +# and submitted pull request. This can help catch issues that only occur on +# certain platforms or Java versions, and provides a first line of defence +# against bad commits. + +name: build +on: [pull_request, push] + +jobs: + build: + strategy: + matrix: + # Use these Java versions + java: [ + 21, # Current Java LTS + ] + runs-on: ubuntu-22.04 + steps: + - name: checkout repository + uses: actions/checkout@v4 + - name: validate gradle wrapper + uses: gradle/wrapper-validation-action@v2 + - name: setup jdk ${{ matrix.java }} + uses: actions/setup-java@v4 + with: + java-version: ${{ matrix.java }} + distribution: 'microsoft' + - name: make gradle wrapper executable + run: chmod +x ./gradlew + - name: build + run: ./gradlew build + - name: capture build artifacts + if: ${{ matrix.java == '21' }} # Only upload artifacts built from latest java + uses: actions/upload-artifact@v4 + with: + name: Artifacts + path: build/libs/ \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c476faf --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +# gradle + +.gradle/ +build/ +out/ +classes/ + +# eclipse + +*.launch + +# idea + +.idea/ +*.iml +*.ipr +*.iws + +# vscode + +.settings/ +.vscode/ +bin/ +.classpath +.project + +# macos + +*.DS_Store + +# fabric + +run/ + +# java + +hs_err_*.log +replay_*.log +*.hprof +*.jfr diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..5383d23 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,73 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + java + id("fabric-loom") version "1.6-SNAPSHOT" + id("maven-publish") + id("com.github.johnrengelman.shadow") version "8.1.1" + id("org.jetbrains.kotlin.jvm") version "2.0.0" +} + +version = rootProject.property("mod_version").toString() +group = rootProject.property("maven_group").toString() + +repositories { + mavenCentral() + + maven("https://pkgs.dev.azure.com/djtheredstoner/DevAuth/_packaging/public/maven/v1") + maven("https://maven.notenoughupdates.org/releases/") +} + +val shadowImpl: Configuration by configurations.creating { + configurations.implementation.get().extendsFrom(this) +} + +val shadowModImpl: Configuration by configurations.creating { + configurations.modImplementation.get().extendsFrom(this) +} + +dependencies { + // To change the versions see the gradle.properties file + "minecraft"("com.mojang:minecraft:${rootProject.property("minecraft_version")}") + "mappings"("net.fabricmc:yarn:${rootProject.property("yarn_mappings")}:v2") + modImplementation("net.fabricmc:fabric-loader:${rootProject.property("loader_version")}") + + modApi("net.fabricmc.fabric-api:fabric-api:${rootProject.property("fabric_version")}") + modImplementation("net.fabricmc:fabric-language-kotlin:${rootProject.property("fabric_kotlin_version")}") + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + + shadowImpl("com.github.kwhat:jnativehook:2.2.2") + shadowModImpl("org.notenoughupdates.moulconfig:modern:3.0.0-beta.11") +} + +loom { + runs { + named("client") { + property("devauth.enabled", "true") + property("mixin.debug", "true") + } + } +} + +tasks.shadowJar { + configurations = listOf(shadowModImpl) + relocate("io.github.notenoughupdates.moulconfig", "dev.vixid.vsm.deps.moulconfig") +} + +tasks.processResources { + inputs.property("version", project.version) + + filesMatching("fabric.mod.json") { + expand(inputs.properties) + } +} + +val compileKotlin: KotlinCompile by tasks +compileKotlin.kotlinOptions { + jvmTarget = "21" +} + +java { + withSourcesJar() + toolchain.languageVersion.set(JavaLanguageVersion.of(21)) +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..553b443 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,18 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G +org.gradle.parallel=true + +# Fabric Properties +# check these on https://fabricmc.net/develop +minecraft_version=1.20.6 +yarn_mappings=1.20.6+build.3 +loader_version=0.15.11 +fabric_kotlin_version=1.11.0+kotlin.2.0.0 + +# Mod Properties +mod_version=1.0.0 +maven_group=dev.vixid.vsm +archives_base_name=vsm + +# Dependencies +fabric_version=0.99.4+1.20.6 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e644113 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..b82aa23 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1aa94a4 --- /dev/null +++ b/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# 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 +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..25da30d --- /dev/null +++ b/gradlew.bat @@ -0,0 +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 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..75c4d72 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,10 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + mavenCentral() + gradlePluginPortal() + } +} \ No newline at end of file diff --git a/src/main/java/dev/vixid/vsm/config/SpotifyConfig.java b/src/main/java/dev/vixid/vsm/config/SpotifyConfig.java new file mode 100644 index 0000000..7583a07 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/SpotifyConfig.java @@ -0,0 +1,39 @@ +package dev.vixid.vsm.config; + +import com.google.gson.annotations.Expose; +import dev.vixid.vsm.config.core.Position; +import dev.vixid.vsm.config.core.annotations.ConfigEditorKeybind; +import dev.vixid.vsm.overlays.OverlayPositions; +import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean; +import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorButton; +import io.github.notenoughupdates.moulconfig.annotations.ConfigOption; + +public class SpotifyConfig { + + @Expose + @ConfigOption(name = "Enable", desc = "Enable Spotify Overlay") + @ConfigEditorBoolean + public boolean enabled = true; + + @Expose + public Position overlayPosition = new Position(10, 10); + + @Expose + @ConfigOption(name = "Skip Forward", desc = "Key bind to skip forward a song") + @ConfigEditorKeybind(defaultKey = "key.keyboard.unknown") + public String skipForwardKey = "key.keyboard.unknown"; + + @Expose + @ConfigOption(name = "Skip Backward", desc = "Key bind to skip backward a song") + @ConfigEditorKeybind(defaultKey = "key.keyboard.unknown") + public String skipBackwardKey = "key.keyboard.unknown"; + + @Expose + @ConfigOption(name = "Play / Pause", desc = "Key bind to play / pause a song") + @ConfigEditorKeybind(defaultKey = "key.keyboard.unknown") + public String playPauseKey = "key.keyboard.unknown"; + + @ConfigOption(name = "Edit Song Location", desc = "Change the position of the song overlay") + @ConfigEditorButton(buttonText = "Edit") + public Runnable editPosition = OverlayPositions.INSTANCE::openPositionEditor; +} diff --git a/src/main/java/dev/vixid/vsm/config/VSMConfig.java b/src/main/java/dev/vixid/vsm/config/VSMConfig.java new file mode 100644 index 0000000..bbaa26e --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/VSMConfig.java @@ -0,0 +1,59 @@ +package dev.vixid.vsm.config; + +import com.google.gson.annotations.Expose; +import com.mojang.brigadier.CommandDispatcher; +import dev.vixid.vsm.VSM; +import dev.vixid.vsm.utils.ChatUtils; +import io.github.notenoughupdates.moulconfig.Config; +import io.github.notenoughupdates.moulconfig.Social; +import io.github.notenoughupdates.moulconfig.annotations.Category; +import io.github.notenoughupdates.moulconfig.common.MyResourceLocation; +import java.util.Collections; +import java.util.List; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.command.CommandRegistryAccess; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class VSMConfig extends Config { + + public static final MyResourceLocation GITHUB = new MyResourceLocation("minecraft", "textures/block/amethyst_block.png"); + + public void initialise() { + ClientCommandRegistrationCallback.EVENT.register(this::configCommands); + } + + private void configCommands(CommandDispatcher dispatcher, CommandRegistryAccess registryAccess) { + dispatcher.register(literal("vsm") + .executes(command -> { + MinecraftClient.getInstance().send(() -> VSM.INSTANCE.getConfig().openConfigGui()); + return 0; + }) + .then(literal("saveconfig").executes(command -> { + VSM.INSTANCE.getConfig().getInstance().saveNow(); + return 0; + })) + ); + } + + @Override + public List getSocials() { + return Collections.singletonList(Social.forLink("GitHub", GITHUB, "https://www.github.com/VixidDev")); + } + + @Override + public String getTitle() { + return "§bVixid's Skyblock Mod Config"; + } + + @Override + public void saveNow() { + ChatUtils.INSTANCE.chat("Saved config"); + super.saveNow(); + } + + @Expose + @Category(name = "Spotify", desc = "Settings for local Spotify control") + public SpotifyConfig spotifyConfig = new SpotifyConfig(); +} diff --git a/src/main/java/dev/vixid/vsm/config/core/Position.java b/src/main/java/dev/vixid/vsm/config/core/Position.java new file mode 100644 index 0000000..2760203 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/core/Position.java @@ -0,0 +1,42 @@ +package dev.vixid.vsm.config.core; + +import com.google.gson.annotations.Expose; +import java.util.UUID; + +public class Position { + @Expose + private int x; + @Expose + private int y; + private UUID uuid = UUID.randomUUID(); + + public Position(int x, int y) { + this.x = x; + this.y = y; + } + + public Position(double x, double y) { + this((int)x, (int)y); + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + + public void add(int x, int y) { + this.x += x; + this.y += y; + } + + public UUID getUuid() { + if (this.uuid == null) { + this.uuid = UUID.randomUUID(); + return this.uuid; + } + return this.uuid; + } +} diff --git a/src/main/java/dev/vixid/vsm/config/core/VSMGsonMapper.java b/src/main/java/dev/vixid/vsm/config/core/VSMGsonMapper.java new file mode 100644 index 0000000..ce7ceb0 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/core/VSMGsonMapper.java @@ -0,0 +1,45 @@ +package dev.vixid.vsm.config.core; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import io.github.notenoughupdates.moulconfig.ChromaColour; +import io.github.notenoughupdates.moulconfig.LegacyStringChromaColourTypeAdapter; +import io.github.notenoughupdates.moulconfig.managed.DataMapper; +import io.github.notenoughupdates.moulconfig.observer.PropertyTypeAdapterFactory; +import java.lang.reflect.InvocationTargetException; +import org.jetbrains.annotations.NotNull; + +public class VSMGsonMapper implements DataMapper { + + public Class clazz; + + public Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(new PropertyTypeAdapterFactory()) + .registerTypeAdapter(ChromaColour.class, new LegacyStringChromaColourTypeAdapter(true)) + .excludeFieldsWithoutExposeAnnotation() + .create(); + + public VSMGsonMapper(Class clazz) { + this.clazz = clazz; + } + + @NotNull + @Override + public String serialize(T value) { + return gson.toJson(value); + } + + @Override + public T createDefault() { + try { + return clazz.getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + @Override + public T deserialize(@NotNull String string) { + return gson.fromJson(string, clazz); + } +} diff --git a/src/main/java/dev/vixid/vsm/config/core/annotations/ConfigEditorKeybind.java b/src/main/java/dev/vixid/vsm/config/core/annotations/ConfigEditorKeybind.java new file mode 100644 index 0000000..3ffeb26 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/core/annotations/ConfigEditorKeybind.java @@ -0,0 +1,12 @@ +package dev.vixid.vsm.config.core.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface ConfigEditorKeybind { + String defaultKey(); +} diff --git a/src/main/java/dev/vixid/vsm/config/core/gui/GuiOptionEditorKeybind.java b/src/main/java/dev/vixid/vsm/config/core/gui/GuiOptionEditorKeybind.java new file mode 100644 index 0000000..1033de2 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/config/core/gui/GuiOptionEditorKeybind.java @@ -0,0 +1,118 @@ +package dev.vixid.vsm.config.core.gui; + +import io.github.notenoughupdates.moulconfig.GuiTextures; +import io.github.notenoughupdates.moulconfig.common.RenderContext; +import io.github.notenoughupdates.moulconfig.gui.GuiOptionEditor; +import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent; +import io.github.notenoughupdates.moulconfig.gui.MouseEvent; +import io.github.notenoughupdates.moulconfig.processor.ProcessedOption; +import net.minecraft.client.util.InputUtil; + +public class GuiOptionEditorKeybind extends GuiOptionEditor { + private final String defaultKeycode; + private boolean editingKeycode; + + private int width = -1; + private final int height; + + + public GuiOptionEditorKeybind(ProcessedOption option, String defaultKey) { + super(option); + this.defaultKeycode = defaultKey; + this.height = getHeight(); + } + + @Override + public void render(RenderContext context, int x, int y, int width) { + super.render(context, x, y, width); + + if (this.width == -1) this.width = width; + + int buttonX = x + this.width / 6 - 24; + int buttonY = y + this.height - 7 - 14; + + context.bindTexture(GuiTextures.BUTTON); + context.drawTexturedRect(buttonX, buttonY, 48, 16); + + String keyName = getKeyName((String) option.get()); + String buttonText = editingKeycode ? "> " + keyName + " <" : keyName; + + context.drawStringCenteredScaledMaxWidth( + buttonText, + context.getMinecraft().getDefaultFontRenderer(), + buttonX + 24, + buttonY + 8, + false, + 40, + 0xFF303030 + ); + + context.bindTexture(GuiTextures.RESET); + context.drawTexturedRect(buttonX + 51, buttonY + 3, 10, 11); + } + + @Override + public boolean mouseInput(int x, int y, int width, int mouseX, int mouseY, MouseEvent mouseEvent) { + if (mouseEvent instanceof MouseEvent.Click click) { + if (click.getMouseState()) { + if (editingKeycode) { + editingKeycode = false; + option.set(InputUtil.Type.MOUSE.createFromCode(click.getMouseButton()).getTranslationKey()); + return true; + } + + if (isOverButton(x, y, mouseX, mouseY)) { + editingKeycode = true; + return true; + } + + if (isOverResetButton(x, y, mouseX, mouseY)) { + option.set(defaultKeycode); + return true; + } + } + } + return false; + } + + private boolean isOverButton(int x, int y, int mouseX, int mouseY) { + int buttonX = x + width / 6 - 24; + int buttonY = y + height - 7 - 14; + + return mouseX > buttonX && mouseX < buttonX + 48 && + mouseY > buttonY && mouseY < buttonY + 16; + } + + private boolean isOverResetButton(int x, int y, int mouseX, int mouseY) { + int buttonX = x + width / 6 - 24 + 51; + int buttonY = y + height - 7 - 14 + 3; + + return mouseX > buttonX && mouseX < buttonX + 10 && + mouseY > buttonY && mouseY < buttonY + 11; + } + + @Override + public boolean keyboardInput(KeyboardEvent event) { + if (event instanceof KeyboardEvent.KeyPressed key && key.getPressed()) { + if (editingKeycode) { + editingKeycode = false; + if (key.getKeycode() == 256) { + option.set("key.keyboard.unknown"); + return true; + } + String translationKey = InputUtil.fromKeyCode(key.getKeycode(), 0).getTranslationKey(); + option.set(translationKey); + return true; + } + } + return false; + } + + private String getKeyName(String translationKey) { + if (translationKey.equals("key.keyboard.unknown")) { + return "NONE"; + } else { + return InputUtil.fromTranslationKey(translationKey).getLocalizedText().getString(); + } + } +} diff --git a/src/main/java/dev/vixid/vsm/mixin/MixinKeyboard.java b/src/main/java/dev/vixid/vsm/mixin/MixinKeyboard.java new file mode 100644 index 0000000..acbe3c4 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/mixin/MixinKeyboard.java @@ -0,0 +1,27 @@ +package dev.vixid.vsm.mixin; + +import dev.vixid.vsm.events.KeyPress; +import dev.vixid.vsm.spotify.ControlUtils; +import net.minecraft.client.Keyboard; +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(Keyboard.class) +public class MixinKeyboard { + + @Inject(method = "onKey", at = @At("HEAD"), cancellable = true) + public void onKey(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) { + if (ControlUtils.INSTANCE.getIgnoreKeyCallback()) { + ControlUtils.INSTANCE.setIgnoreKeyCallback(false); + ci.cancel(); + return; + } + if (action == 1) { + KeyPress.Companion.getPRESSED().invoker().onPressed(key, scancode, action, modifiers); + } else if (action == 0) { + KeyPress.Companion.getRELEASED().invoker().onReleased(key, scancode, action, modifiers); + } + } +} diff --git a/src/main/java/dev/vixid/vsm/mixin/MixinMinecraftClient.java b/src/main/java/dev/vixid/vsm/mixin/MixinMinecraftClient.java new file mode 100644 index 0000000..d8a76a1 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/mixin/MixinMinecraftClient.java @@ -0,0 +1,10 @@ +package dev.vixid.vsm.mixin; + +import net.minecraft.client.MinecraftClient; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(MinecraftClient.class) +public class MixinMinecraftClient { + + +} diff --git a/src/main/java/dev/vixid/vsm/mixin/MixinMouse.java b/src/main/java/dev/vixid/vsm/mixin/MixinMouse.java new file mode 100644 index 0000000..89c6030 --- /dev/null +++ b/src/main/java/dev/vixid/vsm/mixin/MixinMouse.java @@ -0,0 +1,21 @@ +package dev.vixid.vsm.mixin; + +import dev.vixid.vsm.events.MousePress; +import net.minecraft.client.Mouse; +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(Mouse.class) +public class MixinMouse { + + @Inject(method = "onMouseButton", at = @At("HEAD")) + public void onMouseButtonPressed(long window, int button, int action, int mods, CallbackInfo ci) { + if (action == 1) { + MousePress.getPRESSED().invoker().onPressed(button, action, mods); + } else if (action == 0) { + MousePress.getRELEASED().invoker().onReleased(button, action, mods); + } + } +} diff --git a/src/main/kotlin/dev/vixid/vsm/VSM.kt b/src/main/kotlin/dev/vixid/vsm/VSM.kt new file mode 100644 index 0000000..5e7f4f0 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/VSM.kt @@ -0,0 +1,48 @@ +package dev.vixid.vsm + +import com.github.kwhat.jnativehook.GlobalScreen +import dev.vixid.vsm.config.VSMConfig +import dev.vixid.vsm.config.core.VSMGsonMapper +import dev.vixid.vsm.config.core.annotations.ConfigEditorKeybind +import dev.vixid.vsm.config.core.gui.GuiOptionEditorKeybind +import dev.vixid.vsm.overlays.OverlayPositions +import dev.vixid.vsm.spotify.SpotifyDisplay +import io.github.notenoughupdates.moulconfig.managed.ManagedConfig +import java.io.File +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import org.slf4j.LoggerFactory + +object VSM { + private val logger = LoggerFactory.getLogger("vsm") + + val config = ManagedConfig.create(File("config/vsm/config.json"), VSMConfig::class.java) { + // Overwrite the GsonMapper to our own, so we can exclude fields without Expose annotation + mapper = VSMGsonMapper(this.clazz) + + // Add in Keybind gui editor + this.customProcessor(ConfigEditorKeybind::class.java) { + processedOption, keybind -> + GuiOptionEditorKeybind(processedOption, keybind.defaultKey) + } + } + + private val globalJob = Job() + val coroutineScope = CoroutineScope(CoroutineName("VSM") + SupervisorJob(globalJob)) + + @JvmStatic + fun onInitialise() {} + + @JvmStatic + fun onInitialiseClient() { + GlobalScreen.registerNativeHook() + + config.instance.initialise() + SpotifyDisplay.initialise() + + // Initialise after all overlay objects have been initialised + OverlayPositions.initialise() + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/events/KeyPress.kt b/src/main/kotlin/dev/vixid/vsm/events/KeyPress.kt new file mode 100644 index 0000000..35c5422 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/events/KeyPress.kt @@ -0,0 +1,38 @@ +package dev.vixid.vsm.events + +import dev.vixid.vsm.events.KeyPress.Pressed +import dev.vixid.vsm.events.KeyPress.Released +import net.fabricmc.fabric.api.event.Event +import net.fabricmc.fabric.api.event.EventFactory + +class KeyPress { + companion object { + @JvmStatic + var PRESSED: Event = + EventFactory.createArrayBacked(Pressed::class.java) { listeners: Array -> + Pressed { key: Int, scancode: Int, action: Int, modifiers: Int -> + for (listener in listeners) { + listener.onPressed(key, scancode, action, modifiers) + } + } + } + + @JvmStatic + var RELEASED: Event = + EventFactory.createArrayBacked(Released::class.java) { listeners: Array -> + Released { key: Int, scancode: Int, action: Int, modifiers: Int -> + for (listener in listeners) { + listener.onReleased(key, scancode, action, modifiers) + } + } + } + } + + fun interface Pressed { + fun onPressed(key: Int, scancode: Int, action: Int, modifiers: Int) + } + + fun interface Released { + fun onReleased(key: Int, scancode: Int, action: Int, modifiers: Int) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/events/MousePress.kt b/src/main/kotlin/dev/vixid/vsm/events/MousePress.kt new file mode 100644 index 0000000..b23aaae --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/events/MousePress.kt @@ -0,0 +1,38 @@ +package dev.vixid.vsm.events + +import dev.vixid.vsm.events.MousePress.Pressed +import dev.vixid.vsm.events.MousePress.Released +import net.fabricmc.fabric.api.event.Event +import net.fabricmc.fabric.api.event.EventFactory + +class MousePress { + companion object { + @JvmStatic + var PRESSED: Event = + EventFactory.createArrayBacked(Pressed::class.java) { listeners: Array -> + Pressed { button: Int, actions: Int, mods: Int -> + for (listener in listeners) { + listener.onPressed(button, actions, mods) + } + } + } + + @JvmStatic + var RELEASED: Event = + EventFactory.createArrayBacked(Released::class.java) { listeners: Array -> + Released { button: Int, actions: Int, mods: Int -> + for (listener in listeners) { + listener.onReleased(button, actions, mods) + } + } + } + } + + fun interface Pressed { + fun onPressed(button: Int, actions: Int, mods: Int) + } + + fun interface Released { + fun onReleased(button: Int, actions: Int, mods: Int) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/overlays/Overlay.kt b/src/main/kotlin/dev/vixid/vsm/overlays/Overlay.kt new file mode 100644 index 0000000..ed44151 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/overlays/Overlay.kt @@ -0,0 +1,8 @@ +package dev.vixid.vsm.overlays + +import net.minecraft.client.gui.DrawContext + +abstract class Overlay { + + abstract fun renderOverlay(context: DrawContext) +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/overlays/OverlayPositions.kt b/src/main/kotlin/dev/vixid/vsm/overlays/OverlayPositions.kt new file mode 100644 index 0000000..01df45e --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/overlays/OverlayPositions.kt @@ -0,0 +1,41 @@ +package dev.vixid.vsm.overlays + +import dev.vixid.vsm.config.core.Position +import java.util.UUID +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback +import net.minecraft.client.MinecraftClient +import net.minecraft.client.gui.DrawContext + +object OverlayPositions { + + private val overlays: MutableList = mutableListOf() + private val editorOverlays: MutableMap> = mutableMapOf() + + fun initialise() { + HudRenderCallback.EVENT.register(this::renderOverlays) + } + + fun addOverlay(overlay: Overlay) { + this.overlays.add(overlay) + } + + fun getOverlays() : List = overlays + + fun add(uuid: UUID, pair: Pair) { + this.editorOverlays.putIfAbsent(uuid, pair) + } + + fun getEditorOverlays() : Map> = editorOverlays + + private fun renderOverlays(context: DrawContext, tickDelta: Float) { + if (MinecraftClient.getInstance().currentScreen is PositionEditor) return + + for (overlay: Overlay in overlays) { + overlay.renderOverlay(context) + } + } + + fun openPositionEditor() { + MinecraftClient.getInstance().send { MinecraftClient.getInstance().setScreen(PositionEditor(editorOverlays)) } + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/overlays/PositionEditor.kt b/src/main/kotlin/dev/vixid/vsm/overlays/PositionEditor.kt new file mode 100644 index 0000000..126055c --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/overlays/PositionEditor.kt @@ -0,0 +1,43 @@ +package dev.vixid.vsm.overlays + +import dev.vixid.vsm.config.core.Position +import dev.vixid.vsm.utils.RenderUtils +import java.util.UUID +import net.minecraft.client.gui.DrawContext +import net.minecraft.client.gui.screen.Screen +import net.minecraft.text.Text + +class PositionEditor(private val editorOverlays: Map>) : Screen(Text.literal("Position Editor")) { + + override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) { + this.renderDarkening(context) + + renderOverlays(context) + } + + private fun renderOverlays(context: DrawContext) { + RenderUtils.drawCenteredTextWithShadow(context, "VSM Overlay Position Editor", this.width / 2, 20) + + for (entry in editorOverlays) { + RenderUtils.drawTextWithShadowBoxed(context, entry.value.first, entry.value.second) + } + } + + override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, deltaX: Double, deltaY: Double): Boolean { + val position = mouseIsOverAnOverlay(mouseX, mouseY) + if (position.x != -1 && position.y != -1 && button == 0) { + position.add(deltaX.toInt(), deltaY.toInt()) + } + return true + } + + private fun mouseIsOverAnOverlay(mouseX: Double, mouseY: Double) : Position { + for (overlay in editorOverlays) { + val string = overlay.value.first + val position = overlay.value.second + val bounds = RenderUtils.getRectangleBoundsForString(string, position) + if (bounds.containsPosition(Position(mouseX, mouseY))) return overlay.value.second + } + return Position(-1, -1) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/spotify/ControlUtils.kt b/src/main/kotlin/dev/vixid/vsm/spotify/ControlUtils.kt new file mode 100644 index 0000000..e61d5a0 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/spotify/ControlUtils.kt @@ -0,0 +1,63 @@ +package dev.vixid.vsm.spotify + +import com.github.kwhat.jnativehook.GlobalScreen +import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent + +/** + * Object to handle sending multimedia key events to the OS. + * + * Since GLFW detects these key events but does not map them correctly + * since it doesn't understand the key value, we need to explicitly tell Minecraft + * to ignore the next onKey callback immediately after one of these key events + * are sent to the OS. + */ +object ControlUtils { + + var ignoreKeyCallback = false + + fun postSkipSong() { + GlobalScreen.postNativeEvent(NativeKeyEvent( + NativeKeyEvent.NATIVE_KEY_PRESSED, + 0, + 176, + NativeKeyEvent.VC_MEDIA_NEXT, + NativeKeyEvent.CHAR_UNDEFINED + )) + ignoreKeyCallback = true + } + + fun postPreviousSong() { + GlobalScreen.postNativeEvent(NativeKeyEvent( + NativeKeyEvent.NATIVE_KEY_PRESSED, + 0, + 176, + NativeKeyEvent.VC_MEDIA_PREVIOUS, + NativeKeyEvent.CHAR_UNDEFINED + )) + ignoreKeyCallback = true + } + + fun postPlaySong() { + GlobalScreen.postNativeEvent(NativeKeyEvent( + NativeKeyEvent.NATIVE_KEY_PRESSED, + 0, + 176, + NativeKeyEvent.VC_MEDIA_PLAY, + NativeKeyEvent.CHAR_UNDEFINED + )) + ignoreKeyCallback = true + } + + @Deprecated("Use postPlaySong() instead, since that can pause / play. postPauseSong() can only pause") + fun postPauseSong() { + GlobalScreen.postNativeEvent(NativeKeyEvent( + NativeKeyEvent.NATIVE_KEY_PRESSED, + 0, + 176, + NativeKeyEvent.VC_MEDIA_STOP, + NativeKeyEvent.CHAR_UNDEFINED + )) + ignoreKeyCallback = true + } + +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/spotify/SpotifyDisplay.kt b/src/main/kotlin/dev/vixid/vsm/spotify/SpotifyDisplay.kt new file mode 100644 index 0000000..738584c --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/spotify/SpotifyDisplay.kt @@ -0,0 +1,90 @@ +package dev.vixid.vsm.spotify + +import dev.vixid.vsm.VSM +import dev.vixid.vsm.config.SpotifyConfig +import dev.vixid.vsm.events.KeyPress +import dev.vixid.vsm.events.MousePress +import dev.vixid.vsm.overlays.Overlay +import dev.vixid.vsm.overlays.OverlayPositions +import dev.vixid.vsm.utils.ChatUtils +import dev.vixid.vsm.utils.RenderUtils.drawTextWithShadow +import java.io.BufferedReader +import java.io.InputStreamReader +import kotlinx.coroutines.launch +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents +import net.minecraft.client.MinecraftClient +import net.minecraft.client.gui.DrawContext +import net.minecraft.client.util.InputUtil + +object SpotifyDisplay : Overlay() { + + private val config: SpotifyConfig get() = VSM.config.instance.spotifyConfig + + private var songName: String = "§cCannot detect song name!" + private var totalTicks = 0 + + fun initialise() { + OverlayPositions.addOverlay(this) + + ClientTickEvents.END_CLIENT_TICK.register(this::onTick) + KeyPress.PRESSED.register(this::onKeyPress) + MousePress.PRESSED.register(this::onMousePress) + } + + override fun renderOverlay(context: DrawContext) { + config.overlayPosition.drawTextWithShadow(context, songName) + } + + private fun onTick(client: MinecraftClient) { + if (!isEnabled(client)) return + + totalTicks++ + + if (totalTicks % 20 == 0) { + VSM.coroutineScope.launch { + songName = getSongFromSpotifyProcess() + } + } + } + + private fun onKeyPress(key: Int, scancode: Int, action: Int, modifiers: Int) { + val translationKey = InputUtil.fromKeyCode(key, 0).translationKey + onPress(translationKey) + } + + private fun onMousePress(button: Int, actions: Int, mods: Int) { + val translationKey = InputUtil.Type.MOUSE.createFromCode(button).translationKey + onPress(translationKey) + } + + private fun onPress(key: String) { + when (key) { + config.skipForwardKey -> ControlUtils.postSkipSong() + config.skipBackwardKey -> ControlUtils.postPreviousSong() + config.playPauseKey -> ControlUtils.postPlaySong() + } + } + + private fun getSongFromSpotifyProcess() : String { + return try { + val process = Runtime.getRuntime().exec("powershell.exe (ps Spotify | ? MainWindowTitle | select MainWindowTitle).MainWindowTitle") + val reader = BufferedReader(InputStreamReader(process.inputStream)) + var line = reader.readLine() + reader.close() + if (line == "Spotify") { + line = songName + } else if (line != null) { + line = "§a${line}".replace(" - ", " §f-§b ") + if (!line.equals(songName)) { + ChatUtils.chat("§bVSM §f> $line") + } + } + line ?: "§cCannot detect song name!" + } catch (error: Exception) { + error.printStackTrace() + "§cCannot detect song name!" + } + } + + private fun isEnabled(client: MinecraftClient) = client.world != null && config.enabled +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/utils/ChatUtils.kt b/src/main/kotlin/dev/vixid/vsm/utils/ChatUtils.kt new file mode 100644 index 0000000..019893a --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/utils/ChatUtils.kt @@ -0,0 +1,12 @@ +package dev.vixid.vsm.utils + +import net.minecraft.client.MinecraftClient +import net.minecraft.text.Text + +object ChatUtils { + private val mc = MinecraftClient.getInstance() + + fun chat(string: String) { + mc.inGameHud.chatHud.addMessage(Text.literal(string)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/utils/RenderUtils.kt b/src/main/kotlin/dev/vixid/vsm/utils/RenderUtils.kt new file mode 100644 index 0000000..b8b8307 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/utils/RenderUtils.kt @@ -0,0 +1,63 @@ +package dev.vixid.vsm.utils + +import dev.vixid.vsm.config.core.Position +import dev.vixid.vsm.overlays.OverlayPositions +import java.awt.Color +import net.minecraft.client.MinecraftClient +import net.minecraft.client.font.TextRenderer +import net.minecraft.client.gui.DrawContext + +object RenderUtils { + private val textRenderer: TextRenderer by lazy { MinecraftClient.getInstance().textRenderer } + + fun drawCenteredTextWithShadow(context: DrawContext, string: String, centerX: Int, y: Int) { + drawCenteredTextWithShadow(context, string, centerX, y, Color.WHITE.rgb) + } + + fun drawCenteredTextWithShadow(context: DrawContext, string: String, centerX: Int, y: Int, color: Int) { + context.drawCenteredTextWithShadow(textRenderer, string, centerX, y, color) + } + + fun drawTextWithShadow(context: DrawContext, string: String, position: Position) { + drawTextWithShadow(context, string, position.x, position.y) + } + + fun drawTextWithShadow(context: DrawContext, string: String, x: Int, y: Int) { + drawTextWithShadow(context, string, x, y, Color.WHITE.rgb) + } + + fun drawTextWithShadow(context: DrawContext, string: String, x: Int, y: Int, color: Int) { + context.drawTextWithShadow(textRenderer, string, x, y, color) + } + + fun drawTextWithShadowBoxed(context: DrawContext, string: String, position: Position) { + drawTextWithShadow(context, string, position) + val stringWidth = textRenderer.getWidth(string) + val stringHeight = textRenderer.fontHeight + drawRectangleOutline(context, position, stringWidth, stringHeight) + } + + fun drawRectangleOutline(context: DrawContext, position: Position, width: Int, height: Int) { + context.drawHorizontalLine(position.x - 3, position.x + width + 2, position.y - 3, Color.WHITE.rgb) + context.drawHorizontalLine(position.x - 3, position.x + width + 2, position.y + height + 1, Color.WHITE.rgb) + context.drawVerticalLine(position.x - 3, position.y - 3, position.y + height + 1, Color.WHITE.rgb) + context.drawVerticalLine(position.x + width + 2, position.y - 3, position.y + height + 1, Color.WHITE.rgb) + } + + fun getRectangleBoundsForString(string: String, position: Position) : Vec4f { + val width = textRenderer.getWidth(string) + val height = textRenderer.fontHeight + + val bounds = Vec4f() + bounds.x = position.x - 3f + bounds.y = position.y - 3f + bounds.z = position.x + width + 2f + bounds.w = position.y + height + 1f + return bounds + } + + fun Position.drawTextWithShadow(context: DrawContext, string: String) { + drawTextWithShadow(context, string, this) + OverlayPositions.add(this.uuid, Pair(string, this)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/vixid/vsm/utils/Vec4f.kt b/src/main/kotlin/dev/vixid/vsm/utils/Vec4f.kt new file mode 100644 index 0000000..78d2e05 --- /dev/null +++ b/src/main/kotlin/dev/vixid/vsm/utils/Vec4f.kt @@ -0,0 +1,13 @@ +package dev.vixid.vsm.utils + +import dev.vixid.vsm.config.core.Position + +class Vec4f( + var x: Float = 0f, + var y: Float = 0f, + var z: Float = 0f, + var w: Float = 0f +) { + + fun containsPosition(position: Position) : Boolean = position.x > x && position.x < z && position.y > y && position.y < w +} \ No newline at end of file diff --git a/src/main/resources/assets/vsm/icon.png b/src/main/resources/assets/vsm/icon.png new file mode 100644 index 0000000..d2469de Binary files /dev/null and b/src/main/resources/assets/vsm/icon.png differ diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..1372ffc --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,35 @@ +{ + "schemaVersion": 1, + "id": "vsm", + "version": "${version}", + "name": "Vixid's Skyblock Mod", + "description": "This is an example description! Tell everyone what your mod is about!", + "authors": [ + "Vixid" + ], + "contact": { + "homepage": "https://fabricmc.net/", + "sources": "https://github.com/VixidDev" + }, + "license": "CC0-1.0", + "icon": "assets/vsm/icon.png", + "environment": "client", + "entrypoints": { + "main": [ + "dev.vixid.vsm.VSM::onInitialise" + ], + "client": [ + "dev.vixid.vsm.VSM::onInitialiseClient" + ] + }, + "mixins": [ + "vsm.mixins.json" + ], + "depends": { + "fabricloader": ">=0.15.11", + "minecraft": "~1.20.6", + "java": ">=21", + "fabric-api": "*", + "fabric-language-kotlin": "*" + } +} \ No newline at end of file diff --git a/src/main/resources/vsm.mixins.json b/src/main/resources/vsm.mixins.json new file mode 100644 index 0000000..6087823 --- /dev/null +++ b/src/main/resources/vsm.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "package": "dev.vixid.vsm.mixin", + "compatibilityLevel": "JAVA_21", + "mixins": [ + "MixinKeyboard", + "MixinMouse", + "MixinMinecraftClient" + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file