All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yungnickyoung.minecraft.yungsapi.json.ItemAdapter Maven / Gradle / Ivy

The newest version!
package com.yungnickyoung.minecraft.yungsapi.json;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.yungnickyoung.minecraft.yungsapi.YungsApiCommon;
import java.io.IOException;
import net.minecraft.class_1792;
import net.minecraft.class_1802;
import net.minecraft.class_2960;
import net.minecraft.class_7923;

/**
 * GSON TypeAdapter to serialize/deserialize {@link class_1792}.
 */
public class ItemAdapter extends TypeAdapter {
    public class_1792 read(JsonReader reader) throws IOException {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return null;
        }

        return resolveItem(reader.nextString());
    }

    public void write(JsonWriter writer, class_1792 item) throws IOException {
        if (item == null) {
            writer.nullValue();
            return;
        }

        String itemString = String.valueOf(item);
        writer.value(itemString);
    }

    public static class_1792 resolveItem(String itemString) {
        class_1792 item;
        try {
            item = class_7923.field_41178.method_10223(class_2960.method_60654(itemString));
        } catch (Exception e) {
            YungsApiCommon.LOGGER.error("JSON: Unable to read item '{}': {}", itemString, e.toString());
            YungsApiCommon.LOGGER.error("Using air instead...");
            return class_1802.field_8162;
        }

        return item;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy