This commit is contained in:
2024-10-04 20:00:56 +02:00
commit 69f9b5e431
57 changed files with 8385 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.vaporvee.enoughmemory;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
@Config(name = EnoughMemory.MOD_ID)
public class EMConfig implements ConfigData {
float minMemory = 4.0f;
String errorTitle = "Error: Not enough Java memory!";
String errorDescription = "Please allocate at least {minMemory} GB of Java memory to your Minecraft Instance!";
}

View File

@@ -0,0 +1,99 @@
package com.vaporvee.enoughmemory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class EMWindow {
static boolean windowOpen = false;
private static JFrame window;
public static void createWindow(MinecraftClient minecraftClient, Screen screen) {
if(screen instanceof TitleScreen){
minecraftClient.setScreen(new BlockedScreen());
if(window != null){
window.dispose();
}
return;
}
try {
if (!windowOpen) {
windowOpen = true;
SwingUtilities.invokeLater(() -> {
window = new JFrame(EnoughMemoryClient.getWarningMessage()[0]);
window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
window.setSize(400, 200);
window.setLocationRelativeTo(null);
JLabel message = new JLabel("<html><p style=\"width:200px\">"+EnoughMemoryClient.getWarningMessage()[1]+"</p></html>", JLabel.CENTER);
JButton exitButton = new JButton("OK");
exitButton.addActionListener(e -> {
window.dispose();
minecraftClient.scheduleStop();
});
window.setLayout(new BorderLayout());
window.add(message, BorderLayout.CENTER);
window.add(exitButton, BorderLayout.SOUTH);
window.setVisible(true);
});
}
} catch (RuntimeException e) {
EnoughMemory.logger.error(String.valueOf(e));
}
}
@Environment(EnvType.CLIENT)
public static class BlockedScreen extends Screen {
protected BlockedScreen() {
super(Text.literal("BlockedScreen"));
}
@Override
public void init(){
ButtonWidget closeButton = ButtonWidget.builder(Text.literal("Quit Game"), button -> {
MinecraftClient.getInstance().scheduleStop();
})
.dimensions(width / 2 - 100, height - 35 , 200, 20)
.build();
addDrawableChild(closeButton);
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
String title = EnoughMemoryClient.getWarningMessage()[0];
String body = EnoughMemoryClient.getWarningMessage()[1];
int lineHeight = textRenderer.fontHeight + 5;
int maxTextWidth = width - 40;
List<OrderedText> wrappedLines = textRenderer.wrapLines(Text.literal(body), maxTextWidth);
int totalTextHeight = (wrappedLines.size() * lineHeight) + lineHeight + 10;
int startY = (height / 2) - (totalTextHeight / 2);
context.drawCenteredTextWithShadow(textRenderer, Text.literal(title), width / 2, startY, 0xf94449);
int bodyStartY = startY + lineHeight + 10;
// Automatic line breaks
for (int i = 0; i < wrappedLines.size(); i++) {
OrderedText line = wrappedLines.get(i);
context.drawCenteredTextWithShadow(textRenderer, line, width / 2, bodyStartY + (i * lineHeight), 0xffffff);
}
}
}
}

View File

@@ -0,0 +1,31 @@
package com.vaporvee.enoughmemory;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
public class EnoughMemoryClient implements ClientModInitializer {
public static EMConfig config;
@Override
public void onInitializeClient() {
AutoConfig.register(EMConfig.class, Toml4jConfigSerializer::new);
config = AutoConfig.getConfigHolder(EMConfig.class).getConfig();
float allocatedMemoryInGB = Runtime.getRuntime().totalMemory() / 1073741824f; // Hardcoded value for GB
EnoughMemory.logger.info(String.format("Allocated Memory: %.1f GB", allocatedMemoryInGB));
if(config.minMemory > allocatedMemoryInGB){
System.setProperty("java.awt.headless", "false"); // Hacky stupid thing but it works I guess...
ScreenEvents.BEFORE_INIT.register(EnoughMemoryClient::beforeWindowInit);
}
}
public static String[] getWarningMessage(){
return new String[]{config.errorTitle, config.errorDescription.replace("{minMemory}", String.valueOf(config.minMemory))};
};
private static void beforeWindowInit(MinecraftClient minecraftClient, Screen screen, int i, int i1) {
EMWindow.createWindow(minecraftClient, screen);
}
}

View File

@@ -0,0 +1,23 @@
package com.vaporvee.enoughmemory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EnoughMemory implements ModInitializer {
public static final String MOD_ID = "enoughmemory";
public static final Logger logger = LoggerFactory.getLogger("EnoughMemory");
@Override
public void onInitialize() {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
logger.info(MOD_ID + " is a client mod only!");
return;
}
logger.info("Loading EnoughMemory mod.");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@@ -0,0 +1,8 @@
{
"required": true,
"package": "com.vaporvee.enoughmemory.mixin",
"compatibilityLevel": "JAVA_21",
"injectors": {
"defaultRequire": 1
}
}

View File

@@ -0,0 +1,46 @@
{
"schemaVersion": 1,
"id": "enoughmemory",
"version": "${version}",
"name": "Enough Memory",
"description": "It will show the user when they have set too less Java Memory. The amount of required memory can be adjusted with the config.",
"authors": [
{
"name": "vaporvee",
"contact": {
"homepage" : "https://vaporvee.com"
}
}
],
"contact": {
"sources": "https://github.com/vaporvee/EnoughMemory",
"issues": "https://github.com/vaporvee/EnoughMemory/issues"
},
"custom": {
"modmenu": {
"links": {
"modmenu.discord": "https://discord.gg/StMHnsC9s2"
}
}
},
"license": "Apache License 2.0",
"icon": "assets/enoughmemory/icon.png",
"environment": "*",
"entrypoints": {
"main": [
"com.vaporvee.enoughmemory.EnoughMemory"
],
"client": [
"com.vaporvee.enoughmemory.EnoughMemoryClient"
]
},
"depends": {
"fabricloader": ">=0.16.5",
"minecraft": "~1.21.1",
"java": ">=21",
"fabric-api": "*"
},
"suggests": {
"another-mod": "*"
}
}