Skip to content

Commit

Permalink
Implement command to export map item images
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Feb 18, 2024
1 parent 9f9e820 commit 3b1b93c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Objects;
import java.util.function.ToIntFunction;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -176,8 +177,7 @@ public void postConstruct() {
var z = IntegerArgumentType.getInteger(c, "z");

return executePathfinding(new PosGoal(x, y, z));
})))))
);
}))))));
dispatcher.register(
literal("stop-path")
.executes(
Expand Down Expand Up @@ -421,6 +421,47 @@ public void postConstruct() {

return Command.SINGLE_SUCCESS;
}))));
dispatcher.register(
literal("export-map")
.then(
argument("map_id", IntegerArgumentType.integer())
.executes(
help(
"Exports a image of a map item by map id. Can be a held item or in a item-frame.",
c -> {
var mapId = IntegerArgumentType.getInteger(c, "map_id");
var currentTime = System.currentTimeMillis();

return forEveryBot(
bot -> {
var mapDataState =
bot.sessionDataManager().mapDataStates().get(mapId);
if (mapDataState == null) {
c.getSource().sendMessage("Map not found!");
return Command.SINGLE_SUCCESS;
}

var image = mapDataState.toBufferedImage();
var fileName =
"map_"
+ mapId
+ "_"
+ currentTime
+ "_"
+ bot.meta().minecraftAccount().username()
+ ".png";
try {
Files.createDirectories(SFPathConstants.MAPS_FOLDER);
var file = SFPathConstants.MAPS_FOLDER.resolve(fileName);
ImageIO.write(image, "png", file.toFile());
c.getSource().sendMessage("Exported map to " + file);
} catch (IOException e) {
log.error("Failed to export map!", e);
}

return Command.SINGLE_SUCCESS;
});
}))));

SoulFireAPI.postEvent(new DispatcherInitEvent(dispatcher));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public int calculateRGBColor(Brightness brightness) {

public static int getColorFromPackedId(int packedId) {
var i = packedId & 0xFF;
return COLORS[i >> 2].calculateRGBColor(Brightness.VALUES[i & 3]);
return fromId(i >> 2).calculateRGBColor(Brightness.VALUES[i & 3]);
}

private static MapColor fromId(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,21 @@ public void setColor(int x, int y, byte color) {
colorData[x + y * 128] = color;
}

public BufferedImage toBufferedImage(MapDataState mapData) {
var image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
public BufferedImage toBufferedImage() {
var image =
new BufferedImage(128, 128, BufferedImage.TYPE_4BYTE_ABGR);
for (var x = 0; x < 128; ++x) {
for (var y = 0; y < 128; ++y) {
image.setRGB(x, y, MapColor.getColorFromPackedId(mapData.getColor(x, y)));
image.setRGB(x, y, convertABGRToARGB(MapColor.getColorFromPackedId(getColor(x, y))));
}
}

return image;
}

private static int convertABGRToARGB(int argbColor) {
var r = (argbColor >> 16) & 0xFF;
var b = argbColor & 0xFF;
return (argbColor & 0xFF00FF00) | (b << 16) | r;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SFPathConstants {
public static final Path PLUGINS_FOLDER = DATA_FOLDER.resolve("plugins");
public static final Path CONFIG_FOLDER = DATA_FOLDER.resolve("config");
public static final Path PROFILES_FOLDER = DATA_FOLDER.resolve("profiles");
public static final Path MAPS_FOLDER = DATA_FOLDER.resolve("maps");

private SFPathConstants() {}

Expand Down

0 comments on commit 3b1b93c

Please sign in to comment.