Skip to content

Commit

Permalink
Add server-side translations
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Jul 17, 2021
1 parent f0002d6 commit b7b99c9
Show file tree
Hide file tree
Showing 23 changed files with 485 additions and 77 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
repositories {
//maven { name 'Carpet'; url 'https://jitpack.io' }
maven { name 'Carpet'; url 'https://masa.dy.fi/maven' }
maven { url "https://maven.nucleoid.xyz/" }
}

archivesBaseName = "${project.mod_id}-mc${project.minecraft_version}"
Expand All @@ -27,6 +28,9 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Translations
modImplementation include("fr.catcore:server-translations-api:${project.server_translations_version}")

// Mods
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
//modImplementation "com.github.gnembon:fabric-carpet:${project.carpet_branch}-SNAPSHOT"
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mod
mod_name = EasyAuth
mod_id = easyauth
mod_version = 1.9.2
mod_version = 1.9.3

# Fabric
minecraft_version=1.17.1
Expand All @@ -12,6 +12,7 @@ fabric_version=0.36.1+1.17
# Dependencies
carpet_core_version = 1.4.43+v210706
carpet_branch = 1.17
server_translations_version=1.4.5+1.17

argon2_version = 2.7
bcrypt_version = 0.9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import xyz.nikitacartes.easyauth.utils.AuthHelper;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;

Expand All @@ -22,7 +22,7 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
.then(literal("unregister")
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(
new LiteralText(config.lang.enterPassword),
new TranslatableText("text.easyauth.enterPassword"),
false
);
return 1;
Expand All @@ -39,7 +39,7 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
.then(argument("old password", word())
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(
new LiteralText(config.lang.enterNewPassword),
new TranslatableText("text.easyauth.enterNewPassword"),
false);
return 1;
})
Expand All @@ -63,7 +63,7 @@ private static int unregister(ServerCommandSource source, String pass) throws Co

if (config.main.enableGlobalPassword) {
player.sendMessage(
new LiteralText(config.lang.cannotUnregister),
new TranslatableText("text.easyauth.cannotUnregister"),
false
);
return 0;
Expand All @@ -73,12 +73,12 @@ private static int unregister(ServerCommandSource source, String pass) throws Co
THREADPOOL.submit(() -> {
if (AuthHelper.checkPassword(((PlayerAuth) player).getFakeUuid(), pass.toCharArray()) == AuthHelper.PasswordOptions.CORRECT) {
DB.deleteUserData(((PlayerAuth) player).getFakeUuid());
player.sendMessage(new LiteralText(config.lang.accountDeleted), false);
player.sendMessage(new TranslatableText("text.easyauth.accountDeleted"), false);
((PlayerAuth) player).setAuthenticated(false);
return;
}
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
new TranslatableText("text.easyauth.wrongPassword"),
false
);
});
Expand All @@ -92,7 +92,7 @@ private static int changePassword(ServerCommandSource source, String oldPass, St

if (config.main.enableGlobalPassword) {
player.sendMessage(
new LiteralText(config.lang.cannotChangePassword),
new TranslatableText("text.easyauth.cannotChangePassword"),
false
);
return 0;
Expand All @@ -101,27 +101,23 @@ private static int changePassword(ServerCommandSource source, String oldPass, St
THREADPOOL.submit(() -> {
if (AuthHelper.checkPassword(((PlayerAuth) player).getFakeUuid(), oldPass.toCharArray()) == AuthHelper.PasswordOptions.CORRECT) {
if (newPass.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false);
player.sendMessage(new TranslatableText("text.easyauth.minPasswordChars", config.main.minPasswordChars), false);
return;
}
else if (newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false);
player.sendMessage(new TranslatableText("text.easyauth.maxPasswordChars", config.main.maxPasswordChars), false);
return;
}
// Changing password in playercache
playerCacheMap.get(((PlayerAuth) player).getFakeUuid()).password = AuthHelper.hashPassword(newPass.toCharArray());
player.sendMessage(
new LiteralText(config.lang.passwordUpdated),
new TranslatableText("text.easyauth.passwordUpdated"),
false
);
}
else
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
new TranslatableText("text.easyauth.wrongPassword"),
false
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
import xyz.nikitacartes.easyauth.storage.AuthConfig;
import xyz.nikitacartes.easyauth.storage.PlayerCache;
Expand Down Expand Up @@ -113,7 +113,7 @@ public static int reloadConfig(Entity sender) {
config = AuthConfig.load(new File("./mods/EasyAuth/config.json"));

if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.configurationReloaded), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.configurationReloaded"), false);
else
logInfo(config.lang.configurationReloaded);
return 1;
Expand All @@ -138,7 +138,7 @@ private static int setGlobalPassword(ServerCommandSource source, String password
});

if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.globalPasswordSet"), false);
else
logInfo(config.lang.globalPasswordSet);
return 1;
Expand Down Expand Up @@ -170,7 +170,7 @@ private static int setSpawn(ServerCommandSource source, Identifier world, double
// Getting sender
Entity sender = source.getEntity();
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.worldSpawnSet), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.worldSpawnSet"), false);
else
logInfo(config.lang.worldSpawnSet);
return 1;
Expand All @@ -191,7 +191,7 @@ private static int removeAccount(ServerCommandSource source, String uuid) {
});

if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.userdataDeleted"), false);
else
logInfo(config.lang.userdataDeleted);
return 1; // Success
Expand Down Expand Up @@ -222,7 +222,7 @@ private static int registerUser(ServerCommandSource source, String uuid, String
playerCacheMap.get(uuid).password = AuthHelper.hashPassword(password.toCharArray());

if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.userdataUpdated"), false);
else
logInfo(config.lang.userdataUpdated);
});
Expand Down Expand Up @@ -253,15 +253,15 @@ private static int updatePassword(ServerCommandSource source, String uuid, Strin
playerCacheMap.put(uuid, playerCache);
if(!playerCacheMap.get(uuid).password.isEmpty()) {
if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userNotRegistered), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.userNotRegistered"), false);
else
logInfo(config.lang.userNotRegistered);
return;
}
playerCacheMap.get(uuid).password = AuthHelper.hashPassword(password.toCharArray());

if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
((PlayerEntity) sender).sendMessage(new TranslatableText("text.easyauth.userdataUpdated"), false);
else
logInfo(config.lang.userdataUpdated);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import xyz.nikitacartes.easyauth.utils.AuthHelper;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;

Expand All @@ -23,7 +23,7 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
.executes(ctx -> login(ctx.getSource(), getString(ctx, "password")) // Tries to authenticate user
))
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(new LiteralText(config.lang.enterPassword), false);
ctx.getSource().getPlayer().sendMessage(new TranslatableText("text.easyauth.enterPassword"), false);
return 0;
}));
}
Expand All @@ -34,7 +34,7 @@ private static int login(ServerCommandSource source, String pass) throws Command
ServerPlayerEntity player = source.getPlayer();
String uuid = ((PlayerAuth) player).getFakeUuid();
if (((PlayerAuth) player).isAuthenticated()) {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
player.sendMessage(new TranslatableText("text.easyauth.alreadyAuthenticated"), false);
return 0;
}
// Putting rest of the command in different thread to avoid lag spikes
Expand All @@ -43,25 +43,25 @@ private static int login(ServerCommandSource source, String pass) throws Command
AuthHelper.PasswordOptions passwordResult = AuthHelper.checkPassword(uuid, pass.toCharArray());

if(playerCacheMap.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) {
player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded));
player.networkHandler.disconnect(new TranslatableText("text.easyauth.loginTriesExceeded"));
return;
}
else if(passwordResult == AuthHelper.PasswordOptions.CORRECT) {
player.sendMessage(new LiteralText(config.lang.successfullyAuthenticated), false);
player.sendMessage(new TranslatableText("text.easyauth.successfullyAuthenticated"), false);
((PlayerAuth) player).setAuthenticated(true);
return;
}
else if(passwordResult == AuthHelper.PasswordOptions.NOT_REGISTERED) {
player.sendMessage(new LiteralText(config.lang.registerRequired), false);
player.sendMessage(new TranslatableText("text.easyauth.registerRequired"), false);
return;
}
// Kicking the player out
else if(maxLoginTries == 1) {
player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword));
player.networkHandler.disconnect(new TranslatableText("text.easyauth.wrongPassword"));
return;
}
// Sending wrong pass message
player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
player.sendMessage(new TranslatableText("text.easyauth.wrongPassword"), false);
// ++ the login tries
playerCacheMap.get(uuid).loginTries += 1;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;

import static net.minecraft.server.command.CommandManager.literal;
Expand All @@ -25,10 +25,10 @@ private static int logout(ServerCommandSource serverCommandSource) throws Comman

if(!mojangAccountNamesCache.contains(player.getGameProfile().getName().toLowerCase())) {
((PlayerAuth) player).setAuthenticated(false);
player.sendMessage(new LiteralText(config.lang.successfulLogout), false);
player.sendMessage(new TranslatableText("text.easyauth.successfulLogout"), false);
}
else
player.sendMessage(new LiteralText(config.lang.cannotLogout), false);
player.sendMessage(new TranslatableText("text.easyauth.cannotLogout"), false);
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import xyz.nikitacartes.easyauth.storage.PlayerCache;
import xyz.nikitacartes.easyauth.utils.PlayerAuth;

Expand All @@ -27,7 +27,7 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
.executes( ctx -> register(ctx.getSource(), getString(ctx, "password"), getString(ctx, "passwordAgain")))
))
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(new LiteralText(config.lang.enterPassword), false);
ctx.getSource().getPlayer().sendMessage(new TranslatableText("text.easyauth.enterPassword"), false);
return 0;
}));
}
Expand All @@ -36,41 +36,37 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
private static int register(ServerCommandSource source, String pass1, String pass2) throws CommandSyntaxException {
ServerPlayerEntity player = source.getPlayer();
if(config.main.enableGlobalPassword) {
player.sendMessage(new LiteralText(config.lang.loginRequired), false);
player.sendMessage(new TranslatableText("text.easyauth.loginRequired"), false);
return 0;
}
else if(((PlayerAuth) player).isAuthenticated()) {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
player.sendMessage(new TranslatableText("text.easyauth.alreadyAuthenticated"), false);
return 0;
}
else if(!pass1.equals(pass2)) {
player.sendMessage(new LiteralText(config.lang.matchPassword), false);
player.sendMessage(new TranslatableText("text.easyauth.matchPassword"), false);
return 0;
}
// Different thread to avoid lag spikes
THREADPOOL.submit(() -> {
if(pass1.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false);
player.sendMessage(new TranslatableText("text.easyauth.minPasswordChars", config.main.minPasswordChars), false);
return;
}
else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false);
player.sendMessage(new TranslatableText("text.easyauth.maxPasswordChars", config.main.maxPasswordChars), false);
return;
}

PlayerCache playerCache = playerCacheMap.get(((PlayerAuth) player).getFakeUuid());
if (playerCache.password.isEmpty()) {
((PlayerAuth) player).setAuthenticated(true);
player.sendMessage(new LiteralText(config.lang.registerSuccess), false);
player.sendMessage(new TranslatableText("text.easyauth.registerSuccess"), false);

playerCache.password = hashPassword(pass1.toCharArray());
return;
}
player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false);
player.sendMessage(new TranslatableText("text.easyauth.alreadyRegistered"), false);
});
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -148,12 +148,8 @@ public void setAuthenticated(boolean authenticated) {
public Text getAuthMessage() {
final PlayerCache cache = playerCacheMap.get(((PlayerAuth) player).getFakeUuid());
if(!config.main.enableGlobalPassword && cache.password.isEmpty())
return new LiteralText(
config.lang.notAuthenticated+ "\n" + config.lang.registerRequired
);
return new LiteralText(
config.lang.notAuthenticated + "\n" + config.lang.loginRequired
);
return new TranslatableText("text.easyauth.notAuthenticated").append("\n").append(new TranslatableText("text.easyauth.registerRequired"));
return new TranslatableText("text.easyauth.notAuthenticated").append("\n").append(new TranslatableText("text.easyauth.loginRequired"));
}

/**
Expand Down Expand Up @@ -192,7 +188,7 @@ private void playerTick(CallbackInfo ci) {
if(!this.isAuthenticated()) {
// Checking player timer
if(kickTimer <= 0 && player.networkHandler.getConnection().isOpen()) {
player.networkHandler.disconnect(new LiteralText(config.lang.timeExpired));
player.networkHandler.disconnect(new TranslatableText("text.easyauth.timeExpired"));
}
else {
// Sending authentication prompt every 10 seconds
Expand Down
Loading

0 comments on commit b7b99c9

Please sign in to comment.