use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.
the class ProgWidgetConfig method readFromJson.
@Override
protected void readFromJson(JsonObject json) {
JsonArray array = json.get("blacklist").getAsJsonArray();
blacklistedPieces.clear();
for (JsonElement element : array) {
blacklistedPieces.add(element.getAsString());
}
}
use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.
the class JsonToNBTConverter method getTag.
private NBTTagCompound getTag(JsonObject object) {
NBTTagCompound nbt = new NBTTagCompound();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
JsonObject keyObject = entry.getValue().getAsJsonObject();
int type = keyObject.get("type").getAsInt();
JsonElement element = keyObject.get("value");
switch(type) {
case 1:
nbt.setByte(entry.getKey(), (byte) element.getAsDouble());
break;
case 2:
nbt.setShort(entry.getKey(), (short) element.getAsDouble());
case 3:
nbt.setInteger(entry.getKey(), (int) element.getAsDouble());
break;
case 4:
nbt.setLong(entry.getKey(), (long) element.getAsDouble());
break;
case 5:
nbt.setFloat(entry.getKey(), (float) element.getAsDouble());
break;
case 6:
nbt.setDouble(entry.getKey(), element.getAsDouble());
break;
// break;
case 8:
nbt.setString(entry.getKey(), element.getAsString());
break;
case 9:
JsonArray array = element.getAsJsonArray();
NBTTagList tagList = new NBTTagList();
for (JsonElement e : array) {
tagList.appendTag(getTag(e.getAsJsonObject()));
}
nbt.setTag(entry.getKey(), tagList);
break;
case 10:
nbt.setTag(entry.getKey(), getTag(element.getAsJsonObject()));
break;
case 11:
array = element.getAsJsonArray();
int[] intArray = new int[array.size()];
for (int i = 0; i < array.size(); i++) {
intArray[i] = array.get(i).getAsInt();
}
nbt.setTag(entry.getKey(), new NBTTagIntArray(intArray));
break;
default:
throw new IllegalArgumentException("NBT type no " + type + " is not supported by the Json to NBT converter!");
}
}
return nbt;
}
use of com.google.gson.JsonArray in project PneumaticCraft by MineMaarten.
the class NBTToJsonConverter method getObject.
private JsonObject getObject(NBTTagCompound tag) {
Set<String> keys = tag.func_150296_c();
JsonObject jsonRoot = new JsonObject();
for (String key : keys) {
JsonObject keyObject = new JsonObject();
jsonRoot.add(key, keyObject);
NBTBase nbt = tag.getTag(key);
keyObject.addProperty("type", nbt.getId());
if (nbt instanceof NBTTagCompound) {
keyObject.add("value", getObject((NBTTagCompound) nbt));
} else if (nbt instanceof NBTPrimitive) {
keyObject.addProperty("value", ((NBTPrimitive) nbt).func_150286_g());
} else if (nbt instanceof NBTTagString) {
keyObject.addProperty("value", ((NBTTagString) nbt).func_150285_a_());
} else if (nbt instanceof NBTTagList) {
JsonArray array = new JsonArray();
NBTTagList tagList = (NBTTagList) nbt;
for (int i = 0; i < tagList.tagCount(); i++) {
array.add(getObject(tagList.getCompoundTagAt(i)));
}
keyObject.add("value", array);
} else if (nbt instanceof NBTTagIntArray) {
JsonArray array = new JsonArray();
NBTTagIntArray intArray = (NBTTagIntArray) nbt;
for (int i : intArray.func_150302_c()) {
array.add(new JsonPrimitive(i));
}
keyObject.add("value", array);
} else {
throw new IllegalArgumentException("NBT to JSON converter doesn't support the nbt tag: " + NBTBase.NBTTypes[nbt.getId()] + ", tag: " + nbt);
}
}
return jsonRoot;
}
use of com.google.gson.JsonArray in project MinecraftForge by MinecraftForge.
the class MetadataCollection method from.
public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
if (inputStream == null) {
return new MetadataCollection();
}
InputStreamReader reader = new InputStreamReader(inputStream);
try {
MetadataCollection collection;
Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
JsonParser parser = new JsonParser();
JsonElement rootElement = parser.parse(reader);
if (rootElement.isJsonArray()) {
collection = new MetadataCollection();
JsonArray jsonList = rootElement.getAsJsonArray();
collection.modList = new ModMetadata[jsonList.size()];
int i = 0;
for (JsonElement mod : jsonList) {
collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
}
} else {
collection = gson.fromJson(rootElement, MetadataCollection.class);
}
collection.parseModMetadataList();
return collection;
} catch (JsonParseException e) {
FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
return new MetadataCollection();
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of com.google.gson.JsonArray in project MinecraftForge by MinecraftForge.
the class FMLNetworkHandler method enhanceStatusQuery.
public static void enhanceStatusQuery(JsonObject jsonobject) {
JsonObject fmlData = new JsonObject();
fmlData.addProperty("type", "FML");
JsonArray modList = new JsonArray();
for (ModContainer mc : Loader.instance().getActiveModList()) {
JsonObject modData = new JsonObject();
modData.addProperty("modid", mc.getModId());
modData.addProperty("version", mc.getVersion());
modList.add(modData);
}
fmlData.add("modList", modList);
jsonobject.add("modinfo", fmlData);
}
Aggregations