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 all commits
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
74 changes: 74 additions & 0 deletions src/main/java/com/koralix/oneforall/lang/Language.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.koralix.oneforall.lang;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koralix.oneforall.OneForAll;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

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

public enum Language {
SPANISH("es_es"),
GALICIAN("gl_es");

private static final Map<String, Language> LANGUAGES;

static {
Map<String, Language> languages = new HashMap<>();
for (Language language : values()) {
languages.put(language.code, language);
}
LANGUAGES = Map.copyOf(languages);
}

private final String code;
private final Map<String, String> translations;

Language(String code) throws IllegalArgumentException {
this.code = code;

OneForAll.getInstance().getLogger().info("Loading translations from {}", code);

Map<String, String> translationMap = new HashMap<>();
String languageFile = "/assets/oneforall/lang/" + code + ".json";

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());
}

this.translations = Map.copyOf(translationMap);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to load translations from " + code, e);
}
}

public String getCode() {
return code;
}

public boolean containsKey(String code) {
return translations.containsKey(code);
}

public Optional<String> translate(String code) {
return Optional.ofNullable(translations.get(code));
}

@Override
public String toString() {
return code;
}

public static Language fromCode(String s) {
return LANGUAGES.get(s);
}
}
65 changes: 65 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,65 @@
package com.koralix.oneforall.lang;

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.util.ListIterator;
import java.util.Optional;
import java.util.UUID;

public class TranslationUnit {
private final static Language DEFAULT_LANGUAGE = Language.SPANISH;
private static Optional<UUID> TARGET_PLAYER = Optional.empty();

public static String translate(Language lang, String code) {
return lang.translate(code)
.or(() -> ServerSettings.DEFAULT_LANGUAGE.value().translate(code))
.or(() -> DEFAULT_LANGUAGE.translate(code))
.orElse(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) { // TODO: Version dependent translations
return result;
}

if (!text.getSiblings().isEmpty()) {
ListIterator<Text> iterator = text.getSiblings().listIterator();

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

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

return result;
}

}
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.ClientSession;
import com.koralix.oneforall.network.ClientSessionWrapper;
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,23 @@
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.ModifyArg;

@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,28 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.network.ClientSession;
import com.koralix.oneforall.network.ClientSessionWrapper;
import net.minecraft.network.ClientConnection;
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;

@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,55 @@
package com.koralix.oneforall.mixin.server.protocol;

import com.koralix.oneforall.lang.TranslationUnit;
import com.koralix.oneforall.network.ActOnPlayPacketAction;
import com.koralix.oneforall.network.ActOnPlayPacketHandler;
import com.koralix.oneforall.network.ClientSession;
import com.koralix.oneforall.network.ClientSessionWrapper;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.ClientSettingsC2SPacket;
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;

@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<>();
}
}
32 changes: 32 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,32 @@
package com.koralix.oneforall.network;

import com.koralix.oneforall.lang.Language;

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

public Language language() {
return language;
}

public void language(Language 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