-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'neoforged/1.21.x' into ui
- Loading branch information
Showing
8 changed files
with
348 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,82 @@ | ||
--- a/net/minecraft/locale/Language.java | ||
+++ b/net/minecraft/locale/Language.java | ||
@@ -37,7 +_,8 @@ | ||
@@ -36,8 +_,10 @@ | ||
private static Language loadDefault() { | ||
Builder<String, String> builder = ImmutableMap.builder(); | ||
BiConsumer<String, String> biconsumer = builder::put; | ||
parseTranslations(biconsumer, "/assets/minecraft/lang/en_us.json"); | ||
- parseTranslations(biconsumer, "/assets/minecraft/lang/en_us.json"); | ||
- final Map<String, String> map = builder.build(); | ||
+ Map<String, net.minecraft.network.chat.Component> componentMap = new java.util.HashMap<>(); | ||
+ parseTranslations(biconsumer, componentMap::put, "/assets/minecraft/lang/en_us.json"); | ||
+ final Map<String, String> map = new java.util.HashMap<>(builder.build()); | ||
+ net.neoforged.neoforge.server.LanguageHook.captureLanguageMap(map); | ||
+ net.neoforged.neoforge.server.LanguageHook.captureLanguageMap(map, componentMap); | ||
return new Language() { | ||
@Override | ||
public String getOrDefault(String p_128127_, String p_265421_) { | ||
@@ -64,6 +_,11 @@ | ||
@@ -64,21 +_,51 @@ | ||
) | ||
.isPresent(); | ||
} | ||
+ | ||
+ @Override | ||
+ public Map<String, String> getLanguageData() { | ||
+ return map; | ||
+ } | ||
+ | ||
+ @Override | ||
+ public @org.jetbrains.annotations.Nullable net.minecraft.network.chat.Component getComponent(String key) { | ||
+ return componentMap.get(key); | ||
+ } | ||
}; | ||
} | ||
|
||
@@ -91,6 +_,9 @@ | ||
+ @Deprecated | ||
private static void parseTranslations(BiConsumer<String, String> p_282031_, String p_283638_) { | ||
+ parseTranslations(p_282031_, (key, value) -> {}, p_283638_); | ||
+ } | ||
+ | ||
+ private static void parseTranslations(BiConsumer<String, String> p_282031_, BiConsumer<String, net.minecraft.network.chat.Component> componentConsumer, String p_283638_) { | ||
try (InputStream inputstream = Language.class.getResourceAsStream(p_283638_)) { | ||
- loadFromJson(inputstream, p_282031_); | ||
+ loadFromJson(inputstream, p_282031_, componentConsumer); | ||
} catch (JsonParseException | IOException ioexception) { | ||
LOGGER.error("Couldn't read strings from {}", p_283638_, ioexception); | ||
} | ||
} | ||
|
||
public static void loadFromJson(InputStream p_128109_, BiConsumer<String, String> p_128110_) { | ||
+ loadFromJson(p_128109_, p_128110_, (key, value) -> {}); | ||
+ } | ||
+ | ||
+ public static void loadFromJson(InputStream p_128109_, BiConsumer<String, String> p_128110_, BiConsumer<String, net.minecraft.network.chat.Component> componentConsumer) { | ||
JsonObject jsonobject = GSON.fromJson(new InputStreamReader(p_128109_, StandardCharsets.UTF_8), JsonObject.class); | ||
|
||
for (Entry<String, JsonElement> entry : jsonobject.entrySet()) { | ||
+ if (entry.getValue().isJsonArray()) { | ||
+ var component = net.minecraft.network.chat.ComponentSerialization.CODEC | ||
+ .parse(com.mojang.serialization.JsonOps.INSTANCE, entry.getValue()) | ||
+ .getOrThrow(msg -> new com.google.gson.JsonParseException("Error parsing translation for " + entry.getKey() + ": " + msg)); | ||
+ | ||
+ p_128110_.accept(entry.getKey(), component.getString()); | ||
+ componentConsumer.accept(entry.getKey(), component); | ||
+ | ||
+ continue; | ||
+ } | ||
+ | ||
String s = UNSUPPORTED_FORMAT_PATTERN.matcher(GsonHelper.convertToString(entry.getValue(), entry.getKey())).replaceAll("%$1s"); | ||
p_128110_.accept(entry.getKey(), s); | ||
} | ||
@@ -90,6 +_,13 @@ | ||
|
||
public static void inject(Language p_128115_) { | ||
instance = p_128115_; | ||
} | ||
+ } | ||
+ | ||
+ // FORGE START | ||
+ public Map<String, String> getLanguageData() { return ImmutableMap.of(); } | ||
+ | ||
+ public @org.jetbrains.annotations.Nullable net.minecraft.network.chat.Component getComponent(String key) { | ||
+ return null; | ||
} | ||
|
||
public String getOrDefault(String p_128111_) { | ||
return this.getOrDefault(p_128111_, p_128111_); |
11 changes: 11 additions & 0 deletions
11
patches/net/minecraft/network/chat/ComponentSerialization.java.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- a/net/minecraft/network/chat/ComponentSerialization.java | ||
+++ b/net/minecraft/network/chat/ComponentSerialization.java | ||
@@ -104,7 +_,7 @@ | ||
|
||
private static Codec<Component> createCodec(Codec<Component> p_304554_) { | ||
ComponentContents.Type<?>[] type = new ComponentContents.Type[]{ | ||
- PlainTextContents.TYPE, TranslatableContents.TYPE, KeybindContents.TYPE, ScoreContents.TYPE, SelectorContents.TYPE, NbtContents.TYPE | ||
+ PlainTextContents.TYPE, TranslatableContents.TYPE, KeybindContents.TYPE, ScoreContents.TYPE, SelectorContents.TYPE, NbtContents.TYPE, net.neoforged.neoforge.common.util.InsertingContents.TYPE | ||
}; | ||
MapCodec<ComponentContents> mapcodec = createLegacyComponentMatcher(type, ComponentContents.Type::codec, ComponentContents::type, "type"); | ||
Codec<Component> codec = RecordCodecBuilder.create( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/net/neoforged/neoforge/common/util/InsertingContents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.common.util; | ||
|
||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.Optional; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.ComponentContents; | ||
import net.minecraft.network.chat.FormattedText; | ||
import net.minecraft.network.chat.Style; | ||
import net.minecraft.network.chat.contents.TranslatableContents; | ||
import net.minecraft.util.ExtraCodecs; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public record InsertingContents(int index) implements ComponentContents { | ||
public static final MapCodec<InsertingContents> CODEC = RecordCodecBuilder.mapCodec(i -> i.group( | ||
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("index").forGetter(InsertingContents::index)) | ||
.apply(i, InsertingContents::new)); | ||
|
||
public static final ComponentContents.Type<InsertingContents> TYPE = new ComponentContents.Type<>(CODEC, "neoforge:inserting"); | ||
|
||
private static final ThreadLocal<Deque<TranslatableContents>> TRANSLATION_STACK = ThreadLocal.withInitial(ArrayDeque::new); | ||
|
||
@ApiStatus.Internal | ||
public static boolean pushTranslation(TranslatableContents contents) { | ||
for (TranslatableContents other : TRANSLATION_STACK.get()) { | ||
if (contents == other) { | ||
return false; | ||
} | ||
} | ||
|
||
TRANSLATION_STACK.get().push(contents); | ||
return true; | ||
} | ||
|
||
@ApiStatus.Internal | ||
public static void popTranslation() { | ||
TRANSLATION_STACK.get().pop(); | ||
} | ||
|
||
@Override | ||
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> visitor) { | ||
var translation = TRANSLATION_STACK.get().peek(); | ||
|
||
if (translation == null || translation.getArgs().length <= index) | ||
return visitor.accept("%" + (index + 1) + "$s"); | ||
|
||
Object arg = translation.getArgs()[index]; | ||
|
||
if (arg instanceof Component component) { | ||
return component.visit(visitor); | ||
} else { | ||
return visitor.accept(arg.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> visitor, Style style) { | ||
var translation = TRANSLATION_STACK.get().peek(); | ||
|
||
if (translation == null || translation.getArgs().length <= index) | ||
return visitor.accept(style, "%" + (index + 1) + "$s"); | ||
|
||
Object arg = translation.getArgs()[index]; | ||
|
||
if (arg instanceof Component component) { | ||
return component.visit(visitor, style); | ||
} else { | ||
return visitor.accept(style, arg.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public Type<?> type() { | ||
return TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.