net.guizhanss.guizhanlib.minecraft.LanguageHelper Maven / Gradle / Ivy
The newest version!
package net.guizhanss.guizhanlib.minecraft;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.experimental.UtilityClass;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* 从Minecraft的语言文件中获取内容。
* 只有读取语言文件后,其他包内的助手才能正常使用。
*
* @author ybw0014
*/
@UtilityClass
@SuppressWarnings("ConstantConditions")
public final class LanguageHelper {
private static final Gson GSON = new Gson();
private static Map lang = new HashMap<>();
/**
* 从输入流({@link InputStream})中读取语言文件
*
* @param stream 输入流({@link InputStream})
* @throws IllegalArgumentException 如果输入流为空
*/
public static void loadFromStream(@Nonnull InputStream stream) {
Preconditions.checkArgument(stream != null, "输入流不能为空");
BufferedReader reader = new BufferedReader(new InputStreamReader(
stream, StandardCharsets.UTF_8
));
// @formatter:off
Type type = new TypeToken