aboutsummaryrefslogblamecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java
blob: 2bc7096bea58cdda27dbbf431e6c607e4467c815 (plain) (tree)




















































                                                                                                                         
                                                                                


























                                                                                                  
package org.chrisoft.trashyaddon.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.commands.Command;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.decoration.ItemFrameEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.item.FilledMapItem;
import net.minecraft.item.map.MapState;
import net.minecraft.util.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteOrder;
import java.util.zip.GZIPOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import java.util.OptionalInt;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;

public class MapDumpCommand extends Command {
    private final MinecraftClient mc = MinecraftClient.getInstance();
    public MapDumpCommand() {
        super("md", "Dump all maps in view distance.");
    }

    @Override
    public void build(LiteralArgumentBuilder<CommandSource> builder) {
        builder.executes(context -> {
            int nmaps = 0;
            Vector<ByteBuffer> bufs = new Vector<>();
            for (Entity e : mc.world.getEntities()) {
                if (e.getType() != EntityType.ITEM_FRAME && e.getType() != EntityType.GLOW_ITEM_FRAME)
                    continue;
                ItemFrameEntity ife = (ItemFrameEntity) e;
                OptionalInt oid = ife.getMapId();
                if (oid.isEmpty())
                    continue;
                int id = oid.getAsInt();
                MapState mapState = FilledMapItem.getMapState(id, mc.world);
                if (mapState == null)
                    continue;
                String name = ife.getHeldItemStack().hasCustomName() ? ife.getHeldItemStack().getName().getString() : "";
                byte[] u8name = name.getBytes(StandardCharsets.UTF_8);
                int bsz = 4 + 4 + u8name.length + 16384;
                ByteBuffer buf = ByteBuffer.allocate(bsz);
                buf.order(ByteOrder.LITTLE_ENDIAN);
                buf.putInt(id);
                buf.putInt(u8name.length  | (mapState.locked ? 0x80000000 : 0));
                buf.put(u8name);
                buf.put(mapState.colors);
                bufs.add(buf);
                ++nmaps;
            }
            info("Maps found: " + nmaps);
            try {
                File dirpath = new File(MeteorClient.FOLDER, "mapman");
                if (!dirpath.isDirectory())
                    dirpath.mkdir();
                File dumppath = new File(dirpath, "md-" + Util.getFormattedCurrentTime() + ".gz");
                FileOutputStream fo = new FileOutputStream(dumppath);
                GZIPOutputStream output = new GZIPOutputStream(fo);
                for (ByteBuffer buf : bufs)
                    output.write(buf.array());
                output.close();
                fo.close();
                info("Maps dumped to " + dumppath.toString());
            }
            catch (Exception e) {
                error("Cannot write output:" + e.getMessage());
                return 0;
            }
            return SINGLE_SUCCESS;
        });
    }
}