Search in sources :

Example 1 with IMorphNBTHandler

use of de.budschie.bmorph.morph.fallback.IMorphNBTHandler 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

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 StringReader (com.mojang.brigadier.StringReader)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 IMorphNBTHandler (de.budschie.bmorph.morph.fallback.IMorphNBTHandler)1 HashMap (java.util.HashMap)1 EntityType (net.minecraft.entity.EntityType)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 JsonToNBT (net.minecraft.nbt.JsonToNBT)1 ResourceLocation (net.minecraft.util.ResourceLocation)1