-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMinecraftUtil.java
More file actions
90 lines (76 loc) · 2.88 KB
/
Copy pathMinecraftUtil.java
File metadata and controls
90 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package customskinloader.utils;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.SkinManager;
/**
* @author Alexander Xia
* @since 13.6
*/
public class MinecraftUtil {
public static File getMinecraftDataDir() {
return Minecraft.getMinecraft().gameDir;
}
public static TextureManager getTextureManager() {
return Minecraft.getMinecraft().getTextureManager();
}
public static SkinManager getSkinManager() {
return Minecraft.getMinecraft().getSkinManager();
}
private static String minecraftMainVersion = null;
public static String getMinecraftMainVersion() {
//Check if cached version found
if (minecraftMainVersion != null) {
return minecraftMainVersion;
}
//version.json can be found in 1.14+
URL versionFile = ClassLoader.getSystemClassLoader().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) {
}
}
//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
return "unknown";
}
// (domain|ip)(:port)
public static String getServerAddress() {
ServerData data = Minecraft.getMinecraft().getCurrentServerData();
if (data == null)//Single Player
return null;
return data.serverIP;
}
// ip:port
public static String getStandardServerAddress() {
return HttpUtil0.parseAddress(getServerAddress());
}
public static boolean isLanServer() {
return HttpUtil0.isLanServer(getStandardServerAddress());
}
public static String getCredential(GameProfile profile) {
return profile == null ? null : profile.toString();
}
}