Palette .java

net.minecraft.client.resources.palette.Palette (class, 84 lines): members: ID_CONVERTER, listAndLoad, colors, load, isEmpty, size, EMPTY, get, LOGGER.

net.minecraft.client.resources.paletteclass84 linesclient2.6 KB

API Surface (7)

CompletableFuture<Map<Identifier, Palette>> listAndLoad(final ResourceManager resourceManager, final Executor taskExecutor)
Palette load(final Resource resource)
Palette from(final NativeImage image)
Palette of(final int... colors)
int get(final int index)
int size()
boolean isEmpty()

Fields & Constants (4)

LOGGER
EMPTY
ID_CONVERTER
colors

String literals (2)

"textures/palettes"
".png"

Full Source

Palette.java84 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.resources.palette;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.mojang.blaze3d.platform.NativeImage;
5
import com.mojang.logging.LogUtils;
6
import it.unimi.dsi.fastutil.ints.IntArrays;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.util.Map;
10
import java.util.Map.Entry;
11
import java.util.concurrent.CompletableFuture;
12
import java.util.concurrent.Executor;
13
import java.util.stream.Collectors;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.resources.FileToIdConverter;
17
import net.minecraft.resources.Identifier;
18
import net.minecraft.server.packs.resources.Resource;
19
import net.minecraft.server.packs.resources.ResourceManager;
20
import net.minecraft.util.thread.ParallelMapTransform;
21
import org.slf4j.Logger;
22
23
@Environment(EnvType.CLIENT)
24
public class Palette {
25
	private static final Logger LOGGER = LogUtils.getLogger();
26
	public static final Palette EMPTY = new Palette(IntArrays.EMPTY_ARRAY);
27
	public static final FileToIdConverter ID_CONVERTER = new FileToIdConverter("textures/palettes", ".png");
28
	private final int[] colors;
29
30
	private Palette(final int[] colors) {
31
		this.colors = colors;
32
	}
33
34
	public static CompletableFuture<Map<Identifier, Palette>> listAndLoad(final ResourceManager resourceManager, final Executor taskExecutor) {
35
		return CompletableFuture.<Map<Identifier, Resource>>supplyAsync(() -> ID_CONVERTER.listMatchingResources(resourceManager), taskExecutor)
36
			.thenCompose(resources -> ParallelMapTransform.schedule((Map<Identifier, Resource>)resources, Palette::tryLoad, taskExecutor))
37
			.thenApply(
38
				palettes -> palettes.entrySet()
39
					.stream()
40
					.filter(entry -> !entry.getValue().isEmpty())
41
					.collect(Collectors.toMap(entry -> ID_CONVERTER.fileToId(entry.getKey()), Entry::getValue))
42
			);
43
	}
44
45
	private static Palette tryLoad(final Identifier id, final Resource resource) {
46
		try {
47
			return load(resource);
48
		} catch (IOException e) {
49
			LOGGER.error("Failed to load palette with id {}", id, e);
50
			return EMPTY;
51
		}
52
	}
53
54
	public static Palette load(final Resource resource) throws IOException {
55
		try (
56
			InputStream input = resource.open();
57
			NativeImage image = NativeImage.read(input);
58
		) {
59
			return from(image);
60
		}
61
	}
62
63
	public static Palette from(final NativeImage image) {
64
		return new Palette(image.getPixels());
65
	}
66
67
	@VisibleForTesting
68
	public static Palette of(final int... colors) {
69
		return new Palette(IntArrays.copy(colors));
70
	}
71
72
	public int get(final int index) {
73
		return this.colors[index];
74
	}
75
76
	public int size() {
77
		return this.colors.length;
78
	}
79
80
	public boolean isEmpty() {
81
		return this.colors.length == 0;
82
	}
83
}
84

Decompiled Minecraft 26.3 source of Palette (net/minecraft/client/resources/palette/Palette.java) with Mojang official mappings, stored verbatim on the site.