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

133 lines
4.0 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;
import java.net.URLClassLoader;
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;
2016-09-24 20:47:08 +08:00
import org.apache.commons.lang3.StringUtils;
2018-02-03 18:40:04 +08:00
import com.mojang.authlib.GameProfile;
2016-12-25 17:05:30 +08:00
import com.mojang.authlib.minecraft.MinecraftSessionService;
import net.minecraft.client.renderer.texture.TextureManager;
2016-04-30 22:12:26 +08:00
/**
* @author Alexander Xia
* @since 13.6
*
*/
public class MinecraftUtil {
2016-12-25 17:05:30 +08:00
public static File getMinecraftDataDir(){
2016-04-30 22:12:26 +08:00
return net.minecraft.client.Minecraft.getMinecraft().mcDataDir;
}
2016-12-25 17:05:30 +08:00
public static MinecraftSessionService getSessionService(){
return net.minecraft.client.Minecraft.getMinecraft().getSessionService();
}
public static TextureManager getTextureManager(){
return net.minecraft.client.Minecraft.getMinecraft().getTextureManager();
2016-04-30 22:12:26 +08:00
}
2016-09-24 20:47:08 +08:00
private static ArrayList<String> minecraftVersion=new ArrayList<String>();
private static String minecraftMainVersion=null;
2016-11-11 19:48:42 +08:00
private final static Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile(".*?(\\d+\\.\\d+[\\.]?\\d*).*?");
2016-09-24 20:47:08 +08:00
public static ArrayList<String> getMinecraftVersions(){
if(minecraftVersion!=null&&!minecraftVersion.isEmpty())
2016-05-07 13:11:08 +08:00
return minecraftVersion;
testProbe();
return minecraftVersion;
}
2016-09-24 20:47:08 +08:00
public static String getMinecraftVersionText(){
StringBuilder sb=new StringBuilder();
for(String version:getMinecraftVersions())
sb.append(version).append(" ");
return StringUtils.trim(sb.toString());
}
public static String getMinecraftMainVersion(){
if(minecraftMainVersion!=null)
return minecraftMainVersion;
for(String version:getMinecraftVersions()){
Matcher m = null;
try{
2016-11-11 19:48:42 +08:00
m=MINECRAFT_VERSION_PATTERN.matcher(version);
2016-09-24 20:47:08 +08:00
}catch(Exception e){
e.printStackTrace();
}
2016-11-11 19:48:42 +08:00
if(m==null || !m.matches())
2016-09-24 20:47:08 +08:00
continue;
2016-11-11 19:48:42 +08:00
minecraftMainVersion=m.group(m.groupCount());
2016-09-24 20:47:08 +08:00
break;
}
return minecraftMainVersion;
}
2016-05-07 13:11:08 +08:00
2016-11-12 19:33:01 +08:00
// (domain|ip)(:port)
2016-10-18 21:48:07 +08:00
public static String getServerAddress(){
net.minecraft.client.multiplayer.ServerData data=net.minecraft.client.Minecraft.getMinecraft().getCurrentServerData();
if(data==null)//Single Player
return null;
return data.serverIP;
}
2016-11-12 19:33:01 +08:00
// ip:port
public static String getStandardServerAddress(){
return HttpUtil0.parseAddress(getServerAddress());
}
2016-10-18 21:48:07 +08:00
public static boolean isLanServer(){
2016-11-12 19:33:01 +08:00
return HttpUtil0.isLanServer(getStandardServerAddress());
2016-10-18 21:48:07 +08:00
}
2016-11-11 19:48:42 +08:00
public static String getCurrentUsername(){
return net.minecraft.client.Minecraft.getMinecraft().getSession().getProfile().getName();
}
2016-10-18 21:48:07 +08:00
2016-08-29 10:43:40 +08:00
private final static Pattern MINECRAFT_CORE_FILE_PATTERN = Pattern.compile("^(.*?)/versions/([^\\/\\\\]*?)/([^\\/\\\\]*?).jar$");
2016-05-07 19:41:32 +08:00
private static void testProbe(){
2016-09-24 20:47:08 +08:00
minecraftVersion.clear();
2016-05-07 13:11:08 +08:00
URLClassLoader ucl = (URLClassLoader)new MinecraftUtil().getClass().getClassLoader();
URL urls[] = ucl.getURLs();
for(URL url:urls){
2016-05-07 19:41:32 +08:00
Matcher m = null;
try{
m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
}catch(Exception e){
e.printStackTrace();
}
2016-09-24 20:47:08 +08:00
if(m==null || !m.matches())
2016-05-07 13:11:08 +08:00
continue;
2016-09-24 20:47:08 +08:00
minecraftVersion.add(m.group(2));
2016-05-07 13:11:08 +08:00
}
2016-04-30 22:12:26 +08:00
}
2016-12-25 17:05:30 +08:00
2016-07-31 22:00:46 +08:00
public static boolean isCoreFile(URL url){
Matcher m = null;
try{
m=MINECRAFT_CORE_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
}catch(Exception e){
e.printStackTrace();
return false;
}
2016-09-24 20:47:08 +08:00
return m!=null && m.matches();
}
private final static Pattern LIBRARY_FILE_PATTERN = Pattern.compile("^(.*?)/libraries/(.*?)/([^\\/\\\\]*?).jar$");
public static boolean isLibraryFile(URL url){
Matcher m = null;
try{
m=LIBRARY_FILE_PATTERN.matcher(URLDecoder.decode(url.getPath(),"UTF-8"));
}catch(Exception e){
e.printStackTrace();
2016-07-31 22:00:46 +08:00
return false;
2016-09-24 20:47:08 +08:00
}
return m!=null && m.matches();
2016-07-31 22:00:46 +08:00
}
2018-02-03 18:40:04 +08:00
public static String getCredential(GameProfile profile) {
if(profile!=null&&profile.hashCode()!=0)
return new StringBuilder().
append(profile.getName()).append("-").append(profile.getId().toString()).toString();
else
return null;
}
2016-04-30 22:12:26 +08:00
}