Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added translation system. #12

Open
wants to merge 4 commits into
base: settings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/koralix/oneforall/ServerOneForAll.java
JohanVonElectrum marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.koralix.oneforall;

import com.koralix.oneforall.lang.TranslationUnit;
import com.koralix.oneforall.network.ServerLoginManager;
import com.koralix.oneforall.platform.Platform;
import com.koralix.oneforall.settings.ServerSettings;
import com.koralix.oneforall.settings.SettingsManager;
import net.fabricmc.fabric.api.networking.v1.ServerLoginConnectionEvents;

public class ServerOneForAll extends OneForAll {

Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/koralix/oneforall/lang/TranslationUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.koralix.oneforall.lang;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koralix.oneforall.OneForAll;
import com.koralix.oneforall.network.ClientSession;
import com.koralix.oneforall.network.ServerLoginManager;
import com.koralix.oneforall.settings.ServerSettings;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableTextContent;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Stream;

import static net.minecraft.datafixer.fix.BlockEntitySignTextStrictJsonFix.GSON;

public class TranslationUnit {
private final static Map<String, Map<String, String>> TRANSLATIONS = new HashMap<>();
private final static String DEFAULT_LANGUAGE = "es_es";
private static Optional<UUID> TARGET_PLAYER = Optional.empty();

static {
JohanVonElectrum marked this conversation as resolved.
Show resolved Hide resolved
Stream.of(
"es_es",
"gl_es"
).forEach(TranslationUnit::load);
}

public static void load(String lang) {
OneForAll.getInstance().getLogger().info("Loading translations from {}", lang);

Map<String, String> translationMap = new HashMap<>();
String languageFile = "/assets/lang/" + lang + ".json";
JohanVonElectrum marked this conversation as resolved.
Show resolved Hide resolved

try (InputStream inputStream = TranslationUnit.class.getResourceAsStream(languageFile)) {
JsonObject jsonObject = GSON.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);

for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
translationMap.put(entry.getKey(), entry.getValue().getAsString());
}

TRANSLATIONS.put(lang, translationMap);
} catch (Exception e) {
OneForAll.getInstance().getLogger().error("Failed to load translations from {}", lang, e);
}
}

public static String translate(String lang, String code) {
if (TRANSLATIONS.containsKey(lang) && TRANSLATIONS.get(lang).containsKey(code)) {
return TRANSLATIONS.get(lang).get(code);
} else if (TRANSLATIONS.containsKey(ServerSettings.DEFAULT_LANGUAGE.value()) && TRANSLATIONS.get(ServerSettings.DEFAULT_LANGUAGE.value()).containsKey(code)) {
return TRANSLATIONS.get(ServerSettings.DEFAULT_LANGUAGE.value()).get(code);
} else {
return TRANSLATIONS.get(DEFAULT_LANGUAGE).get(code);
}
}

public static void prepare(UUID uuid) {
TARGET_PLAYER = Optional.of(uuid);
}

public static Text adaptText(Text text) {
Text result = text;

ClientSession session = null;
if (TARGET_PLAYER.isPresent()) {
session = ServerLoginManager.SESSIONS.get(TARGET_PLAYER.get());
}

if (session == null || session.modVersion() == null) {
JohanVonElectrum marked this conversation as resolved.
Show resolved Hide resolved
if (!text.getSiblings().isEmpty()) {
ListIterator<Text> iterator = text.getSiblings().listIterator();

while (iterator.hasNext()) {
iterator.set(adaptText(iterator.next()));
}
}

if (text.getContent() instanceof TranslatableTextContent textContent) {
if (TRANSLATIONS.get(DEFAULT_LANGUAGE).containsKey(textContent.getKey())) {
String userLanguage = session != null
? (session.language() != null ? session.language() : ServerSettings.DEFAULT_LANGUAGE.value())
: ServerSettings.DEFAULT_LANGUAGE.value();
String translation = translate(userLanguage, textContent.getKey());
MutableText content = Text.translatableWithFallback(textContent.getKey(), translation);
for (Text sibling: text.getSiblings()) {
content.append(sibling);
}

result = content;
}
}
}

return result;
}

public static boolean hasLanguage(String language) {
return TRANSLATIONS.containsKey(language);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.koralix.oneforall.mixin.fapi;
package com.koralix.oneforall.mixin.server.fapi;

import net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry;
import net.fabricmc.fabric.impl.networking.server.ServerLoginNetworkAddon;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.network.ClientSessionWrapper;
import com.koralix.oneforall.network.ClientSession;
import net.minecraft.network.ClientConnection;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(ClientConnection.class)
public class ClientConnectionMixin implements ClientSessionWrapper {
private ClientSession session;

@Override
public ClientSession session() {
return session;
}

@Override
public void session(ClientSession session) {
this.session = session;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.lang.TranslationUnit;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
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.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyArgs;

@Mixin(PacketByteBuf.class)
public class PacketByteBufMixin {
@ModifyArg(
method = "Lnet/minecraft/network/PacketByteBuf;writeText(Lnet/minecraft/text/Text;)Lnet/minecraft/network/PacketByteBuf;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/text/Text$Serializer;toJson(Lnet/minecraft/text/Text;)Ljava/lang/String;"
),
index = 0
)
private Text writeTextInjector(Text text) {
return TranslationUnit.adaptText(text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.network.ClientSessionWrapper;
import com.koralix.oneforall.network.ClientSession;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

@Mixin(ServerLoginNetworkHandler.class)
public class ServerLoginNetworkHandlerMixin implements ClientSessionWrapper {
@Shadow private ClientConnection connection;

@Override
public ClientSession session() {
throw new UnsupportedOperationException();
}

@Override
public void session(@NotNull ClientSession session) {
ClientSessionWrapper modConnection = (ClientSessionWrapper) Objects.requireNonNull(connection);

modConnection.session(session);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.lang.TranslationUnit;
import com.koralix.oneforall.network.*;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.encryption.PublicPlayerSession;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.*;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;
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;

@Mixin(ServerPlayNetworkHandler.class)
public class ServerPlayNetworkHandlerMixin implements ClientSessionWrapper {
@Shadow
ServerPlayerEntity player;
@Shadow
ClientConnection connection;


@Shadow private @Nullable PublicPlayerSession session;
JohanVonElectrum marked this conversation as resolved.
Show resolved Hide resolved

@Inject(
method = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/PacketCallbacks;)V",
at = @At("HEAD")
)
private void setTranslationTargetUUID(Packet<?> packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) {
TranslationUnit.prepare(player.getUuid());
}

@Override
public ClientSession session() {
return ((ClientSessionWrapper) connection).session();
}

@Override
public void session(ClientSession session) {
((ClientSessionWrapper) connection).session(session);
}

@Inject(
method = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;onClientSettings(Lnet/minecraft/network/packet/c2s/play/ClientSettingsC2SPacket;)V",
at = @At("HEAD")
)
private void onClientSettings(ClientSettingsC2SPacket packet, CallbackInfo ci) {
for (ActOnPlayPacketAction<ClientSettingsC2SPacket> action: ActOnPlayPacketHandler.getActionsFor(this.session(), ClientSettingsC2SPacket.class)) {
action.execute(connection, player, packet);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.koralix.oneforall.network;

import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.Packet;
import net.minecraft.server.network.ServerPlayerEntity;

@FunctionalInterface
public interface ActOnPlayPacketAction<T extends Packet<?>> {
void execute(ClientConnection connection, ServerPlayerEntity player, T packet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.koralix.oneforall.network;

import net.minecraft.network.packet.Packet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActOnPlayPacketHandler {
private static final Map<ClientSession, Map<Class<Packet<?>>, List<ActOnPlayPacketAction<?>>>> REGISTRY = new HashMap<>();

public static <T extends Packet<?>> void register(Class<T> clazz, ClientSession session, ActOnPlayPacketAction<T> action) {
if (!REGISTRY.containsKey(session)) REGISTRY.put(session, new HashMap<>());
if (!REGISTRY.get(session).containsKey(clazz)) REGISTRY.get(session).put((Class<Packet<?>>) clazz, new ArrayList<>());

REGISTRY.get(session).get(clazz).add(action);
}

public static <T extends Packet<?>> List<ActOnPlayPacketAction<T>> getActionsFor(ClientSession session, Class<T> clazz) {
if (REGISTRY.containsKey(session) && REGISTRY.get(session).containsKey(clazz)) {
// This cast should be safe since T extends Packet<?>
return (List<ActOnPlayPacketAction<T>>)(Object)REGISTRY.get(session).get(clazz);
}
return new ArrayList<>();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/koralix/oneforall/network/ClientSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.koralix.oneforall.network;

public class ClientSession {
private String language;
private String modVersion;

public String language() {
return language;
}

public void language(String language) {
this.language = language;
}

public String modVersion() {
return modVersion;
}

public void modVersion(String modVersion) {
this.modVersion = modVersion;
}

@Override
public String toString() {
return "ClientSession{" +
"language='" + language + '\'' +
", modVersion='" + modVersion + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.koralix.oneforall.network;

public interface ClientSessionWrapper {
ClientSession session();
void session(ClientSession session);
}
Loading