Skip to content

Commit

Permalink
Added a console window
Browse files Browse the repository at this point in the history
Changed version
  • Loading branch information
xHookman committed Feb 1, 2023
1 parent 215e838 commit ad85cb6
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 41 deletions.
3 changes: 0 additions & 3 deletions META-INF/MANIFEST.MF

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.12
# Mod Properties
mod_version=1.0-SNAPSHOT
mod_version=1.1
maven_group=xHookman
archives_base_name=soundboard

Expand Down
62 changes: 62 additions & 0 deletions src/main/java/xhookman/soundboard/LogWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package xhookman.soundboard;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

import static xhookman.soundboard.soundboard.FilesUtil.getJarPath;
import static xhookman.soundboard.soundboard.FilesUtil.getNewJarName;

public class LogWindow {
private final JFrame frame;
private final JTextArea textArea;
private final JButton button;

public LogWindow(){
frame = new JFrame("Soundboard Updater");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea();
button = new JButton("Ok");
frame.add(textArea, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
textArea.setEditable(false);
textArea.setText("Please wait while the mod is updating...");
button.addActionListener((ActionEvent e) -> {
removeJar();
frame.dispose();
});

frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
removeJar();
}
});
}
public void writeToLog(String text){
textArea.append("\n" + text);
frame.pack();
}

public void addOkButton(){
frame.add(button, BorderLayout.SOUTH);
frame.pack();
}

private static void removeJar(){ // The file is removed after 2 seconds
try {
String jarName = getJarPath().substring(getJarPath().lastIndexOf("/")+1);
String command = "Start-Sleep -Seconds 2;Remove-Item -Path '"+ jarName + "' -Force;" +
"Rename-Item " + getNewJarName() + " " + getJarPath().substring(getJarPath().lastIndexOf("/")+1);
ProcessBuilder pb = new ProcessBuilder("powershell", "-Command", command); // Obliged because Windows 11 is trash and doing it with a cmd does not work
pb.directory(new File(getJarPath()).getParentFile());
System.out.println("Deleting jar...");
pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
35 changes: 6 additions & 29 deletions src/main/java/xhookman/soundboard/ModLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,19 @@

import xhookman.soundboard.soundboard.Converter;
import xhookman.soundboard.soundboard.FilesUtil;
import javax.swing.*;
import java.io.File;

import static xhookman.soundboard.soundboard.FilesUtil.*;

public class ModLauncher {

private static void removeJar(){ // The file is removed after 2 seconds
try {
String jarName = getJarPath().substring(getJarPath().lastIndexOf("/")+1);
String command = "Start-Sleep -Seconds 2;Remove-Item -Path '"+ jarName + "' -Force;" +
"Rename-Item " + getNewJarName() + " " + getJarPath().substring(getJarPath().lastIndexOf("/")+1);
// ProcessBuilder pb = new ProcessBuilder("powershell", "-Command", "Start-Sleep -Seconds 2; Start-Process -FilePath 'deleteJar.bat'"); // Obliged because Windows 11 is trash and doing it with a cmd does not work
// ProcessBuilder pb = new ProcessBuilder("powershell -Command Start-Sleep -Seconds 2;Remove-Item -Path '"+ getWindowsJarPath() + "' -Force"); // Obliged because Windows 11 is trash and doing it with a cmd does not work
ProcessBuilder pb = new ProcessBuilder("powershell", "-Command", command); // Obliged because Windows 11 is trash and doing it with a cmd does not work
pb.directory(new File(getJarPath()).getParentFile());
System.out.println("Deleting jar...");
pb.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
LogWindow logWindow = new LogWindow();
FilesUtil.setLogWindow(logWindow);
FilesUtil.createSoundboardDir();
File dir = FilesUtil.getSoundboardDir();
System.out.println("Checking files name...");
logWindow.writeToLog("Checking and converting files...");
Converter.checkAndConvertFiles(dir);
System.out.println("Generating new jar...");
logWindow.writeToLog("Generating new jar...");
FilesUtil.generateFiles(dir);
System.out.println("Mod updated! The old jar will be deleted 2 seconds after you close this window.");
//show a message box

String message = "Mod updated! The old jar will be deleted 2 seconds after you close this window.";
String title = "Soundboard Updater";
JOptionPane.showConfirmDialog(null, message, title, JOptionPane.DEFAULT_OPTION);

removeJar();
logWindow.writeToLog("Mod updated! The old jar will be deleted 2 seconds after you close this window.");
logWindow.addOkButton();
}
}
5 changes: 2 additions & 3 deletions src/main/java/xhookman/soundboard/soundboard/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class Converter {
Expand All @@ -30,12 +29,12 @@ private static void convert(File source, File target) throws EncoderException {
public static void checkAndConvertFiles(File dir){ // Check if the files are in the right format and convert them if needed
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (!file.getName().endsWith(".ogg")) {
System.out.println("Converting " + file.getName() + " to ogg...");
FilesUtil.getLogWindow().writeToLog("Converting " + file.getName() + " to ogg...");
try {
convert(file, new File(dir, getValidFileName(getFileNameWithoutExtension(file.getName())) + ".ogg"));
file.delete();
} catch (EncoderException e) {
System.out.println("Could not convert " + file.getName() + " :(");
FilesUtil.getLogWindow().writeToLog("Could not convert " + file.getName() + " :(");
}
} else {
file.renameTo(new File(dir, getValidFileName(getFileNameWithoutExtension(file.getName())) + ".ogg"));
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/xhookman/soundboard/soundboard/FilesUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xhookman.soundboard.soundboard;

import xhookman.soundboard.LogWindow;
import xhookman.soundboard.ModLauncher;

import java.io.*;
Expand All @@ -17,6 +18,15 @@
public class FilesUtil {
private static File soundboardDir;
private static String newJarName;
private static LogWindow logWindow;

public static void setLogWindow(LogWindow logWindow){
FilesUtil.logWindow = logWindow;
}

public static LogWindow getLogWindow(){
return logWindow;
}

public static void createSoundboardDir(){ // Create the soundboard folder
soundboardDir = new File(getJarParentPath() + "/soundboard/");
Expand Down Expand Up @@ -78,9 +88,9 @@ public static void generateFiles(File folder) { // Generate the new .jar file
for(int i=0; i<files.length; i++)
if(files[i].getName().endsWith(".ogg")) {
addFile(jos, "assets/" + MOD_ID + "/sounds/" + soundsFiles[i].getName(), soundboardDir.getPath() + "/" + soundsFiles[i].getName());
System.out.println("Copie du son " + soundsFiles[i].getName());
logWindow.writeToLog("Copying " + soundsFiles[i].getName());
}
System.out.println("Copie du fichier sounds.json");
logWindow.writeToLog("Copying sounds.json");
SoundJsonUtils.addFile(jos, "assets/" + MOD_ID + "/sounds.json", generateSoundsJson(folder));

// Close the streams
Expand Down

0 comments on commit ad85cb6

Please sign in to comment.