Skip to content

Commit

Permalink
fix: fabric-apiが入っていない場合に文章が壊れる問題の修正 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toshimichi0915 committed Aug 22, 2023
1 parent 8bbbd80 commit a9bd8d0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/toshimichi/kzeplus/KzePlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.client.option.KeyBinding;
import net.toshimichi.kzeplus.context.game.GameContextRegistry;
import net.toshimichi.kzeplus.context.weapon.WeaponContext;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class KzePlus implements ModInitializer {
public static final KeyBinding VISIBILITY_TOGGLE_KEY = new KeyBinding("kze_plus.key.toggle_visibility", GLFW.GLFW_KEY_V, KZE_CATEGORY);

@Getter private static KzePlus instance;
@Getter private ModContainer modContainer;
@Getter private KzeOptions options;
@Getter private EventRegistry eventRegistry;
@Getter private WeaponRegistry weaponRegistry;
Expand All @@ -58,6 +60,7 @@ public class KzePlus implements ModInitializer {
@Override
public void onInitialize() {
instance = this;
modContainer = FabricLoader.getInstance().getModContainer("kze_plus").orElseThrow();

// load options
if (Files.exists(CONFIG_PATH)) {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/net/toshimichi/kzeplus/mixins/MixinLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.toshimichi.kzeplus.mixins;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Language;
import net.toshimichi.kzeplus.KzePlus;
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 java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;

@Mixin(Language.class)
public class MixinLanguage {

@Final @Shadow
private static Pattern TOKEN_PATTERN;
@Final @Shadow
private static Gson GSON;

@Inject(method = "load", at = @At("HEAD"))
private static void addCustomKeys(InputStream inputStream, BiConsumer<String, String> entryConsumer, CallbackInfo ci) {
Path path = KzePlus.getInstance()
.getModContainer()
.findPath("assets/kze_plus/lang/en_us.json")
.orElseThrow();

try (InputStream in = path.toUri().toURL().openStream()) {
JsonObject jsonObject = GSON.fromJson(new InputStreamReader(in, StandardCharsets.UTF_8), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
String string = TOKEN_PATTERN.matcher(JsonHelper.asString(entry.getValue(), entry.getKey())).replaceAll("%$1s");
entryConsumer.accept(entry.getKey(), string);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/kzeplus.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"MixinEntity",
"MixinGameOptions",
"MixinInGameHud",
"MixinLanguage",
"MixinLivingEntity",
"MixinLivingEntityRenderer",
"MixinMessageHandler",
Expand Down

0 comments on commit a9bd8d0

Please sign in to comment.