Skip to content

Commit

Permalink
修复MixinLanguage导致的报错(最新Fabric版本已不再需要此类)
Browse files Browse the repository at this point in the history
Fix #96
  • Loading branch information
Xujiayao committed Dec 10, 2022
1 parent 4b835e8 commit 8a4697b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,13 @@
//#if MC >= 11600
//#if MC >= 11900
package top.xujiayao.mcdiscordchat.minecraft.mixins;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Language;
//#if MC >= 11800
import org.slf4j.Logger;
//#else
//$$ import org.apache.logging.log4j.Logger;
//#endif
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.Redirect;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/**
* @author Xujiayao
*/
@Mixin(Language.class)
public abstract class MixinLanguage {

@Final
@Shadow
private static Logger LOGGER;

@Final
@Shadow
private static Gson GSON;

@Final
@Shadow
private static Pattern TOKEN_PATTERN;

@SuppressWarnings({"unchecked", "mapping"})
@Redirect(method = "create", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;build()Lcom/google/common/collect/ImmutableMap;"))
private static <K, V> ImmutableMap<K, V> build(Builder<K, V> builder) {
LinkedHashMap<String, String> map = new LinkedHashMap<>((ImmutableMap<String, String>) builder.build());

FabricLoader.getInstance().getAllMods().forEach(modContainer -> {
Optional<Path> optional = modContainer.findPath("/assets/" + modContainer.getMetadata().getId() + "/lang/en_us.json");

if (optional.isPresent()) {
try (InputStream inputStream = Files.newInputStream(optional.get())) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
String string = TOKEN_PATTERN.matcher(JsonHelper.asString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
map.put(entry.getKey(), string);
}
} catch (Exception e) {
LOGGER.error("Couldn't read strings from /assets/{}", modContainer.getMetadata().getId() + "/lang/en_us.json", e);
}
}
});

return (ImmutableMap<K, V>) ImmutableMap.builder().putAll(map).build();
}
}
//#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//#if MC >= 11600
package top.xujiayao.mcdiscordchat.minecraft.mixins;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Language;
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.ModifyVariable;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import static top.xujiayao.mcdiscordchat.Main.LOGGER;

/**
* @author Xujiayao
*/
@Mixin(Language.class)
public abstract class MixinLanguage {

@Final
@Shadow
private static Gson GSON;

@Final
@Shadow
private static Pattern TOKEN_PATTERN;

@ModifyVariable(method = "create", at = @At("STORE"), ordinal = 0)
private static Map<String, String> mapInjected(Map<String, String> originalMap) {
LinkedHashMap<String, String> map = new LinkedHashMap<>(originalMap);

FabricLoader.getInstance().getAllMods().forEach(modContainer -> {
Optional<Path> optional = modContainer.findPath("/assets/" + modContainer.getMetadata().getId() + "/lang/en_us.json");

if (optional.isPresent()) {
try (InputStream inputStream = Files.newInputStream(optional.get())) {
JsonObject json = GSON.fromJson(new InputStreamReader(inputStream, StandardCharsets.UTF_8), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
String string = TOKEN_PATTERN.matcher(JsonHelper.asString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
map.put(entry.getKey(), string);
}
} catch (Exception e) {
LOGGER.error("Couldn't read strings from /assets/{}", modContainer.getMetadata().getId() + "/lang/en_us.json", e);
}
}
});

return map;
}
}
//#endif

0 comments on commit 8a4697b

Please sign in to comment.