I wrote a program to parse Minecraft map images into json so they can be used by a plugin to write into the world. My program runs without fault each time and doesn't throw any errors. However, when I open the json file, it seems to be cut off at the end. Can anyone provide any clarification on this? Full code:
import com.google.gson.JsonArray;
import org.bukkit.Material;
import org.json.simple.JSONObject;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args){
List<Color> colors = new ArrayList<Color>();
String[] s = new String[]{"127 178 56","247 233 163","199 199 199","255 0 0","160 160 255","167 167 167","0 124 0","255 255 255","164 168 184","151 109 77","112 112 112","64 64 255","143 119 72","255 252 245","216 127 51","178 76 216","102 153 216","229 229 51","127 204 25","242 127 165","76 76 76","153 153 153","76 127 153","127 63 178","51 76 178","102 76 51 ","102 127 51","153 51 51","25 25 25","250 238 77","92 219 213","74 128 255","0 217 58","129 86 49","112 2 0","209 177 161","159 82 36","149 87 108","112 108 138","186 133 36","103 117 53","160 77 78","57 41 35","135 107 98","87 92 92","122 73 88","76 62 92","76 50 35","76 82 42","142 60 46","37 22 16"};
for(int i = 0; i < 51; i++){
String[] sl = s[i].split(" ");
int r = Integer.parseInt(sl[0]);
int g = Integer.parseInt(sl[1]);
int b = Integer.parseInt(sl[2]);
Color c = new Color(r,g,b);
colors.add(c);
}
Material[] mats = new Material[]{Material.SLIME_BLOCK,Material.SANDSTONE,Material.COBWEB,Material.REDSTONE_BLOCK,Material.BLUE_ICE,Material.IRON_BLOCK,Material.BAMBOO,Material.WHITE_WOOL,Material.CLAY,Material.COARSE_DIRT,Material.STONE,Material.WATER,Material.OAK_LOG,Material.QUARTZ_BLOCK,Material.ORANGE_WOOL,Material.MAGENTA_WOOL,Material.LIGHT_BLUE_WOOL,Material.YELLOW_WOOL,Material.LIME_WOOL,Material.PINK_WOOL,Material.GRAY_WOOL,Material.LIGHT_GRAY_WOOL,Material.CYAN_WOOL,Material.PURPLE_WOOL,Material.BLUE_WOOL,Material.BROWN_WOOL,Material.GREEN_WOOL,Material.RED_WOOL,Material.BLACK_WOOL,Material.GOLD_BLOCK,Material.DIAMOND_BLOCK,Material.LAPIS_BLOCK,Material.EMERALD_BLOCK,Material.PODZOL,Material.NETHERRACK,Material.WHITE_TERRACOTTA,Material.ORANGE_TERRACOTTA,Material.MAGENTA_TERRACOTTA,Material.LIGHT_BLUE_TERRACOTTA,Material.YELLOW_TERRACOTTA,Material.LIME_TERRACOTTA,Material.PINK_TERRACOTTA,Material.GRAY_TERRACOTTA,Material.LIGHT_GRAY_TERRACOTTA,Material.CYAN_TERRACOTTA,Material.PURPLE_TERRACOTTA,Material.BLUE_TERRACOTTA,Material.BROWN_TERRACOTTA,Material.GREEN_TERRACOTTA,Material.RED_TERRACOTTA,Material.BLACK_TERRACOTTA};
try {
BufferedImage image = ImageIO.read(new File("image/image.png"));
JSONObject obj = new JSONObject();
for(int y = 0; y<128; y++) {
JsonArray lines = new JsonArray();
for(int x = 0; x<128; x++) {
Material blockmat = null;
for(int i = 0; i < mats.length; i++) {
if (image.getRGB(x, y) == colors.get(i).getRGB()){
lines.add(String.valueOf(mats[i]));
}
}
}
obj.put(y,lines);
}
FileWriter file = new FileWriter("image.json");
file.write(obj.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
}
}