Skip to content

Commit

Permalink
Fixed packicon.png. Also made it auto-scale, so source image size sho…
Browse files Browse the repository at this point in the history
…uld no longer matter. Does not work on mac tho
  • Loading branch information
LatvianModder committed Nov 10, 2023
1 parent 21ab56e commit 6f2d3de
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static Path dir(Path dir) {
Path COMMON_PROPERTIES = CONFIG.resolve("common.properties");
Path CLIENT_PROPERTIES = CONFIG.resolve("client.properties");
Path CONFIG_DEV_PROPERTIES = CONFIG.resolve("dev.properties");
Path PACKICON = CONFIG.resolve("packicon.png");
Path README = DIRECTORY.resolve("README.txt");
Path LOCAL = dir(Platform.getGameFolder().resolve("local").resolve("kubejs"));
Path LOCAL_CACHE = dir(LOCAL.resolve("cache"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.util.KubeJSPlugins;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.util.Mth;

import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.OptionalInt;
import java.util.Properties;

Expand All @@ -30,8 +27,7 @@ public static void reload() {

private final Properties properties;
private boolean writeProperties;
private Path icon;
private boolean tempIconCancel = true;
private final boolean tempIconCancel = true;

public String title;
private boolean showTagNames;
Expand Down Expand Up @@ -78,22 +74,6 @@ private ClientProperties() {
menuInnerBackgroundBrightness = Mth.clamp(get("menuInnerBackgroundBrightness", 32), 0, 255);
menuBackgroundScale = (float) Mth.clamp(get("menuBackgroundScale", 32D), 0.0625D, 1024D);

var iconFile = KubeJSPaths.CONFIG.resolve("packicon.png");

try {
var p0 = KubeJSPaths.DIRECTORY.resolve("packicon.png");

if (Files.exists(p0)) {
Files.move(p0, iconFile);
}
} catch (Exception ex) {
ex.printStackTrace();
}

if (Files.exists(iconFile)) {
icon = iconFile;
}

KubeJSPlugins.forEachPlugin(p -> p.loadClientProperties(this));

if (writeProperties) {
Expand Down Expand Up @@ -153,29 +133,6 @@ public float[] getColor3f(int color) {
return c;
}

@Environment(EnvType.CLIENT)
public boolean cancelIconUpdate() {
if (tempIconCancel) {
if (icon != null) {
try (var stream16 = Files.newInputStream(icon);
var stream32 = Files.newInputStream(icon)) {
tempIconCancel = false;
// todo: ~~buy noose~~ fix this
//Minecraft.getInstance().getWindow().setIcon(stream16, stream32);
tempIconCancel = true;
} catch (Exception ex) {
ex.printStackTrace();
}

return true;
} else {
return false;
}
}

return false;
}

public boolean getShowTagNames() {
return showTagNames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ default JsonObject toJsonJS() {
return OutputItem.of(kjs$self(), chance);
}

default ItemStack kjs$withLore(Component... text) {
default ItemStack kjs$withLore(Component[] text) {
var is = kjs$self().copy();

if (text.length > 0) {
Expand Down
63 changes: 63 additions & 0 deletions common/src/main/java/dev/latvian/mods/kubejs/core/WindowKJS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.latvian.mods.kubejs.core;

import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.script.data.GeneratedData;
import dev.latvian.mods.kubejs.util.Lazy;
import net.minecraft.server.packs.resources.IoSupplier;

import javax.imageio.ImageIO;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.function.Supplier;

public interface WindowKJS {
record KJSScaledIconProvider(BufferedImage original, int target) implements Supplier<byte[]> {
@Override
public byte[] get() {
try {
var out = new ByteArrayOutputStream();

if (original.getWidth() == target && original.getHeight() == target) {
ImageIO.write(original, "png", out);
} else {
var img = new BufferedImage(target, target, BufferedImage.TYPE_INT_ARGB);
var g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawImage(original, 0, 0, target, target, null);
g.dispose();
ImageIO.write(img, "png", out);
}

return out.toByteArray();
} catch (Exception ex) {
throw new IllegalStateException(original.toString(), ex);
}
}
}

default List<IoSupplier<InputStream>> kjs$loadIcons(List<IoSupplier<InputStream>> original) throws IOException {
if (Files.exists(KubeJSPaths.PACKICON)) {
System.out.println("Hello");

try (var in = Files.newInputStream(KubeJSPaths.PACKICON)) {
var img = ImageIO.read(in);

return List.of(
new GeneratedData(KubeJS.id("icon_16x.png"), Lazy.of(new KJSScaledIconProvider(img, 16)), true),
new GeneratedData(KubeJS.id("icon_32x.png"), Lazy.of(new KJSScaledIconProvider(img, 32)), true),
new GeneratedData(KubeJS.id("icon_48x.png"), Lazy.of(new KJSScaledIconProvider(img, 48)), true),
new GeneratedData(KubeJS.id("icon_128.png"), Lazy.of(new KJSScaledIconProvider(img, 128)), true),
new GeneratedData(KubeJS.id("icon_256x.png"), Lazy.of(new KJSScaledIconProvider(img, 256)), true)
);
}
}

return original;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package dev.latvian.mods.kubejs.core.mixin.common;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.mojang.blaze3d.platform.IconSet;
import com.mojang.blaze3d.platform.Window;
import dev.latvian.mods.kubejs.core.WindowKJS;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.resources.IoSupplier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Mixin(Window.class)
public class WindowMixin {
/*
@Inject(method = "setIcon", at = @At("HEAD"), cancellable = true)
private void setWindowIcon(InputStream icon16, InputStream icon32, CallbackInfo ci) {
if (ClientProperties.get().cancelIconUpdate()) {
ci.cancel();
}
public class WindowMixin implements WindowKJS {
@ModifyExpressionValue(method = "setIcon", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/platform/IconSet;getStandardIcons(Lnet/minecraft/server/packs/PackResources;)Ljava/util/List;"))
private List<IoSupplier<InputStream>> kjs$icons(List<IoSupplier<InputStream>> original, PackResources packResources, IconSet set) throws IOException {
return kjs$loadIcons(original);
}
*/
}

0 comments on commit 6f2d3de

Please sign in to comment.