use of net.minecraft.client.resources.IResource in project Almura by AlmuraDev.
the class OBJModelParser method parseMaterialLibrary.
private MaterialLibrary parseMaterialLibrary(final ResourceLocation source) throws Exception {
final MaterialLibrary.Builder mtllibBuilder = MaterialLibrary.builder();
final IResource resource = this.resourceManager.getResource(source);
try (final InputStream stream = resource.getInputStream()) {
MaterialDefinition.Builder mtlBuilder = null;
String currentMaterial = null;
final List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
for (int i = 0; i < lines.size(); i++) {
final String line = lines.get(i);
// Skip comments and newlines
if (line.startsWith(OBJModelConfig.COMMENT) || line.isEmpty()) {
continue;
}
final String[] combinedLineContents = line.split(" ");
final String lineHeader = combinedLineContents[0];
final String[] lineContents = Arrays.copyOfRange(combinedLineContents, 1, combinedLineContents.length);
switch(lineHeader) {
case OBJModelConfig.Material.NEW_MATERIAL:
if (mtlBuilder != null) {
mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
}
mtlBuilder = MaterialDefinition.builder();
currentMaterial = lineContents[0];
break;
case OBJModelConfig.Material.DIFFUSE:
if (mtlBuilder == null) {
throw new MalformedMaterialLibraryException("Material attribute cannot occur before defining new material " + "definition! Source -> Line: " + (i + 1) + ", Content: " + Arrays.toString(combinedLineContents));
}
@Nullable final ResourceLocation parentLocation = getParent(this.source);
String parentPath = null;
if (parentLocation != null) {
// Making some assumptions here...carry on now
final int lastSlashIndex = parentLocation.getResourcePath().lastIndexOf("/");
if (lastSlashIndex != -1) {
parentPath = parentLocation.getResourcePath().substring(lastSlashIndex + 1, parentLocation.getResourcePath().length());
}
}
mtlBuilder.diffuseTexture(ResourceLocations.buildResourceLocationPath(lineContents[0], parentPath));
break;
default:
}
}
if (mtlBuilder != null) {
mtllibBuilder.materialDefinition(mtlBuilder.build(currentMaterial));
}
}
return mtllibBuilder.build(source, getFileName(source).split("\\.")[0]);
}
use of net.minecraft.client.resources.IResource in project MinecraftForge by MinecraftForge.
the class ModelBlockAnimation method loadVanillaAnimation.
/**
* Load armature associated with a vanilla model.
*/
public static ModelBlockAnimation loadVanillaAnimation(IResourceManager manager, ResourceLocation armatureLocation) {
try {
IResource resource = null;
try {
resource = manager.getResource(armatureLocation);
} catch (FileNotFoundException e) {
// this is normal. FIXME: error reporting?
return defaultModelBlockAnimation;
}
ModelBlockAnimation mba = mbaGson.fromJson(new InputStreamReader(resource.getInputStream(), "UTF-8"), ModelBlockAnimation.class);
//String json = mbaGson.toJson(mba);
return mba;
} catch (IOException e) {
FMLLog.log(Level.ERROR, e, "Exception loading vanilla model animation %s, skipping", armatureLocation);
return defaultModelBlockAnimation;
} catch (JsonParseException e) {
FMLLog.log(Level.ERROR, e, "Exception loading vanilla model animation %s, skipping", armatureLocation);
return defaultModelBlockAnimation;
}
}
use of net.minecraft.client.resources.IResource in project ArsMagica2 by Mithion.
the class ArcaneCompendium method getPackagedCompendium.
private InputStream getPackagedCompendium(Language lang) {
ResourceLocation rLoc = new ResourceLocation("arsmagica2", String.format("docs/ArcaneCompendium_%s.xml", lang.getLanguageCode()));
IResource resource = null;
try {
resource = Minecraft.getMinecraft().getResourceManager().getResource(rLoc);
} catch (IOException e) {
} finally {
if (resource == null) {
LogHelper.info("Unable to find localized compendium. Defaulting to en_US");
rLoc = new ResourceLocation("arsmagica2", "docs/ArcaneCompendium_en_US.xml");
try {
resource = Minecraft.getMinecraft().getResourceManager().getResource(rLoc);
} catch (IOException e) {
}
}
}
if (resource != null)
return resource.getInputStream();
throw new MissingResourceException("No packaged version of the compendium was found. You may have a corrupted download.", "compendium", "Arcane Compendium");
}
use of net.minecraft.client.resources.IResource in project ImmersiveEngineering by BluSunrize.
the class TileRenderAutoWorkbench method getBlueprintDrawable.
public static BlueprintLines getBlueprintDrawable(ItemStack stack, World world) {
if (stack == null)
return null;
EntityPlayer player = ClientUtils.mc().thePlayer;
ArrayList<BufferedImage> images = new ArrayList<>();
try {
IBakedModel ibakedmodel = ClientUtils.mc().getRenderItem().getItemModelWithOverrides(stack, world, player);
HashSet<String> textures = new HashSet();
Collection<BakedQuad> quads = ibakedmodel.getQuads(null, null, 0);
for (BakedQuad quad : quads) if (quad != null && quad.getSprite() != null)
textures.add(quad.getSprite().getIconName());
for (String s : textures) {
ResourceLocation rl = new ResourceLocation(s);
rl = new ResourceLocation(rl.getResourceDomain(), String.format("%s/%s%s", "textures", rl.getResourcePath(), ".png"));
IResource resource = ClientUtils.mc().getResourceManager().getResource(rl);
BufferedImage bufferedImage = TextureUtil.readBufferedImage(resource.getInputStream());
if (bufferedImage != null)
images.add(bufferedImage);
}
} catch (Exception e) {
}
if (images.isEmpty())
return null;
ArrayList<Pair<TexturePoint, TexturePoint>> lines = new ArrayList();
HashSet testSet = new HashSet();
HashMultimap<Integer, TexturePoint> area = HashMultimap.create();
int wMax = 0;
for (BufferedImage bufferedImage : images) {
Set<Pair<TexturePoint, TexturePoint>> temp_lines = new HashSet<>();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
if (h > w)
h = w;
if (w > wMax)
wMax = w;
for (int hh = 0; hh < h; hh++) for (int ww = 0; ww < w; ww++) {
int argb = bufferedImage.getRGB(ww, hh);
float r = (argb >> 16 & 255) / 255f;
float g = (argb >> 8 & 255) / 255f;
float b = (argb & 255) / 255f;
float intesity = (r + b + g) / 3f;
int alpha = (argb >> 24) & 255;
if (alpha > 0) {
boolean added = false;
//Check colour sets for similar colour to shade it later
TexturePoint tp = new TexturePoint(ww, hh, w);
if (!testSet.contains(tp)) {
for (Integer key : area.keySet()) {
for (Point p : area.get(key)) {
int pColour = bufferedImage.getRGB(p.x, p.y);
float dR = (r - (pColour >> 16 & 255) / 255f);
float dG = (g - (pColour >> 8 & 255) / 255f);
float dB = (b - (pColour & 255) / 255f);
double delta = Math.sqrt(dR * dR + dG * dG + dB * dB);
if (delta < .25) {
area.put(key, tp);
added = true;
break;
}
}
if (added)
break;
}
if (!added)
area.put(argb, tp);
testSet.add(tp);
}
//Compare to direct neighbour
for (int i = 0; i < 4; i++) {
int xx = (i == 0 ? -1 : i == 1 ? 1 : 0);
int yy = (i == 2 ? -1 : i == 3 ? 1 : 0);
int u = ww + xx;
int v = hh + yy;
int neighbour = 0;
float delta = 1;
boolean notTransparent = false;
if (u >= 0 && u < w && v >= 0 && v < h) {
neighbour = bufferedImage.getRGB(u, v);
notTransparent = ((neighbour >> 24) & 255) > 0;
if (notTransparent) {
float neighbourIntesity = ((neighbour >> 16 & 255) + (neighbour >> 8 & 255) + (neighbour & 255)) / 765f;
float intesityDelta = Math.max(0, Math.min(1, Math.abs(intesity - neighbourIntesity)));
float rDelta = Math.max(0, Math.min(1, Math.abs(r - (neighbour >> 16 & 255) / 255f)));
float gDelta = Math.max(0, Math.min(1, Math.abs(g - (neighbour >> 8 & 255) / 255f)));
float bDelta = Math.max(0, Math.min(1, Math.abs(b - (neighbour & 255) / 255f)));
delta = Math.max(intesityDelta, Math.max(rDelta, Math.max(gDelta, bDelta)));
delta = delta < .25 ? 0 : delta > .4 ? 1 : delta;
}
}
if (delta > 0) {
Pair<TexturePoint, TexturePoint> l = Pair.of(new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 0), hh + (i == 2 ? 0 : i == 3 ? 1 : 0), w), new TexturePoint(ww + (i == 0 ? 0 : i == 1 ? 1 : 1), hh + (i == 2 ? 0 : i == 3 ? 1 : 1), w));
temp_lines.add(l);
}
}
}
}
lines.addAll(temp_lines);
}
ArrayList<Integer> lumiSort = new ArrayList<>(area.keySet());
Collections.sort(lumiSort, (rgb1, rgb2) -> Double.compare(getLuminance(rgb1), getLuminance(rgb2)));
HashMultimap<ShadeStyle, Point> complete_areaMap = HashMultimap.create();
int lineNumber = 2;
int lineStyle = 0;
for (Integer i : lumiSort) {
complete_areaMap.putAll(new ShadeStyle(lineNumber, lineStyle), area.get(i));
++lineStyle;
lineStyle %= 3;
if (lineStyle == 0)
lineNumber += 1;
}
Set<Pair<Point, Point>> complete_lines = new HashSet<>();
for (Pair<TexturePoint, TexturePoint> line : lines) {
TexturePoint p1 = line.getKey();
TexturePoint p2 = line.getValue();
complete_lines.add(Pair.of(new Point((int) (p1.x / (float) p1.scale * wMax), (int) (p1.y / (float) p1.scale * wMax)), new Point((int) (p2.x / (float) p2.scale * wMax), (int) (p2.y / (float) p2.scale * wMax))));
}
return new BlueprintLines(wMax, complete_lines, complete_areaMap);
}
Aggregations