Skip to content

Commit 2f84792

Browse files
committed
Make size-less commands automatically rescale the image to valid dimensions
1 parent 65da92e commit 2f84792

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/main/java/space/essem/image2map/Image2Map.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@ private int openPreview(CommandContext<CommandSourceStack> context) throws Comma
151151
if (GuiHelpers.getCurrentGui(source.getPlayer()) instanceof PreviewGui previewGui) {
152152
previewGui.close();
153153
}
154-
new PreviewGui(context.getSource().getPlayer(), image, input, DitherMode.NONE, image.getWidth(), image.getHeight());
154+
155+
var width = image.getWidth();
156+
var height = image.getHeight();
157+
158+
if (height > CONFIG.maxSize || width > CONFIG.maxSize) {
159+
var scaleDown = Math.min(CONFIG.maxSize / (double) height, CONFIG.maxSize / (double) width);
160+
width = (int) (width * scaleDown);
161+
height = (int) (height * scaleDown);
162+
}
163+
164+
new PreviewGui(context.getSource().getPlayer(), image, input, DitherMode.NONE, width, height);
155165

156166
return null;
157167
}, source.getServer());
@@ -299,18 +309,27 @@ private int createMap(CommandContext<CommandSourceStack> context) throws Command
299309
try {
300310
width = IntegerArgumentType.getInteger(context, "width");
301311
height = IntegerArgumentType.getInteger(context, "height");
312+
313+
if (height > CONFIG.maxSize || width > CONFIG.maxSize) {
314+
int finalHeight = height;
315+
int finalWidth = width;
316+
source.sendSuccess(() -> Component.literal("Map size exceeds maximum allowed (" + CONFIG.maxSize + "x" + CONFIG.maxSize + "), was " + finalWidth + "x" + finalHeight), false);
317+
return null;
318+
}
302319
} catch (Throwable e) {
303320
width = image.getWidth();
304321
height = image.getHeight();
322+
323+
if (height > CONFIG.maxSize || width > CONFIG.maxSize) {
324+
var scaleDown = Math.min(CONFIG.maxSize / (double) height, CONFIG.maxSize / (double) width);
325+
width = (int) (width * scaleDown);
326+
height = (int) (height * scaleDown);
327+
}
305328
}
306329

307330
int finalHeight = height;
308331
int finalWidth = width;
309332

310-
if (finalHeight > CONFIG.maxSize || finalWidth > CONFIG.maxSize) {
311-
source.sendSuccess(() -> Component.literal("Map size exceeds maximum allowed (" + CONFIG.maxSize + "x" + CONFIG.maxSize + "), was " + finalWidth + "x" + finalHeight), false);
312-
return null;
313-
}
314333
source.sendSuccess(() -> Component.literal("Converting into maps..."), false);
315334

316335
CompletableFuture.supplyAsync(() -> MapRenderer.render(image, mode, finalWidth, finalHeight)).thenAcceptAsync(mapImage -> {

src/main/java/space/essem/image2map/config/Image2MapConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Image2MapConfig {
1717
public boolean allowLocalFiles = false;
1818

1919
public int minPermLevel = 2;
20-
public int maxSize = 1024;
20+
public int maxSize = 2048;
2121

2222
public static Image2MapConfig loadOrCreateConfig() {
2323
try {

0 commit comments

Comments
 (0)