use of com.massivecraft.massivecore.xlib.gson.JsonObject in project MassiveCore by MassiveCraft.
the class AdapterBannerPatterns method deserialize.
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
// Default serialization is fine.
// We do however provide a smarter deserialiser.
// This is for the sole purpose of upgrading from the old deprecated database format.
//
// In the old version (around version 2.8.10) we made use of a list with primitives.
// id, color, id, color...
// They were just coming in that order and were not wrapped in some kind of entry.
//
// The data types are the same in the old and new version.
// String, Number, String, Number...
//
// In the new version we do however wrap them in the DataBannerPattern.
@Override
public MassiveListDef<DataBannerPattern> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// Create
MassiveListDef<DataBannerPattern> ret = new MassiveListDef<>();
// Null (Def implementation is never null)
if (json == null)
return ret;
if (json.equals(JsonNull.INSTANCE))
return ret;
// It is an array in both old and new version
JsonArray array = json.getAsJsonArray();
// Empty?
if (array.size() == 0)
return ret;
// First element indicates version
JsonElement first = array.get(0);
if (first instanceof JsonObject) {
// New
for (JsonElement element : array) {
DataBannerPattern dataBannerPattern = context.deserialize(element, DataBannerPattern.class);
ret.add(dataBannerPattern);
}
} else {
// Old aka Upgrade Mode
Iterator<JsonElement> iterator = array.iterator();
while (iterator.hasNext()) {
DataBannerPattern dataBannerPattern = new DataBannerPattern();
JsonElement idElement = iterator.next();
String id = idElement.getAsString();
dataBannerPattern.setId(id);
JsonElement colorElement = iterator.next();
Integer color = colorElement.getAsInt();
dataBannerPattern.setColor(color);
ret.add(dataBannerPattern);
}
}
return ret;
}
use of com.massivecraft.massivecore.xlib.gson.JsonObject in project MassiveCore by MassiveCraft.
the class AdapterInventory method toJson.
// -------------------------------------------- //
// IMPLEMENTATION
// -------------------------------------------- //
public static JsonElement toJson(Inventory src) {
// The return value is this object:
JsonObject jsonInventory = new JsonObject();
// These variables are used in loops and repetitive logic.
ItemStack itemStack = null;
JsonElement jsonItemStack = null;
String index = null;
// Every inventory has a content part.
ItemStack[] itemStacks = src.getContents();
if (src instanceof PlayerInventory) {
// Add the size "player"
jsonInventory.addProperty(SIZE, PLAYER);
// Cast to PlayerInventory
PlayerInventory psrc = (PlayerInventory) src;
// Helmet
itemStack = psrc.getHelmet();
if (itemStack != null) {
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
jsonInventory.add(HELMET, jsonItemStack);
}
// Chestplate
itemStack = psrc.getChestplate();
if (itemStack != null) {
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
jsonInventory.add(CHESTPLATE, jsonItemStack);
}
// Leggings
itemStack = psrc.getLeggings();
if (itemStack != null) {
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
jsonInventory.add(LEGGINGS, jsonItemStack);
}
// Boots
itemStack = psrc.getBoots();
if (itemStack != null) {
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
jsonInventory.add(BOOTS, jsonItemStack);
}
// Shield (Minecraft 1.9)
itemStack = null;
if (INDEX_PLAYER_SHIELD < itemStacks.length)
itemStack = itemStacks[INDEX_PLAYER_SHIELD];
if (itemStack != null) {
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
jsonInventory.add(SHIELD, jsonItemStack);
}
// Storage Range (Minecraft 1.9)
itemStacks = range(itemStacks, INDEX_PLAYER_STORAGE_FROM, INDEX_PLAYER_STORAGE_TO);
} else {
// Add the size *length*
jsonInventory.addProperty(SIZE, itemStacks.length);
}
// Add the content at the end since we like to have it at the bottom of return json.
for (Entry<Integer, DataItemStack> entry : DataItemStack.fromBukkitContents(itemStacks).entrySet()) {
index = String.valueOf(entry.getKey());
jsonItemStack = MassiveCore.gson.toJsonTree(entry.getValue());
jsonInventory.add(index, jsonItemStack);
}
// Add version
int version = MigratorUtil.getTargetVersion(DataItemStack.class);
jsonInventory.addProperty(MigratorUtil.VERSION_FIELD_NAME, version);
return jsonInventory;
}
use of com.massivecraft.massivecore.xlib.gson.JsonObject in project MassiveCore by MassiveCraft.
the class AdapterInventory method fromJson.
public static Inventory fromJson(JsonElement json) {
// If must be an object!
if (!json.isJsonObject())
return null;
JsonObject jsonInventory = json.getAsJsonObject();
// The return value
Inventory ret = null;
// These variables are used in loops and repetitive logic.
ItemStack itemStack = null;
JsonElement jsonItemStack = null;
// There must be a size entry!
if (!jsonInventory.has(SIZE))
return null;
JsonPrimitive jsonSize = jsonInventory.get(SIZE).getAsJsonPrimitive();
int size = 0;
// What size/type is it?
if (jsonSize.isString() && jsonSize.getAsString().equals(PLAYER)) {
// We use 36 here since it's the size of the player inventory (without armor)
size = SIZE_PLAYER_STORAGE;
// This is a PlayerInventory
ret = MixinInventory.get().createPlayerInventory();
PlayerInventory pret = (PlayerInventory) ret;
// Helmet
if (jsonInventory.has(HELMET)) {
itemStack = getItemStack(jsonInventory, HELMET);
pret.setHelmet(itemStack);
}
// Chestplate
if (jsonInventory.has(CHESTPLATE)) {
itemStack = getItemStack(jsonInventory, CHESTPLATE);
pret.setChestplate(itemStack);
}
// Leggings
if (jsonInventory.has(LEGGINGS)) {
jsonItemStack = jsonInventory.get(LEGGINGS);
itemStack = MassiveCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setLeggings(itemStack);
}
// Boots
if (jsonInventory.has(BOOTS)) {
itemStack = getItemStack(jsonInventory, BOOTS);
pret.setBoots(itemStack);
}
// Shield (Minecraft 1.9)
if (jsonInventory.has(SHIELD) && INDEX_PLAYER_SHIELD < pret.getSize()) {
itemStack = getItemStack(jsonInventory, SHIELD);
pret.setItem(INDEX_PLAYER_SHIELD, itemStack);
}
} else {
// A custom size were specified
size = jsonSize.getAsInt();
// This is a "Custom" Inventory (content only).
ret = MixinInventory.get().createInventory(null, size, "");
}
// Now process content
for (int i = 0; i < size; i++) {
String stackIdx = String.valueOf(i);
itemStack = getItemStack(jsonInventory, stackIdx);
if (itemStack == null)
continue;
ret.setItem(i, itemStack);
}
return ret;
}
use of com.massivecraft.massivecore.xlib.gson.JsonObject in project MassiveCore by MassiveCraft.
the class AdapterPolymorphic method serialize.
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return JsonNull.INSTANCE;
}
JsonObject ret = new JsonObject();
String type = src.getClass().getCanonicalName();
ret.addProperty(TYPE, type);
JsonElement value = context.serialize(src);
ret.add(VALUE, value);
return ret;
}
use of com.massivecraft.massivecore.xlib.gson.JsonObject in project MassiveCore by MassiveCraft.
the class CmdMassiveCoreStoreCopydb method perform.
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void perform() throws MassiveException {
// Args
final String fromAlias = this.readArg();
final Db fromDb = MStore.getDb(fromAlias);
if (fromDb == null) {
msg("<b>could not get the from-database.");
return;
}
final String toAlias = this.readArg();
final Db toDb = MStore.getDb(toAlias);
if (toDb == null) {
msg("<b>could not get the to-database.");
return;
}
// Prepare
Set<String> collnames = fromDb.getCollnames();
// Statistics
int countCollCurrent = 0;
int countCollTotal = collnames.size();
// Do it!
long before = System.currentTimeMillis();
msg("<i>Now copying database with <h>%d <i>collections.", countCollTotal);
for (String collname : fromDb.getCollnames()) {
countCollCurrent++;
final Coll fromColl = new Coll(collname, Entity.class, fromDb, MassiveCore.get());
final Coll toColl = new Coll(collname, Entity.class, toDb, MassiveCore.get());
Collection<String> ids = fromDb.getIds(fromColl);
msg("<i>Now copying collection <h>%d/%d %s <i>with <h>%d <i>documents.", countCollCurrent, countCollTotal, collname, ids.size());
// Do a load check to verify we have access to this folder.
if (ids.size() > 0 && fromDb.load(fromColl, ids.iterator().next()) == null) {
msg("<b>Skipping <h>%s <b>since could not load data.", collname);
continue;
}
for (String id : ids) {
Entry<JsonObject, Long> data = fromDb.load(fromColl, id);
toDb.save(toColl, id, data.getKey());
}
}
long after = System.currentTimeMillis();
long duration = after - before;
msg("<g>The copy is now complete. <i>It took <h>%dms<i>.", duration);
}
Aggregations