1
0
mirror of https://github.com/chylex/Minecraft-Window-Title.git synced 2024-11-24 20:42:48 +01:00

Compare commits

...

4 Commits

Author SHA1 Message Date
73dab6b277
Fix errors in README 2024-10-19 14:21:39 +02:00
3de5e01e32
Release 1.4.0 for Minecraft 1.21 or newer 2024-10-19 14:02:33 +02:00
05a081364d
Migrate from NeoGradle to ModDevGradle 2024-10-19 13:16:03 +02:00
Kolotheegg
941ece888b Reimplement icon support 2024-10-19 13:11:53 +02:00
13 changed files with 240 additions and 88 deletions

View File

@ -11,7 +11,7 @@ public class CustomWindowTitle implements ClientModInitializer {
private final TitleConfig config;
public CustomWindowTitle() {
config = TitleConfig.read(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
config = TitleConfig.load(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
}
@Override

View File

@ -1,24 +1,33 @@
val modId: String by project
val neoForgeVersion: String by project
plugins {
id("net.neoforged.gradle.userdev")
id("net.neoforged.gradle.mixin")
id("net.neoforged.moddev")
}
dependencies {
implementation("net.neoforged:neoforge:$neoForgeVersion")
}
runs {
val runJvmArgs: Set<String> by project
neoForge {
version = neoForgeVersion
configureEach {
workingDirectory = file("../run")
modSource(project.sourceSets.main.get())
jvmArguments(runJvmArgs)
mods {
register(modId) {
sourceSet(sourceSets.main.get())
sourceSet(rootProject.sourceSets.main.get())
}
}
removeIf { it.name != "client" }
runs {
val runJvmArgs: Set<String> by project
configureEach {
gameDirectory = file("../run")
jvmArguments.addAll(runJvmArgs)
}
register("client") {
ideName.set("NeoForge Client")
client()
}
}
}
tasks.processResources {

View File

@ -16,7 +16,7 @@ public class CustomWindowTitle {
private final TitleConfig config;
public CustomWindowTitle(IEventBus eventBus) {
config = TitleConfig.read(FMLPaths.CONFIGDIR.get().toString());
config = TitleConfig.load(FMLPaths.CONFIGDIR.get().toString());
eventBus.addListener(this::onClientSetup);
CommonTokenData.register(new TokenProvider());
}

View File

@ -14,8 +14,7 @@ To change the title or icon, navigate to the `.minecraft/config` folder, and ope
```toml
title = 'Minecraft {mcversion}'
icon16 = ''
icon32 = ''
icon = ''
```
Only edit text inside quotes or apostrophes.
@ -32,23 +31,30 @@ If any of the tokens aren't working, search the game log for **CustomWindowTitle
### Changing the Icon
**This feature is currently not supported in Minecraft 1.20+.**
#### Minecraft 1.21+
You must create two PNG images with sizes 16x16 and 32x32 pixels. The images **must be saved with transparency** even if they don't use it, **otherwise the icons will appear corrupted**. In Krita for example, you must check _Store alpha channel (transparency)_ when saving the image.
**This feature is only available in Custom Window Title 1.4.0 and newer.**
The _icon16_ and _icon32_ configuration entries point to the PNG files relative to the `.minecraft/config` folder. For example, if you place the two icons in a folder named _customwindowtitle_ as follows:
Create a square PNG image whose dimensions are a power of two, such as 32x32 or 48x48. Put the PNG file into the `.minecraft/config` folder, either directly or into a subfolder.
* `.minecraft/config/customwindowtitle-client.toml`
* `.minecraft/config/customwindowtitle/icon16.png`
* `.minecraft/config/customwindowtitle/icon32.png`
The icon **must be saved with transparency** even if it doesn't use it, otherwise the icon may be corrupted or not appear at all. In Krita, for example, you must check _Store alpha channel (transparency)_ when saving.
Then, the two icon entries should look like this:
The _icon_ configuration entry points to the PNG file relative to `.minecraft/config`.
For example, if you placed the icon into `.minecraft/config/customwindowtitle/icon.png`, then the configuration entry should look like this:
```toml
icon16 = 'customwindowtitle/icon16.png'
icon32 = 'customwindowtitle/icon32.png'
icon = 'customwindowtitle/icon.png'
```
#### Minecraft 1.20
This feature is not available in Minecraft 1.20.
#### Minecraft 1.19 and older
Instead of one `icon` configuration entry, there are two configuration entries `icon16` and `icon32` for icons with dimensions 16x16 and 32x32.
## Screenshots
These screenshots were taken using the following example configuration:

View File

@ -133,10 +133,8 @@ subprojects {
archivesName.set("$modNameStripped-${project.name}")
}
listOf("compileJava", "compileTestJava").forEach {
tasks.named<JavaCompile>(it) {
source({ rootProject.sourceSets.main.get().allSource })
}
tasks.compileJava {
source({ rootProject.sourceSets.main.get().allSource })
}
tasks.processResources {

View File

@ -17,6 +17,7 @@ icon32 = ''
```
Only edit text inside quotes or apostrophes.
### Changing the Title
You can use the following special tokens in the _title_ configuration entry:
@ -29,24 +30,32 @@ If any of the tokens aren't working, search the game log for **CustomWindowTitle
### Changing the Icon
**This feature is currently not supported in Minecraft 1.20+.**
#### Minecraft 1.21+
You must create two PNG images with sizes 16x16 and 32x32 pixels. The images **must be saved with transparency** even if they don't use it, **otherwise the icons will appear corrupted**. In Krita for example, you must check _Store alpha channel (transparency)_ when saving the image.
**This feature is only available in Custom Window Title 1.4.0 and newer.**
The _icon16_ and _icon32_ configuration entries point to the PNG files relative to the `.minecraft/config` folder. For example, if you place the two icons in a folder named _customwindowtitle_ as follows:
Create a square PNG image whose dimensions are a power of two, such as 32x32 or 48x48. Put the PNG file into the `.minecraft/config` folder, either directly or into a subfolder.
* `.minecraft/config/customwindowtitle-client.toml`
* `.minecraft/config/customwindowtitle/icon16.png`
* `.minecraft/config/customwindowtitle/icon32.png`
The icon **must be saved with transparency** even if it doesn't use it, otherwise the icon may be corrupted or not appear at all. In Krita, for example, you must check _Store alpha channel (transparency)_ when saving.
Then, the two icon entries should look like this:
The _icon_ configuration entry points to the PNG file relative to `.minecraft/config`.
For example, if you placed the icon into `.minecraft/config/customwindowtitle/icon.png`, then the configuration entry should look like this:
```toml
icon16 = 'customwindowtitle/icon16.png'
icon32 = 'customwindowtitle/icon32.png'
icon = 'customwindowtitle/icon.png'
```
#### Minecraft 1.20
This feature is not available in Minecraft 1.20.
#### Minecraft 1.19 and older
Instead of one `icon` configuration entry, there are two configuration entries `icon16` and `icon32` for icons with dimensions 16x16 and 32x32.
## Screenshots
These screenshots were taken using the following example configuration:
```toml

View File

@ -17,6 +17,7 @@ icon32 = ''
```
Only edit text inside quotes or apostrophes.
### Changing the Title
You can use the following special tokens in the _title_ configuration entry:
@ -29,24 +30,32 @@ If any of the tokens aren't working, search the game log for **CustomWindowTitle
### Changing the Icon
**This feature is currently not supported in Minecraft 1.20+.**
#### Minecraft 1.21+
You must create two PNG images with sizes 16x16 and 32x32 pixels. The images **must be saved with transparency** even if they don't use it, **otherwise the icons will appear corrupted**. In Krita for example, you must check _Store alpha channel (transparency)_ when saving the image.
**This feature is only available in Custom Window Title 1.4.0 and newer.**
The _icon16_ and _icon32_ configuration entries point to the PNG files relative to the `.minecraft/config` folder. For example, if you place the two icons in a folder named _customwindowtitle_ as follows:
Create a square PNG image whose dimensions are a power of two, such as 32x32 or 48x48. Put the PNG file into the `.minecraft/config` folder, either directly or into a subfolder.
* `.minecraft/config/customwindowtitle-client.toml`
* `.minecraft/config/customwindowtitle/icon16.png`
* `.minecraft/config/customwindowtitle/icon32.png`
The icon **must be saved with transparency** even if it doesn't use it, otherwise the icon may be corrupted or not appear at all. In Krita, for example, you must check _Store alpha channel (transparency)_ when saving.
Then, the two icon entries should look like this:
The _icon_ configuration entry points to the PNG file relative to `.minecraft/config`.
For example, if you placed the icon into `.minecraft/config/customwindowtitle/icon.png`, then the configuration entry should look like this:
```toml
icon16 = 'customwindowtitle/icon16.png'
icon32 = 'customwindowtitle/icon32.png'
icon = 'customwindowtitle/icon.png'
```
#### Minecraft 1.20
This feature is not available in Minecraft 1.20.
#### Minecraft 1.19 and older
Instead of one `icon` configuration entry, there are two configuration entries `icon16` and `icon32` for icons with dimensions 16x16 and 32x32.
## Screenshots
These screenshots were taken using the following example configuration:
```toml

View File

@ -1,9 +1,9 @@
# Mod
modId=customwindowtitle
modName=Custom Window Title
modDescription=Customize window title.
modDescription=Customize window title and icon.
modAuthor=chylex
modVersion=1.3.0
modVersion=1.4.0
modLicense=Unlicense
modSourcesURL=https://github.com/chylex/Minecraft-Window-Title
modIssuesURL=https://github.com/chylex/Minecraft-Window-Title/issues
@ -12,12 +12,12 @@ modSides=client
# Dependencies
minecraftVersion=1.21
neoForgeVersion=21.0.0-beta
neoGradleVersion=7.0.152
neoModDevVersion=1.0.21
fabricVersion=0.15.11
loomVersion=1.7
mixinVersion=0.12.5+mixin.0.8.5
# https://projects.neoforged.net/neoforged/neogradle/
# https://projects.neoforged.net/neoforged/moddevgradle
# https://fabricmc.net/develop/
# https://github.com/FabricMC/fabric-loom/releases

View File

@ -8,10 +8,9 @@ pluginManagement {
}
plugins {
val neoGradleVersion = settings.extra.get("neoGradleVersion") as? String
if (neoGradleVersion != null) {
id("net.neoforged.gradle.userdev") version neoGradleVersion
id("net.neoforged.gradle.mixin") version neoGradleVersion
val neoModDevVersion = settings.extra.get("neoModDevVersion") as? String
if (neoModDevVersion != null) {
id("net.neoforged.moddev") version neoModDevVersion
}
val loomVersion = settings.extra.get("loomVersion") as? String

View File

@ -0,0 +1,60 @@
package chylex.customwindowtitle;
import net.minecraft.client.Minecraft;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWImage;
import org.lwjgl.stb.STBImage;
import org.lwjgl.system.MemoryStack;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
public final class IconChanger {
private IconChanger() {}
public static void setIcon(Path iconPath) {
long windowHandle = Minecraft.getInstance().getWindow().getWindow();
setWindowIcon(windowHandle, iconPath);
}
private static void setWindowIcon(long windowHandle, Path iconPath) {
try (MemoryStack stack = MemoryStack.stackPush()) {
IntBuffer w = stack.mallocInt(1);
IntBuffer h = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
ByteBuffer icon = loadIcon(iconPath, w, h, channels);
if (icon == null) {
return;
}
try (GLFWImage glfwImage1 = GLFWImage.malloc(); GLFWImage glfwImage2 = GLFWImage.malloc(); GLFWImage.Buffer icons = GLFWImage.malloc(2)) {
glfwImage1.set(w.get(0), h.get(0), icon);
glfwImage2.set(w.get(0), h.get(0), icon);
icons.put(0, glfwImage1);
icons.put(1, glfwImage2);
GLFW.glfwSetWindowIcon(windowHandle, icons);
}
} catch (Exception e) {
System.err.println("Failed to set window icon: " + iconPath);
e.printStackTrace();
}
}
private static ByteBuffer loadIcon(Path path, IntBuffer w, IntBuffer h, IntBuffer channels) throws IOException {
byte[] iconBytes = Files.readAllBytes(path);
ByteBuffer buffer = ByteBuffer.allocateDirect(iconBytes.length).put(iconBytes).flip();
ByteBuffer icon = STBImage.stbi_load_from_memory(buffer, w, h, channels, 4);
if (icon == null) {
System.err.println("Failed to load image from memory for: " + path + " - " + STBImage.stbi_failure_reason());
}
return icon;
}
}

View File

@ -1,7 +1,6 @@
package chylex.customwindowtitle;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -12,47 +11,79 @@ import java.util.Map;
import java.util.stream.Collectors;
public final class TitleConfig {
private static final String KEY_TITLE = "title";
private static final String KEY_ICON = "icon";
private static final ImmutableMap<String, String> DEFAULTS = ImmutableMap.<String, String>builder()
.put("title", "Minecraft {mcversion}")
.build();
.put(KEY_TITLE, "Minecraft {mcversion}")
.put(KEY_ICON, "")
.buildOrThrow();
private static final ImmutableSet<String> IGNORED_KEYS = ImmutableSet.of(
"icon16",
"icon32"
);
private static volatile TitleConfig instance;
public static TitleConfig read(final String folder) {
final Path configFile = Paths.get(folder, "customwindowtitle-client.toml");
final Map<String, String> config = new LinkedHashMap<>(DEFAULTS);
public static TitleConfig getInstance() {
return instance;
}
public static TitleConfig load(String folder) {
if (instance != null) {
throw new IllegalStateException("TitleConfig has already been loaded and cannot be loaded again.");
}
if (instance == null) {
synchronized(TitleConfig.class) {
if (instance == null) {
instance = loadImpl(folder);
}
}
}
return instance;
}
private static TitleConfig loadImpl(String folder) {
Path configFile = Paths.get(folder, "customwindowtitle-client.toml");
Map<String, String> config = new LinkedHashMap<>(DEFAULTS);
try {
if (!Files.exists(configFile)) {
Files.write(configFile, config.entrySet().stream().map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue())).collect(Collectors.toList()), StandardCharsets.UTF_8);
Files.write(configFile, config.entrySet().stream()
.map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue()))
.collect(Collectors.toList()), StandardCharsets.UTF_8);
}
else {
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream().map(String::trim).filter(line -> !line.isEmpty()).forEach(line -> {
final String[] split = line.split("=", 2);
if (split.length != 2) {
throw new RuntimeException("CustomWindowTitle configuration has an invalid line: " + line);
}
final String key = split[0].trim();
final String value = parseTrimmedValue(split[1].trim());
if (config.containsKey(key)) {
config.put(key, value);
}
else if (!IGNORED_KEYS.contains(key)) {
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
}
});
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream()
.map(String::trim)
.filter(line -> !line.isEmpty())
.forEach(line -> {
String[] split = line.split("=", 2);
if (split.length != 2) {
throw new RuntimeException("CustomWindowTitle configuration has an invalid line: " + line);
}
String key = split[0].trim();
String value = parseTrimmedValue(split[1].trim());
if (config.containsKey(key)) {
config.put(key, value);
}
else {
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
}
});
}
} catch (final IOException e) {
} catch (IOException e) {
throw new RuntimeException("CustomWindowTitle configuration error", e);
}
return new TitleConfig(config.get("title"));
String iconPathStr = config.get(KEY_ICON);
Path iconPath = iconPathStr.isEmpty() ? null : Paths.get(folder, iconPathStr);
if (iconPath != null && Files.notExists(iconPath)) {
throw new RuntimeException("CustomWindowTitle configuration points to a missing icon: " + iconPath);
}
return new TitleConfig(config.get(KEY_TITLE), iconPath);
}
private static String parseTrimmedValue(String value) {
@ -75,12 +106,22 @@ public final class TitleConfig {
}
private final String title;
private final Path icon;
private TitleConfig(final String title) {
private TitleConfig(final String title, final Path icon) {
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public boolean hasIcon() {
return icon != null;
}
public Path getIconPath() {
return icon;
}
}

View File

@ -0,0 +1,20 @@
package chylex.customwindowtitle.mixin;
import chylex.customwindowtitle.IconChanger;
import chylex.customwindowtitle.TitleConfig;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(Minecraft.class)
public final class InitializeCustomIcon {
@Inject(method = "onResourceLoadFinished", at = @At("HEAD"))
private void onFinishedLoading(CallbackInfo callbackInfo) {
TitleConfig config = TitleConfig.getInstance();
if (config != null && config.hasIcon()) {
IconChanger.setIcon(config.getIconPath());
}
}
}

View File

@ -5,7 +5,8 @@
"refmap": "customwindowtitle.refmap.json",
"compatibilityLevel": "JAVA_17",
"client": [
"DisableVanillaTitle"
"DisableVanillaTitle",
"InitializeCustomIcon"
],
"injectors": {
"defaultRequire": 1