Search in sources :

Example 1 with JsonToNBT

use of net.minecraft.nbt.JsonToNBT in project MCMOD-Industria by M-Marvin.

the class JigsawFileManager method loadListNBT.

@SuppressWarnings("deprecation")
private static ListNBT loadListNBT(ServerWorld world, ResourceLocation location) {
    IResourceManager resourceManager = getResourceManager(world);
    ResourceLocation resourcePath = new ResourceLocation(location.getNamespace(), "structures/" + location.getPath() + ".mcmeta");
    try (IResource resource = resourceManager.getResource(resourcePath)) {
        InputStream inputStream = resource.getInputStream();
        String jsonString = IOUtils.toString(inputStream);
        CompoundNBT fileNBT = new JsonToNBT(new StringReader(jsonString)).readStruct();
        ListNBT list = fileNBT.getList("structures", 10);
        return list;
    } catch (FileNotFoundException e) {
        return null;
    } catch (Throwable throwable) {
        Industria.LOGGER.error("Couldn't load Jigsaw-Structure-List {}: {}", location, throwable.toString());
        throwable.printStackTrace();
        return null;
    }
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) InputStream(java.io.InputStream) JsonToNBT(net.minecraft.nbt.JsonToNBT) ResourceLocation(net.minecraft.util.ResourceLocation) StringReader(com.mojang.brigadier.StringReader) FileNotFoundException(java.io.FileNotFoundException) IResourceManager(net.minecraft.resources.IResourceManager) IResource(net.minecraft.resources.IResource)

Example 2 with JsonToNBT

use of net.minecraft.nbt.JsonToNBT in project BudschieMorphMod by Budschie.

the class MorphNBTHandler method apply.

@Override
protected void apply(Map<ResourceLocation, JsonElement> objectIn, IResourceManager resourceManagerIn, IProfiler profilerIn) {
    HashMap<EntityType<?>, IMorphNBTHandler> nbtDataHandlers = new HashMap<>();
    MorphManagerHandlers.FALLBACK.setDataHandlers(nbtDataHandlers);
    objectIn.forEach((resourceLocation, json) -> {
        try {
            JsonObject jsonObject = json.getAsJsonObject();
            String entityTypeString = jsonObject.get("entity_type").getAsString();
            // Load in the entity type
            final EntityType<?> entityType = ForgeRegistries.ENTITIES.getValue(new ResourceLocation(entityTypeString));
            if (entityType == null)
                LOGGER.warn("The entity ", entityTypeString, " doesn't exist. Please make sure to only load this when the mod for the entity is present. You can do this by putting this JSON file in \"data/<modname>/morph_nbt\".");
            // Load in the tracked nbt keys as a NBTPath array
            JsonElement tracked = jsonObject.get("tracked_nbt_keys");
            final NBTPath[] trackedNbtKeys = new NBTPath[tracked == null ? 1 : tracked.getAsJsonArray().size() + 1];
            if (tracked != null) {
                for (int i = 0; i < tracked.getAsJsonArray().size(); i++) trackedNbtKeys[i] = NBTPath.valueOf(tracked.getAsJsonArray().get(i).getAsString());
            }
            trackedNbtKeys[trackedNbtKeys.length - 1] = new NBTPath("CustomName");
            JsonElement defaultNBTObject = jsonObject.get("default_nbt");
            final CompoundNBT defaultNBT = defaultNBTObject == null ? new CompoundNBT() : new JsonToNBT(new StringReader(defaultNBTObject.getAsString())).readStruct();
            // Build SpecialDataHandler
            JsonMorphNBTHandler nbtHandler = new JsonMorphNBTHandler(defaultNBT, trackedNbtKeys);
            nbtDataHandlers.put(entityType, nbtHandler);
        } catch (CommandSyntaxException e) {
            e.printStackTrace();
        }
    });
    MorphNBTHandlersLoadedEvent event = new MorphNBTHandlersLoadedEvent(nbtDataHandlers);
    // Fire an event when we are finished...
    MinecraftForge.EVENT_BUS.post(event);
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) IMorphNBTHandler(de.budschie.bmorph.morph.fallback.IMorphNBTHandler) EntityType(net.minecraft.entity.EntityType) JsonElement(com.google.gson.JsonElement) JsonToNBT(net.minecraft.nbt.JsonToNBT) ResourceLocation(net.minecraft.util.ResourceLocation) StringReader(com.mojang.brigadier.StringReader) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Aggregations

StringReader (com.mojang.brigadier.StringReader)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 JsonToNBT (net.minecraft.nbt.JsonToNBT)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 IMorphNBTHandler (de.budschie.bmorph.morph.fallback.IMorphNBTHandler)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 EntityType (net.minecraft.entity.EntityType)1 ListNBT (net.minecraft.nbt.ListNBT)1 IResource (net.minecraft.resources.IResource)1 IResourceManager (net.minecraft.resources.IResourceManager)1