Skip to content

Commit 9261757

Browse files
committed
Server subproject deletion. Ignore server task for versions without server
1 parent dba694d commit 9261757

5 files changed

Lines changed: 104 additions & 52 deletions

File tree

src/main/java/org/mcphackers/mcp/MCP.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.fusesource.jansi.Ansi;
55
import org.fusesource.jansi.AnsiConsole;
66
import org.mcphackers.mcp.tasks.info.TaskInfo;
7+
import org.mcphackers.mcp.tools.VersionsParser;
78

9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
811
import java.util.*;
912

1013
public class MCP {
@@ -33,15 +36,29 @@ public static void main(String[] args) {
3336
input = new Scanner(System.in);
3437
logger.log("Operating system: " + System.getProperty("os.name"));
3538
logger.log("RetroMCP " + VERSION);
39+
40+
if(Files.exists(Paths.get(MCPConfig.VERSION)))
41+
{
42+
try {
43+
VersionsParser.setCurrentVersion(new String(Files.readAllBytes(Paths.get(MCPConfig.VERSION))));
44+
} catch (Exception e) {
45+
logger.info(new Ansi().fgBrightRed().a("Unable to get current version!").fgDefault().toString());
46+
}
47+
}
3648

3749
boolean startedWithNoParams = false;
3850
boolean exit = false;
3951

4052
if (args.length <= 0) {
4153
startedWithNoParams = true;
4254
logger.println(logo);
43-
logger.println("Enter a command to execute:");
4455
}
56+
if(Files.exists(Paths.get(MCPConfig.VERSION))) {
57+
logger.info(new Ansi().a("Current version: ").fgBrightCyan().a(VersionsParser.getCurrentVersion()).fgDefault().toString());
58+
}
59+
if (args.length <= 0) {
60+
logger.println("Enter a command to execute:");
61+
}
4562
int executeTimes = 0;
4663
while (startedWithNoParams && !exit || !startedWithNoParams && executeTimes < 1) {
4764
while (args.length < 1) {
@@ -196,7 +213,9 @@ private static void processMultitasks(TaskInfo task) throws Exception {
196213
}
197214

198215
if(config.onlySide < 0 || config.onlySide == SideThread.SERVER) {
199-
threads.add(new SideThread(SideThread.SERVER, task.newTask(SideThread.SERVER)));
216+
if(VersionsParser.hasServer()) {
217+
threads.add(new SideThread(SideThread.SERVER, task.newTask(SideThread.SERVER)));
218+
}
200219
}
201220
logger.newLine();
202221
for (SideThread thread : threads) {

src/main/java/org/mcphackers/mcp/tasks/TaskRun.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ else if (side == CLIENT) {
4949

5050

5151
String cp = String.join(";", cpList);
52-
53-
String version = new String(Files.readAllBytes(Paths.get(MCPConfig.VERSION)));
5452

5553
List<String> args = new ArrayList<String>(
5654
Arrays.asList(new String[] {
@@ -59,7 +57,7 @@ else if (side == CLIENT) {
5957
"-Xmx1024M",
6058
"-Djava.util.Arrays.useLegacyMergeSort=true",
6159
"-Dhttp.proxyHost=betacraft.uk",
62-
"-Dhttp.proxyPort=" + VersionsParser.getProxyPort(version),
60+
"-Dhttp.proxyPort=" + VersionsParser.getProxyPort(),
6361
"-Dorg.lwjgl.librarypath=" + natives,
6462
"-Dnet.java.games.input.librarypath=" + natives,
6563
"-cp", cp,

src/main/java/org/mcphackers/mcp/tasks/TaskSetup.java

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.mcphackers.mcp.tasks;
22

3-
import java.io.BufferedWriter;
4-
import java.io.FileWriter;
53
import java.net.URI;
64
import java.nio.file.Files;
75
import java.nio.file.Path;
@@ -71,54 +69,32 @@ public void doTask() throws Exception {
7169
}
7270

7371
FileUtil.createDirectories(Paths.get(MCPConfig.CONF));
74-
try(BufferedWriter writer = new BufferedWriter(new FileWriter(MCPConfig.VERSION))) {
75-
writer.write(chosenVersion);
76-
}
72+
VersionsParser.setCurrentVersion(chosenVersion);
7773

7874
long startTime = System.currentTimeMillis();
7975
MCP.logger.info(" Downloading mappings");
80-
FileUtil.downloadFile(VersionsParser.downloadVersion(chosenVersion), Paths.get(MCPConfig.CONF, "conf.zip"));
76+
FileUtil.downloadFile(VersionsParser.downloadVersion(), Paths.get(MCPConfig.CONF, "conf.zip"));
8177
FileUtil.unzip(Paths.get(MCPConfig.CONF, "conf.zip"), Paths.get(MCPConfig.CONF), true);
8278

83-
// Create Eclipse workspace
8479
MCP.logger.info(" Setting up workspace");
8580
FileUtil.deleteDirectoryIfExists(Paths.get("workspace"));
8681
FileUtil.copyResource(MCP.class.getClassLoader().getResourceAsStream("workspace.zip"), Paths.get("workspace.zip"));
8782
FileUtil.unzip(Paths.get("workspace.zip"), Paths.get("workspace"), true);
88-
89-
// Create Intellij workspace
90-
String[] projects = { "Client", "Server" };
91-
for (String project : projects) {
92-
Path launch = Paths.get("eclipse", ".metadata", ".plugins", "org.eclipse.debug.core", ".launches", project + ".launch");
93-
if (Files.exists(launch)) {
94-
List<String> lines = Files.readAllLines(launch);
95-
String replace = "-Dhttp.proxyPort=%s";
96-
for (int i = 0; i < lines.size(); i++) {
97-
lines.set(i, lines.get(i).replace(replace, String.format(replace, VersionsParser.getProxyPort(chosenVersion))));
98-
}
99-
Files.write(launch, lines);
100-
}
101-
Path imlPath = Paths.get("eclipse", project, project + ".iml");
102-
if (Files.exists(imlPath)) {
103-
List<String> lines = Files.readAllLines(imlPath);
104-
for (int i = 0; i < lines.size(); i++) {
105-
lines.set(i, lines.get(i).replace("$MCP_LOC$", Paths.get(System.getProperty("user.dir")).toAbsolutePath().toString().replace("\\", "/")));
106-
}
107-
Files.write(imlPath, lines);
108-
}
109-
}
83+
84+
setWorkspace();
11085
MCP.logger.info(" Done in " + Util.time(System.currentTimeMillis() - startTime));
11186

11287
// Delete Minecraft.jar and Minecraft_server.jar if they exist.
11388
Files.deleteIfExists(Paths.get(MCPConfig.CLIENT));
11489
Files.deleteIfExists(Paths.get(MCPConfig.SERVER));
11590

11691
// Download Minecraft
117-
if (!chosenVersion.equals("custom")) {
118-
for(int side = 0; side <= (VersionsParser.hasServer(chosenVersion) ? 1 : 0); side++) {
92+
//if (!chosenVersion.equals("custom")) TODO
93+
{
94+
for(int side = 0; side <= (VersionsParser.hasServer() ? 1 : 0); side++) {
11995
startTime = System.currentTimeMillis();
12096
MCP.logger.info(" Downloading Minecraft " + (side == CLIENT ? "client" : "server") + "...");
121-
String url = VersionsParser.getDownloadURL(chosenVersion, side);
97+
String url = VersionsParser.getDownloadURL(side);
12298
// TODO Classic server zips
12399
Path pathOut = Paths.get(side == CLIENT ? MCPConfig.CLIENT : MCPConfig.SERVER);
124100
FileUtil.downloadFile(new URI(url).toURL(), pathOut);
@@ -139,6 +115,47 @@ public void doTask() throws Exception {
139115
FileUtil.unzip(Paths.get(MCPConfig.LIB + "natives.zip"), Paths.get(MCPConfig.NATIVES), true);
140116
}
141117

118+
private void setWorkspace() throws Exception {
119+
String[] projects = { "Client", "Server" };
120+
for (String project : projects) {
121+
Path[] filetoRead = new Path[] {
122+
Paths.get("workspace", ".metadata", ".plugins", "org.eclipse.debug.core", ".launches", project + ".launch"),
123+
Paths.get("workspace", project, ".idea", "workspace.xml"),
124+
Paths.get("workspace", project, project + ".iml"),
125+
Paths.get("workspace", ".metadata", ".plugins", "org.eclipse.debug.ui", "launchConfigurationHistory.xml")};
126+
for(int j = 0; j < filetoRead.length; j++) {
127+
if (Files.exists(filetoRead[j])) {
128+
List<String> lines = Files.readAllLines(filetoRead[j]);
129+
String replace = "-Dhttp.proxyPort=%s";
130+
for (int i = 0; i < lines.size(); i++) {
131+
switch (j) {
132+
case 0:
133+
case 1:
134+
lines.set(i, lines.get(i).replace(replace, String.format(replace, VersionsParser.getProxyPort())));
135+
break;
136+
case 2:
137+
lines.set(i, lines.get(i).replace("$MCP_LOC$", Paths.get(System.getProperty("user.dir")).toAbsolutePath().toString().replace("\\", "/")));
138+
break;
139+
case 3:
140+
if(!VersionsParser.hasServer() && lines.get(i).contains("path=&quot;Server&quot;")) {
141+
lines.remove(i);
142+
}
143+
break;
144+
}
145+
}
146+
Files.write(filetoRead[j], lines);
147+
}
148+
}
149+
}
150+
if(!VersionsParser.hasServer()) {
151+
FileUtil.deleteDirectoryIfExists(Paths.get("workspace", "Server"));
152+
FileUtil.deleteDirectoryIfExists(Paths.get("workspace/.metadata/.plugins/org.eclipse.core.resources/.projects/Server"));
153+
Files.deleteIfExists(Paths.get("workspace/.metadata/.plugins/org.eclipse.debug.core/.launches/Server.launch"));
154+
Files.deleteIfExists(Paths.get("workspace/.metadata/.plugins/org.eclipse.core.resources/.root/0.tree"));
155+
FileUtil.copyResource(MCP.class.getClassLoader().getResourceAsStream("0.tree"), Paths.get("workspace/.metadata/.plugins/org.eclipse.core.resources/.root/0.tree"));
156+
}
157+
}
158+
142159
private static String getTable(List<String> versions) {
143160
int rows = (int)Math.ceil(versions.size() / 3D);
144161
List<String>[] tableList = new List[rows];

src/main/java/org/mcphackers/mcp/tools/VersionsParser.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.mcphackers.mcp.tools;
22

3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.net.URI;
@@ -10,10 +12,12 @@
1012

1113
import org.json.JSONException;
1214
import org.json.JSONObject;
15+
import org.mcphackers.mcp.MCPConfig;
1316

1417
public class VersionsParser {
1518

1619
private static final String jsonURL = "https://mcphackers.github.io/versions/versions.json";
20+
private static String currentVersion = null;
1721
private static Exception cause = null;
1822

1923
public static final JSONObject json = getJson();
@@ -42,38 +46,52 @@ private static JSONObject getJson() {
4246
return null;
4347
}
4448

45-
public static int getProxyPort(String chosenVersion) throws Exception {
49+
public static int getProxyPort() throws Exception {
4650
checkJson();
47-
return json.getJSONObject(chosenVersion).getInt("proxy_port");
51+
return json.getJSONObject(currentVersion).getInt("proxy_port");
4852
}
4953

50-
public static String getServerVersion(String chosenVersion) throws Exception {
54+
public static String getServerVersion() throws Exception {
5155
checkJson();
52-
if(json.getJSONObject(chosenVersion).has("server")) {
53-
return json.getJSONObject(chosenVersion).getString("server");
56+
if(json.getJSONObject(currentVersion).has("server")) {
57+
return json.getJSONObject(currentVersion).getString("server");
5458
}
5559
return null;
5660
}
5761

58-
public static boolean hasServer(String chosenVersion) throws Exception {
62+
public static boolean hasServer() throws Exception {
5963
checkJson();
60-
return json.getJSONObject(chosenVersion).has("server");
64+
return json.getJSONObject(currentVersion).has("server_url");
6165
}
6266

63-
public static String getDownloadURL(String chosenVersion, int side) throws Exception {
67+
public static String getDownloadURL(int side) throws Exception {
6468
checkJson();
6569
String url = side == 0 ? "client_url" : side == 1 ? "server_url" : null;
66-
if(json.getJSONObject(chosenVersion).has(url)) {
67-
return json.getJSONObject(chosenVersion).getString(url);
70+
if(json.getJSONObject(currentVersion).has(url)) {
71+
return json.getJSONObject(currentVersion).getString(url);
6872
}
69-
return null;
73+
throw new JSONException("Could not get download link for " + (side == 0 ? "client" : "server"));
7074
}
7175

72-
public static URL downloadVersion(String chosenVersion) throws Exception {
76+
public static URL downloadVersion() throws Exception {
7377
checkJson();
74-
if(json.getJSONObject(chosenVersion).has("resources")) {
75-
return new URI("https://mcphackers.github.io/versions/" + json.getJSONObject(chosenVersion).getString("resources")).toURL();
78+
if(json.getJSONObject(currentVersion).has("resources")) {
79+
return new URI("https://mcphackers.github.io/versions/" + json.getJSONObject(currentVersion).getString("resources")).toURL();
7680
}
77-
return null;
81+
throw new JSONException("Could not get download link for mappings");
82+
}
83+
84+
public static void setCurrentVersion(String version) throws Exception {
85+
if(!json.has(version)) {
86+
throw new Exception("Invalid version detected!");
87+
}
88+
try(BufferedWriter writer = new BufferedWriter(new FileWriter(MCPConfig.VERSION))) {
89+
writer.write(version);
90+
}
91+
currentVersion = version;
92+
}
93+
94+
public static String getCurrentVersion() {
95+
return currentVersion;
7896
}
7997
}

src/main/resources/workspace.zip

-65 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)