将我的世界数据存储在当前目录中

发布于 2024-12-10 21:00:23 字数 5040 浏览 0 评论 0原文

编辑:使用不同的反编译器现在包含 Util$OS.class 文件

我正在尝试修改minecraft启动器以检查当前工作目录中的minecraft文件夹,如果如果不存在,则使用已建立的例程前往克里特岛并下载所需的文件。这是我第一次涉足java编程,所以我感觉有点失落。这是有问题的类文件的来源:(我认为需要修改的块从第 15 行开始)

File 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;
  }
}

我已经做了一些关于获取当前工作目录的研究,但我不确定需要修改什么。如果有人至少可以解释文件各个部分的含义,那将非常有帮助。

EDIT: used different decompiler now includes the Util$OS.class file

I am trying to modify the mine craft launcher to check for a minecraft folder in the current working directory and if none exists then use the established routines to Crete and download the needed files. This is my first foray into java programing so I am feeling a bit lost. Here is the source of the offending class file: (the block that i think needs modifying starts on line 15)

File 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;
  }
}

I have done some research on getting the current working directory but i am not sure what needs modifing. If someone could at least explain what the various parts of the file mean that would be very helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞旧 2024-12-17 21:00:23
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 文件夹,因此在任何操作系统上都不会有任何问题。

希望这有帮助!

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;
}

Sorry for the confusion, this should work just fine for you.
It will create a minecraft folder in the same directory as your launcher.

Note: On OS X this will still create the folder in the folder as the .app, not the .app/Contents/Resources/Java folder that the actual JAR is in, so you wont have any problem on any operating system.

Hope this helps!

一刻暧昧 2024-12-17 21:00:23

我仍然不完全确定我理解你的目标。

如果您想让它为您下载“Minecraft”,我会尝试在批处理文件和 shell 脚本中执行此操作,然后只运行适合您系统的那个。

如果你想以某种方式从某个地方“下载”你的世界、材质包和模组,那么你也可以这样做。

如果您想要的是您正在玩的每个 Minecraft 安装都使用您的数据(例如 USB 记忆棒或其他东西),您可能有批处理文件,这些文件要么在运行 Minecraft 之前复制数据,要么使用“ln”替换目录我的世界认为它将与您自己的 USB 记忆棒一起使用。

I'm still not completely sure I understand your goals.

If you want to have it download "Minecraft" for you I'd try to do it in a batch file and shell script and just run the one that was appropriate for your system.

If you want to somehow "download" your worlds, texture packs and mods from somewhere then you could do the same.

If what you want is for every minecraft install you are playing on to use your data (like on a USB stick or something) you might have batch files that either copy over the data before running minecraft or perhaps use "ln" to replace the directories that minecraft thinks it's going to use with your own on your usb stick.

左耳近心 2024-12-17 21:00:23

您始终可以修改指向 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 记忆棒上、自定义文件夹中等)

You can always modify the field that points to the directory where MC puts its saves and change it to whatever you want. Here's a snippet from my launcher (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();
}

This takes the hardcoded path field inside the Minecraft class and modifies it to whatever you want it to be. (e.g. on a USB stick, in a custom folder, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文