Skip to content

Commit

Permalink
Properly load default language entries from mods and Minecraft. (#229)
Browse files Browse the repository at this point in the history
* Properly load default language entries from mods and Minecraft.

This fixes dedicated servers not having any language entries loaded.

* Update library/core/resource_loader/src/main/java/org/quiltmc/qsl/resource/loader/mixin/server/LanguageMixin.java

Co-authored-by: Eli Orona <[email protected]>

Co-authored-by: Eli Orona <[email protected]>
  • Loading branch information
LambdAurora and OroArmor authored Dec 7, 2022
1 parent 2e7e9d8 commit b3fbb43
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.quiltmc.qsl.resource.loader.impl;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -52,8 +53,10 @@
import net.minecraft.resource.pack.ResourcePack;
import net.minecraft.resource.pack.ResourcePackProfile;
import net.minecraft.resource.pack.ResourcePackProvider;
import net.minecraft.resource.pack.VanillaDataPackProvider;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Language;
import net.minecraft.util.Pair;

import org.quiltmc.loader.api.ModContainer;
Expand Down Expand Up @@ -499,4 +502,31 @@ public static void registerBuiltinResourcePacks(ResourceType type, Consumer<Reso
}
}
}

/* Language stuff */

/**
* Appends to the given map all the default language entries.
*
* @param map the language map
*/
public static void appendLanguageEntries(@NotNull Map<String, String> map) {
var pack = ResourceLoaderImpl.buildMinecraftResourcePack(ResourceType.CLIENT_RESOURCES,
new DefaultResourcePack(VanillaDataPackProvider.DEFAULT_PACK_METADATA, "minecraft")
);

try (var manager = new MultiPackResourceManager(ResourceType.CLIENT_RESOURCES, List.of(pack))) {
for (var namespace : manager.getAllNamespaces()) {
var langId = new Identifier(namespace, "lang/" + Language.DEFAULT_LANGUAGE + ".json");

for (var resource : manager.getAllResources(langId)) {
try (var stream = resource.open()) {
Language.load(stream, map::put);
} catch (IOException e) {
LOGGER.error("Couldn't load language file {} from pack {}.", langId, resource.getSourceName());
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.resource.loader.mixin.server;

import com.google.common.collect.ImmutableMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.util.Language;

import org.quiltmc.loader.api.minecraft.DedicatedServerOnly;
import org.quiltmc.qsl.resource.loader.impl.ResourceLoaderImpl;

@DedicatedServerOnly
@Mixin(Language.class)
public class LanguageMixin {
@Redirect(
method = "create",
at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap$Builder;build()Lcom/google/common/collect/ImmutableMap;")
)
private static ImmutableMap<String, String> create(ImmutableMap.Builder<String, String> builder) {
var map = new Object2ObjectOpenHashMap<>(builder.buildOrThrow());
ResourceLoaderImpl.appendLanguageEntries(map);
return ImmutableMap.copyOf(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
"server.MainMixin"
],
"client": [
"client.IntegratedServerLoaderMixin",
"client.ClientBuiltinResourcePackProviderMixin",
"client.CreateWorldScreenMixin",
"client.EntryListWidgetAccessor",
"client.FontManagerResourceReloadListenerMixin",
"client.GameOptionsMixin",
"client.IntegratedServerLoaderMixin",
"client.KeyedClientResourceReloaderMixin",
"client.MinecraftClientMixin",
"client.PackScreenMixin",
"client.ResourcePackEntryAccessor"
],
"server": [
"server.LanguageMixin"
],
"injectors": {
"defaultRequire": 1
}
Expand Down

0 comments on commit b3fbb43

Please sign in to comment.