Search in sources :

Example 1 with Resource

use of net.minecraft.server.packs.resources.Resource in project MinecraftForge by MinecraftForge.

the class LootModifierManager method apply.

@Override
protected void apply(Map<ResourceLocation, JsonElement> resourceList, ResourceManager resourceManagerIn, ProfilerFiller profilerIn) {
    Builder<ResourceLocation, IGlobalLootModifier> builder = ImmutableMap.builder();
    // old way (for reference)
    /*Map<IGlobalLootModifier, ResourceLocation> toLocation = new HashMap<IGlobalLootModifier, ResourceLocation>();
        resourceList.forEach((location, object) -> {
            try {
                IGlobalLootModifier modifier = deserializeModifier(location, object);
                builder.put(location, modifier);
                toLocation.put(modifier, location);
            } catch (Exception exception) {
                LOGGER.error("Couldn't parse loot modifier {}", location, exception);
            }
        });
        builder.orderEntriesByValue((x,y) -> {
            return toLocation.get(x).compareTo(toLocation.get(y));
        });*/
    // new way
    ArrayList<ResourceLocation> finalLocations = new ArrayList<ResourceLocation>();
    ResourceLocation resourcelocation = new ResourceLocation("forge", "loot_modifiers/global_loot_modifiers.json");
    try {
        // read in all data files from forge:loot_modifiers/global_loot_modifiers in order to do layering
        for (Resource iresource : resourceManagerIn.getResources(resourcelocation)) {
            try (InputStream inputstream = iresource.getInputStream();
                Reader reader = new BufferedReader(new InputStreamReader(inputstream, StandardCharsets.UTF_8))) {
                JsonObject jsonobject = GsonHelper.fromJson(GSON_INSTANCE, reader, JsonObject.class);
                boolean replace = jsonobject.get("replace").getAsBoolean();
                if (replace)
                    finalLocations.clear();
                JsonArray entryList = jsonobject.get("entries").getAsJsonArray();
                for (JsonElement entry : entryList) {
                    String loc = entry.getAsString();
                    ResourceLocation res = new ResourceLocation(loc);
                    if (finalLocations.contains(res))
                        finalLocations.remove(res);
                    finalLocations.add(res);
                }
            } catch (RuntimeException | IOException ioexception) {
                LOGGER.error("Couldn't read global loot modifier list {} in data pack {}", resourcelocation, iresource.getSourceName(), ioexception);
            } finally {
                IOUtils.closeQuietly((Closeable) iresource);
            }
        }
    } catch (IOException ioexception1) {
        LOGGER.error("Couldn't read global loot modifier list from {}", resourcelocation, ioexception1);
    }
    // use layered config to fetch modifier data files (modifiers missing from config are disabled)
    finalLocations.forEach(location -> {
        try {
            IGlobalLootModifier modifier = deserializeModifier(location, resourceList.get(location));
            if (modifier != null)
                builder.put(location, modifier);
        } catch (Exception exception) {
            LOGGER.error("Couldn't parse loot modifier {}", location, exception);
        }
    });
    ImmutableMap<ResourceLocation, IGlobalLootModifier> immutablemap = builder.build();
    this.registeredLootModifiers = immutablemap;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(net.minecraft.server.packs.resources.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) BufferedReader(java.io.BufferedReader)

Example 2 with Resource

use of net.minecraft.server.packs.resources.Resource in project MinecraftForge by MinecraftForge.

the class MinecraftForgeClient method getImageLayer.

@Nonnull
public static NativeImage getImageLayer(ResourceLocation resourceLocation, ResourceManager resourceManager) throws IOException {
    Supplier<NativeImage> supplier = bufferedImageSuppliers.get(resourceLocation);
    if (supplier != null)
        return supplier.get();
    Resource iresource1 = resourceManager.getResource(resourceLocation);
    return NativeImage.read(iresource1.getInputStream());
}
Also used : NativeImage(com.mojang.blaze3d.platform.NativeImage) Resource(net.minecraft.server.packs.resources.Resource) Nonnull(javax.annotation.Nonnull)

Example 3 with Resource

use of net.minecraft.server.packs.resources.Resource in project MinecraftForge by MinecraftForge.

the class DataGeneratorTest method testModelResults.

private static <T extends ModelBuilder<T>> List<String> testModelResults(Map<ResourceLocation, T> models, ExistingFileHelper existingFileHelper, Set<ResourceLocation> toIgnore) {
    List<String> ret = new ArrayList<>();
    models.forEach((loc, model) -> {
        if (toIgnore.contains(loc))
            return;
        JsonObject generated = model.toJson();
        if (generated.has("parent")) {
            generated.addProperty("parent", toVanillaModel(generated.get("parent").getAsString()));
        }
        try {
            Resource vanillaResource = existingFileHelper.getResource(new ResourceLocation(loc.getPath()), PackType.CLIENT_RESOURCES, ".json", "models");
            JsonObject existing = GSON.fromJson(new InputStreamReader(vanillaResource.getInputStream()), JsonObject.class);
            JsonElement generatedDisplay = generated.remove("display");
            JsonElement vanillaDisplay = existing.remove("display");
            if (generatedDisplay == null && vanillaDisplay != null) {
                ret.add("Model " + loc + " is missing transforms");
                return;
            } else if (generatedDisplay != null && vanillaDisplay == null) {
                ret.add("Model " + loc + " has transforms when vanilla equivalent does not");
                return;
            } else if (generatedDisplay != null) {
                // Both must be non-null
                ItemTransforms generatedTransforms = GSON.fromJson(generatedDisplay, ItemTransforms.class);
                ItemTransforms vanillaTransforms = GSON.fromJson(vanillaDisplay, ItemTransforms.class);
                for (Perspective type : Perspective.values()) {
                    if (!generatedTransforms.getTransform(type.vanillaType).equals(vanillaTransforms.getTransform(type.vanillaType))) {
                        ret.add("Model " + loc + " has transforms that differ from vanilla equivalent for perspective " + type.name());
                        return;
                    }
                }
            }
            JsonElement generatedTextures = generated.remove("textures");
            JsonElement vanillaTextures = existing.remove("textures");
            if (generatedTextures == null && vanillaTextures != null) {
                ret.add("Model " + loc + " is missing textures");
            } else if (generatedTextures != null && vanillaTextures == null) {
                ret.add("Model " + loc + " has textures when vanilla equivalent does not");
            } else if (generatedTextures != null) {
                // Both must be non-null
                for (Map.Entry<String, JsonElement> e : generatedTextures.getAsJsonObject().entrySet()) {
                    String vanillaTexture = vanillaTextures.getAsJsonObject().get(e.getKey()).getAsString();
                    if (!e.getValue().getAsString().equals(vanillaTexture)) {
                        ret.add("Texture for variable '" + e.getKey() + "' for model " + loc + " does not match vanilla equivalent");
                    }
                }
                if (generatedTextures.getAsJsonObject().size() != vanillaTextures.getAsJsonObject().size()) {
                    ret.add("Model " + loc + " is missing textures from vanilla equivalent");
                }
            }
            if (!existing.equals(generated)) {
                ret.add("Model " + loc + " does not match vanilla equivalent");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    return ret;
}
Also used : InputStreamReader(org.jline.utils.InputStreamReader) ArrayList(java.util.ArrayList) Resource(net.minecraft.server.packs.resources.Resource) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ItemTransforms(net.minecraft.client.renderer.block.model.ItemTransforms) Perspective(net.minecraftforge.client.model.generators.ModelBuilder.Perspective) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) Map(java.util.Map)

Aggregations

Resource (net.minecraft.server.packs.resources.Resource)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 JsonArray (com.google.gson.JsonArray)1 NativeImage (com.mojang.blaze3d.platform.NativeImage)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 ItemTransforms (net.minecraft.client.renderer.block.model.ItemTransforms)1 Perspective (net.minecraftforge.client.model.generators.ModelBuilder.Perspective)1 InputStreamReader (org.jline.utils.InputStreamReader)1