|
1 | 1 | package com.github.games647.minecraftunblocker; |
2 | 2 |
|
3 | 3 | import java.awt.FlowLayout; |
4 | | -import java.awt.event.ActionEvent; |
5 | | -import java.awt.event.ActionListener; |
6 | 4 | import java.io.File; |
7 | | -import java.io.FileReader; |
8 | | -import java.io.IOException; |
9 | | -import java.nio.charset.StandardCharsets; |
10 | | -import java.nio.file.Files; |
11 | | -import java.nio.file.Path; |
12 | 5 | import java.util.ArrayList; |
13 | | -import java.util.Arrays; |
14 | 6 | import java.util.List; |
15 | | -import java.util.logging.Level; |
16 | | -import java.util.logging.Logger; |
17 | 7 | import java.util.stream.Stream; |
18 | 8 |
|
19 | 9 | import javax.swing.JButton; |
20 | 10 | import javax.swing.JComboBox; |
21 | 11 | import javax.swing.JFrame; |
22 | 12 | import javax.swing.JLabel; |
23 | | -import javax.swing.JOptionPane; |
24 | | - |
25 | | -import org.json.simple.JSONArray; |
26 | | -import org.json.simple.JSONObject; |
27 | | -import org.json.simple.JSONValue; |
28 | | -import org.json.simple.parser.ParseException; |
29 | 13 |
|
30 | 14 | public class MinecraftUnblocker { |
31 | 15 |
|
32 | 16 | public static void main(String[] args) { |
33 | | - File minecraftFolder = getWorkingDirectory(); |
34 | | - final File versionsFolder = new File(minecraftFolder, "versions/"); |
| 17 | + File versionsFolder = new File(getMinecraftFolder(), "versions"); |
35 | 18 |
|
36 | 19 | List<String> selectItems = new ArrayList<>(); |
| 20 | + //show a default item |
37 | 21 | selectItems.add("SELECT"); |
| 22 | + //add all versions to the list by getting the folders |
38 | 23 | Stream.of(versionsFolder.list()).forEach((fileName) -> selectItems.add(fileName)); |
39 | | - versionsFolder.listFiles(); |
40 | 24 |
|
| 25 | + //start a new window |
41 | 26 | JFrame frame = new JFrame(); |
42 | | - FlowLayout experimentLayout = new FlowLayout(); |
43 | | - frame.setLayout(experimentLayout); |
44 | | - |
| 27 | + frame.setLayout(new FlowLayout()); |
45 | 28 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 29 | + |
| 30 | + //components |
46 | 31 | frame.add(new JLabel("Select the version you want to unblock")); |
47 | | - final JComboBox<String> list = new JComboBox(selectItems.toArray()); |
| 32 | + JComboBox<String> list = new JComboBox(selectItems.toArray()); |
48 | 33 | frame.add(list); |
49 | 34 |
|
50 | 35 | JButton button = new JButton("Unblock"); |
| 36 | + button.addActionListener(new UnblockButtonListener(list, versionsFolder, frame)); |
51 | 37 | frame.add(button); |
52 | 38 |
|
| 39 | + //finish init |
53 | 40 | frame.pack(); |
54 | 41 | frame.setVisible(true); |
55 | | - button.addActionListener(new ActionListener() { |
56 | | - @Override |
57 | | - public void actionPerformed(ActionEvent actionEvent) { |
58 | | - String selectedVersion = (String) list.getSelectedItem(); |
59 | | - File sourceFolder = new File(versionsFolder, selectedVersion); |
60 | | - if (!sourceFolder.exists() || !sourceFolder.isDirectory()) { |
61 | | - JOptionPane.showMessageDialog(frame, "Folder not found"); |
62 | | - } else { |
63 | | - File destinationDir = new File(versionsFolder, selectedVersion + "-Unblock"); |
64 | | - destinationDir.mkdir(); |
65 | | - |
66 | | - Path sourceJsonFile = new File(sourceFolder, selectedVersion + ".json").toPath(); |
67 | | - Path targetJsonFile = new File(destinationDir, selectedVersion + "-Unblock" + ".json").toPath(); |
68 | | - |
69 | | - Path sourceJarFile = new File(sourceFolder, selectedVersion + ".jar").toPath(); |
70 | | - Path targetJarFile = new File(destinationDir, selectedVersion + "-Unblock" + ".jar").toPath(); |
71 | | - try { |
72 | | - Files.copy(sourceJarFile, targetJarFile); |
73 | | - Files.copy(sourceJsonFile, targetJsonFile); |
74 | | - |
75 | | - JSONObject json = (JSONObject) JSONValue |
76 | | - .parseWithException(new FileReader(targetJsonFile.toFile())); |
77 | | - json.put("id", selectedVersion + "-Unblock"); |
78 | | - JSONArray libraries = (JSONArray) json.get("libraries"); |
79 | | - for (Object libraryObj : libraries) { |
80 | | - JSONObject library = (JSONObject) libraryObj; |
81 | | - String name = (String) library.get("name"); |
82 | | - if (name.startsWith("com.mojang:netty")) { |
83 | | - libraries.remove(libraryObj); |
84 | | - break; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - Files.write(targetJsonFile, Arrays.asList(json.toJSONString()), StandardCharsets.UTF_8); |
89 | | - JOptionPane.showMessageDialog(frame, "Sucessfully created an unblocked version. \n" |
90 | | - + "Now restart your launcher and select the version with the suffix -Unblock"); |
91 | | - } catch (IOException | ParseException ex) { |
92 | | - Logger.getLogger(MinecraftUnblocker.class.getName()).log(Level.SEVERE, null, ex); |
93 | | - JOptionPane.showMessageDialog(frame, "Error " + ex.getMessage()); |
94 | | - } |
95 | | - } |
96 | | - } |
97 | | - }); |
98 | 42 | } |
99 | 43 |
|
100 | | - public static File getWorkingDirectory() { |
| 44 | + public static File getMinecraftFolder() { |
101 | 45 | String userHome = System.getProperty("user.home", "."); |
102 | 46 | File workingDirectory; |
103 | 47 | switch (OS.getPlatform()){ |
104 | 48 | case LINUX: |
105 | 49 | case SOLARIS: |
106 | 50 | workingDirectory = new File(userHome, ".minecraft/"); |
107 | 51 | break; |
| 52 | + case MACOS: |
| 53 | + workingDirectory = new File(userHome, "Library/Application Support/minecraft"); |
| 54 | + break; |
108 | 55 | case WINDOWS: |
109 | 56 | String applicationData = System.getenv("APPDATA"); |
110 | 57 | String folder = applicationData != null ? applicationData : userHome; |
111 | 58 |
|
112 | 59 | workingDirectory = new File(folder, ".minecraft/"); |
113 | 60 | break; |
114 | | - case MACOS: |
115 | | - workingDirectory = new File(userHome, "Library/Application Support/minecraft"); |
116 | | - break; |
117 | 61 | default: |
118 | 62 | workingDirectory = new File(userHome, "minecraft/"); |
119 | 63 | } |
120 | | - |
| 64 | + |
121 | 65 | return workingDirectory; |
122 | 66 | } |
123 | 67 | } |
0 commit comments