diff options
| author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-27 11:53:57 -0500 |
|---|---|---|
| committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-27 11:53:57 -0500 |
| commit | b09f774d422263ce15b97d6d0804beddf856176d (patch) | |
| tree | e542258481d7496b15679f3c329ef9e087c7d8fc /src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java | |
| parent | 22cb02adbeb24b7ec98f843bcaba99cebe3e4f03 (diff) | |
| download | notenoughupdates-b09f774d422263ce15b97d6d0804beddf856176d.tar.gz notenoughupdates-b09f774d422263ce15b97d6d0804beddf856176d.tar.bz2 notenoughupdates-b09f774d422263ce15b97d6d0804beddf856176d.zip | |
feat: improve formating :)
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java | 569 |
1 files changed, 321 insertions, 248 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java index f4ccf0ad..1157e73a 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CustomSkulls.java @@ -34,252 +34,325 @@ import java.nio.charset.StandardCharsets; import java.util.*; public class CustomSkulls implements IResourceManagerReloadListener { - private static final CustomSkulls INSTANCE = new CustomSkulls(); - - public static CustomSkulls getInstance() { - return INSTANCE; - } - - private final ResourceLocation atlas = new ResourceLocation("notenoughupdates:custom_skull_textures_atlas"); - private final ResourceLocation configuration = new ResourceLocation("notenoughupdates:custom_skull_textures/customskull.json"); - protected final TextureMap textureMap = new TextureMap("custom_skull_textures"); - - public static ItemCameraTransforms.TransformType mostRecentTransformType = ItemCameraTransforms.TransformType.NONE; - - protected final Map<ResourceLocation, TextureAtlasSprite> sprites = Maps.newHashMap(); - - private final FaceBakery faceBakery = new FaceBakery(); - private final ModelSkeletonHead humanoidHead = new ModelHumanoidHead(); - - private final HashMap<String, CustomSkull> customSkulls = new HashMap<>(); - - private final Gson gson = new GsonBuilder().create(); - - private static class CustomSkull { - private ModelBlock model; - private IBakedModel modelBaked; - - private ResourceLocation texture; - } - - @Override - public void onResourceManagerReload(IResourceManager resourceManager) { - customSkulls.clear(); - - try (BufferedReader reader = new BufferedReader(new InputStreamReader( - Minecraft.getMinecraft().getResourceManager().getResource(configuration).getInputStream(), StandardCharsets.UTF_8))) { - JsonObject json = gson.fromJson(reader, JsonObject.class); - - if (json == null) return; - - for (Map.Entry<String, JsonElement> entry : json.entrySet()) { - if (entry.getValue().isJsonObject()) { - JsonObject obj = entry.getValue().getAsJsonObject(); - if (obj.has("model")) { - String location = obj.get("model").getAsString(); - ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".json"); - - CustomSkull skull = new CustomSkull(); - skull.model = ModelBlock.deserialize(new InputStreamReader(Minecraft.getMinecraft().getResourceManager().getResource(loc).getInputStream())); - - customSkulls.put(entry.getKey(), skull); - } else if (obj.has("texture")) { - String location = obj.get("texture").getAsString(); - ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".png"); - - CustomSkull skull = new CustomSkull(); - skull.texture = loc; - - Minecraft.getMinecraft().getTextureManager().deleteTexture(skull.texture); - - customSkulls.put(entry.getKey(), skull); - } - } - } - - loadSprites(); - - for (CustomSkull skull : customSkulls.values()) { - if (skull.model != null) { - skull.modelBaked = bakeModel(skull.model, ModelRotation.X0_Y0, false); - } - } - - Minecraft.getMinecraft().getTextureManager().loadTexture(atlas, textureMap); - } catch (Exception ignored) {} - } - - private void loadSprites() { - final Set<ResourceLocation> set = this.getAllTextureLocations(); - set.remove(TextureMap.LOCATION_MISSING_TEXTURE); - IIconCreator iiconcreator = iconRegistry -> { - for (ResourceLocation resourcelocation : set) { - TextureAtlasSprite textureatlassprite = iconRegistry.registerSprite(resourcelocation); - CustomSkulls.this.sprites.put(resourcelocation, textureatlassprite); - } - }; - this.textureMap.loadSprites(Minecraft.getMinecraft().getResourceManager(), iiconcreator); - this.sprites.put(new ResourceLocation("missingno"), this.textureMap.getMissingSprite()); - } - - protected Set<ResourceLocation> getAllTextureLocations() { - Set<ResourceLocation> set = new HashSet<>(); - - for (CustomSkull skull : customSkulls.values()) { - if (skull.model != null) { - set.addAll(getTextureLocations(skull.model)); - } - } - - return set; - } - - protected Set<ResourceLocation> getTextureLocations(ModelBlock modelBlock) { - Set<ResourceLocation> set = Sets.newHashSet(); - - for (BlockPart blockpart : modelBlock.getElements()) { - for (BlockPartFace blockpartface : blockpart.mapFaces.values()) { - ResourceLocation resourcelocation = new ResourceLocation("notenoughupdates", modelBlock.resolveTextureName(blockpartface.texture)); - set.add(resourcelocation); - } - } - - set.add(new ResourceLocation("notenoughupdates", modelBlock.resolveTextureName("particle"))); - return set; - } - - protected IBakedModel bakeModel(ModelBlock modelBlockIn, net.minecraftforge.client.model.ITransformation modelRotationIn, boolean uvLocked) { - TextureAtlasSprite textureatlassprite = this.sprites.get(new ResourceLocation("notenoughupdates", modelBlockIn.resolveTextureName("particle"))); - SimpleBakedModel.Builder simplebakedmodel$builder = (new SimpleBakedModel.Builder(modelBlockIn)).setTexture(textureatlassprite); - - for (BlockPart blockpart : modelBlockIn.getElements()) { - for (EnumFacing enumfacing : blockpart.mapFaces.keySet()) { - BlockPartFace blockpartface = blockpart.mapFaces.get(enumfacing); - TextureAtlasSprite textureatlassprite1 = this.sprites.get(new ResourceLocation("notenoughupdates", modelBlockIn.resolveTextureName(blockpartface.texture))); - - if (blockpartface.cullFace == null || !net.minecraftforge.client.model.TRSRTransformation.isInteger(modelRotationIn.getMatrix())) { - simplebakedmodel$builder.addGeneralQuad(this.makeBakedQuad(blockpart, blockpartface, textureatlassprite1, enumfacing, modelRotationIn, uvLocked)); - } else { - simplebakedmodel$builder.addFaceQuad(modelRotationIn.rotate(blockpartface.cullFace), this.makeBakedQuad(blockpart, blockpartface, textureatlassprite1, enumfacing, modelRotationIn, uvLocked)); - } - } - } - - return simplebakedmodel$builder.makeBakedModel(); - } - - private BakedQuad makeBakedQuad(BlockPart p_177589_1_, BlockPartFace p_177589_2_, TextureAtlasSprite p_177589_3_, EnumFacing p_177589_4_, ModelRotation p_177589_5_, boolean p_177589_6_) { - return makeBakedQuad(p_177589_1_, p_177589_2_, p_177589_3_, p_177589_4_, (net.minecraftforge.client.model.ITransformation) p_177589_5_, p_177589_6_); - } - - protected BakedQuad makeBakedQuad(BlockPart p_177589_1_, BlockPartFace p_177589_2_, TextureAtlasSprite p_177589_3_, EnumFacing p_177589_4_, net.minecraftforge.client.model.ITransformation p_177589_5_, boolean p_177589_6_) { - return this.faceBakery.makeBakedQuad(p_177589_1_.positionFrom, p_177589_1_.positionTo, p_177589_2_, p_177589_3_, p_177589_4_, p_177589_5_, p_177589_1_.partRotation, p_177589_6_, p_177589_1_.shade); - } - - private void renderModel(IBakedModel model, int color) { - Tessellator tessellator = Tessellator.getInstance(); - WorldRenderer worldrenderer = tessellator.getWorldRenderer(); - worldrenderer.begin(7, DefaultVertexFormats.ITEM); - - for (EnumFacing enumfacing : EnumFacing.values()) { - this.renderQuads(worldrenderer, model.getFaceQuads(enumfacing), color); - } - - this.renderQuads(worldrenderer, model.getGeneralQuads(), color); - tessellator.draw(); - } - - private void renderQuads(WorldRenderer renderer, List<BakedQuad> quads, int color) { - int i = 0; - - for (int j = quads.size(); i < j; ++i) { - BakedQuad bakedquad = quads.get(i); - int k = color; - - net.minecraftforge.client.model.pipeline.LightUtil.renderQuadColor(renderer, bakedquad, k); - } - } - - public boolean renderSkull(float xOffset, float yOffset, float zOffset, EnumFacing placedDirection, - float rotationDeg, int skullType, GameProfile skullOwner, int damage) { - if (NotEnoughUpdates.INSTANCE.config.misc.disableSkullRetexturing) { - return false; - } - if (placedDirection != EnumFacing.UP || skullType != 3) { - return false; - } - if (skullOwner == null || skullOwner.getId() == null) { - return false; - } - - CustomSkull skull = customSkulls.get(skullOwner.getId().toString()); - if (skull == null) { - return false; - } - - if (skull.modelBaked != null && skull.model != null) { - Minecraft.getMinecraft().getTextureManager().bindTexture(atlas); - GlStateManager.pushMatrix(); - GlStateManager.disableCull(); - GlStateManager.enableLighting(); - - GlStateManager.translate(xOffset + 0.5F, yOffset, zOffset + 0.5F); - - GlStateManager.enableRescaleNormal(); - GlStateManager.enableAlpha(); - - GlStateManager.rotate(rotationDeg, 0, 1, 0); - - GlStateManager.translate(0, 0.25f, 0); - - if (xOffset == -0.5 && yOffset == 0 && zOffset == -0.5 && rotationDeg == 180) { - skull.model.getAllTransforms().applyTransform(ItemCameraTransforms.TransformType.HEAD); - } else { - skull.model.getAllTransforms().applyTransform(mostRecentTransformType); - } - - GlStateManager.translate(-0.5f, 0, -0.5f); - - renderModel(skull.modelBaked, 0xffffffff); - GlStateManager.popMatrix(); - } else if (skull.texture != null) { - if (Minecraft.getMinecraft().getTextureManager().getTexture(skull.texture) == null) { - try { - BufferedImage image = ImageIO.read(Minecraft.getMinecraft().getResourceManager().getResource(skull.texture).getInputStream()); - int size = Math.max(image.getHeight(), image.getWidth()); - - Minecraft.getMinecraft().getTextureManager().loadTexture(skull.texture, new AbstractTexture() { - @Override - public void loadTexture(IResourceManager resourceManager) { - TextureUtil.allocateTexture(this.getGlTextureId(), size, size); - - int[] rgb = new int[size * size]; - - image.getRGB(0, 0, image.getWidth(), image.getHeight(), rgb, 0, image.getWidth()); - - TextureUtil.uploadTexture(this.getGlTextureId(), rgb, size, size); - } - }); - } catch (IOException ignored) {} - } - - Minecraft.getMinecraft().getTextureManager().bindTexture(skull.texture); - - GlStateManager.pushMatrix(); - GlStateManager.disableCull(); - - GlStateManager.translate(xOffset + 0.5F, yOffset, zOffset + 0.5F); - - float f = 0.0625F; - GlStateManager.enableRescaleNormal(); - GlStateManager.scale(-1.0F, -1.0F, 1.0F); - GlStateManager.enableAlpha(); - humanoidHead.render(null, 0.0F, 0.0F, 0.0F, rotationDeg, 0.0F, f); - GlStateManager.popMatrix(); - } else { - return false; - } - - return true; - } + private static final CustomSkulls INSTANCE = new CustomSkulls(); + + public static CustomSkulls getInstance() { + return INSTANCE; + } + + private final ResourceLocation atlas = new ResourceLocation("notenoughupdates:custom_skull_textures_atlas"); + private final ResourceLocation configuration = new ResourceLocation( + "notenoughupdates:custom_skull_textures/customskull.json"); + protected final TextureMap textureMap = new TextureMap("custom_skull_textures"); + + public static ItemCameraTransforms.TransformType mostRecentTransformType = ItemCameraTransforms.TransformType.NONE; + + protected final Map<ResourceLocation, TextureAtlasSprite> sprites = Maps.newHashMap(); + + private final FaceBakery faceBakery = new FaceBakery(); + private final ModelSkeletonHead humanoidHead = new ModelHumanoidHead(); + + private final HashMap<String, CustomSkull> customSkulls = new HashMap<>(); + + private final Gson gson = new GsonBuilder().create(); + + private static class CustomSkull { + private ModelBlock model; + private IBakedModel modelBaked; + + private ResourceLocation texture; + } + + @Override + public void onResourceManagerReload(IResourceManager resourceManager) { + customSkulls.clear(); + + try ( + BufferedReader reader = new BufferedReader(new InputStreamReader( + Minecraft.getMinecraft().getResourceManager().getResource(configuration).getInputStream(), + StandardCharsets.UTF_8 + )) + ) { + JsonObject json = gson.fromJson(reader, JsonObject.class); + + if (json == null) return; + + for (Map.Entry<String, JsonElement> entry : json.entrySet()) { + if (entry.getValue().isJsonObject()) { + JsonObject obj = entry.getValue().getAsJsonObject(); + if (obj.has("model")) { + String location = obj.get("model").getAsString(); + ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".json"); + + CustomSkull skull = new CustomSkull(); + skull.model = ModelBlock.deserialize(new InputStreamReader(Minecraft + .getMinecraft() + .getResourceManager() + .getResource(loc) + .getInputStream())); + + customSkulls.put(entry.getKey(), skull); + } else if (obj.has("texture")) { + String location = obj.get("texture").getAsString(); + ResourceLocation loc = new ResourceLocation("notenoughupdates:custom_skull_textures/" + location + ".png"); + + CustomSkull skull = new CustomSkull(); + skull.texture = loc; + + Minecraft.getMinecraft().getTextureManager().deleteTexture(skull.texture); + + customSkulls.put(entry.getKey(), skull); + } + } + } + + loadSprites(); + + for (CustomSkull skull : customSkulls.values()) { + if (skull.model != null) { + skull.modelBaked = bakeModel(skull.model, ModelRotation.X0_Y0, false); + } + } + + Minecraft.getMinecraft().getTextureManager().loadTexture(atlas, textureMap); + } catch (Exception ignored) { + } + } + + private void loadSprites() { + final Set<ResourceLocation> set = this.getAllTextureLocations(); + set.remove(TextureMap.LOCATION_MISSING_TEXTURE); + IIconCreator iiconcreator = iconRegistry -> { + for (ResourceLocation resourcelocation : set) { + TextureAtlasSprite textureatlassprite = iconRegistry.registerSprite(resourcelocation); + CustomSkulls.this.sprites.put(resourcelocation, textureatlassprite); + } + }; + this.textureMap.loadSprites(Minecraft.getMinecraft().getResourceManager(), iiconcreator); + this.sprites.put(new ResourceLocation("missingno"), this.textureMap.getMissingSprite()); + } + + protected Set<ResourceLocation> getAllTextureLocations() { + Set<ResourceLocation> set = new HashSet<>(); + + for (CustomSkull skull : customSkulls.values()) { + if (skull.model != null) { + set.addAll(getTextureLocations(skull.model)); + } + } + + return set; + } + + protected Set<ResourceLocation> getTextureLocations(ModelBlock modelBlock) { + Set<ResourceLocation> set = Sets.newHashSet(); + + for (BlockPart blockpart : modelBlock.getElements()) { + for (BlockPartFace blockpartface : blockpart.mapFaces.values()) { + ResourceLocation resourcelocation = new ResourceLocation( + "notenoughupdates", + modelBlock.resolveTextureName(blockpartface.texture) + ); + set.add(resourcelocation); + } + } + + set.add(new ResourceLocation("notenoughupdates", modelBlock.resolveTextureName("particle"))); + return set; + } + + protected IBakedModel bakeModel( + ModelBlock modelBlockIn, + net.minecraftforge.client.model.ITransformation modelRotationIn, + boolean uvLocked + ) { + TextureAtlasSprite textureatlassprite = this.sprites.get(new ResourceLocation( + "notenoughupdates", + modelBlockIn.resolveTextureName("particle") + )); + SimpleBakedModel.Builder simplebakedmodel$builder = (new SimpleBakedModel.Builder(modelBlockIn)).setTexture( + textureatlassprite); + + for (BlockPart blockpart : modelBlockIn.getElements()) { + for (EnumFacing enumfacing : blockpart.mapFaces.keySet()) { + BlockPartFace blockpartface = blockpart.mapFaces.get(enumfacing); + TextureAtlasSprite textureatlassprite1 = this.sprites.get(new ResourceLocation( + "notenoughupdates", + modelBlockIn.resolveTextureName(blockpartface.texture) + )); + + if (blockpartface.cullFace == null || !net.minecraftforge.client.model.TRSRTransformation.isInteger( + modelRotationIn.getMatrix())) { + simplebakedmodel$builder.addGeneralQuad(this.makeBakedQuad( + blockpart, + blockpartface, + textureatlassprite1, + enumfacing, + modelRotationIn, + uvLocked + )); + } else { + simplebakedmodel$builder.addFaceQuad( + modelRotationIn.rotate(blockpartface.cullFace), + this.makeBakedQuad(blockpart, blockpartface, textureatlassprite1, enumfacing, modelRotationIn, uvLocked) + ); + } + } + } + + return simplebakedmodel$builder.makeBakedModel(); + } + + private BakedQuad makeBakedQuad( + BlockPart p_177589_1_, + BlockPartFace p_177589_2_, + TextureAtlasSprite p_177589_3_, + EnumFacing p_177589_4_, + ModelRotation p_177589_5_, + boolean p_177589_6_ + ) { + return makeBakedQuad( + p_177589_1_, + p_177589_2_, + p_177589_3_, + p_177589_4_, + (net.minecraftforge.client.model.ITransformation) p_177589_5_, + p_177589_6_ + ); + } + + protected BakedQuad makeBakedQuad( + BlockPart p_177589_1_, + BlockPartFace p_177589_2_, + TextureAtlasSprite p_177589_3_, + EnumFacing p_177589_4_, + net.minecraftforge.client.model.ITransformation p_177589_5_, + boolean p_177589_6_ + ) { + return this.faceBakery.makeBakedQuad( + p_177589_1_.positionFrom, + p_177589_1_.positionTo, + p_177589_2_, + p_177589_3_, + p_177589_4_, + p_177589_5_, + p_177589_1_.partRotation, + p_177589_6_, + p_177589_1_.shade + ); + } + + private void renderModel(IBakedModel model, int color) { + Tessellator tessellator = Tessellator.getInstance(); + WorldRenderer worldrenderer = tessellator.getWorldRenderer(); + worldrenderer.begin(7, DefaultVertexFormats.ITEM); + + for (EnumFacing enumfacing : EnumFacing.values()) { + this.renderQuads(worldrenderer, model.getFaceQuads(enumfacing), color); + } + + this.renderQuads(worldrenderer, model.getGeneralQuads(), color); + tessellator.draw(); + } + + private void renderQuads(WorldRenderer renderer, List<BakedQuad> quads, int color) { + int i = 0; + + for (int j = quads.size(); i < j; ++i) { + BakedQuad bakedquad = quads.get(i); + int k = color; + + net.minecraftforge.client.model.pipeline.LightUtil.renderQuadColor(renderer, bakedquad, k); + } + } + + public boolean renderSkull( + float xOffset, float yOffset, float zOffset, EnumFacing placedDirection, + float rotationDeg, int skullType, GameProfile skullOwner, int damage + ) { + if (NotEnoughUpdates.INSTANCE.config.misc.disableSkullRetexturing) { + return false; + } + if (placedDirection != EnumFacing.UP || skullType != 3) { + return false; + } + if (skullOwner == null || skullOwner.getId() == null) { + return false; + } + + CustomSkull skull = customSkulls.get(skullOwner.getId().toString()); + if (skull == null) { + return false; + } + + if (skull.modelBaked != null && skull.model != null) { + Minecraft.getMinecraft().getTextureManager().bindTexture(atlas); + GlStateManager.pushMatrix(); + GlStateManager.disableCull(); + GlStateManager.enableLighting(); + + GlStateManager.translate(xOffset + 0.5F, yOffset, zOffset + 0.5F); + + GlStateManager.enableRescaleNormal(); + GlStateManager.enableAlpha(); + + GlStateManager.rotate(rotationDeg, 0, 1, 0); + + GlStateManager.translate(0, 0.25f, 0); + + if (xOffset == -0.5 && yOffset == 0 && zOffset == -0.5 && rotationDeg == 180) { + skull.model.getAllTransforms().applyTransform(ItemCameraTransforms.TransformType.HEAD); + } else { + skull.model.getAllTransforms().applyTransform(mostRecentTransformType); + } + + GlStateManager.translate(-0.5f, 0, -0.5f); + + renderModel(skull.modelBaked, 0xffffffff); + GlStateManager.popMatrix(); + } else if (skull.texture != null) { + if (Minecraft.getMinecraft().getTextureManager().getTexture(skull.texture) == null) { + try { + BufferedImage image = ImageIO.read(Minecraft + .getMinecraft() + .getResourceManager() + .getResource(skull.texture) + .getInputStream()); + int size = Math.max(image.getHeight(), image.getWidth()); + + Minecraft.getMinecraft().getTextureManager().loadTexture(skull.texture, new AbstractTexture() { + @Override + public void loadTexture(IResourceManager resourceManager) { + TextureUtil.allocateTexture(this.getGlTextureId(), size, size); + + int[] rgb = new int[size * size]; + + image.getRGB(0, 0, image.getWidth(), image.getHeight(), rgb, 0, image.getWidth()); + + TextureUtil.uploadTexture(this.getGlTextureId(), rgb, size, size); + } + }); + } catch (IOException ignored) { + } + } + + Minecraft.getMinecraft().getTextureManager().bindTexture(skull.texture); + + GlStateManager.pushMatrix(); + GlStateManager.disableCull(); + + GlStateManager.translate(xOffset + 0.5F, yOffset, zOffset + 0.5F); + + float f = 0.0625F; + GlStateManager.enableRescaleNormal(); + GlStateManager.scale(-1.0F, -1.0F, 1.0F); + GlStateManager.enableAlpha(); + humanoidHead.render(null, 0.0F, 0.0F, 0.0F, rotationDeg, 0.0F, f); + GlStateManager.popMatrix(); + } else { + return false; + } + + return true; + } } |
