Skip to content

Commit

Permalink
updated to 1.21.
Browse files Browse the repository at this point in the history
  • Loading branch information
quadrat-ik committed Aug 8, 2024
1 parent 260f66f commit ad3de26
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

A small mod for managing client side of basic authentication for you.

# Компиляция
Для компиляции нужен Gradle минимум 8.9. Java минимум 21 (как для Gradle, так и для проекта).
## Compile
To compile, you need Gradle at least 8.9. Java at least 21 (for both Gradle and the project).

## Features
- Manage basic authentication process on client for servers using /register /login scheme.
Expand Down
32 changes: 22 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.archivesName
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("fabric-loom") version Versions.fabric_loom
kotlin("jvm") version embeddedKotlinVersion
kotlin("jvm") version Versions.kotlin
}

repositories {
Expand All @@ -13,8 +13,23 @@ dependencies {
minecraft(Dependencies.minecraft)
mappings(Dependencies.yarn)
modImplementation(Dependencies.fabric_loader)

modImplementation(Dependencies.fabric_api)
implementation("commons-codec:commons-codec:1.16.0")
implementation("org.slf4j:slf4j-api:1.7.30")
runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl:2.13.3")
implementation("org.apache.logging.log4j:log4j-api:2.13.3")
implementation("org.apache.logging.log4j:log4j-core:2.13.3")
}

tasks.withType<JavaCompile> {
sourceCompatibility = Versions.java.toString()
targetCompatibility = Versions.java.toString()
}

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = Versions.java.toString()
}
}

tasks.processResources {
Expand All @@ -29,23 +44,20 @@ tasks.processResources {
"version" to Versions.mod,
"minecraft_version" to Versions.minecraft,
"loader_version" to Versions.fabric_loader
)
)
))
}
}

val archiveName = "${Properties.mod_name.lowercase()}-${Versions.minecraft}-fabric"

tasks.withType(JavaCompile::class).configureEach {
this.options.encoding = "UTF-8"
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}

tasks.jar {
archivesName.set(archiveName)
version = Versions.mod
}

tasks.build {
archivesName.set(archiveName)
version = Versions.mod
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.jvmargs=-Xmx10G

maven_group = ru.energostalin
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
1 change: 1 addition & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
22 changes: 13 additions & 9 deletions gradlew
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# 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
Expand Down Expand Up @@ -130,26 +131,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
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=SC3045
# 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=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -198,11 +202,11 @@ 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, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# 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" \
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ru/energostalin/autoauth/AutoAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package ru.energostalin.autoauth
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import ru.energostalin.autoauth.lib.PasswordManagerFactory
import ru.energostalin.autoauth.lib.osutil.open
import kotlin.io.path.createDirectory
import kotlin.io.path.exists

val logger: Logger = LoggerFactory.getLogger(AutoAuth::class.java)

class AutoAuth : ModInitializer {

override fun onInitialize() {
if(!Config.Static.dir.exists())
Config.Static.dir.createDirectory()
Expand All @@ -21,5 +26,7 @@ class AutoAuth : ModInitializer {
})
)
})

logger.info("Initialized AutoAuth")
}
}
3 changes: 2 additions & 1 deletion src/main/java/ru/energostalin/autoauth/Config.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ru.energostalin.autoauth

import net.fabricmc.loader.api.FabricLoader
import java.nio.file.Path

class Config {
object Static {
val dir = FabricLoader.getInstance().configDir.resolve("AutoAuth")
val dir: Path = FabricLoader.getInstance().configDir.resolve("AutoAuth")
}
}
8 changes: 4 additions & 4 deletions src/main/java/ru/energostalin/autoauth/lib/provider/AuthMe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class AuthMe(private val client: MinecraftClient) {
login()
}

if ((msg.contains("/login") || msg.contains("/register"))) {
if (msg.contains("/login") && AuthState.state != AuthState.State.MANUAL_LOGIN_REQUIRED) {
if ((msg.contains("/login") || msg.contains("/l") || msg.contains("/register") || msg.contains("/reg"))) {
if (msg.contains("/login") && AuthState.state != AuthState.State.MANUAL_LOGIN_REQUIRED || msg.contains("/l") && AuthState.state != AuthState.State.MANUAL_LOGIN_REQUIRED) {
login()
} else if (msg.contains("/register")) {
} else if (msg.contains("/register") || msg.contains("/reg")) {
register()
}
} else if (msg.contains("Successful login!")) {
Expand All @@ -69,7 +69,7 @@ class AuthMe(private val client: MinecraftClient) {
fun handleChatCommand(msg: String) {
if(AuthState.state == AuthState.State.LOGGED_IN) return

if (msg.contains("login") && AuthState.state == AuthState.State.MANUAL_LOGIN_REQUIRED) {
if (msg.contains("login") && AuthState.state == AuthState.State.MANUAL_LOGIN_REQUIRED || msg.contains("l") && AuthState.state == AuthState.State.MANUAL_LOGIN_REQUIRED) {
savePassword(msg.split(' ')[1])
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/ru/energostalin/autoauth/lib/storages/JsonStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import org.apache.commons.codec.binary.Base64
import java.nio.file.Files
import java.nio.file.Path


class JsonStorage(private val datadir: Path) : MutableStorage {
class JsonStorage(datadir: Path) : MutableStorage {

override val uri: Path = datadir.resolve("passwords.json")

private val gson: Gson = Gson()

init {
if(!Files.exists(uri))
if (!Files.exists(uri))
Files.createFile(uri)
}

Expand All @@ -27,16 +27,20 @@ class JsonStorage(private val datadir: Path) : MutableStorage {
}

override fun getOne(ip: String, name: String): Storage.ServerRecord? {
return readList().find { e -> e.ip == ip && e.user == name; }
return readList().find { e -> e.ip == ip && e.user == name }
}

private fun readList(): MutableList<Storage.ServerRecord> {
@Suppress("UNNECESSARY_SAFE_CALL")
return try {
Files.newInputStream(uri)?.use { ifs ->
ifs.bufferedReader().use {br ->
ifs.bufferedReader().use { br ->
JsonReader(br).use {
gson.fromJson(it, object : TypeToken<ArrayList<Storage.ServerRecord>>() {}.type)
val records = gson.fromJson(it, object : TypeToken<ArrayList<Storage.ServerRecord>>() {}.type) as ArrayList<Storage.ServerRecord>
records.forEach { record ->
record.pass = String(Base64.decodeBase64(record.pass))
}
records
}
}
} ?: mutableListOf()
Expand All @@ -46,6 +50,9 @@ class JsonStorage(private val datadir: Path) : MutableStorage {
}

private fun saveList(passwords: MutableList<Storage.ServerRecord>) {
passwords.forEach { record ->
record.pass = Base64.encodeBase64String(record.pass.toByteArray(Charsets.UTF_8))
}
Files.newOutputStream(uri).use { ofs ->
ofs.bufferedWriter().use { bw ->
JsonWriter(bw).use {
Expand All @@ -54,4 +61,4 @@ class JsonStorage(private val datadir: Path) : MutableStorage {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ru.energostalin.autoauth.lib.storages
import java.nio.file.Path

interface Storage {
data class ServerRecord(val ip: String, val user: String, val pass: String)
data class ServerRecord(val ip: String, val user: String, var pass: String)
fun getOne(ip: String, name: String): ServerRecord?

val uri: Path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package ru.energostalin.autoauth.mixin;

import net.minecraft.network.packet.s2c.play.*;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Unique;
import ru.energostalin.autoauth.AutoAuthKt;
import ru.energostalin.autoauth.lib.AuthState;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.network.ClientPlayNetworkHandler;


@Mixin(ClientPlayNetworkHandler.class)
public abstract class ClientPlayNetworkHandlerMixin {
@Shadow @Final private MinecraftClient client;
@SuppressWarnings({"FieldCanBeLocal", "unused"})
@Unique
@Mutable
@Final private final MinecraftClient client;

protected ClientPlayNetworkHandlerMixin(MinecraftClient client) {
this.client = client;
}

@Inject(method = "onGameJoin", at = @At("RETURN"))
private void onGameJoin(GameJoinS2CPacket packet, CallbackInfo ci) {
AuthState.INSTANCE.setState(AuthState.State.UNKNOWN);

AutoAuthKt.getLogger().info("joined to the server");
}

@Inject(method = "sendChatCommand", at = @At("RETURN"))
private void sendChatCommand(String command, CallbackInfo ci) {
if(AuthState.INSTANCE.getState() == AuthState.State.LOGGED_IN) {
return;
}

AuthState.INSTANCE.getProvider().handleChatCommand(command);
}

Expand All @@ -34,6 +47,8 @@ private void onGameMessage(GameMessageS2CPacket packet, CallbackInfo ci) {
if(AuthState.INSTANCE.getState() == AuthState.State.LOGGED_IN) {
return;
}

AuthState.INSTANCE.getProvider().handleGameMessage(packet.content().getString());
}

}
4 changes: 2 additions & 2 deletions src/main/resources/autoauth.mixins.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"required": true,
"minVersion": "0.8",
"minVersion": "0.8.7",
"package": "ru.energostalin.autoauth.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"mixins": [
],
"client": [
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit ad3de26

Please sign in to comment.