将Minecraft数据存储在当前目录中
题
编辑:使用的不同的分解器现在包括util $ os.class文件
我正在尝试修改地雷工艺发射器,以检查 minecraft
当前工作目录中的文件夹,如果不存在,则使用已建立的例程来克里特岛并下载所需的文件。这是我第一次涉足Java编程,所以我感到有些迷路。这是有问题的类文件的来源:(我认为需要修改的块从第15行开始)
文件util.class
package net.minecraft;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.security.PublicKey;
import java.security.cert.Certificate;
import javax.net.ssl.HttpsURLConnection;
public class Util
{
private static File workDir = null;
public static File getWorkingDirectory() {
if (workDir == null) workDir = getWorkingDirectory("minecraft");
return workDir;
}
public static File getWorkingDirectory(String applicationName) {
String userHome = System.getProperty("user.home", ".");
File workingDirectory;
File workingDirectory;
File workingDirectory;
File workingDirectory;
switch ($SWITCH_TABLE$net$minecraft$Util$OS()[getPlatform().ordinal()]) {
case 1:
case 2:
workingDirectory = new File(userHome, '.' + applicationName + '/');
break;
case 3:
String applicationData = System.getenv("APPDATA");
File workingDirectory;
if (applicationData != null) workingDirectory = new File(applicationData, "." + applicationName + '/'); else
workingDirectory = new File(userHome, '.' + applicationName + '/');
break;
case 4:
workingDirectory = new File(userHome, "Library/Application Support/" + applicationName);
break;
default:
workingDirectory = new File(userHome, applicationName + '/');
}
if ((!workingDirectory.exists()) && (!workingDirectory.mkdirs())) throw new RuntimeException("The working directory could not be created: " + workingDirectory);
return workingDirectory;
}
private static OS getPlatform() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) return OS.windows;
if (osName.contains("mac")) return OS.macos;
if (osName.contains("solaris")) return OS.solaris;
if (osName.contains("sunos")) return OS.solaris;
if (osName.contains("linux")) return OS.linux;
if (osName.contains("unix")) return OS.linux;
return OS.unknown;
}
public static String excutePost(String targetURL, String urlParameters)
{
HttpsURLConnection connection = null;
try
{
URL url = new URL(targetURL);
connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
Certificate[] certs = connection.getServerCertificates();
byte[] bytes = new byte[294];
DataInputStream dis = new DataInputStream(Util.class.getResourceAsStream("minecraft.key"));
dis.readFully(bytes);
dis.close();
Certificate c = certs[0];
PublicKey pk = c.getPublicKey();
byte[] data = pk.getEncoded();
for (int i = 0; i < data.length; i++) {
if (data[i] == bytes[i]) continue; throw new RuntimeException("Public key mismatch");
}
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
String line;
response.append(line);
response.append('\r');
}
rd.close();
String str1 = response.toString();
return str1;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
connection.disconnect();
}
throw localObject;
}
public static boolean isEmpty(String str) {
return (str == null) || (str.length() == 0);
}
public static void openLink(URI uri) {
try {
Object o = Class.forName("java.awt.Desktop").getMethod("getDesktop", new Class[0]).invoke(null, new Object[0]);
o.getClass().getMethod("browse", new Class[] { URI.class }).invoke(o, new Object[] { uri });
} catch (Throwable e) {
System.out.println("Failed to open link " + uri.toString());
}
}
private static enum OS
{
linux, solaris, windows, macos, unknown;
}
}
我已经对获取当前的工作目录进行了一些研究,但我不确定需要修改什么。如果有人至少可以解释文件的各个部分意味着什么都非常有帮助。
解决方案
public static File getWorkingDirectory(String applicationName) {
File workingDirectory = new File("." + File.separator + applicationName);
if ((!workingDirectory.exists()) && (!workingDirectory.mkdirs()))
throw new RuntimeException("The working directory could not be created: " + workingDirectory);
return workingDirectory;
}
对不起,这对您来说应该很好。它将与启动器相同的目录中创建一个Minecraft文件夹。
注意:在OS X上,这仍然将在文件夹中创建文件夹作为.App,而不是实际jar所在的.App/contents/resources/java文件夹,因此您在任何操作系统上都不会有任何问题。
希望这可以帮助!
其他提示
您始终可以修改指向MC放置并将其保存的目录指向目录的字段。这是我发射器的摘要(http://www.github.com/lekro/moddishlauncher):
ClassLoader cl = new URLClassLoader(urls, ModdishLauncher.class.getClassLoader());
Class<?> mc = null;
try {
mc = cl.loadClass("net.minecraft.client.Minecraft");
} catch (ClassNotFoundException e2) {
System.err.println("Couldn't find Minecraft main class!");
e2.printStackTrace();
}
Field[] fields = mc.getDeclaredFields();
Field mcPathField = null;
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (f.getType() != File.class) {
continue;
}
if (f.getModifiers() != (Modifier.PRIVATE + Modifier.STATIC)) {
continue;
}
mcPathField = f;
break;
}
mcPathField.setAccessible(true);
try {
mcPathField.set(null, new File(myDir + "/minecrafts/"+minecraftType+"/"));
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e2) {
e2.printStackTrace();
}
这将采用Minecraft类内的硬编码路径字段,并将其修改为您想要的任何东西。 (例如,在USB棒上,在自定义文件夹中等)
我仍然不确定我了解您的目标。
如果您想为您下载“ Minecraft”,我会尝试在批处理文件和Shell脚本中进行操作,然后运行适合您的系统的文件。
如果您想以某种方式“下载”自己的世界,纹理包和mod,那么您可以做同样的事情。
如果您想要的是每个Minecraft安装,您正在播放使用数据(例如在USB棒或其他内容),您可能会有批处理文件,这些文件可能会在运行Minecraft之前通过数据复制或使用“ LN”来替换目录我的Minecraft认为它将在USB棒上与您自己一起使用。