use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.
the class Coll method logModification.
protected void logModification(E entity, Modification modification) {
JsonObject lastRaw = entity.getLastRaw();
if (lastRaw == null) {
List<String> messages = new MassiveList<>();
messages.add(Txt.parse("<pink>%s", this.getDebugName()));
messages.add(Txt.parse("<aqua>%s", entity.getId()));
messages.add(Txt.parse("<blue>%s", modification));
String message = Txt.implode(messages, Txt.parse("<silver> | "));
message = Txt.parse("<b>[lastRaw null] %s", message);
MassiveCore.get().log(message);
return;
}
JsonObject currentRaw = this.getGson().toJsonTree(entity).getAsJsonObject();
List<String> changes = new MassiveList<>();
// Check removal and modification.
for (Entry<String, JsonElement> entry : lastRaw.entrySet()) {
String name = entry.getKey();
JsonElement currentValue = currentRaw.get(name);
if (currentValue == null) {
changes.add(Txt.parse("<b>%s", name));
continue;
}
JsonElement lastValue = entry.getValue();
if (MStore.equal(currentValue, lastValue))
continue;
changes.add(Txt.parse("<i>%s", name));
}
// Check for addition
for (Entry<String, JsonElement> entry : currentRaw.entrySet()) {
String name = entry.getKey();
if (lastRaw.has(name))
continue;
changes.add(Txt.parse("<g>%s", name));
}
// Log
if (changes.isEmpty())
return;
changes.add(0, Txt.parse("<pink>%s", this.getDebugName()));
changes.add(1, Txt.parse("<aqua>%s", entity.getId()));
String change = Txt.implode(changes, Txt.parse("<silver> | "));
String message = Txt.parse("<b>[Unreported Modification] %s", change);
MassiveCore.get().log(message);
}
use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.
the class MigratorDataItemStack001StringId method migrateInner.
// -------------------------------------------- //
// CONVERSION
// -------------------------------------------- //
@Override
public void migrateInner(JsonObject json) {
// Get the id
JsonElement id = json.get("id");
if (id == null || id.isJsonNull())
return;
// The id is either a number or string, always a JsonPrimitive
JsonPrimitive primitive = id.getAsJsonPrimitive();
// Only convert if number
if (!primitive.isNumber())
return;
int typeId = primitive.getAsInt();
String name = id2name.get(typeId);
if (name == null)
throw new RuntimeException(String.valueOf(typeId));
JsonElement newValue = new JsonPrimitive(name);
json.add("id", newValue);
}
use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.
the class MigratorFieldConvert method migrate.
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void migrate(JsonObject entity) {
JsonElement value = entity.get(this.getFieldName());
if (value == null)
value = JsonNull.INSTANCE;
value = getElement(migrateInner(value));
entity.add(this.getFieldName(), value);
}
use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.
the class MigratorFieldRename method migrate.
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void migrate(JsonObject entity) {
String from = this.getFrom();
String to = this.getTo();
JsonElement element = entity.remove(from);
if (element != null)
entity.add(to, element);
}
use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.
the class MigratorUtil method migrate.
// -------------------------------------------- //
// MIGRATE
// -------------------------------------------- //
public static boolean migrate(Type realType, JsonElement jsonElement) {
if (jsonElement == null)
throw new NullPointerException("element");
if (jsonElement.isJsonNull())
return false;
if (jsonElement.isJsonPrimitive())
return false;
Type jsonType = getJsonRepresentation(realType);
if (jsonElement.isJsonObject()) {
if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType))) {
ParameterizedType parameterizedType = (ParameterizedType) jsonType;
Type keyType = parameterizedType.getActualTypeArguments()[0];
Type valueType = parameterizedType.getActualTypeArguments()[1];
JsonObject object = jsonElement.getAsJsonObject();
boolean migrated = false;
for (Entry<String, JsonElement> entry : object.entrySet()) {
migrated = migrate(valueType, entry.getValue()) | migrated;
}
return migrated;
}
boolean migrated = false;
JsonObject object = jsonElement.getAsJsonObject();
Type classType = jsonType != null ? jsonType : realType;
migrated = migrateClass(classType, object) | migrated;
if (jsonType != null)
migrated = migrateFields(jsonType, object) | migrated;
return migrated;
}
if (jsonElement.isJsonArray()) {
Class<?> clazz = getClassType(jsonType);
Type elementType = null;
if (clazz.isArray()) {
elementType = clazz.getComponentType();
} else if (Collection.class.isAssignableFrom(clazz)) {
ParameterizedType parameterizedType = (ParameterizedType) jsonType;
elementType = parameterizedType.getActualTypeArguments()[0];
}
boolean migrated = false;
for (JsonElement element1 : jsonElement.getAsJsonArray()) {
migrated = migrate(elementType, element1) | migrated;
}
return migrated;
}
throw new RuntimeException();
}
Aggregations