Files
MCCustomSkinLoader/Common/source/customskinloader/utils/MinecraftUtil.java
T

109 lines
3.5 KiB
Java
Raw Normal View History

2016-04-30 22:12:26 +08:00
package customskinloader.utils;
import java.io.File;
import java.net.URL;
2016-05-07 19:41:32 +08:00
import java.net.URLDecoder;
2016-09-24 20:47:08 +08:00
import java.util.ArrayList;
2016-04-30 22:12:26 +08:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
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;
import org.apache.commons.lang3.StringUtils;
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 ArrayList<String> minecraftVersion = new ArrayList<String>();
private static String minecraftMainVersion = null;
2018-02-12 12:20:28 +08:00
private final static Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile(".*?(\\d+\\.\\d+[\\.]?\\d*).*?");
2019-09-18 22:15:58 +08:00
public static ArrayList<String> getMinecraftVersions() {
if (minecraftVersion != null && !minecraftVersion.isEmpty())
2018-02-12 12:20:28 +08:00
return minecraftVersion;
testProbe();
return minecraftVersion;
}
2019-09-18 22:15:58 +08:00
public static String getMinecraftVersionText() {
StringBuilder sb = new StringBuilder();
for (String version : getMinecraftVersions())
2018-02-12 12:20:28 +08:00
sb.append(version).append(" ");
return StringUtils.trim(sb.toString());
}
2019-09-18 22:15:58 +08:00
public static String getMinecraftMainVersion() {
if (minecraftMainVersion != null)
2018-02-12 12:20:28 +08:00
return minecraftMainVersion;
2019-09-18 22:15:58 +08:00
for (String version : getMinecraftVersions()) {
2018-02-12 12:20:28 +08:00
Matcher m = null;
2019-09-18 22:15:58 +08:00
try {
m = MINECRAFT_VERSION_PATTERN.matcher(version);
} catch (Exception e) {
2018-02-12 12:20:28 +08:00
e.printStackTrace();
}
2019-09-18 22:15:58 +08:00
if (m == null || !m.matches())
2018-02-12 12:20:28 +08:00
continue;
2019-09-18 22:15:58 +08:00
minecraftMainVersion = m.group(m.groupCount());
2018-02-12 12:20:28 +08:00
break;
}
return minecraftMainVersion;
}
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() {
2018-08-26 07:04:25 +08:00
net.minecraft.client.multiplayer.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
private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/([^\\/\\\\]*?)/([^\\/\\\\]*?).jar$");
2019-09-18 22:15:58 +08:00
private static void testProbe() {
2018-02-12 12:20:28 +08:00
minecraftVersion.clear();
2018-07-20 23:23:37 +08:00
URL urls[] = JavaUtil.getClasspath();
2019-09-18 22:15:58 +08:00
for (URL url : urls) {
2018-02-12 12:20:28 +08:00
Matcher m = null;
2019-09-18 22:15:58 +08:00
try {
m = MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(), "UTF-8"));
} catch (Exception e) {
2018-02-12 12:20:28 +08:00
e.printStackTrace();
}
2019-09-18 22:15:58 +08:00
if (m == null || !m.matches())
2018-02-12 12:20:28 +08:00
continue;
minecraftVersion.add(m.group(2));
}
}
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
}