Skip to content

Commit fcc925e

Browse files
committed
Clean up code
1 parent 8620e43 commit fcc925e

File tree

6 files changed

+147
-104
lines changed

6 files changed

+147
-104
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 games647 and contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

ReadMe.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
## Description
44

5-
Copies a select Minecraft version and removes the blocking server implementation.
5+
Copies a select Minecraft version and removes the blocking server implementation. So you can play on Mojang blacklisted
6+
servers again.
67

7-
An alternative to downloading the forge client
8+
An alternative to downloading the forge client.
9+
10+
## Downloads
11+
12+
https://github.com/games647/MinecraftUnblocker/releases
13+
14+
## Requirements
15+
16+
* Java 8
17+
* Minecraft
818

919
## Images
1020

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<packaging>jar</packaging>
88

99
<name>MinecraftUnblocker</name>
10-
<version>1.0</version>
10+
<version>1.1</version>
1111
<inceptionYear>2016</inceptionYear>
1212

1313
<url>https://github.com/games647/MinecraftUnblocker/</url>
@@ -54,6 +54,7 @@
5454
<include>com.googlecode.json-simple:json-simple</include>
5555
</includes>
5656
</artifactSet>
57+
<createDependencyReducedPom>false</createDependencyReducedPom>
5758
</configuration>
5859
<executions>
5960
<execution>
Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,67 @@
11
package com.github.games647.minecraftunblocker;
22

33
import java.awt.FlowLayout;
4-
import java.awt.event.ActionEvent;
5-
import java.awt.event.ActionListener;
64
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;
125
import java.util.ArrayList;
13-
import java.util.Arrays;
146
import java.util.List;
15-
import java.util.logging.Level;
16-
import java.util.logging.Logger;
177
import java.util.stream.Stream;
188

199
import javax.swing.JButton;
2010
import javax.swing.JComboBox;
2111
import javax.swing.JFrame;
2212
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;
2913

3014
public class MinecraftUnblocker {
3115

3216
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");
3518

3619
List<String> selectItems = new ArrayList<>();
20+
//show a default item
3721
selectItems.add("SELECT");
22+
//add all versions to the list by getting the folders
3823
Stream.of(versionsFolder.list()).forEach((fileName) -> selectItems.add(fileName));
39-
versionsFolder.listFiles();
4024

25+
//start a new window
4126
JFrame frame = new JFrame();
42-
FlowLayout experimentLayout = new FlowLayout();
43-
frame.setLayout(experimentLayout);
44-
27+
frame.setLayout(new FlowLayout());
4528
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29+
30+
//components
4631
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());
4833
frame.add(list);
4934

5035
JButton button = new JButton("Unblock");
36+
button.addActionListener(new UnblockButtonListener(list, versionsFolder, frame));
5137
frame.add(button);
5238

39+
//finish init
5340
frame.pack();
5441
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-
});
9842
}
9943

100-
public static File getWorkingDirectory() {
44+
public static File getMinecraftFolder() {
10145
String userHome = System.getProperty("user.home", ".");
10246
File workingDirectory;
10347
switch (OS.getPlatform()){
10448
case LINUX:
10549
case SOLARIS:
10650
workingDirectory = new File(userHome, ".minecraft/");
10751
break;
52+
case MACOS:
53+
workingDirectory = new File(userHome, "Library/Application Support/minecraft");
54+
break;
10855
case WINDOWS:
10956
String applicationData = System.getenv("APPDATA");
11057
String folder = applicationData != null ? applicationData : userHome;
11158

11259
workingDirectory = new File(folder, ".minecraft/");
11360
break;
114-
case MACOS:
115-
workingDirectory = new File(userHome, "Library/Application Support/minecraft");
116-
break;
11761
default:
11862
workingDirectory = new File(userHome, "minecraft/");
11963
}
120-
64+
12165
return workingDirectory;
12266
}
12367
}

src/main/java/com/github/games647/minecraftunblocker/OS.java

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
1-
/*
2-
* The MIT License
3-
*
4-
* Copyright 2016 Win7Home.
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
* THE SOFTWARE.
23-
*/
241
package com.github.games647.minecraftunblocker;
252

3+
import java.util.Locale;
4+
265
public enum OS {
27-
WINDOWS,
6+
LINUX,
287
MACOS,
298
SOLARIS,
30-
LINUX,
9+
WINDOWS,
3110
UNKNOWN;
3211

3312
private OS() {
3413
}
3514

3615
public static OS getPlatform() {
37-
String osName = System.getProperty("os.name").toLowerCase();
16+
String osName = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
17+
if (osName.contains("linux")) {
18+
return OS.LINUX;
19+
}
20+
3821
if (osName.contains("win")) {
3922
return OS.WINDOWS;
4023
}
@@ -43,10 +26,6 @@ public static OS getPlatform() {
4326
return OS.MACOS;
4427
}
4528

46-
if (osName.contains("linux")) {
47-
return OS.LINUX;
48-
}
49-
5029
if (osName.contains("unix")) {
5130
return OS.LINUX;
5231
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.github.games647.minecraftunblocker;
2+
3+
import java.awt.Component;
4+
import java.awt.HeadlessException;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
import java.io.File;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.Arrays;
14+
import java.util.logging.Level;
15+
import java.util.logging.Logger;
16+
17+
import javax.swing.JComboBox;
18+
import javax.swing.JOptionPane;
19+
20+
import org.json.simple.JSONArray;
21+
import org.json.simple.JSONObject;
22+
import org.json.simple.JSONValue;
23+
import org.json.simple.parser.ParseException;
24+
25+
public class UnblockButtonListener implements ActionListener {
26+
27+
private static final String UNBLOCK_SUFFIX = "-Unblock";
28+
29+
private final JComboBox<String> list;
30+
private final File versionsFolder;
31+
private final Component parentComp;
32+
33+
public UnblockButtonListener(JComboBox<String> list, File versionsFolder, Component parentComp) {
34+
this.list = list;
35+
this.versionsFolder = versionsFolder;
36+
this.parentComp = parentComp;
37+
}
38+
39+
@Override
40+
public void actionPerformed(ActionEvent e) {
41+
String selectedVersion = (String) list.getSelectedItem();
42+
File sourceFolder = new File(versionsFolder, selectedVersion);
43+
if (!sourceFolder.exists() || !sourceFolder.isDirectory()) {
44+
JOptionPane.showMessageDialog(parentComp, "Folder not found");
45+
} else {
46+
unblock(selectedVersion, sourceFolder);
47+
}
48+
}
49+
50+
private void unblock(String selectedVersion, File sourceFolder) throws HeadlessException {
51+
File destinationDir = new File(versionsFolder, selectedVersion + UNBLOCK_SUFFIX);
52+
destinationDir.mkdir();
53+
54+
Path sourceJsonFile = new File(sourceFolder, selectedVersion + ".json").toPath();
55+
Path targetJsonFile = new File(destinationDir, selectedVersion + UNBLOCK_SUFFIX + ".json").toPath();
56+
57+
Path sourceJarFile = new File(sourceFolder, selectedVersion + ".jar").toPath();
58+
Path targetJarFile = new File(destinationDir, selectedVersion + UNBLOCK_SUFFIX + ".jar").toPath();
59+
try {
60+
Files.copy(sourceJarFile, targetJarFile);
61+
Files.copy(sourceJsonFile, targetJsonFile);
62+
63+
JSONObject json = (JSONObject) JSONValue.parseWithException(new FileReader(targetJsonFile.toFile()));
64+
json.put("id", selectedVersion + UNBLOCK_SUFFIX);
65+
JSONArray libraries = (JSONArray) json.get("libraries");
66+
removeMojangNetty(libraries);
67+
68+
Files.write(targetJsonFile, Arrays.asList(json.toJSONString()), StandardCharsets.UTF_8);
69+
JOptionPane.showMessageDialog(parentComp, "Sucessfully created an unblocked version. \n"
70+
+ "Now restart your launcher and select the version with the suffix -Unblock");
71+
} catch (IOException | ParseException ex) {
72+
Logger.getLogger(MinecraftUnblocker.class.getName()).log(Level.SEVERE, null, ex);
73+
JOptionPane.showMessageDialog(parentComp, "Error " + ex.getMessage());
74+
}
75+
}
76+
77+
private void removeMojangNetty(JSONArray libraries) {
78+
for (Object libraryObj : libraries) {
79+
JSONObject library = (JSONObject) libraryObj;
80+
String name = (String) library.get("name");
81+
//found the mojangs custom netty implementation for block the servers
82+
if (name.startsWith("com.mojang:netty")) {
83+
libraries.remove(libraryObj);
84+
break;
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)