Skip to content

Commit

Permalink
clean up the save map command
Browse files Browse the repository at this point in the history
  • Loading branch information
Wide-Cat committed Sep 10, 2024
1 parent 0c59ec3 commit 4b05fba
Showing 1 changed file with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,15 +20,13 @@
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;
import org.lwjgl.system.MemoryUtil;
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;
Expand All @@ -52,55 +52,43 @@ public SaveMapCommand() {
@Override
public void build(LiteralArgumentBuilder<CommandSource> 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());
}
}

Expand All @@ -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() {
Expand Down

0 comments on commit 4b05fba

Please sign in to comment.