This commit is contained in:
parent
e147e995bb
commit
cde1c48b7a
8 changed files with 645 additions and 1 deletions
|
@ -0,0 +1,65 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import static me.unurled.sacredrealms.sr.utils.Logger.error;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import java.lang.reflect.Type;
|
||||
import me.unurled.sacredrealms.sr.components.clientbuild.ClientBuild;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
public class ClientBuildDeserializer implements JsonDeserializer<ClientBuild> {
|
||||
|
||||
/**
|
||||
* Gson invokes this call-back method during deserialization when it encounters a field of the
|
||||
* specified type.
|
||||
*
|
||||
* <p>In the implementation of this call-back method, you should consider invoking {@link
|
||||
* JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects for any
|
||||
* non-trivial field of the returned object. However, you should never invoke it on the same type
|
||||
* passing {@code json} since that will cause an infinite loop (Gson will call your call-back
|
||||
* method again).
|
||||
*
|
||||
* @param json The Json data being deserialized
|
||||
* @param typeOfT The type of the Object to deserialize to
|
||||
* @param context
|
||||
* @return a deserialized object of the specified type typeOfT which is a subclass of {@code T}
|
||||
* @throws JsonParseException if json is not in the expected format of {@code typeofT}
|
||||
*/
|
||||
@Override
|
||||
public ClientBuild deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
String[] data = json.getAsString().split(";");
|
||||
ClientBuild build = new ClientBuild(data[0]);
|
||||
for (String block : data[1].split(",")) {
|
||||
String[] spl = block.replace("{", "").replace("}", "").split("\\|");
|
||||
World world = Bukkit.getWorld(spl[0]);
|
||||
if (world == null) {
|
||||
error("World " + spl[0] + " does not exist");
|
||||
continue;
|
||||
}
|
||||
Location loc;
|
||||
BlockData bData;
|
||||
try {
|
||||
loc =
|
||||
new Location(
|
||||
world,
|
||||
Integer.parseInt(spl[1]),
|
||||
Integer.parseInt(spl[2]),
|
||||
Integer.parseInt(spl[3]));
|
||||
|
||||
bData = Bukkit.createBlockData(spl[4]);
|
||||
} catch (NumberFormatException e) {
|
||||
error("Invalid block data: " + block);
|
||||
continue;
|
||||
}
|
||||
build.addBlock(loc, bData);
|
||||
}
|
||||
return build;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package me.unurled.sacredrealms.sr.data.gson;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map.Entry;
|
||||
import me.unurled.sacredrealms.sr.components.clientbuild.ClientBuild;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
public class ClientBuildSerializer implements JsonSerializer<ClientBuild> {
|
||||
|
||||
/**
|
||||
* Gson invokes this call-back method during serialization when it encounters a field of the
|
||||
* specified type.
|
||||
*
|
||||
* <p>In the implementation of this call-back method, you should consider invoking {@link
|
||||
* JsonSerializationContext#serialize(Object, Type)} method to create JsonElements for any
|
||||
* non-trivial field of the {@code src} object. However, you should never invoke it on the {@code
|
||||
* src} object itself since that will cause an infinite loop (Gson will call your call-back method
|
||||
* again).
|
||||
*
|
||||
* @param src the object that needs to be converted to Json.
|
||||
* @param typeOfSrc the actual type (fully genericized version) of the source object.
|
||||
* @param context
|
||||
* @return a JsonElement corresponding to the specified object.
|
||||
*/
|
||||
@Override
|
||||
public JsonElement serialize(ClientBuild src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(src.getName());
|
||||
s.append(";");
|
||||
for (Entry<Location, BlockData> entry : src.getBlocks().entrySet()) {
|
||||
s.append("{");
|
||||
s.append(entry.getKey().getWorld().getName());
|
||||
s.append("|");
|
||||
s.append(entry.getKey().getBlockX());
|
||||
s.append("|");
|
||||
s.append(entry.getKey().getBlockY());
|
||||
s.append("|");
|
||||
s.append(entry.getKey().getBlockZ());
|
||||
s.append("|");
|
||||
s.append(entry.getValue().getAsString());
|
||||
s.append("},");
|
||||
}
|
||||
return new JsonPrimitive(s.toString());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue