Search in sources :

Example 1 with Resource

use of net.modificationstation.stationapi.api.client.resource.Resource in project StationAPI by ModificationStation.

the class SpriteAtlasTexture method loadSprites.

private Collection<Sprite.Info> loadSprites(TexturePack resourceManager, Set<Identifier> ids) {
    ArrayList<CompletableFuture<Void>> list = new ArrayList<>();
    ConcurrentLinkedQueue<Sprite.Info> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
    for (Identifier identifier : ids) {
        if (MissingSprite.getMissingSpriteId().equals(identifier))
            continue;
        list.add(CompletableFuture.runAsync(() -> {
            Sprite.Info info3;
            Identifier identifier2 = this.getTexturePath(identifier);
            try (Resource resource = Resource.of(resourceManager.getResourceAsStream(ResourceManager.ASSETS.toPath(identifier2)))) {
                BufferedImage image = ImageIO.read(resource.getInputStream());
                AnimationResourceMetadata animationResourceMetadata = resource.getMetadata(AnimationResourceMetadata.READER);
                if (animationResourceMetadata == null)
                    animationResourceMetadata = AnimationResourceMetadata.EMPTY;
                Pair<Integer, Integer> pair = animationResourceMetadata.getResolution(image.getWidth(), image.getHeight());
                info3 = new Sprite.Info(identifier, pair.getFirst(), pair.getSecond(), animationResourceMetadata);
            } catch (RuntimeException runtimeException) {
                LOGGER.error("Unable to parse metadata from {} : {}", identifier2, runtimeException);
                return;
            } catch (IOException iOException) {
                LOGGER.error("Using missing texture, unable to load {} : {}", identifier2, iOException);
                return;
            }
            concurrentLinkedQueue.add(info3);
        }, Util.getMainWorkerExecutor()));
    }
    CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
    return concurrentLinkedQueue;
}
Also used : Resource(net.modificationstation.stationapi.api.client.resource.Resource) AnimationResourceMetadata(net.modificationstation.stationapi.api.client.resource.metadata.AnimationResourceMetadata) Identifier(net.modificationstation.stationapi.api.registry.Identifier) Pair(com.mojang.datafixers.util.Pair)

Aggregations

Pair (com.mojang.datafixers.util.Pair)1 Resource (net.modificationstation.stationapi.api.client.resource.Resource)1 AnimationResourceMetadata (net.modificationstation.stationapi.api.client.resource.metadata.AnimationResourceMetadata)1 Identifier (net.modificationstation.stationapi.api.registry.Identifier)1