Skip to content

Commit

Permalink
Handle a couple of model loading exceptions better
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Dec 1, 2019
1 parent 95bbb83 commit b21cdf1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
21 changes: 13 additions & 8 deletions src/main/java/knightminer/inspirations/common/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package knightminer.inspirations.common;

import knightminer.inspirations.Inspirations;
import knightminer.inspirations.shared.client.TextureModel;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.color.BlockColors;
Expand Down Expand Up @@ -58,14 +59,18 @@ protected static void registerItemColors(ItemColors itemColors, IItemColor handl
}

protected static void replaceModel(ModelBakeEvent event, ModelResourceLocation location, BiFunction<IBakedModel, IUnbakedModel, TextureModel> modelMaker) {
// model to be retextured
IUnbakedModel model = ModelLoaderRegistry.getModelOrLogError(location, "Error loading model for " + location);
// model for rendering properties
IBakedModel standard = event.getModelRegistry().get(location);
// model to replace standard
TextureModel finalModel = modelMaker.apply(standard, model);
finalModel.fetchChildren(event.getModelLoader());
event.getModelRegistry().put(location, finalModel);
try {
// model to be retextured
IUnbakedModel model = ModelLoaderRegistry.getModel(location);
// model for rendering properties
IBakedModel standard = event.getModelRegistry().get(location);
// model to replace standard
TextureModel finalModel = modelMaker.apply(standard, model);
finalModel.fetchChildren(event.getModelLoader());
event.getModelRegistry().put(location, finalModel);
} catch(Exception e) {
Inspirations.log.error("Caught exception trying to replace model for " + location, e);
}
}

protected static void replaceTexturedModel(ModelBakeEvent event, ModelResourceLocation location, String key, boolean item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,31 @@ private static Integer getItemColorRaw(Item key) {
}
float r = 0, g = 0, b = 0, count = 0;
float[] hsb = new float[3];
for (int x = 0; x < sprite.getWidth(); x++) {
for (int y = 0; y < sprite.getHeight(); y++) {
int argb = sprite.getPixelRGBA(0, x, y);
// integer is in format of 0xAABBGGRR
int cr = argb & 0xFF;
int cg = argb >> 8 & 0xFF;
int cb = argb >> 16 & 0xFF;
int ca = argb >> 24 & 0xFF;
if (ca > 0x7F && NumberUtils.max(cr, cg, cb) > 0x1F) {
Color.RGBtoHSB(ca, cr, cg, hsb);
float weight = hsb[1];
r += cr * weight;
g += cg * weight;
b += cb * weight;
count += weight;
try {
for(int x = 0; x < sprite.getWidth(); x++) {
for(int y = 0; y < sprite.getHeight(); y++) {
// TODO: apparently want a try/catch here, just return 0x00000000 if it fails I guess
int argb = sprite.getPixelRGBA(0, x, y);
// integer is in format of 0xAABBGGRR
int cr = argb & 0xFF;
int cg = argb >> 8 & 0xFF;
int cb = argb >> 16 & 0xFF;
int ca = argb >> 24 & 0xFF;
if(ca > 0x7F && NumberUtils.max(cr, cg, cb) > 0x1F) {
Color.RGBtoHSB(ca, cr, cg, hsb);
float weight = hsb[1];
r += cr * weight;
g += cg * weight;
b += cb * weight;
count += weight;
}
}
}
} catch (NullPointerException e) {
// there is a random bug where models do not properly load, leading to a null frame data
// so just catch that and treat it as another error state
InspirationsRegistry.log.error("Caught exception reading sprite for " + key.getRegistryName(), e);
return -1;
}
if (count > 0) {
r /= count;
Expand Down

0 comments on commit b21cdf1

Please sign in to comment.