From 40432e083b11271cf3148b9c38156cf759436699 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Tue, 5 Sep 2023 23:22:33 -0400 Subject: Initial code dump. --- .../trashyaddon/commands/MapDumpCommand.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java (limited to 'src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java') diff --git a/src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java b/src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java new file mode 100644 index 0000000..667501a --- /dev/null +++ b/src/main/java/org/chrisoft/trashyaddon/commands/MapDumpCommand.java @@ -0,0 +1,81 @@ +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 builder) { + builder.executes(context -> { + int nmaps = 0; + Vector 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); + 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; + }); + } +} -- cgit v1.2.3