|
| 1 | +package com.github.games647.minecraftunblocker; |
| 2 | + |
| 3 | +import java.awt.FlowLayout; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | +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 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.logging.Level; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import javax.swing.JButton; |
| 20 | +import javax.swing.JComboBox; |
| 21 | +import javax.swing.JFrame; |
| 22 | +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 | + |
| 30 | +public class MinecraftUnblocker { |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + File minecraftFolder = getWorkingDirectory(); |
| 34 | + final File versionsFolder = new File(minecraftFolder, "versions/"); |
| 35 | + |
| 36 | + List<String> selectItems = new ArrayList<>(); |
| 37 | + selectItems.add("SELECT"); |
| 38 | + Stream.of(versionsFolder.list()).forEach((fileName) -> selectItems.add(fileName)); |
| 39 | + versionsFolder.listFiles(); |
| 40 | + |
| 41 | + JFrame frame = new JFrame(); |
| 42 | + FlowLayout experimentLayout = new FlowLayout(); |
| 43 | + frame.setLayout(experimentLayout); |
| 44 | + |
| 45 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 46 | + frame.add(new JLabel("Select the version you want to unblock")); |
| 47 | + final JComboBox<String> list = new JComboBox(selectItems.toArray()); |
| 48 | + frame.add(list); |
| 49 | + |
| 50 | + JButton button = new JButton("Unblock"); |
| 51 | + frame.add(button); |
| 52 | + |
| 53 | + frame.pack(); |
| 54 | + 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 | + } |
| 99 | + |
| 100 | + public static File getWorkingDirectory() { |
| 101 | + String userHome = System.getProperty("user.home", "."); |
| 102 | + File workingDirectory; |
| 103 | + switch (OS.getPlatform()){ |
| 104 | + case LINUX: |
| 105 | + case SOLARIS: |
| 106 | + workingDirectory = new File(userHome, ".minecraft/"); |
| 107 | + break; |
| 108 | + case WINDOWS: |
| 109 | + String applicationData = System.getenv("APPDATA"); |
| 110 | + String folder = applicationData != null ? applicationData : userHome; |
| 111 | + |
| 112 | + workingDirectory = new File(folder, ".minecraft/"); |
| 113 | + break; |
| 114 | + case MACOS: |
| 115 | + workingDirectory = new File(userHome, "Library/Application Support/minecraft"); |
| 116 | + break; |
| 117 | + default: |
| 118 | + workingDirectory = new File(userHome, "minecraft/"); |
| 119 | + } |
| 120 | + |
| 121 | + return workingDirectory; |
| 122 | + } |
| 123 | +} |
0 commit comments