Skip to content

Commit c786170

Browse files
committed
unhardcode map size
1 parent 62c67aa commit c786170

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/main/java/net/earthcomputer/clientcommands/command/MapCommand.java

+40-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
77
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
88
import net.minecraft.block.MapColor;
9-
import net.minecraft.block.entity.BlockEntity;
109
import net.minecraft.client.MinecraftClient;
1110
import net.minecraft.client.network.ClientPlayerEntity;
1211
import net.minecraft.client.texture.NativeImage;
@@ -30,8 +29,10 @@
3029
import java.util.stream.Collectors;
3130
import java.util.stream.StreamSupport;
3231

33-
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
34-
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;
32+
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
33+
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
34+
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
35+
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
3536

3637
public class MapCommand {
3738
private static final SimpleCommandExceptionType NO_HELD_MAP_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.cmap.noHeld"));
@@ -64,8 +65,8 @@ private static MapInfo[][] getMaps(ClientPlayerEntity player, MinecraftClient cl
6465
private static int exportMap(FabricClientCommandSource source, int upscale) throws CommandSyntaxException {
6566
MapInfo[][] mapInfo = getMaps(source.getPlayer(), source.getClient());
6667
// calculate width and height
67-
int width = mapInfo[0].length * 128 * upscale;
68-
int height = mapInfo.length * 128 * upscale;
68+
int width = mapInfo[0].length * FilledMapItem.field_30907 * upscale;
69+
int height = mapInfo.length * FilledMapItem.field_30908 * upscale;
6970

7071
try (NativeImage image = new NativeImage(NativeImage.Format.RGBA, width, height, true)) {
7172
for (int i = 0; i < mapInfo.length; ++i) {
@@ -74,7 +75,7 @@ private static int exportMap(FabricClientCommandSource source, int upscale) thro
7475
if (info == null) {
7576
continue;
7677
}
77-
drawMapOffset(image, info, j * 128 * upscale, i * 128 * upscale, upscale);
78+
drawMapOffset(image, info, j * FilledMapItem.field_30907 * upscale, i * FilledMapItem.field_30908 * upscale, upscale);
7879
}
7980
}
8081
File screenshotDir = new File(source.getClient().runDirectory, "screenshots");
@@ -93,8 +94,8 @@ private static int exportMap(FabricClientCommandSource source, int upscale) thro
9394
}
9495

9596
private static void drawMapOffset(NativeImage image, MapInfo map, int xOff, int yOff, int upscale) {
96-
for (int i = 0; i < 128; ++i) {
97-
for (int j = 0; j < 128; ++j) {
97+
for (int i = 0; i < FilledMapItem.field_30907; ++i) {
98+
for (int j = 0; j < FilledMapItem.field_30908; ++j) {
9899
int color = MapColor.getRenderColor(map.getColor(i, j));
99100
for (int k = 0; k < upscale; k++) {
100101
for (int l = 0; l < upscale; l++) {
@@ -117,7 +118,7 @@ private static MapInfo fromHand(ClientPlayerEntity player) throws CommandSyntaxE
117118
return null;
118119
}
119120

120-
return new MapInfo(fromItemStack(player.world, map), 0);
121+
return new MapInfo(fromItemStack(player.getWorld(), map), 0);
121122
}
122123

123124
private static MapInfo[][] fromWorld(ClientPlayerEntity player, Entity target) {
@@ -227,7 +228,7 @@ private static MapState fromItemStack(World world, ItemStack map) throws Command
227228

228229
private static MapState fromItemFrame(ItemFrameEntity entity) {
229230
Integer mapId = entity.getMapId().orElseThrow();
230-
return FilledMapItem.getMapState(mapId, entity.world);
231+
return FilledMapItem.getMapState(mapId, entity.getWorld());
231232
}
232233

233234
private record MapInfo(MapState state, int rotation) {
@@ -236,33 +237,54 @@ public int getColor(int x, int y) {
236237
// rotate x/y
237238
switch (rotation % 4) {
238239
case 0 -> {
239-
return state.colors[x + y * 128];
240+
return state.colors[x + y * FilledMapItem.field_30907];
240241
}
241242
case 1 -> {
242243
// 90 clockwise
243244
int newX = y;
244-
int newY = 127 - x;
245-
return state.colors[newX + newY * 128];
245+
int newY = FilledMapItem.field_30907 - x - 1;
246+
return state.colors[newX + newY * FilledMapItem.field_30907];
246247
}
247248
case 2 -> {
248249
// 180 clockwise
249-
int newX = 127 - x;
250-
int newY = 127 - y;
251-
return state.colors[newX + newY * 128];
250+
int newX = FilledMapItem.field_30907 - x - 1;
251+
int newY = FilledMapItem.field_30908 - y - 1;
252+
return state.colors[newX + newY * FilledMapItem.field_30907];
252253
}
253254
case 3 -> {
254255
// 270 clockwise
255-
int newX = 127 - y;
256+
int newX = FilledMapItem.field_30908 - y - 1;
256257
int newY = x;
257-
return state.colors[newX + newY * 128];
258+
return state.colors[newX + newY * FilledMapItem.field_30907];
258259
}
259260
// this should never be possible
260261
default -> throw new IllegalStateException("Unexpected value: " + rotation);
261262
}
262263
}
264+
265+
public int width() {
266+
if (rotation % 2 == 0) {
267+
return FilledMapItem.field_30907;
268+
} else {
269+
return FilledMapItem.field_30908;
270+
}
271+
}
272+
273+
public int height() {
274+
if (rotation % 2 == 0) {
275+
return FilledMapItem.field_30908;
276+
} else {
277+
return FilledMapItem.field_30907;
278+
}
279+
}
263280
}
264281

265282
private record FramePos(BlockPos pos, Direction facing) {
283+
284+
}
285+
286+
private record Vec2i(int x, int z) {
287+
266288
}
267289

268290
}

0 commit comments

Comments
 (0)