Files

91 lines
3.0 KiB
Java
Raw Permalink Normal View History

2016-04-30 22:12:26 +08:00
package customskinloader.utils;
import java.io.File;
2022-07-27 17:23:50 +08:00
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
2016-04-30 22:12:26 +08:00
import java.net.URL;
2022-07-27 17:23:50 +08:00
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
2018-02-03 18:40:04 +08:00
import com.mojang.authlib.GameProfile;
2018-08-26 07:04:25 +08:00
import net.minecraft.client.Minecraft;
2022-12-07 19:38:24 +08:00
import net.minecraft.client.multiplayer.ServerData;
2016-12-25 17:05:30 +08:00
import net.minecraft.client.renderer.texture.TextureManager;
2019-12-11 18:53:57 +08:00
import net.minecraft.client.resources.SkinManager;
2016-12-25 17:05:30 +08:00
2016-04-30 22:12:26 +08:00
/**
* @author Alexander Xia
* @since 13.6
*/
public class MinecraftUtil {
2019-09-18 22:15:58 +08:00
public static File getMinecraftDataDir() {
2018-08-11 07:34:13 +08:00
return Minecraft.getMinecraft().gameDir;
2018-02-12 12:20:28 +08:00
}
2019-09-18 22:15:58 +08:00
public static TextureManager getTextureManager() {
2018-08-26 07:04:25 +08:00
return Minecraft.getMinecraft().getTextureManager();
2018-02-12 12:20:28 +08:00
}
2019-09-18 22:15:58 +08:00
public static SkinManager getSkinManager() {
return Minecraft.getMinecraft().getSkinManager();
}
private static String minecraftMainVersion = null;
public static String getMinecraftMainVersion() {
2022-07-27 17:23:50 +08:00
//Check if cached version found
if (minecraftMainVersion != null) {
2018-02-12 12:20:28 +08:00
return minecraftMainVersion;
2022-07-27 17:23:50 +08:00
}
//version.json can be found in 1.14+
URL versionFile = MinecraftUtil.class.getResource("/version.json");
if (versionFile != null) {
try (
InputStream is = versionFile.openStream();
InputStreamReader isr = new InputStreamReader(is)
) {
JsonObject obj = new JsonParser().parse(isr).getAsJsonObject();
minecraftMainVersion = obj.get("name").getAsString();
return minecraftMainVersion;
} catch (Exception ignored) {
2018-02-12 12:20:28 +08:00
}
}
2022-07-27 17:23:50 +08:00
//RealmsSharedConstants.VERSION_STRING is available in 1.16-
try {
Class<?> realmsSharedConstants = Class.forName("net.minecraft.realms.RealmsSharedConstants");
MethodHandle mh = MethodHandles.publicLookup().findStaticGetter(realmsSharedConstants, "VERSION_STRING", String.class);
minecraftMainVersion = (String) mh.invoke();
return minecraftMainVersion;
} catch (Throwable ignored) {
}
//No version can be found
2022-07-27 19:31:32 +08:00
return "unknown";
2018-02-12 12:20:28 +08:00
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
// (domain|ip)(:port)
2019-09-18 22:15:58 +08:00
public static String getServerAddress() {
2022-12-07 19:38:24 +08:00
ServerData data = Minecraft.getMinecraft().getCurrentServerData();
2019-09-18 22:15:58 +08:00
if (data == null)//Single Player
2018-02-12 12:20:28 +08:00
return null;
return data.serverIP;
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
// ip:port
2019-09-18 22:15:58 +08:00
public static String getStandardServerAddress() {
2018-02-12 12:20:28 +08:00
return HttpUtil0.parseAddress(getServerAddress());
}
2019-09-18 22:15:58 +08:00
public static boolean isLanServer() {
2018-02-12 12:20:28 +08:00
return HttpUtil0.isLanServer(getStandardServerAddress());
}
2019-09-18 22:15:58 +08:00
2018-02-12 12:20:28 +08:00
public static String getCredential(GameProfile profile) {
2019-09-18 22:15:58 +08:00
return (profile == null || profile.hashCode() == 0) ? null :
(profile.getId() == null ? profile.getName() : String.format("%s-%s", profile.getName(), profile.getId()));
2018-02-12 12:20:28 +08:00
}
2016-04-30 22:12:26 +08:00
}