Skip to content
This repository was archived by the owner on Dec 14, 2025. It is now read-only.

Commit 9583ef1

Browse files
authored
Merge pull request #2 from qiang-di/main
小优化
2 parents 1738840 + 476fba3 commit 9583ef1

File tree

4 files changed

+98
-13
lines changed

4 files changed

+98
-13
lines changed

src/main/java/org/mcdcl/launcher/GameLauncher.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,13 @@ public void stopGame() {
213213
public boolean isRunning() {
214214
return gameProcess != null && gameProcess.isAlive();
215215
}
216-
}
216+
}
217+
218+
// 在launchGame方法中添加更多的预检查
219+
// 例如检查磁盘空间是否足够
220+
File minecraftFolder = minecraftDir.getRoot();
221+
long freeSpace = minecraftFolder.getFreeSpace();
222+
long requiredSpace = 200 * 1024 * 1024; // 假设需要200MB空间
223+
if (freeSpace < requiredSpace) {
224+
throw new LaunchException("磁盘空间不足,至少需要" + (requiredSpace / (1024 * 1024)) + "MB可用空间");
225+
}

src/main/java/org/mcdcl/ui/ErrorView.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ private void initUI() {
101101
scrollPane
102102
);
103103

104+
// 在initUI方法中添加常见问题解决建议
105+
Label helpLabel = new Label("常见解决方法:");
106+
helpLabel.setFont(Font.font("System", FontWeight.BOLD, 14));
107+
108+
VBox helpBox = new VBox(5);
109+
helpBox.getChildren().addAll(
110+
new Label("1. 检查Java版本是否与游戏版本兼容"),
111+
new Label("2. 确保网络连接正常"),
112+
new Label("3. 检查是否有足够的磁盘空间"),
113+
new Label("4. 尝试重新下载游戏文件")
114+
);
115+
116+
// 将帮助信息添加到界面
117+
centerBox.getChildren().addAll(helpLabel, helpBox);
104118
setTop(topBox);
105119
setCenter(centerBox);
106120
setBottom(buttonBox);
@@ -116,4 +130,4 @@ private String getStackTraceAsString(Throwable throwable) {
116130
throwable.printStackTrace(pw);
117131
return sw.toString();
118132
}
119-
}
133+
}

src/main/java/org/mcdcl/ui/MultiplayerView.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,36 @@ private String generateRandomKey() {
345345
}
346346

347347
private boolean validateKey(String key) {
348-
// 简单验证密钥格式
349-
return key != null && key.length() == 16 &&
350-
key.matches("^[a-zA-Z0-9]+$");
348+
// 更强的密钥验证
349+
if (key == null || key.isEmpty()) {
350+
return false;
351+
}
352+
353+
// 检查长度
354+
if (key.length() < 16 || key.length() > 32) {
355+
return false;
356+
}
357+
358+
// 检查格式 - 要求包含字母和数字的组合
359+
boolean hasLetter = false;
360+
boolean hasDigit = false;
361+
362+
for (char c : key.toCharArray()) {
363+
if (Character.isLetter(c)) {
364+
hasLetter = true;
365+
} else if (Character.isDigit(c)) {
366+
hasDigit = true;
367+
} else if (!Character.isLetterOrDigit(c)) {
368+
// 不允许特殊字符
369+
return false;
370+
}
371+
372+
if (hasLetter && hasDigit) {
373+
break;
374+
}
375+
}
376+
377+
return hasLetter && hasDigit;
351378
}
352379

353380
private String establishP2PConnection(String key) throws Exception {
@@ -374,4 +401,4 @@ private void joinSelectedServer() {
374401
mainView.showAlert("未选择服务器", "请先从列表中选择一个服务器");
375402
}
376403
}
377-
}
404+
}

src/main/java/org/mcdcl/util/ResourcePackManager.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,50 @@ public static void downloadResourcePack(String packName, Consumer<Double> progre
3434
// 确保目录存在
3535
resourcePackDir.mkdirs();
3636

37-
// TODO: 实现实际的资源包下载逻辑
38-
// 这里暂时使用模拟下载
39-
for (double progress = 0; progress <= 1; progress += 0.1) {
40-
Thread.sleep(500);
41-
progressCallback.accept(progress);
37+
// 查找资源包下载链接
38+
URL url = new URL(RESOURCE_PACK_API);
39+
JSONObject json = new JSONObject(new String(url.openStream().readAllBytes()));
40+
JSONArray hits = json.getJSONArray("hits");
41+
42+
String downloadUrl = null;
43+
for (int i = 0; i < hits.length(); i++) {
44+
JSONObject pack = hits.getJSONObject(i);
45+
if (pack.getString("title").equals(packName)) {
46+
// 假设API返回的结构中包含下载链接
47+
downloadUrl = pack.getString("download_url");
48+
break;
49+
}
50+
}
51+
52+
if (downloadUrl == null) {
53+
throw new IOException("找不到资源包: " + packName);
4254
}
4355

56+
// 执行下载
57+
URL packUrl = new URL(downloadUrl);
58+
String fileName = packName.replaceAll("[^a-zA-Z0-9.-]", "_") + ".zip";
59+
File outputFile = new File(resourcePackDir, fileName);
60+
61+
try (InputStream in = packUrl.openStream();
62+
FileOutputStream out = new FileOutputStream(outputFile)) {
63+
64+
byte[] buffer = new byte[8192];
65+
long totalBytes = 0;
66+
int bytesRead;
67+
long fileSize = packUrl.openConnection().getContentLengthLong();
68+
69+
while ((bytesRead = in.read(buffer)) != -1) {
70+
out.write(buffer, 0, bytesRead);
71+
totalBytes += bytesRead;
72+
73+
if (fileSize > 0) {
74+
double progress = (double) totalBytes / fileSize;
75+
progressCallback.accept(progress);
76+
}
77+
}
78+
}
4479
} catch (Exception e) {
45-
throw new IOException("下载资源包失败: " + e.getMessage());
80+
throw new IOException("下载资源包失败: " + e.getMessage(), e);
4681
}
4782
}
48-
}
83+
}

0 commit comments

Comments
 (0)