| 1 | package net.minecraft.client.resources;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 9 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.world.level.material.MapColor;
|
| 12 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 13 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class MapTextureManager implements AutoCloseable {
|
| 17 | private final Int2ObjectMap<MapTextureManager.MapInstance> maps = new Int2ObjectOpenHashMap<>();
|
| 18 | private final TextureManager textureManager;
|
| 19 |
|
| 20 | public MapTextureManager(final TextureManager textureManager) {
|
| 21 | this.textureManager = textureManager;
|
| 22 | }
|
| 23 |
|
| 24 | public void update(final MapId id, final MapItemSavedData data) {
|
| 25 | this.getOrCreateMapInstance(id, data).forceUpload();
|
| 26 | }
|
| 27 |
|
| 28 | public Identifier prepareMapTexture(final MapId id, final MapItemSavedData data) {
|
| 29 | MapTextureManager.MapInstance mapInstance = this.getOrCreateMapInstance(id, data);
|
| 30 | mapInstance.updateTextureIfNeeded();
|
| 31 | return mapInstance.location;
|
| 32 | }
|
| 33 |
|
| 34 | public void resetData() {
|
| 35 | for (MapTextureManager.MapInstance mapInstance : this.maps.values()) {
|
| 36 | mapInstance.close();
|
| 37 | }
|
| 38 |
|
| 39 | this.maps.clear();
|
| 40 | }
|
| 41 |
|
| 42 | private MapTextureManager.MapInstance getOrCreateMapInstance(final MapId id, final MapItemSavedData data) {
|
| 43 | return this.maps.compute(id.id(), (k, instance) -> {
|
| 44 | if (instance == null) {
|
| 45 | return new MapTextureManager.MapInstance(k, data);
|
| 46 | }
|
| 47 |
|
| 48 | instance.replaceMapData(data);
|
| 49 | return (MapTextureManager.MapInstance)instance;
|
| 50 | });
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void close() {
|
| 55 | this.resetData();
|
| 56 | }
|
| 57 |
|
| 58 | @Environment(EnvType.CLIENT)
|
| 59 | private class MapInstance implements AutoCloseable {
|
| 60 | private MapItemSavedData data;
|
| 61 | private final DynamicTexture texture;
|
| 62 | private boolean requiresUpload = true;
|
| 63 | private final Identifier location;
|
| 64 |
|
| 65 | private MapInstance(final int id, final MapItemSavedData data) {
|
| 66 | this.data = data;
|
| 67 | this.texture = new DynamicTexture(() -> "Map " + id, 128, 128, true);
|
| 68 | this.location = Identifier.withDefaultNamespace("map/" + id);
|
| 69 | MapTextureManager.this.textureManager.register(this.location, this.texture);
|
| 70 | }
|
| 71 |
|
| 72 | private void replaceMapData(final MapItemSavedData data) {
|
| 73 | boolean dataChanged = this.data != data;
|
| 74 | this.data = data;
|
| 75 | this.requiresUpload |= dataChanged;
|
| 76 | }
|
| 77 |
|
| 78 | public void forceUpload() {
|
| 79 | this.requiresUpload = true;
|
| 80 | }
|
| 81 |
|
| 82 | private void updateTextureIfNeeded() {
|
| 83 | if (this.requiresUpload) {
|
| 84 | NativeImage pixels = this.texture.getPixels();
|
| 85 |
|
| 86 | for (int y = 0; y < 128; y++) {
|
| 87 | for (int x = 0; x < 128; x++) {
|
| 88 | int i = x + y * 128;
|
| 89 | pixels.setPixel(x, y, MapColor.getColorFromPackedId(this.data.colors[i]));
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | this.texture.upload();
|
| 94 | this.requiresUpload = false;
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void close() {
|
| 100 | this.texture.close();
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|