Fix serialization

This commit is contained in:
Norbi Peti 2020-01-06 16:59:13 +01:00
parent bdbc3ac184
commit b05197fa36
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 24 additions and 15 deletions

View file

@ -42,7 +42,7 @@
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.8.5</version> <version>2.8.6</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,14 +1,11 @@
package io.github.norbipeti.gcmc; package io.github.norbipeti.gcmc;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
@Data @Data
@AllArgsConstructor
public class Blocks { public class Blocks {
private Location start; private Location start;
private Location end; private Location end;
private Material material; private String material;
} }

View file

@ -1,7 +1,7 @@
package io.github.norbipeti.gcmc; package io.github.norbipeti.gcmc;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.gson.Gson; import com.google.gson.*;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -15,6 +15,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,7 +31,7 @@ public class PluginMain extends JavaPlugin {
sender.sendMessage("§cUsage: /export <x1> <y1> <z1> <x2> <y2> <z2>"); sender.sendMessage("§cUsage: /export <x1> <y1> <z1> <x2> <y2> <z2>");
return true; return true;
} }
int[] xyz = new int[6]; final int[] xyz = new int[6];
for (int i = 0; i < args.length; i++) for (int i = 0; i < args.length; i++)
xyz[i] = Integer.parseInt(args[i]); xyz[i] = Integer.parseInt(args[i]);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
@ -42,16 +43,18 @@ public class PluginMain extends JavaPlugin {
} }
World world = sender instanceof Player ? ((Player) sender).getWorld() : Bukkit.getWorlds().get(0); World world = sender instanceof Player ? ((Player) sender).getWorld() : Bukkit.getWorlds().get(0);
val list = new ArrayList<Blocks>(); val list = new ArrayList<Blocks>();
for (int y = xyz[1]; y < xyz[4]; y++) { for (int y = xyz[1]; y <= xyz[4]; y++) {
Blocks blocks = new Blocks(null, null, null); Blocks blocks = new Blocks();
for (int x = xyz[0]; x < xyz[3]; x++) { for (int x = xyz[0]; x <= xyz[3]; x++) {
for (int z = xyz[2]; z < xyz[5]; z++) { for (int z = xyz[2]; z <= xyz[5]; z++) {
Block block = world.getBlockAt(x, y, z); Block block = world.getBlockAt(x, y, z);
Material mat = block.getType(); Material mat = block.getType();
if (blocks.getMaterial() != mat) { if (!mat.name().equals(blocks.getMaterial())) {
if (blocks.getStart() != null) if (blocks.getStart() != null) {
list.add(blocks); list.add(blocks);
blocks.setMaterial(mat); blocks = new Blocks();
}
blocks.setMaterial(mat.name());
blocks.setStart(new Location(null, x, y, z)); blocks.setStart(new Location(null, x, y, z));
blocks.setEnd(blocks.getStart()); blocks.setEnd(blocks.getStart());
} else } else
@ -60,7 +63,16 @@ public class PluginMain extends JavaPlugin {
} }
list.add(blocks); list.add(blocks);
} }
Gson gson = new Gson(); Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new JsonSerializer<Location>() {
@Override
public JsonElement serialize(Location src, Type typeOfSrc, JsonSerializationContext context) {
val jo = new JsonObject();
jo.addProperty("x", src.getBlockX() - xyz[0]);
jo.addProperty("y", src.getBlockY() - xyz[1]);
jo.addProperty("z", src.getBlockZ() - xyz[2]);
return jo;
}
}).create();
try { try {
Files.write(gson.toJson(list), new File("result.txt"), StandardCharsets.UTF_8); Files.write(gson.toJson(list), new File("result.txt"), StandardCharsets.UTF_8);
sender.sendMessage("§bSuccess!"); sender.sendMessage("§bSuccess!");