added ram alloc error jframe back

This commit is contained in:
2025-06-21 13:04:46 +02:00
parent 8f07d36dfd
commit 5dbf6dd823
6 changed files with 74 additions and 23 deletions

View File

@@ -1,5 +1,10 @@
package com.vaporvee.loadsupport;
import com.vaporvee.loadsupport.platform.Services;
import javax.swing.*;
import java.awt.*;
public class Allocated {
public static float memoryInGB;
public static void init(){
@@ -9,4 +14,43 @@ public class Allocated {
public static void printAllocated() {
Constants.LOG.info(String.format("Allocated Memory: %.1f GB", memoryInGB));
}
public static String[] getWarningMessage(){
Config config = Services.CONFIG.getConfig();
return new String[]{config.errorTitle, config.errorDescription
.replace("{minMemory}", String.valueOf(config.minMemory))
.replace("{currentMemory}", String.valueOf(memoryInGB))};
};
static boolean errorWindowOpen = false;
private static JFrame errorWindow;
public static void createErrorWindow() {
try {
if (!errorWindowOpen) {
errorWindowOpen = true;
SwingUtilities.invokeLater(() -> {
errorWindow = new JFrame(getWarningMessage()[0]);
errorWindow.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
errorWindow.setSize(400, 200);
errorWindow.setLocationRelativeTo(null);
JLabel message = new JLabel("<html><p style=\"width:200px\">"+ getWarningMessage()[1]+"</p></html>", JLabel.CENTER);
JButton exitButton = new JButton("OK");
exitButton.addActionListener(e -> {
errorWindow.dispose();
//minecraftClient.stop(); // how to get client crossplatform?
});
errorWindow.setLayout(new BorderLayout());
errorWindow.add(message, BorderLayout.CENTER);
errorWindow.add(exitButton, BorderLayout.SOUTH);
errorWindow.setVisible(true);
});
}
} catch (RuntimeException e) {
Constants.LOG.error(String.valueOf(e));
}
}
}

View File

@@ -13,17 +13,17 @@ public class CommonClass {
Allocated.printAllocated();
Services.CONFIG.InitConfig();
}
public static void checkConfig(LSConfig config) {
Constants.LOG.info("Load config test!");
public static void checkConfig(Config config) {
Constants.LOG.info("Config loaded!");
if (config != null) {
if(config.minMemory > Allocated.memoryInGB){
System.setProperty("java.awt.headless", "false");
Constants.LOG.info("Not enough memory! Allocated memory in GB is {} but set in config is {}",
Constants.LOG.error("Not enough memory! Allocated memory in GB is {} but set in config is {}",
Allocated.memoryInGB, config.minMemory);
//create jframe window
Allocated.createErrorWindow();
}
} else {
Constants.LOG.info("Load config is null!");
Constants.LOG.warn("Load config is null!");
}
}
}

View File

@@ -1,10 +1,9 @@
package com.vaporvee.loadsupport;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
@Config(name = Constants.MOD_ID)
public class LSConfig implements ConfigData {
@me.shedaniel.autoconfig.annotation.Config(name = Constants.MOD_ID)
public class Config implements ConfigData {
boolean startSound = true;
float minMemory = 4.0f;
String errorTitle = "Error: Not enough Java memory!";

View File

@@ -1,10 +1,16 @@
package com.vaporvee.loadsupport.platform.services;
import com.vaporvee.loadsupport.LSConfig;
import com.vaporvee.loadsupport.Config;
public interface IConfig {
/**
* Initializes config on available platforms
*/
void InitConfig();
/**
* Gets the populated config class with local config data when loaded correctly
* @return pupulated Config object
*/
Config getConfig();
}