mirror of
https://github.com/chylex/Minecraft-Window-Title.git
synced 2025-09-15 17:32:14 +02:00
Compare commits
11 Commits
6854f05c05
...
master
Author | SHA1 | Date | |
---|---|---|---|
50d77446f5
|
|||
1f92896e55
|
|||
2a17b46a1d
|
|||
![]() |
5e64fd81e3 | ||
425eb3f380
|
|||
43698d23b5
|
|||
dbbd5b60f5
|
|||
73dab6b277
|
|||
3de5e01e32
|
|||
05a081364d
|
|||
![]() |
941ece888b |
2
.github/FUNDING.yml
vendored
2
.github/FUNDING.yml
vendored
@@ -1,3 +1 @@
|
||||
github: chylex
|
||||
patreon: chylex
|
||||
ko_fi: chylex
|
||||
|
2
.idea/gradle.xml
generated
2
.idea/gradle.xml
generated
@@ -5,7 +5,6 @@
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="openjdk-21" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
@@ -13,6 +12,7 @@
|
||||
<option value="$PROJECT_DIR$/NeoForge" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="resolveExternalAnnotations" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
|
@@ -1,6 +1,8 @@
|
||||
import net.fabricmc.loom.configuration.ide.RunConfigSettings
|
||||
import org.gradle.jvm.tasks.Jar
|
||||
|
||||
val modId: String by project
|
||||
val modSides: String by project
|
||||
val minecraftVersion: String by project
|
||||
val fabricVersion: String by project
|
||||
|
||||
@@ -24,12 +26,24 @@ loom {
|
||||
ideConfigGenerated(true)
|
||||
}
|
||||
|
||||
named("client") {
|
||||
fun side(name: String, configure: RunConfigSettings.() -> Unit) {
|
||||
if (modSides == "both" || modSides == name) {
|
||||
named(name, configure)
|
||||
}
|
||||
else {
|
||||
findByName(name)?.let(::remove)
|
||||
}
|
||||
}
|
||||
|
||||
side("client") {
|
||||
configName = "Fabric Client"
|
||||
client()
|
||||
}
|
||||
|
||||
findByName("server")?.let(::remove)
|
||||
side("server") {
|
||||
configName = "Fabric Server"
|
||||
server()
|
||||
}
|
||||
}
|
||||
|
||||
mixin {
|
||||
|
@@ -1,16 +1,15 @@
|
||||
package chylex.customwindowtitle.fabric;
|
||||
|
||||
import chylex.customwindowtitle.IconChanger;
|
||||
import chylex.customwindowtitle.TitleChanger;
|
||||
import chylex.customwindowtitle.TitleConfig;
|
||||
import chylex.customwindowtitle.TitleParser;
|
||||
import chylex.customwindowtitle.data.CommonTokenData;
|
||||
import chylex.customwindowtitle.mixin.DisableVanillaTitle;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
public class CustomWindowTitle implements ClientModInitializer {
|
||||
private final TitleConfig config;
|
||||
|
||||
public CustomWindowTitle() {
|
||||
config = TitleConfig.load(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
|
||||
}
|
||||
@@ -18,10 +17,6 @@ public class CustomWindowTitle implements ClientModInitializer {
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
CommonTokenData.register(new TokenProvider());
|
||||
Minecraft.getInstance().execute(this::updateTitle);
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
||||
Minecraft.getInstance().execute(() -> TitleChanger.setTitle(config));
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +1,47 @@
|
||||
import net.neoforged.moddevgradle.dsl.RunModel
|
||||
|
||||
val modId: String by project
|
||||
val modSides: 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")
|
||||
}
|
||||
neoForge {
|
||||
version = neoForgeVersion
|
||||
|
||||
runs {
|
||||
mods {
|
||||
register(modId) {
|
||||
sourceSet(sourceSets.main.get())
|
||||
sourceSet(rootProject.sourceSets.main.get())
|
||||
}
|
||||
}
|
||||
|
||||
runs {
|
||||
val runJvmArgs: Set<String> by project
|
||||
|
||||
configureEach {
|
||||
workingDirectory = file("../run")
|
||||
modSource(project.sourceSets.main.get())
|
||||
jvmArguments(runJvmArgs)
|
||||
gameDirectory = file("../run")
|
||||
jvmArguments.addAll(runJvmArgs)
|
||||
}
|
||||
|
||||
removeIf { it.name != "client" }
|
||||
fun side(name: String, configure: RunModel.() -> Unit) {
|
||||
if (modSides == "both" || modSides == name) {
|
||||
register(name, configure)
|
||||
}
|
||||
}
|
||||
|
||||
side("client") {
|
||||
ideName.set("NeoForge Client")
|
||||
client()
|
||||
}
|
||||
|
||||
side("server") {
|
||||
ideName.set("NeoForge Server")
|
||||
server()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tasks.processResources {
|
||||
|
@@ -1,9 +1,8 @@
|
||||
package chylex.customwindowtitle.neoforge;
|
||||
|
||||
import chylex.customwindowtitle.TitleChanger;
|
||||
import chylex.customwindowtitle.TitleConfig;
|
||||
import chylex.customwindowtitle.TitleParser;
|
||||
import chylex.customwindowtitle.data.CommonTokenData;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.neoforged.bus.api.IEventBus;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
@@ -15,18 +14,14 @@ public class CustomWindowTitle {
|
||||
|
||||
private final TitleConfig config;
|
||||
|
||||
public CustomWindowTitle(final IEventBus eventBus) {
|
||||
public CustomWindowTitle(IEventBus eventBus) {
|
||||
config = TitleConfig.load(FMLPaths.CONFIGDIR.get().toString());
|
||||
eventBus.addListener(this::onClientSetup);
|
||||
CommonTokenData.register(new TokenProvider());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onClientSetup(final FMLClientSetupEvent e) {
|
||||
e.enqueueWork(this::updateTitle);
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
||||
CommonTokenData.register(new TokenProvider());
|
||||
e.enqueueWork(() -> TitleChanger.setTitle(config));
|
||||
}
|
||||
}
|
||||
|
28
README.md
28
README.md
@@ -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:
|
||||
|
@@ -116,6 +116,10 @@ allprojects {
|
||||
inputs.property("minimumMinecraftVersion", minimumMinecraftVersion)
|
||||
inputs.property("minimumNeoForgeVersion", minimumNeoForgeVersion)
|
||||
inputs.property("minimumFabricVersion", minimumFabricVersion)
|
||||
|
||||
from(rootProject.file("logo.png")) {
|
||||
into("assets/$modId")
|
||||
}
|
||||
}
|
||||
|
||||
tasks.withType<AbstractArchiveTask>().configureEach {
|
||||
@@ -133,11 +137,9 @@ subprojects {
|
||||
archivesName.set("$modNameStripped-${project.name}")
|
||||
}
|
||||
|
||||
listOf("compileJava", "compileTestJava").forEach {
|
||||
tasks.named<JavaCompile>(it) {
|
||||
tasks.compileJava {
|
||||
source({ rootProject.sourceSets.main.get().allSource })
|
||||
}
|
||||
}
|
||||
|
||||
tasks.processResources {
|
||||
from(rootProject.sourceSets.main.get().resources) {
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -1,29 +1,30 @@
|
||||
# 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.2
|
||||
modLicense=Unlicense
|
||||
modSourcesURL=https://github.com/chylex/Minecraft-Window-Title
|
||||
modIssuesURL=https://github.com/chylex/Minecraft-Window-Title/issues
|
||||
modSides=client
|
||||
|
||||
# Dependencies
|
||||
minecraftVersion=1.21
|
||||
neoForgeVersion=21.0.0-beta
|
||||
neoGradleVersion=7.0.152
|
||||
fabricVersion=0.15.11
|
||||
loomVersion=1.7
|
||||
minecraftVersion=1.21.6
|
||||
neoForgeVersion=21.6.20-beta
|
||||
neoModDevVersion=2.0.103
|
||||
fabricVersion=0.16.14
|
||||
loomVersion=1.10
|
||||
mixinVersion=0.12.5+mixin.0.8.5
|
||||
|
||||
# https://projects.neoforged.net/neoforged/neogradle/
|
||||
# https://projects.neoforged.net/neoforged/neoforge
|
||||
# https://projects.neoforged.net/neoforged/moddevgradle
|
||||
# https://fabricmc.net/develop/
|
||||
# https://github.com/FabricMC/fabric-loom/releases
|
||||
|
||||
# Constraints
|
||||
minimumMinecraftVersion=1.21
|
||||
minimumNeoForgeVersion=21.0.0-beta
|
||||
minimumMinecraftVersion=1.21.6
|
||||
minimumNeoForgeVersion=21.6.0-beta
|
||||
minimumFabricVersion=0.12.3
|
||||
|
||||
# Gradle
|
||||
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
4
gradlew
vendored
4
gradlew
vendored
@@ -15,6 +15,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
@@ -84,7 +86,7 @@ done
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
2
gradlew.bat
vendored
2
gradlew.bat
vendored
@@ -13,6 +13,8 @@
|
||||
@rem See the License for the specific language governing permissions and
|
||||
@rem limitations under the License.
|
||||
@rem
|
||||
@rem SPDX-License-Identifier: Apache-2.0
|
||||
@rem
|
||||
|
||||
@if "%DEBUG%"=="" @echo off
|
||||
@rem ##########################################################################
|
||||
|
@@ -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
|
||||
|
@@ -1,65 +1,65 @@
|
||||
package chylex.customwindowtitle;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
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.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class IconChanger {
|
||||
private static final Logger LOGGER = LogManager.getLogger("CustomWindowTitle");
|
||||
|
||||
public static void setIcon(final Path iconPath) {
|
||||
final long windowHandle = Minecraft.getInstance().getWindow().getWindow();
|
||||
private IconChanger() {}
|
||||
|
||||
public static void setIcon(Path iconPath) {
|
||||
long windowHandle = Minecraft.getInstance().getWindow().getWindow();
|
||||
setWindowIcon(windowHandle, iconPath);
|
||||
}
|
||||
|
||||
private static void setWindowIcon(final long windowHandle, final Path iconPath) {
|
||||
try (final MemoryStack stack = MemoryStack.stackPush()) {
|
||||
final IntBuffer w = stack.mallocInt(1);
|
||||
final IntBuffer h = stack.mallocInt(1);
|
||||
final IntBuffer channels = stack.mallocInt(1);
|
||||
private static void setWindowIcon(long windowHandle, Path iconPath) {
|
||||
ByteBuffer icon = null;
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
IntBuffer w = stack.mallocInt(1);
|
||||
IntBuffer h = stack.mallocInt(1);
|
||||
IntBuffer channels = stack.mallocInt(1);
|
||||
|
||||
final ByteBuffer icon = loadIcon(iconPath, w, h, channels);
|
||||
if (icon != null) {
|
||||
final GLFWImage glfwImage1 = GLFWImage.malloc();
|
||||
glfwImage1.set(w.get(0), h.get(0), icon);
|
||||
final GLFWImage glfwImage2 = GLFWImage.malloc();
|
||||
glfwImage2.set(w.get(0), h.get(0), icon);
|
||||
|
||||
final GLFWImage.Buffer icons = GLFWImage.malloc(2);
|
||||
icons.put(0, glfwImage1);
|
||||
icons.put(1, glfwImage2);
|
||||
|
||||
org.lwjgl.glfw.GLFW.glfwSetWindowIcon(windowHandle, icons);
|
||||
|
||||
icons.free();
|
||||
glfwImage1.free();
|
||||
glfwImage2.free();
|
||||
} else {
|
||||
System.err.println("Failed to load icon: " + iconPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ByteBuffer loadIcon(final Path path, final IntBuffer w, final IntBuffer h, final IntBuffer channels) {
|
||||
try (final InputStream inputStream = Files.newInputStream(path)) {
|
||||
final byte[] iconBytes = inputStream.readAllBytes();
|
||||
final ByteBuffer buffer = ByteBuffer.allocateDirect(iconBytes.length).put(iconBytes).flip();
|
||||
final ByteBuffer icon = STBImage.stbi_load_from_memory(buffer, w, h, channels, 4);
|
||||
icon = loadIcon(iconPath, w, h, channels);
|
||||
if (icon == null) {
|
||||
System.err.println("Failed to load image from memory for: " + path + " - " + STBImage.stbi_failure_reason());
|
||||
return;
|
||||
}
|
||||
|
||||
try (GLFWImage.Buffer icons = GLFWImage.malloc(1)) {
|
||||
GLFWImage iconImage = icons.get(0);
|
||||
iconImage.set(w.get(0), h.get(0), icon);
|
||||
|
||||
GLFW.glfwSetWindowIcon(windowHandle, icons);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Failed to set window icon from path: {}", iconPath, e);
|
||||
} finally {
|
||||
if (icon != null) {
|
||||
STBImage.stbi_image_free(icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
LOGGER.error("Failed to load image from path: {} - {}", path, STBImage.stbi_failure_reason());
|
||||
}
|
||||
|
||||
return icon;
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
11
src/main/java/chylex/customwindowtitle/TitleChanger.java
Normal file
11
src/main/java/chylex/customwindowtitle/TitleChanger.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package chylex.customwindowtitle;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
public final class TitleChanger {
|
||||
private TitleChanger() {}
|
||||
|
||||
public static void setTitle(TitleConfig config) {
|
||||
Minecraft.getInstance().getWindow().setTitle(TitleParser.parse(config.getTitle()));
|
||||
}
|
||||
}
|
@@ -1,79 +1,89 @@
|
||||
package chylex.customwindowtitle;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class TitleConfig {
|
||||
private static final Map<String, String> DEFAULTS;
|
||||
private static volatile TitleConfig instance = null;
|
||||
private static final String KEY_TITLE = "title";
|
||||
private static final String KEY_ICON = "icon";
|
||||
|
||||
static {
|
||||
final Map<String, String> defaults = new LinkedHashMap<>();
|
||||
defaults.put("title", "Minecraft {mcversion}");
|
||||
defaults.put("squareIcon", "");
|
||||
DEFAULTS = Collections.unmodifiableMap(defaults);
|
||||
}
|
||||
private static final ImmutableMap<String, String> DEFAULTS = ImmutableMap.<String, String>builder()
|
||||
.put(KEY_TITLE, "Minecraft {mcversion}")
|
||||
.put(KEY_ICON, "")
|
||||
.buildOrThrow();
|
||||
|
||||
private static volatile TitleConfig instance;
|
||||
|
||||
public static TitleConfig getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static TitleConfig load(final String folder) {
|
||||
if (instance != null)
|
||||
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) {
|
||||
synchronized(TitleConfig.class) {
|
||||
if (instance == null) {
|
||||
final Path configFile = Paths.get(folder, "customwindowtitle-client.toml");
|
||||
final Map<String, String> config = new LinkedHashMap<>(DEFAULTS);
|
||||
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);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream()
|
||||
.map(String::trim)
|
||||
.filter(line -> !line.isEmpty())
|
||||
.forEach(line -> {
|
||||
final String[] split = line.split("=", 2);
|
||||
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());
|
||||
|
||||
String key = split[0].trim();
|
||||
String value = parseTrimmedValue(split[1].trim());
|
||||
|
||||
if (config.containsKey(key)) {
|
||||
config.put(key, value);
|
||||
} else {
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
final String iconPath = config.get("squareIcon");
|
||||
final Path pathIcon = iconPath.isEmpty() ? null : Paths.get(folder, iconPath);
|
||||
if (pathIcon != null && Files.notExists(pathIcon)) {
|
||||
throw new RuntimeException("CustomWindowTitle icon not found: " + pathIcon);
|
||||
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);
|
||||
}
|
||||
|
||||
instance = new TitleConfig(config.get("title"), pathIcon);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
return new TitleConfig(config.get(KEY_TITLE), iconPath);
|
||||
}
|
||||
|
||||
private static String parseTrimmedValue(String value) {
|
||||
@@ -103,8 +113,6 @@ public final class TitleConfig {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class TitleParser {
|
||||
final class TitleParser {
|
||||
private static final Pattern tokenRegex = Pattern.compile("\\{([a-z]+)(?::([^}]+))?}");
|
||||
private static final Logger logger = LogManager.getLogger("CustomWindowTitle");
|
||||
|
||||
|
@@ -5,10 +5,10 @@ import net.minecraft.client.Minecraft;
|
||||
|
||||
public interface CommonTokenProvider {
|
||||
default String getMinecraftVersion() {
|
||||
return SharedConstants.getCurrentVersion().getName();
|
||||
return SharedConstants.getCurrentVersion().name();
|
||||
}
|
||||
|
||||
String getModVersion(final String modId);
|
||||
String getModVersion(String modId);
|
||||
|
||||
default String getUsername() {
|
||||
return Minecraft.getInstance().getUser().getName();
|
||||
|
@@ -1,7 +1,5 @@
|
||||
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;
|
||||
@@ -10,7 +8,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(Minecraft.class)
|
||||
public final class DisableVanillaTitle {
|
||||
|
||||
@Inject(method = "updateTitle()V", at = @At("HEAD"), cancellable = true)
|
||||
private void updateTitle(final CallbackInfo info) {
|
||||
info.cancel();
|
||||
|
@@ -11,8 +11,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
@Mixin(Minecraft.class)
|
||||
public final class InitializeCustomIcon {
|
||||
@Inject(method = "onResourceLoadFinished", at = @At("HEAD"))
|
||||
private void onFinishedLoading(final CallbackInfo callbackInfo) {
|
||||
final TitleConfig config = TitleConfig.getInstance();
|
||||
private void onFinishedLoading(CallbackInfo callbackInfo) {
|
||||
TitleConfig config = TitleConfig.getInstance();
|
||||
if (config != null && config.hasIcon()) {
|
||||
IconChanger.setIcon(config.getIconPath());
|
||||
}
|
||||
|
Reference in New Issue
Block a user