Search in sources :

Example 6 with SimpleSynchronousResourceReloadListener

use of net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener in project Puzzle by PuzzleMC.

the class PuzzleSplashScreen method onInitializeClient.

public void onInitializeClient() {
    if (!CONFIG_PATH.exists()) {
        // Run when config directory is nonexistent //
        if (CONFIG_PATH.mkdir()) {
            // Create our custom config directory //
            if (Util.getOperatingSystem().equals(Util.OperatingSystem.WINDOWS)) {
                try {
                    Files.setAttribute(CONFIG_PATH.toPath(), "dos:hidden", true);
                } catch (IOException ignored) {
                }
            }
        }
    }
    ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return new Identifier("puzzle", "splash_screen");
        }

        @Override
        public void reload(ResourceManager manager) {
            if (PuzzleConfig.resourcepackSplashScreen) {
                PuzzleSplashScreen.resetColors();
                client.getTextureManager().registerTexture(LOGO, new LogoTexture());
                for (Identifier id : manager.findResources("optifine", path -> path.contains("color.properties"))) {
                    try (InputStream stream = manager.getResource(id).getInputStream()) {
                        Properties properties = new Properties();
                        properties.load(stream);
                        if (properties.get("screen.loading") != null) {
                            Color backgroundColorRGB = ColorUtil.hex2Rgb(properties.get("screen.loading").toString());
                            PuzzleConfig.backgroundColor = ColorHelper.Argb.getArgb(backgroundColorRGB.getAlpha(), backgroundColorRGB.getRed(), backgroundColorRGB.getGreen(), backgroundColorRGB.getGreen());
                        }
                        if (properties.get("screen.loading.bar") != null) {
                            Color progressFrameColorRGB = ColorUtil.hex2Rgb(properties.get("screen.loading.bar").toString());
                            PuzzleConfig.progressFrameColor = ColorHelper.Argb.getArgb(progressFrameColorRGB.getAlpha(), progressFrameColorRGB.getRed(), progressFrameColorRGB.getGreen(), progressFrameColorRGB.getGreen());
                        }
                        if (properties.get("screen.loading.progress") != null) {
                            Color progressBarColorRGB = ColorUtil.hex2Rgb(properties.get("screen.loading.progress").toString());
                            PuzzleConfig.progressBarColor = ColorHelper.Argb.getArgb(progressBarColorRGB.getAlpha(), progressBarColorRGB.getRed(), progressBarColorRGB.getGreen(), progressBarColorRGB.getGreen());
                        }
                        if (properties.get("screen.loading") != null) {
                            PuzzleConfig.write("puzzle");
                        }
                    } catch (Exception e) {
                        LogManager.getLogger("Puzzle").error("Error occurred while loading color.properties " + id.toString(), e);
                    }
                }
                for (Identifier id : manager.findResources("textures", path -> path.contains("mojangstudios.png"))) {
                    try (InputStream stream = manager.getResource(id).getInputStream()) {
                        Files.copy(stream, LOGO_TEXTURE, StandardCopyOption.REPLACE_EXISTING);
                        DefaultResourcePack defaultResourcePack = client.getResourcePackProvider().getPack();
                        InputStream defaultLogo = defaultResourcePack.open(ResourceType.CLIENT_RESOURCES, LOGO);
                        InputStream input = new FileInputStream(String.valueOf(PuzzleSplashScreen.LOGO_TEXTURE));
                        if (input != defaultLogo)
                            client.getTextureManager().registerTexture(LOGO, new NativeImageBackedTexture(NativeImage.read(input)));
                        else
                            Files.delete(LOGO_TEXTURE);
                    } catch (Exception e) {
                        LogManager.getLogger("Puzzle").error("Error occurred while loading custom minecraft logo " + id.toString(), e);
                    }
                }
            }
        }
    });
}
Also used : FabricLoader(net.fabricmc.loader.api.FabricLoader) Util(net.minecraft.util.Util) Environment(net.fabricmc.api.Environment) ColorUtil(net.puzzlemc.core.util.ColorUtil) ColorHelper(net.minecraft.util.math.ColorHelper) StandardCopyOption(java.nio.file.StandardCopyOption) ResourceType(net.minecraft.resource.ResourceType) EnvType(net.fabricmc.api.EnvType) NativeImageBackedTexture(net.minecraft.client.texture.NativeImageBackedTexture) Path(java.nio.file.Path) TextureResourceMetadata(net.minecraft.client.resource.metadata.TextureResourceMetadata) ClientModInitializer(net.fabricmc.api.ClientModInitializer) PuzzleConfig(net.puzzlemc.core.config.PuzzleConfig) ResourceManagerHelper(net.fabricmc.fabric.api.resource.ResourceManagerHelper) Properties(java.util.Properties) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ResourceManager(net.minecraft.resource.ResourceManager) ResourceTexture(net.minecraft.client.texture.ResourceTexture) File(java.io.File) java.awt(java.awt) Paths(java.nio.file.Paths) NativeImage(net.minecraft.client.texture.NativeImage) Identifier(net.minecraft.util.Identifier) MinecraftClient(net.minecraft.client.MinecraftClient) SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) DefaultResourcePack(net.minecraft.resource.DefaultResourcePack) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ResourceManager(net.minecraft.resource.ResourceManager) Properties(java.util.Properties) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) Identifier(net.minecraft.util.Identifier) DefaultResourcePack(net.minecraft.resource.DefaultResourcePack) NativeImageBackedTexture(net.minecraft.client.texture.NativeImageBackedTexture)

Example 7 with SimpleSynchronousResourceReloadListener

use of net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener in project Paradise-Lost by devs-immortal.

the class FluidRenderSetup method setupDenseAercloudRenderingBecauseItJustNeedsToBeASpecialSnowflakeWithOnlyAStillState.

public static void setupDenseAercloudRenderingBecauseItJustNeedsToBeASpecialSnowflakeWithOnlyAStillState(final Fluid still, final Identifier textureFluidId) {
    final Identifier stillSpriteId = new Identifier(textureFluidId.getNamespace(), "block/" + textureFluidId.getPath() + "_still");
    final Identifier flowingSpriteId = new Identifier(textureFluidId.getNamespace(), "block/" + textureFluidId.getPath() + "_flow");
    // If they're not already present, add the sprites to the block atlas
    ClientSpriteRegistryCallback.event(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE).register((atlasTexture, registry) -> {
        registry.register(stillSpriteId);
        registry.register(flowingSpriteId);
    });
    final Identifier fluidId = Registry.FLUID.getId(still);
    final Identifier listenerId = new Identifier(fluidId.getNamespace(), fluidId.getPath() + "_reload_listener");
    final Sprite[] fluidSprites = { null, null };
    ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return listenerId;
        }

        /**
         * Get the sprites from the block atlas when resources are reloaded
         */
        @Override
        public void apply(ResourceManager resourceManager) {
            final Function<Identifier, Sprite> atlas = MinecraftClient.getInstance().getSpriteAtlas(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE);
            fluidSprites[0] = atlas.apply(stillSpriteId);
            fluidSprites[1] = atlas.apply(flowingSpriteId);
        }
    });
    // The FluidRenderer gets the sprites and color from a FluidRenderHandler during rendering
    final FluidRenderHandler renderHandler = new FluidRenderHandler() {

        @Override
        public Sprite[] getFluidSprites(BlockRenderView view, BlockPos pos, FluidState state) {
            return fluidSprites;
        }

        @Override
        public int getFluidColor(BlockRenderView view, BlockPos pos, FluidState state) {
            return 0xFFFFFF;
        }
    };
    FluidRenderHandlerRegistry.INSTANCE.register(still, renderHandler);
}
Also used : SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) Function(java.util.function.Function) Identifier(net.minecraft.util.Identifier) BlockRenderView(net.minecraft.world.BlockRenderView) Sprite(net.minecraft.client.texture.Sprite) BlockPos(net.minecraft.util.math.BlockPos) ResourceManager(net.minecraft.resource.ResourceManager) FluidRenderHandler(net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler) FluidState(net.minecraft.fluid.FluidState)

Aggregations

SimpleSynchronousResourceReloadListener (net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener)7 ResourceManager (net.minecraft.resource.ResourceManager)7 Identifier (net.minecraft.util.Identifier)7 Function (java.util.function.Function)3 FluidRenderHandler (net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandler)3 Sprite (net.minecraft.client.texture.Sprite)3 FluidState (net.minecraft.fluid.FluidState)3 BlockPos (net.minecraft.util.math.BlockPos)3 BlockRenderView (net.minecraft.world.BlockRenderView)3 File (java.io.File)2 EnvType (net.fabricmc.api.EnvType)2 Environment (net.fabricmc.api.Environment)2 ResourceManagerHelper (net.fabricmc.fabric.api.resource.ResourceManagerHelper)2 FabricLoader (net.fabricmc.loader.api.FabricLoader)2 ResourceType (net.minecraft.resource.ResourceType)2 LogManager (org.apache.logging.log4j.LogManager)2 ClientCore (de.siphalor.tweed.client.ClientCore)1 de.siphalor.tweed.config (de.siphalor.tweed.config)1 java.awt (java.awt)1 FileInputStream (java.io.FileInputStream)1