Search in sources :

Example 6 with ResourceManager

use of net.minecraft.resource.ResourceManager 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 7 with ResourceManager

use of net.minecraft.resource.ResourceManager 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)

Example 8 with ResourceManager

use of net.minecraft.resource.ResourceManager in project Liminal-Library by LudoCrypt.

the class NbtPlacerUtil method load.

public static Optional<NbtPlacerUtil> load(ResourceManager manager, Identifier id) {
    try {
        Optional<NbtCompound> nbtOptional = loadNbtFromFile(manager, id);
        if (nbtOptional.isPresent()) {
            NbtCompound nbt = nbtOptional.get();
            NbtList paletteList = nbt.getList("palette", 10);
            HashMap<Integer, BlockState> palette = new HashMap<Integer, BlockState>(paletteList.size());
            List<NbtCompound> paletteCompoundList = paletteList.stream().filter(nbtElement -> nbtElement instanceof NbtCompound).map(element -> (NbtCompound) element).toList();
            for (int i = 0; i < paletteCompoundList.size(); i++) {
                palette.put(i, NbtHelper.toBlockState(paletteCompoundList.get(i)));
            }
            NbtList sizeList = nbt.getList("size", 3);
            BlockPos sizeVectorRotated = new BlockPos(sizeList.getInt(0), sizeList.getInt(1), sizeList.getInt(2));
            BlockPos sizeVector = new BlockPos(Math.abs(sizeVectorRotated.getX()), Math.abs(sizeVectorRotated.getY()), Math.abs(sizeVectorRotated.getZ()));
            NbtList positionsList = nbt.getList("blocks", 10);
            HashMap<BlockPos, Pair<BlockState, NbtCompound>> positions = new HashMap<BlockPos, Pair<BlockState, NbtCompound>>(positionsList.size());
            List<Pair<BlockPos, Pair<BlockState, NbtCompound>>> positionsPairList = positionsList.stream().filter(nbtElement -> nbtElement instanceof NbtCompound).map(element -> (NbtCompound) element).map((nbtCompound) -> Pair.of(new BlockPos(nbtCompound.getList("pos", 3).getInt(0), nbtCompound.getList("pos", 3).getInt(1), nbtCompound.getList("pos", 3).getInt(2)), Pair.of(palette.get(nbtCompound.getInt("state")), nbtCompound.getCompound("nbt")))).sorted(Comparator.comparing((pair) -> pair.getFirst().getX())).sorted(Comparator.comparing((pair) -> pair.getFirst().getY())).sorted(Comparator.comparing((pair) -> pair.getFirst().getZ())).toList();
            positionsPairList.forEach((pair) -> positions.put(pair.getFirst().subtract(positionsPairList.get(0).getFirst()), pair.getSecond()));
            return Optional.of(new NbtPlacerUtil(nbt, positions, nbt.getList("entities", 10), positionsPairList.get(0).getFirst(), sizeVector));
        }
        throw new NullPointerException();
    } catch (Exception e) {
        e.printStackTrace();
        return Optional.empty();
    }
}
Also used : EntityType(net.minecraft.entity.EntityType) NbtIo(net.minecraft.nbt.NbtIo) NbtList(net.minecraft.nbt.NbtList) ChunkRegion(net.minecraft.world.ChunkRegion) HashMap(java.util.HashMap) Resource(net.minecraft.resource.Resource) Direction(net.minecraft.util.math.Direction) NbtDouble(net.minecraft.nbt.NbtDouble) Vec3d(net.minecraft.util.math.Vec3d) BlockState(net.minecraft.block.BlockState) Entity(net.minecraft.entity.Entity) TriConsumer(org.apache.logging.log4j.util.TriConsumer) BlockRotation(net.minecraft.util.BlockRotation) NbtFloat(net.minecraft.nbt.NbtFloat) IOException(java.io.IOException) BlockPos(net.minecraft.util.math.BlockPos) NbtHelper(net.minecraft.nbt.NbtHelper) ResourceManager(net.minecraft.resource.ResourceManager) Pair(com.mojang.datafixers.util.Pair) Blocks(net.minecraft.block.Blocks) NbtInt(net.minecraft.nbt.NbtInt) NbtCompound(net.minecraft.nbt.NbtCompound) List(java.util.List) MathHelper(net.minecraft.util.math.MathHelper) Optional(java.util.Optional) Identifier(net.minecraft.util.Identifier) Comparator(java.util.Comparator) NbtCompound(net.minecraft.nbt.NbtCompound) HashMap(java.util.HashMap) IOException(java.io.IOException) BlockState(net.minecraft.block.BlockState) NbtList(net.minecraft.nbt.NbtList) BlockPos(net.minecraft.util.math.BlockPos) Pair(com.mojang.datafixers.util.Pair)

Example 9 with ResourceManager

use of net.minecraft.resource.ResourceManager in project quilt-standard-libraries by QuiltMC.

the class ResourceReloaderTestMod method setupServerReloadListeners.

private void setupServerReloadListeners() {
    ResourceLoader.get(ResourceType.SERVER_DATA).registerReloader(new SimpleSynchronousResourceReloader() {

        @Override
        public Identifier getQuiltId() {
            return ResourceLoaderTestMod.id("server_second");
        }

        @Override
        public void reload(ResourceManager manager) {
            if (!serverResources) {
                throw new AssertionError("Second resource reloader was called before the first!");
            }
            LOGGER.info("Second server resource reloader is called.");
        }

        @Override
        public Collection<Identifier> getQuiltDependencies() {
            return Collections.singletonList(ResourceLoaderTestMod.id("server_first"));
        }
    });
    ResourceLoader.get(ResourceType.SERVER_DATA).registerReloader(new SimpleSynchronousResourceReloader() {

        @Override
        public Identifier getQuiltId() {
            return ResourceLoaderTestMod.id("server_first");
        }

        @Override
        public void reload(ResourceManager manager) {
            serverResources = true;
            LOGGER.info("First server resource reloader is called.");
        }
    });
}
Also used : Identifier(net.minecraft.util.Identifier) SimpleSynchronousResourceReloader(org.quiltmc.qsl.resource.loader.api.reloader.SimpleSynchronousResourceReloader) Collection(java.util.Collection) ResourceManager(net.minecraft.resource.ResourceManager)

Example 10 with ResourceManager

use of net.minecraft.resource.ResourceManager in project SpeedRunIGT by RedLime.

the class MinecraftClientMixin method onInit.

/**
 * Add import font system
 */
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/debug/DebugRenderer;<init>(Lnet/minecraft/client/MinecraftClient;)V", shift = At.Shift.BEFORE))
public void onInit(RunArgs args, CallbackInfo ci) {
    this.resourceManager.registerListener(new SinglePreparationResourceReloadListener<Map<Identifier, List<Font>>>() {

        @Override
        protected Map<Identifier, List<Font>> prepare(ResourceManager manager, Profiler profiler) {
            SpeedRunIGT.FONT_MAPS.clear();
            try {
                HashMap<Identifier, List<Font>> map = new HashMap<>();
                File[] fontFiles = SpeedRunIGT.FONT_PATH.toFile().listFiles();
                if (fontFiles == null)
                    return new HashMap<>();
                for (File file : Arrays.stream(fontFiles).filter(file -> file.getName().endsWith(".ttf")).collect(Collectors.toList())) {
                    File config = SpeedRunIGT.FONT_PATH.resolve(file.getName().substring(0, file.getName().length() - 4) + ".json").toFile();
                    if (config.exists()) {
                        FontUtils.addFont(map, file, config);
                    } else {
                        FontUtils.addFont(map, file, null);
                    }
                }
                return map;
            } catch (Throwable e) {
                return new HashMap<>();
            }
        }

        @Override
        protected void apply(Map<Identifier, List<Font>> loader, ResourceManager manager, Profiler profiler) {
            try {
                FontManagerAccessor fontManager = (FontManagerAccessor) ((MinecraftClientAccessor) MinecraftClient.getInstance()).getFontManager();
                for (Map.Entry<Identifier, List<Font>> listEntry : loader.entrySet()) {
                    FontStorage fontStorage = new FontStorage(fontManager.getTextureManager(), listEntry.getKey());
                    fontStorage.setFonts(listEntry.getValue());
                    fontManager.getFontStorages().put(listEntry.getKey(), fontStorage);
                }
                TimerDrawer.fontHeightMap.clear();
            } catch (Throwable e) {
                SpeedRunIGT.error("Error! failed import timer fonts!");
                e.printStackTrace();
            }
        }
    });
}
Also used : MinecraftClientAccessor(com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor) HashMap(java.util.HashMap) ReloadableResourceManager(net.minecraft.resource.ReloadableResourceManager) ResourceManager(net.minecraft.resource.ResourceManager) Font(net.minecraft.client.font.Font) Identifier(net.minecraft.util.Identifier) Profiler(net.minecraft.util.profiler.Profiler) FontStorage(net.minecraft.client.font.FontStorage) FontManagerAccessor(com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

ResourceManager (net.minecraft.resource.ResourceManager)11 Identifier (net.minecraft.util.Identifier)10 SimpleSynchronousResourceReloadListener (net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener)6 BlockPos (net.minecraft.util.math.BlockPos)4 Collection (java.util.Collection)3 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 BlockRenderView (net.minecraft.world.BlockRenderView)3 File (java.io.File)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Pair (com.mojang.datafixers.util.Pair)1 FontManagerAccessor (com.redlimerl.speedrunigt.mixins.access.FontManagerAccessor)1 MinecraftClientAccessor (com.redlimerl.speedrunigt.mixins.access.MinecraftClientAccessor)1 ClientCore (de.siphalor.tweed.client.ClientCore)1 de.siphalor.tweed.config (de.siphalor.tweed.config)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1