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

Compare commits

..

2 Commits

Author SHA1 Message Date
Kolotheegg
6854f05c05
Merge 1a9d5e65df into ba22745940 2024-09-13 20:13:55 +02:00
Kolotheegg
1a9d5e65df Implemented the requested changes. 2024-09-13 20:13:42 +02:00
8 changed files with 73 additions and 56 deletions

View File

@ -5,6 +5,7 @@
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="openjdk-21" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

View File

@ -11,9 +11,8 @@ import net.minecraft.client.Minecraft;
public class CustomWindowTitle implements ClientModInitializer { public class CustomWindowTitle implements ClientModInitializer {
private final TitleConfig config; private final TitleConfig config;
public CustomWindowTitle() { public CustomWindowTitle() {
config = TitleConfig.read(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString()); config = TitleConfig.load(FabricLoader.getInstance().getConfigDir().toAbsolutePath().toString());
} }
@Override @Override

View File

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

View File

@ -12,7 +12,7 @@ import java.nio.IntBuffer;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
public class IconChanger { public final class IconChanger {
public static void setIcon(final Path iconPath) { public static void setIcon(final Path iconPath) {
final long windowHandle = Minecraft.getInstance().getWindow().getWindow(); final long windowHandle = Minecraft.getInstance().getWindow().getWindow();

View File

@ -13,7 +13,7 @@ import java.util.stream.Collectors;
public final class TitleConfig { public final class TitleConfig {
private static final Map<String, String> DEFAULTS; private static final Map<String, String> DEFAULTS;
private static TitleConfig instance; private static volatile TitleConfig instance = null;
static { static {
final Map<String, String> defaults = new LinkedHashMap<>(); final Map<String, String> defaults = new LinkedHashMap<>();
@ -22,50 +22,56 @@ public final class TitleConfig {
DEFAULTS = Collections.unmodifiableMap(defaults); DEFAULTS = Collections.unmodifiableMap(defaults);
} }
public static TitleConfig read(final String folder) { public static TitleConfig getInstance() {
return instance;
}
public static TitleConfig load(final String folder) {
if (instance != null)
throw new IllegalStateException("TitleConfig has already been loaded and cannot be loaded again.");
if (instance == null) { if (instance == null) {
final Path configFile = Paths.get(folder, "customwindowtitle-client.toml"); synchronized (TitleConfig.class) {
final Map<String, String> config = new LinkedHashMap<>(DEFAULTS); if (instance == null) {
final Path configFile = Paths.get(folder, "customwindowtitle-client.toml");
final Map<String, String> config = new LinkedHashMap<>(DEFAULTS);
try { try {
if (!Files.exists(configFile)) { if (!Files.exists(configFile)) {
Files.write(configFile, config.entrySet().stream() Files.write(configFile, config.entrySet().stream()
.map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue())) .map(entry -> String.format("%s = '%s'", entry.getKey(), entry.getValue()))
.collect(Collectors.toList()), StandardCharsets.UTF_8); .collect(Collectors.toList()), StandardCharsets.UTF_8);
} else { } else {
Files.readAllLines(configFile, StandardCharsets.UTF_8).stream() Files.readAllLines(configFile, StandardCharsets.UTF_8).stream()
.map(String::trim) .map(String::trim)
.filter(line -> !line.isEmpty()) .filter(line -> !line.isEmpty())
.forEach(line -> { .forEach(line -> {
final String[] split = line.split("=", 2); 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 {
throw new RuntimeException("CustomWindowTitle configuration has an invalid key: " + key);
}
});
}
} catch (final IOException e) {
throw new RuntimeException("CustomWindowTitle configuration error", e);
}
if (split.length != 2) { final String iconPath = config.get("squareIcon");
throw new RuntimeException("CustomWindowTitle configuration has an invalid line: " + line); final Path pathIcon = iconPath.isEmpty() ? null : Paths.get(folder, iconPath);
} if (pathIcon != null && Files.notExists(pathIcon)) {
throw new RuntimeException("CustomWindowTitle icon not found: " + pathIcon);
}
final String key = split[0].trim(); instance = new TitleConfig(config.get("title"), pathIcon);
final 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) {
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);
}
instance = new TitleConfig(config.get("title"), pathIcon);
} }
return instance; return instance;
} }
@ -97,9 +103,7 @@ public final class TitleConfig {
this.icon = icon; this.icon = icon;
} }
public static TitleConfig getInstance() {
return instance;
}
public String getTitle() { public String getTitle() {
return title; return title;

View File

@ -15,12 +15,4 @@ public final class DisableVanillaTitle {
private void updateTitle(final CallbackInfo info) { private void updateTitle(final CallbackInfo info) {
info.cancel(); info.cancel();
} }
@Inject(method = "onResourceLoadFinished", at = @At("HEAD"))
private void onFinishedLoading(final CallbackInfo callbackInfo) {
final TitleConfig config = TitleConfig.getInstance();
if (config != null && config.hasIcon()) {
IconChanger.setIcon(config.getIconPath());
}
}
} }

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(final CallbackInfo callbackInfo) {
final TitleConfig config = TitleConfig.getInstance();
if (config != null && config.hasIcon()) {
IconChanger.setIcon(config.getIconPath());
}
}
}

View File

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