diff --git a/src/main/java/meteordevelopment/meteorclient/commands/commands/SaveMapCommand.java b/src/main/java/meteordevelopment/meteorclient/commands/commands/SaveMapCommand.java index 573cd90d23..ee9bbcabcf 100644 --- a/src/main/java/meteordevelopment/meteorclient/commands/commands/SaveMapCommand.java +++ b/src/main/java/meteordevelopment/meteorclient/commands/commands/SaveMapCommand.java @@ -7,7 +7,9 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import meteordevelopment.meteorclient.MeteorClient; import meteordevelopment.meteorclient.commands.Command; import meteordevelopment.meteorclient.mixin.MapRendererAccessor; import net.minecraft.client.render.MapRenderer; @@ -18,7 +20,6 @@ import net.minecraft.item.Items; import net.minecraft.item.map.MapState; import net.minecraft.text.Text; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; @@ -26,7 +27,6 @@ import org.lwjgl.util.tinyfd.TinyFileDialogs; import javax.imageio.ImageIO; -import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -52,55 +52,43 @@ public SaveMapCommand() { @Override public void build(LiteralArgumentBuilder builder) { builder.executes(context -> { - - MapState state = getMapState(); - if (state == null) throw MAP_NOT_FOUND.create(); - ItemStack map = getMap(); - - String path = getPath(); - if (path == null) throw OOPS.create(); - - saveMap(map, state, path, 128); + saveMap(128); return SINGLE_SUCCESS; }).then(argument("scale", IntegerArgumentType.integer(1)).executes(context -> { - int scale = IntegerArgumentType.getInteger(context, "scale"); - - MapState state = getMapState(); - if (state == null) throw MAP_NOT_FOUND.create(); - ItemStack map = getMap(); - - String path = getPath(); - if (path == null) throw OOPS.create(); - - saveMap(map, state, path, scale); + saveMap(IntegerArgumentType.getInteger(context, "scale")); return SINGLE_SUCCESS; })); } - private void saveMap(@NotNull ItemStack map, MapState state, String path, int scale) { - //this is horrible code but it somehow works + private void saveMap(int scale) throws CommandSyntaxException { + ItemStack map = getMap(); + MapState state = getMapState(); + if (map == null || state == null) throw MAP_NOT_FOUND.create(); + + File path = getPath(); + if (path == null) throw OOPS.create(); MapRenderer mapRenderer = mc.gameRenderer.getMapRenderer(); MapRenderer.MapTexture texture = ((MapRendererAccessor) mapRenderer).invokeGetMapTexture(map.get(DataComponentTypes.MAP_ID), state); + if (texture.texture.getImage() == null) throw OOPS.create(); - int[] data = texture.texture.getImage().makePixelArray(); - BufferedImage image = new BufferedImage(128, 128, 2); - image.setRGB(0, 0, image.getWidth(), image.getHeight(), data, 0, 128); + try { + if (scale == 128) texture.texture.getImage().writeTo(path); + else { + int[] data = texture.texture.getImage().makePixelArray(); + BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB); + image.setRGB(0, 0, image.getWidth(), image.getHeight(), data, 0, 128); - BufferedImage scaledImage = new BufferedImage(scale, scale, 2); - if (scale != 128) { - Graphics2D g = scaledImage.createGraphics(); - g.setComposite(AlphaComposite.Src); - g.drawImage(image, 0, 0, scale, scale, null); - g.dispose(); - } + BufferedImage scaledImage = new BufferedImage(scale, scale, 2); + scaledImage.createGraphics().drawImage(image, 0, 0, scale, scale, null); - try { - ImageIO.write((scale == 128 ? image : scaledImage), "png", new File(path)); + ImageIO.write(scaledImage, "png", path); + } } catch (IOException e) { - e.printStackTrace(); + error("Error writing map texture"); + MeteorClient.LOG.error(e.toString()); } } @@ -111,12 +99,12 @@ private void saveMap(@NotNull ItemStack map, MapState state, String path, int sc return FilledMapItem.getMapState(map.get(DataComponentTypes.MAP_ID), mc.world); } - private @Nullable String getPath() { + private @Nullable File getPath() { String path = TinyFileDialogs.tinyfd_saveFileDialog("Save image", null, filters, null); if (path == null) return null; if (!path.endsWith(".png")) path += ".png"; - return path; + return new File(path); } private @Nullable ItemStack getMap() {