Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
Add a start menu and a modpack launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
CustomCheat authored Apr 9, 2022
1 parent 96cd5c9 commit 53baa50
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 9 deletions.
Binary file not shown.
Binary file added out/production/LinuxCurseGUI/LaunchFrame.class
Binary file not shown.
3 changes: 3 additions & 0 deletions out/production/LinuxCurseGUI/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

Binary file modified out/production/LinuxCurseGUI/Main.class
Binary file not shown.
Binary file modified out/production/LinuxCurseGUI/MainFrame.class
Binary file not shown.
Binary file modified out/production/LinuxCurseGUI/SearchFrame.class
Binary file not shown.
Binary file modified out/production/LinuxCurseGUI/SelectFrame.class
Binary file not shown.
Binary file added out/production/LinuxCurseGUI/StartingFrame.class
Binary file not shown.
67 changes: 67 additions & 0 deletions src/LaunchFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class LaunchFrame extends JFrame implements ActionListener {
ArrayList<JButton> buttons = new ArrayList<>();
int startingX = 0;
int startingY = 0;
File path;
public LaunchFrame() {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int width = 800, height = 500;
double screenHeight = dimension.getHeight();
double screenWidth = dimension.getWidth();
this.setLocation((int)(screenWidth / 2) - (width / 2), (int)(screenHeight / 2) - (height / 2));
// read from a txt file and save to a string then for every line in the string, create a button with the line as the text and add it to the arraylist of buttons and add the actionlistener to the button and add it to the frame
try {
path = new File(System.getProperty("user.home") + "/.linuxcurse/");
File myObj = new File(path.getAbsolutePath() + "/modpacks.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
JButton button = new JButton(data);
button.setBounds(0, 0, width / 3, 40);
button.setLocation(startingX, startingY);
button.addActionListener(this);
buttons.add(button);

add(button);
startingX += button.getWidth();
if(startingX + button.getWidth() > width) {
startingX = 0;
startingY += button.getHeight();
}
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
this.setSize(width,height);
this.setLayout(null);
this.setTitle("Select the modpack from the list");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.GRAY);


}

@Override
public void actionPerformed(ActionEvent e) {
for(JButton button : buttons) {
if(e.getSource() == button) {
String modpackName = button.getText();
String folderName = modpackName.replaceAll("[^A-Za-z0-9\",]|,(?!(([^\"]*\"){2})*[^\"]*$)", "").replace("\"", "").replace(",", "").replace(" ", "");
new ExecuteCommand("minecraft-launcher --workDir " + path.getAbsolutePath() + "/" + folderName + "/" + ".minecraft/");
System.exit(0);
}
}
}
}
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

5 changes: 3 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import java.net.URISyntaxException;

public class Main {
public static SearchFrame search;
static StartingFrame starting;
public static void main(String[] args) throws URISyntaxException {
search = new SearchFrame();
//search = new SearchFrame();
starting = new StartingFrame();
}
}
13 changes: 11 additions & 2 deletions src/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class MainFrame extends JFrame implements ActionListener {
this.setVisible(true);
this.getContentPane().setBackground(Color.GRAY);
}


/* Shoutout to this guy: https://stackoverflow.com/a/5368745/14269129 */
public void copy(File sourceLocation, File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
Expand Down Expand Up @@ -125,6 +127,13 @@ public void actionPerformed(ActionEvent e) {
ex.printStackTrace();
}finally {
try {
// write the modpackName to a file that is located in a specific folder
File file = new File(path.getAbsolutePath() + "/modpacks.txt");
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(modpackName + "\n");
bw.close();

folderName = modpackName.replaceAll("[^A-Za-z0-9\",]|,(?!(([^\"]*\"){2})*[^\"]*$)", "").replace("\"", "").replace(",", "").replace(" ", "");
new ZipFile(Paths.get(path.getAbsolutePath() + "/modpack.zip").toFile())
.extractAll(Paths.get(path.getAbsolutePath() + "/unzip/").toString());
Expand All @@ -138,8 +147,8 @@ public void actionPerformed(ActionEvent e) {
new File(path.getAbsolutePath() + "/" + folderName + "/" + ".minecraft/mods").mkdir();
new File(path.getAbsolutePath() + "/" + folderName + "/" + ".minecraft/versions").mkdir();
if(new File(path.getAbsolutePath() + "/unzip/manifest.json").exists()){
File file = new File(path.getAbsolutePath() + "/unzip/manifest.json");
Scanner reader = new Scanner(file);
File fa = new File(path.getAbsolutePath() + "/unzip/manifest.json");
Scanner reader = new Scanner(fa);
String text = "";
while (reader.hasNextLine()) {
text = text + reader.nextLine();
Expand Down
4 changes: 2 additions & 2 deletions src/SearchFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public static void processResponce(String responce) {
}
}
if(!modpackNames.isEmpty()){
Main.search.setVisible(false);
StartingFrame.search.setVisible(false);
select = new SelectFrame(modpackNames);

}else{
JOptionPane.showMessageDialog(Main.search, "No modpacks found.");
JOptionPane.showMessageDialog(StartingFrame.search, "No modpacks found.");
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/SelectFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class SelectFrame extends JFrame implements ActionListener {
int xLoc = 0;
MainFrame frame;
public SelectFrame(ArrayList<String> info){
this.setLocationRelativeTo(Main.search);
this.setLocationRelativeTo(StartingFrame.search);
// Main.search.dispose();
this.setLocation(Main.search.getLocation());
this.setLocation(StartingFrame.search.getLocation());
int width = 800, height = 500;
info.forEach(i -> {
String modpackName = i.split("A:W:B]")[0];
Expand Down Expand Up @@ -44,7 +44,7 @@ public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JButton){
JButton btn = (JButton) e.getSource();
if(btn.getToolTipText() != null){
Main.search.dispose();
StartingFrame.search.dispose();

String[] info = {btn.getToolTipText(), btn.getText()};
System.out.println("Selected: " + btn.getText());
Expand Down
51 changes: 51 additions & 0 deletions src/StartingFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StartingFrame extends JFrame implements ActionListener {
public static SearchFrame search;
JButton newModpack = new JButton("Install new modpack");
public static SearchFrame searchFrame;
JButton launchModpack = new JButton("Launch a modpack");
public StartingFrame() {
Border border = BorderFactory.createLineBorder(Color.RED, 3);
int width = 800, height = 500;
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

double screenHeight = dimension.getHeight();
double screenWidth = dimension.getWidth();
this.setLocation((int)(screenWidth / 2) - (width / 2), (int)(screenHeight / 2) - (height / 2));
newModpack.setBounds(0,0, 180, 40);
newModpack.setLocation((width / 2) - (newModpack.getWidth() / 2),(height/2) - (newModpack.getHeight()/2) - 40);
newModpack.setFocusable(false);
newModpack.addActionListener(this);
launchModpack.setBounds(0,0, 180, 40);
launchModpack.setLocation((width / 2) - (launchModpack.getWidth() / 2),(height/2) - (launchModpack.getHeight()/2) + 40);
launchModpack.setFocusable(false);
launchModpack.addActionListener(this);
this.setSize(width,height);
this.setLayout(null);
this.setTitle("LinuxCurse");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.add(newModpack);
this.add(launchModpack);

this.setVisible(true);
this.getContentPane().setBackground(Color.GRAY);
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == newModpack) {
this.dispose();
new SearchFrame();
}
if(e.getSource() == launchModpack) {
this.dispose();
new LaunchFrame();
}
}
}

0 comments on commit 53baa50

Please sign in to comment.