Skip to content

Commit 033b050

Browse files
committed
refactor #2: change image data handling from Base64 strings to byte arrays
1 parent b15f669 commit 033b050

7 files changed

Lines changed: 36 additions & 35 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "cn.lunadeer"
10-
version = "3.1-rc.2"
10+
version = "4.0-rc.1"
1111

1212
repositories {
1313
mavenCentral()

src/main/java/cn/lunadeer/reColorfulMap/ImageMapItem.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public static class ImageMapItemText extends ConfigurationPart {
3838

3939
private final Integer x_count;
4040
private final Integer y_count;
41-
private final String[][] tile_b64_matrix; // 0,0 is top left
42-
private final String thumb_b64;
41+
private final byte[][][] tile_b64_matrix; // 0,0 is top left
42+
private final byte[] thumb_b64;
4343
private final World world;
4444
private Integer mapview_id;
4545

@@ -80,7 +80,7 @@ public ImageMapItem(World world, String url, Float scale) throws Exception {
8080
BufferedImage centered_image = ImageUtils.center(resized_image, new_width, new_height);
8181
image_width = centered_image.getWidth();
8282
image_height = centered_image.getHeight();
83-
this.tile_b64_matrix = new String[this.y_count][this.x_count];
83+
this.tile_b64_matrix = new byte[this.y_count][this.x_count][];
8484
for (int y = 0; y < this.y_count; y++) {
8585
for (int x = 0; x < this.x_count; x++) {
8686
int width = Math.min(128, image_width - x * 128);
@@ -97,7 +97,7 @@ public ImageMapItem(World world, String url, Float scale) throws Exception {
9797

9898
MapMeta meta = (MapMeta) this.getItemMeta();
9999
// Store image tile matrix
100-
meta.getPersistentDataContainer().set(THUMB_KEY(), PersistentDataType.STRING, this.thumb_b64);
100+
meta.getPersistentDataContainer().set(THUMB_KEY(), PersistentDataType.BYTE_ARRAY, this.thumb_b64);
101101
meta.getPersistentDataContainer().set(MATRIX_X_KEY(), PersistentDataType.INTEGER, this.tile_b64_matrix[0].length);
102102
meta.getPersistentDataContainer().set(MATRIX_Y_KEY(), PersistentDataType.INTEGER, this.tile_b64_matrix.length);
103103
// Set size info
@@ -155,10 +155,10 @@ public void setUpThumbnail() throws Exception {
155155
public ImageMapItem(ItemStack mapItem) throws Exception {
156156
super(mapItem);
157157
MapMeta meta = (MapMeta) mapItem.getItemMeta();
158-
if (!meta.getPersistentDataContainer().has(THUMB_KEY(), PersistentDataType.STRING)) {
158+
if (!meta.getPersistentDataContainer().has(THUMB_KEY(), PersistentDataType.BYTE_ARRAY)) {
159159
throw new Exception("IGNORING: Not an ImageMapItem");
160160
}
161-
this.thumb_b64 = meta.getPersistentDataContainer().get(THUMB_KEY(), PersistentDataType.STRING);
161+
this.thumb_b64 = meta.getPersistentDataContainer().get(THUMB_KEY(), PersistentDataType.BYTE_ARRAY);
162162
this.x_count = meta.getPersistentDataContainer().get(MATRIX_X_KEY(), PersistentDataType.INTEGER);
163163
this.y_count = meta.getPersistentDataContainer().get(MATRIX_Y_KEY(), PersistentDataType.INTEGER);
164164
if (this.x_count == null || this.y_count == null) {
@@ -169,7 +169,7 @@ public ImageMapItem(ItemStack mapItem) throws Exception {
169169
}
170170
this.world = meta.getMapView().getWorld();
171171
this.mapview_id = meta.getMapView().getId();
172-
this.tile_b64_matrix = new String[this.y_count][this.x_count];
172+
this.tile_b64_matrix = new byte[this.y_count][this.x_count][];
173173
for (int y = 0; y < this.y_count; y++) {
174174
for (int x = 0; x < this.x_count; x++) {
175175
this.tile_b64_matrix[y][x] = getTileFromWorldPDC(x, y);
@@ -194,19 +194,19 @@ public Integer getYCount() {
194194
return y_count;
195195
}
196196

197-
public String getThumbBase64() {
197+
public byte[] getThumbBase64() {
198198
return thumb_b64;
199199
}
200200

201201
public Integer getMapviewId() {
202202
return mapview_id;
203203
}
204204

205-
public String getTileBase64(int x, int y) {
205+
public byte[] getTileBase64(int x, int y) {
206206
return tile_b64_matrix[y][x];
207207
}
208208

209-
public void setTileBase64(int x, int y, String base64) {
209+
public void setTileBase64(int x, int y, byte[] base64) {
210210
tile_b64_matrix[y][x] = base64;
211211
}
212212

@@ -227,15 +227,15 @@ private static NamespacedKey IMAGE_TILE_KEY(int mapId, int x, int y) {
227227
}
228228

229229
public void saveTileToWorldPDC(int x, int y) {
230-
Objects.requireNonNull(world).getPersistentDataContainer().set(IMAGE_TILE_KEY(mapview_id, x, y), PersistentDataType.STRING, tile_b64_matrix[y][x]);
230+
Objects.requireNonNull(world).getPersistentDataContainer().set(IMAGE_TILE_KEY(mapview_id, x, y), PersistentDataType.BYTE_ARRAY, tile_b64_matrix[y][x]);
231231
}
232232

233233
public void removeTileFromWorldPDC(int x, int y) {
234234
Objects.requireNonNull(world).getPersistentDataContainer().remove(IMAGE_TILE_KEY(mapview_id, x, y));
235235
}
236236

237-
public String getTileFromWorldPDC(int x, int y) {
238-
return Objects.requireNonNull(world).getPersistentDataContainer().get(IMAGE_TILE_KEY(mapview_id, x, y), PersistentDataType.STRING);
237+
public byte[] getTileFromWorldPDC(int x, int y) {
238+
return Objects.requireNonNull(world).getPersistentDataContainer().get(IMAGE_TILE_KEY(mapview_id, x, y), PersistentDataType.BYTE_ARRAY);
239239
}
240240

241241

src/main/java/cn/lunadeer/reColorfulMap/ImageRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Pla
4646
}
4747
}
4848

49-
public static MapMeta renderOnMeta(MapMeta meta, String base64) throws Exception {
49+
public static MapMeta renderOnMeta(MapMeta meta, byte[] base64) throws Exception {
5050
BufferedImage image = ImageUtils.decodeBase64ToImage(base64);
5151
return renderOnMeta(meta, image);
5252
}

src/main/java/cn/lunadeer/reColorfulMap/PDCImageManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class PDCImageManager {
1818
public static void addThumbnailToWorldPDC(ImageMapItem imageMap) {
1919
World world = imageMap.getWorld();
2020
int mapViewId = imageMap.getMapviewId();
21-
String base64 = imageMap.getThumbBase64();
21+
byte[] base64 = imageMap.getThumbBase64();
2222
addImageBase64WorldPDC(world, mapViewId, base64);
2323
}
2424

@@ -42,10 +42,10 @@ public static void removeThumbnailFromWorldPDC(ImageMapItem imageMap) {
4242
* @param mapViewId The ID of the MapView associated with the image.
4343
* @param base64 The Base64 string of the image to add.
4444
*/
45-
public static void addImageBase64WorldPDC(World world, int mapViewId, String base64) {
45+
public static void addImageBase64WorldPDC(World world, int mapViewId, byte[] base64) {
4646
world.getPersistentDataContainer().set(
4747
new NamespacedKey("re_colorful_map", "mapview_id_%d".formatted(mapViewId)),
48-
PersistentDataType.STRING,
48+
PersistentDataType.BYTE_ARRAY,
4949
base64
5050
);
5151
}
@@ -57,10 +57,10 @@ public static void addImageBase64WorldPDC(World world, int mapViewId, String bas
5757
* @param mapViewId The ID of the MapView to retrieve the image for.
5858
* @return The Base64 string of the image, or null if not found.
5959
*/
60-
public static String getImageBase64WorldPDC(World world, int mapViewId) {
60+
public static byte[] getImageBase64WorldPDC(World world, int mapViewId) {
6161
return world.getPersistentDataContainer().get(
6262
new NamespacedKey("re_colorful_map", "mapview_id_%d".formatted(mapViewId)),
63-
PersistentDataType.STRING
63+
PersistentDataType.BYTE_ARRAY
6464
);
6565
}
6666

src/main/java/cn/lunadeer/reColorfulMap/events/ImageMapEvent.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static ItemStack[][] getTileMatrix(ItemFrame originItemFrame, ImageMapIt
123123
ItemStack[][] tileMatrix = new ItemStack[imageMapItem.getXCount()][imageMapItem.getYCount()];
124124
for (int j = imageMapItem.getYCount() - 1; j >= 0; j--) {
125125
for (int i = 0; i < imageMapItem.getXCount(); i++) {
126-
String tile_b64 = imageMapItem.getTileBase64(i, j);
126+
byte[] tile_b64 = imageMapItem.getTileBase64(i, j);
127127
ItemStack mapItem = new ItemStack(Material.FILLED_MAP);
128128
MapMeta meta = (MapMeta) mapItem.getItemMeta();
129129
if (i == 0 && j == imageMapItem.getYCount() - 1) {
@@ -133,7 +133,7 @@ private static ItemStack[][] getTileMatrix(ItemFrame originItemFrame, ImageMapIt
133133
meta.getPersistentDataContainer().set(originMapLocationX, PersistentDataType.INTEGER, originItemFrame.getLocation().getBlockX());
134134
meta.getPersistentDataContainer().set(originMapLocationY, PersistentDataType.INTEGER, originItemFrame.getLocation().getBlockY());
135135
meta.getPersistentDataContainer().set(originMapLocationZ, PersistentDataType.INTEGER, originItemFrame.getLocation().getBlockZ());
136-
meta.getPersistentDataContainer().set(tileBase64, PersistentDataType.STRING, tile_b64);
136+
meta.getPersistentDataContainer().set(tileBase64, PersistentDataType.BYTE_ARRAY, tile_b64);
137137
BufferedImage image = decodeBase64ToImage(tile_b64);
138138
MapView mapView = Bukkit.createMap(world);
139139
meta.setMapView(mapView);
@@ -206,7 +206,7 @@ public void breakImageMapsFromItemFrame(EntityDamageByEntityEvent event) {
206206
return null;
207207
}
208208
Location origin = new Location(clicked_item_frame.getWorld(), origin_x, origin_y, origin_z);
209-
return getItemFrame(origin);
209+
return getItemFrame(origin, clicked_item_frame.getFacing());
210210
}
211211

212212
private static @Nullable ImageMapItem TryGetImageMapItem(ItemStack itemStack) {
@@ -231,7 +231,7 @@ private static void HandleBroken(ItemFrame originItemFrame, ImageMapItem imageMa
231231
for (int j = 0; j < imageMapItem.getYCount(); j++) {
232232
for (int i = 0; i < imageMapItem.getXCount(); i++) {
233233
ItemStack mapItem = item_frames[i][j].getItem();
234-
String tile_b64 = mapItem.getItemMeta().getPersistentDataContainer().get(tileBase64, PersistentDataType.STRING);
234+
byte[] tile_b64 = mapItem.getItemMeta().getPersistentDataContainer().get(tileBase64, PersistentDataType.BYTE_ARRAY);
235235
imageMapItem.setTileBase64(i, j, tile_b64);
236236
imageMapItem.saveTileToWorldPDC(i, j);
237237
item_frames[i][j].setItem(new ItemStack(Material.AIR));
@@ -284,7 +284,7 @@ public static ItemFrame[][] getItemFrameMatrix(ItemFrame left_bottom, Integer x,
284284
loc.add(0, 0, -i);
285285
}
286286
loc.add(0, -j, 0);
287-
ItemFrame item_frame = getItemFrame(loc);
287+
ItemFrame item_frame = getItemFrame(loc, facing);
288288
if (item_frame == null) {
289289
throw new Exception(String.format(Language.imageMapEvent.incompleteItemFrameArray, x, y));
290290
}
@@ -294,14 +294,17 @@ public static ItemFrame[][] getItemFrameMatrix(ItemFrame left_bottom, Integer x,
294294
return item_frames;
295295
}
296296

297-
private static ItemFrame getItemFrame(Location loc) {
297+
private static ItemFrame getItemFrame(Location loc, BlockFace facing) {
298298
Collection<Entity> entities = loc.getWorld().getNearbyEntities(loc, 1, 1, 1);
299299
for (Entity entity : entities) {
300300
if (entity.getLocation().getBlockX() != loc.getBlockX() || entity.getLocation().getBlockY() != loc.getBlockY() || entity.getLocation().getBlockZ() != loc.getBlockZ()) {
301301
continue;
302302
}
303-
if (entity instanceof ItemFrame) {
304-
return (ItemFrame) entity;
303+
if (entity instanceof ItemFrame itemFrame) {
304+
if (itemFrame.getFacing() != facing) {
305+
continue;
306+
}
307+
return itemFrame;
305308
}
306309
}
307310
return null;

src/main/java/cn/lunadeer/reColorfulMap/events/MapInitEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void onMapInitEvent(MapInitializeEvent event) {
2020
if (world == null) {
2121
return;
2222
}
23-
String base64 = PDCImageManager.getImageBase64WorldPDC(world, view.getId());
23+
byte[] base64 = PDCImageManager.getImageBase64WorldPDC(world, view.getId());
2424
if (base64 == null) {
2525
return;
2626
}

src/main/java/cn/lunadeer/reColorfulMap/utils/ImageUtils.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ public static void saveImage(BufferedImage image, String outputPath, String form
6868
* @return The decoded BufferedImage.
6969
* @throws IOException If an error occurs while decoding the image.
7070
*/
71-
public static BufferedImage decodeBase64ToImage(String base64) throws IOException {
72-
byte[] imageBytes = Base64.getDecoder().decode(base64);
73-
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
71+
public static BufferedImage decodeBase64ToImage(byte[] base64) throws IOException {
72+
ByteArrayInputStream bis = new ByteArrayInputStream(base64);
7473
return ImageIO.read(bis);
7574
}
7675

@@ -82,11 +81,10 @@ public static BufferedImage decodeBase64ToImage(String base64) throws IOExceptio
8281
* @return The Base64 encoded string representing the image.
8382
* @throws IOException If an error occurs while encoding the image.
8483
*/
85-
public static String encodeImageToBase64(BufferedImage image, String format) throws IOException {
84+
public static byte[] encodeImageToBase64(BufferedImage image, String format) throws IOException {
8685
ByteArrayOutputStream ba_os = new ByteArrayOutputStream();
8786
ImageIO.write(image, format, ba_os);
88-
byte[] imageBytes = ba_os.toByteArray();
89-
return Base64.getEncoder().encodeToString(imageBytes);
87+
return ba_os.toByteArray();
9088
}
9189

9290
/**

0 commit comments

Comments
 (0)