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;
}
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());
}
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;
}
Aggregations