Search in sources :

Example 1 with SimpleSynchronousResourceReloadListener

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

the class PrimevalFluids method setupFluidRendering.

/*
     * Fluid rendering setup from Spectrum,
     * Made by DaFaqs, credit to him for this
     * method
     */
@Environment(EnvType.CLIENT)
private static void setupFluidRendering(final Fluid still, final Fluid flowing, final Identifier textureFluidId, final int color) {
    final Identifier stillSpriteId = new Identifier(textureFluidId.getNamespace(), "block/" + textureFluidId.getPath());
    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() {

        /**
         * Get the sprites from the block atlas when resources are reloaded
         */
        @Override
        public void reload(ResourceManager manager) {
            final Function<Identifier, Sprite> atlas = MinecraftClient.getInstance().getSpriteAtlas(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE);
            fluidSprites[0] = atlas.apply(stillSpriteId);
            fluidSprites[1] = atlas.apply(flowingSpriteId);
        }

        @Override
        public Identifier getFabricId() {
            return listenerId;
        }
    });
    // 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 color;
        }
    };
    FluidRenderHandlerRegistry.INSTANCE.register(still, renderHandler);
// FluidRenderHandlerRegistry.INSTANCE.register(flowing, renderHandler); // Temp removed because of flowing shenanigans
}
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) Environment(net.fabricmc.api.Environment)

Example 2 with SimpleSynchronousResourceReloadListener

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

the class ResourceReloadModClient method onInitializeClient.

@Override
public void onInitializeClient() {
    for (int i = 64; i >= 2; i--) {
        final int _i = i;
        ResourceManagerHelper.get(ResourceType.ASSETS).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

            @Override
            public void apply(ResourceManager var1) {
                System.out.println("Reloading (should run as #" + _i + ")");
            }

            @Override
            public Identifier getFabricId() {
                return new Identifier("fabric:rrmc" + _i);
            }

            @Override
            public Collection<Identifier> getFabricDependencies() {
                return Collections.singletonList(new Identifier("fabric:rrmc" + (_i - 1)));
            }
        });
    }
    ResourceManagerHelper.get(ResourceType.ASSETS).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return new Identifier("fabric:rrmc1");
        }

        @Override
        public void apply(ResourceManager var1) {
            System.out.println("Reloading (should run as #1)");
        }
    });
    ResourceManagerHelper.get(ResourceType.ASSETS).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return new Identifier("fabric:rrmc_should_not_resolve");
        }

        @Override
        public Collection<Identifier> getFabricDependencies() {
            return Collections.singletonList(new Identifier("fabric:rrmc_nonexistent"));
        }

        @Override
        public void apply(ResourceManager var1) {
        }
    });
}
Also used : SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) Identifier(net.minecraft.util.Identifier) Collection(java.util.Collection) ResourceManager(net.minecraft.resource.ResourceManager)

Example 3 with SimpleSynchronousResourceReloadListener

use of net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener in project tweed-api by Siphalor.

the class Tweed method onInitialize.

@Override
public void onInitialize() {
    ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return new Identifier(Tweed.MOD_ID, "resource_reload");
        }

        @Override
        public void apply(ResourceManager resourceManager) {
            try {
                ConfigLoader.loadConfigs(resourceManager, ConfigEnvironment.SERVER, ConfigScope.SMALLEST);
            } catch (Throwable e) {
                Tweed.LOGGER.error("Tweed failed to load config files:");
                e.printStackTrace();
            }
        }
    });
    ServerSidePacketRegistry.INSTANCE.register(REQUEST_SYNC_C2S_PACKET, (packetContext, packetByteBuf) -> {
        String fileName = packetByteBuf.readString(32767);
        for (ConfigFile configFile : TweedRegistry.getConfigFiles()) {
            if (configFile.getName().equals(fileName)) {
                if (Objects.requireNonNull(packetContext.getPlayer().getServer()).getPermissionLevel(packetContext.getPlayer().getGameProfile()) == 4) {
                    configFile.syncToClient((ServerPlayerEntity) packetContext.getPlayer(), packetByteBuf.readEnumConstant(ConfigEnvironment.class), packetByteBuf.readEnumConstant(ConfigScope.class), packetByteBuf.readEnumConstant(ConfigOrigin.class));
                } else {
                    packetByteBuf.readEnumConstant(ConfigEnvironment.class);
                    ConfigScope scope = packetByteBuf.readEnumConstant(ConfigScope.class);
                    packetByteBuf.readEnumConstant(ConfigOrigin.class);
                    configFile.syncToClient((ServerPlayerEntity) packetContext.getPlayer(), ConfigEnvironment.SYNCED, scope, ConfigOrigin.DATAPACK);
                }
                break;
            }
        }
    });
    ServerSidePacketRegistry.INSTANCE.register(TWEED_CLOTH_SYNC_C2S_PACKET, ((packetContext, packetByteBuf) -> {
        String fileName = packetByteBuf.readString(32767);
        for (ConfigFile configFile : TweedRegistry.getConfigFiles()) {
            if (configFile.getName().equals(fileName)) {
                if (Objects.requireNonNull(packetContext.getPlayer().getServer()).getPermissionLevel(packetContext.getPlayer().getGameProfile()) == 4) {
                    ConfigEnvironment environment = packetByteBuf.readEnumConstant(ConfigEnvironment.class);
                    ConfigScope scope = packetByteBuf.readEnumConstant(ConfigScope.class);
                    configFile.read(packetByteBuf, environment, ConfigScope.SMALLEST, ConfigOrigin.MAIN);
                    ConfigLoader.updateMainConfigFile(configFile, environment, scope);
                } else {
                    packetByteBuf.clear();
                }
                break;
            }
        }
    }));
}
Also used : SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) FabricLoader(net.fabricmc.loader.api.FabricLoader) ResourceManagerHelper(net.fabricmc.fabric.api.resource.ResourceManagerHelper) ModInitializer(net.fabricmc.api.ModInitializer) ResourceManager(net.minecraft.resource.ResourceManager) File(java.io.File) Objects(java.util.Objects) MinecraftServer(net.minecraft.server.MinecraftServer) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) Logger(org.apache.logging.log4j.Logger) ResourceType(net.minecraft.resource.ResourceType) EnvType(net.fabricmc.api.EnvType) Identifier(net.minecraft.util.Identifier) ClientCore(de.siphalor.tweed.client.ClientCore) de.siphalor.tweed.config(de.siphalor.tweed.config) ServerSidePacketRegistry(net.fabricmc.fabric.api.network.ServerSidePacketRegistry) SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) LogManager(org.apache.logging.log4j.LogManager) Identifier(net.minecraft.util.Identifier) ResourceManager(net.minecraft.resource.ResourceManager)

Example 4 with SimpleSynchronousResourceReloadListener

use of net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener in project tweed-api by Siphalor.

the class ClientCore method onInitializeClient.

@Override
public void onInitializeClient() {
    ConfigLoader.initialReload(ConfigEnvironment.UNIVERSAL);
    ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SimpleSynchronousResourceReloadListener() {

        @Override
        public Identifier getFabricId() {
            return new Identifier(Tweed.MOD_ID, "assets_listener");
        }

        @Override
        public void apply(ResourceManager resourceManager) {
            try {
                ConfigLoader.loadConfigs(resourceManager, ConfigEnvironment.CLIENT, ConfigScope.SMALLEST);
            } catch (Throwable e) {
                Tweed.LOGGER.error("Tweed failed to load config files");
                e.printStackTrace();
            }
        }
    });
    ServerStartCallback.EVENT.register(minecraftServer -> ConfigLoader.loadConfigs(minecraftServer.getDataManager(), ConfigEnvironment.UNIVERSAL, ConfigScope.WORLD));
    ClientSidePacketRegistry.INSTANCE.register(Tweed.CONFIG_SYNC_S2C_PACKET, (packetContext, packetByteBuf) -> {
        ConfigOrigin origin = packetByteBuf.readEnumConstant(ConfigOrigin.class);
        String fileName = packetByteBuf.readString();
        for (ConfigFile configFile : TweedRegistry.getConfigFiles()) {
            if (configFile.getName().equals(fileName)) {
                configFile.read(packetByteBuf, ConfigEnvironment.SERVER, ConfigScope.WORLD, origin);
                if (scheduledClothBridge != null) {
                    scheduledClothBridge.onSync(configFile);
                }
                break;
            }
        }
    });
}
Also used : SimpleSynchronousResourceReloadListener(net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener) Identifier(net.minecraft.util.Identifier) ResourceManager(net.minecraft.resource.ResourceManager)

Example 5 with SimpleSynchronousResourceReloadListener

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

the class FluidRenderSetup method setupFluidRendering.

public static void setupFluidRendering(final Fluid still, final Fluid flowing, final Identifier textureFluidId, final int color) {
    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 color;
        }
    };
    FluidRenderHandlerRegistry.INSTANCE.register(still, renderHandler);
    FluidRenderHandlerRegistry.INSTANCE.register(flowing, 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)6 ResourceManager (net.minecraft.resource.ResourceManager)6 Identifier (net.minecraft.util.Identifier)6 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 ClientCore (de.siphalor.tweed.client.ClientCore)1 de.siphalor.tweed.config (de.siphalor.tweed.config)1 File (java.io.File)1 Collection (java.util.Collection)1 Objects (java.util.Objects)1 EnvType (net.fabricmc.api.EnvType)1 Environment (net.fabricmc.api.Environment)1 ModInitializer (net.fabricmc.api.ModInitializer)1 ServerSidePacketRegistry (net.fabricmc.fabric.api.network.ServerSidePacketRegistry)1 ResourceManagerHelper (net.fabricmc.fabric.api.resource.ResourceManagerHelper)1 FabricLoader (net.fabricmc.loader.api.FabricLoader)1