Skip to content

Commit 3b88e50

Browse files
committed
Improve the map handling
It will only create one render.
1 parent b31118a commit 3b88e50

File tree

5 files changed

+188
-112
lines changed

5 files changed

+188
-112
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/BuildMapView.java

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class BuildMapView {
3232
private final MapView mapView;
3333
private final World world;
34-
private final Set<MapRendererData> renderers = new HashSet<>();
34+
private final MapRendererData renderer;
3535

3636
private final boolean virtual;
3737
private boolean locked;
@@ -57,12 +57,25 @@ public BuildMapView(@Nonnull final World world) {
5757
* @param mapView the existing {@link MapView} to wrap (non-null)
5858
*/
5959
public BuildMapView(@Nonnull final MapView mapView) {
60+
this(null, mapView);
61+
}
62+
63+
/**
64+
* Wraps an existing {@link MapView} into a {@code BuildMapView},
65+
* preserving its current properties and renderers.
66+
*
67+
* @param renderer A new {@link MapRenderer} instance you want to add.
68+
* @param mapView the existing {@link MapView} to wrap (non-null)
69+
*/
70+
public BuildMapView(@Nullable final MapRenderer renderer, @Nonnull final MapView mapView) {
6071
this.mapView = mapView;
6172
this.virtual = mapView.isVirtual();
6273
this.mapId = retrieveMapId(mapView);
6374
this.world = mapView.getWorld();
75+
this.renderer = renderer != null ? new MapRendererData(renderer) : new MapRendererData();
6476
}
6577

78+
6679
/**
6780
* Get the ID of this map item for use with {@link MapMeta}.
6881
*
@@ -153,81 +166,56 @@ public World getWorld() {
153166
}
154167

155168
/**
156-
* Get a list of MapRenderers currently set.
169+
* Get the set MapRenderer.
157170
*
158-
* @return A {@code List<MapRenderer>} containing each map renderer.
171+
* @return A {@link MapRendererData} containing each pixel set for the renderer.
159172
*/
160173
@Nonnull
161-
public List<MapRenderer> getRenderers() {
162-
if (renderers.isEmpty())
163-
return new ArrayList<>();
164-
return renderers.stream().map(MapRendererData::getMapRenderer).collect(Collectors.toList());
174+
public MapRendererData getRenderer() {
175+
return this.renderer;
165176
}
166177

167178
/**
168-
* Adds a map renderer with the specified renderer and applies configuration
169-
* via the given data consumer.
179+
* Get the set MapRenderer.
170180
*
171-
* @param renderer the {@link MapRenderer} instance to add
172-
* @param dataConsumer a consumer to configure the render data for this renderer
173-
* @return the {@link MapRendererData} instance representing the added renderer
181+
* @return A {@code MapRenderer} containing each pixel set for the renderer.
174182
*/
175-
public MapRendererData addRenderer(@Nonnull final MapRenderer renderer, @Nonnull final Consumer<MapRendererData> dataConsumer) {
176-
MapRendererData mapRenderer = new MapRendererData(renderer);
177-
dataConsumer.accept(mapRenderer);
178-
renderers.add(mapRenderer);
179-
return mapRenderer;
183+
@Nonnull
184+
public MapRenderer getMapRenderer() {
185+
return this.renderer.getMapRenderer();
180186
}
181187

182188
/**
183-
* Adds a map renderer without specifying a renderer instance, and applies
189+
* Give the specifying a renderer instance, and applies
184190
* configuration via the given data consumer.
185191
*
186192
* @param dataConsumer a consumer to configure the render data for the new renderer
187-
* @return the {@link MapRendererData} instance representing the added renderer
193+
* @return the {@link MapRendererData} instance representing the renderer
188194
*/
189-
public MapRendererData addRenderer(@Nonnull final Consumer<MapRendererData> dataConsumer) {
190-
final MapRendererData mapRenderer = new MapRendererData();
191-
dataConsumer.accept(mapRenderer);
192-
renderers.add(mapRenderer);
193-
return mapRenderer;
195+
public MapRendererData addRenderData(@Nonnull final Consumer<MapRendererData> dataConsumer) {
196+
dataConsumer.accept(this.renderer);
197+
return this.renderer;
194198
}
195199

196200
/**
197201
* Add your cached image data.
198202
*
199-
* @param cache the cache to get the image.
200-
*/
201-
public void addAllCachedRenderers(@Nonnull MapRendererDataCache cache) {
202-
this.renderers.addAll(cache.getAllRendererData());
203-
}
204-
/**
205-
* Adds a map list of renderer instance's.
206-
*
207-
* @param renderers the render data list to be added.
208-
*/
209-
public void addAllRenderers(@Nonnull final List<MapRenderer> renderers) {
210-
renderers.forEach(mapRenderer -> {
211-
this.renderers.add(new MapRendererData(mapRenderer));
212-
});
213-
}
214-
215-
/**
216-
* Remove a renderer from this map.
217-
*
218-
* @param renderer The MapRenderer to remove.
203+
* @param cacheId the id set for the image preprocessed you want to add to this map.
204+
* @param cache the cache to get the image.
219205
*/
220-
public void removeRenderer(@Nonnull final MapRendererData renderer) {
221-
renderers.removeIf(mapRenderer -> mapRenderer.getMapRenderId() == renderer.getMapRenderId());
206+
public void addCachedPixels(final int cacheId, @Nonnull MapRendererDataCache cache) {
207+
final MapRendererDataCache.PixelCacheEntry mapRendererData = cache.get(cacheId);
208+
if (mapRendererData == null)
209+
return;
210+
this.renderer.getPixels().addAll(mapRendererData.getPixels());
222211
}
223212

224213
/**
225-
* Remove a renderer from this map.
214+
* Remove the pixels set from this MapRendererData instance.
226215
*
227-
* @param mapRenderId The id of the MapRendererData to remove.
228216
*/
229-
public void removeRenderer(final int mapRenderId) {
230-
renderers.removeIf(mapRenderer -> mapRenderer.getMapRenderId() == mapRenderId);
217+
public void clearPixels() {
218+
this.renderer.clear();
231219
}
232220

233221
/**
@@ -314,14 +302,11 @@ public MapView finalizeMapView() {
314302
if (ItemCreator.getServerVersion() > 10.2F)
315303
mapView.setUnlimitedTracking(unlimited);
316304

317-
if (!renderers.isEmpty()) {
318-
305+
if (!renderer.isPixelsEmpty()) {
319306
for (MapRenderer renderer : mapView.getRenderers()) {
320307
mapView.removeRenderer(renderer);
321308
}
322-
for (MapRendererData data : renderers) {
323-
mapView.addRenderer(data.getMapRenderer());
324-
}
309+
mapView.addRenderer(this.getMapRenderer());
325310
}
326311
return mapView;
327312
}
@@ -358,7 +343,7 @@ public String toString() {
358343
string.appendFieldRecursive("world", (world == null ? "" : world.getName()), 1);
359344
string.appendFieldRecursive("x", getCenterX(), 1);
360345
string.appendFieldRecursive("z", getCenterZ(), 1);
361-
string.appendFieldRecursive("renderers", renderers, 1);
346+
string.appendFieldRecursive("renderer", renderer, 1);
362347

363348
return string.finalizeString() + "";
364349
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/MapRendererData.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
* the image as a background and the text above it.
5050
* </p>
5151
*
52-
* <p>
53-
* When using {@link MapRendererDataCache}, all overlays are preprocessed and
54-
* returned in the deterministic order produced by the cache. In this mode,
55-
* layering is controlled by the cache pipeline. Accessing the underlying
56-
* {@link MapRendererData} from the cache via {@link MapRendererDataCache#get(int)} is possible,
57-
* but adding new data directly is not recommended, as it could alter the
58-
* intended overlay order.
59-
* </p>
6052
*/
6153
public class MapRendererData {
6254
private static int id;
@@ -196,7 +188,7 @@ public void addText(@Nonnull final TextOverlay textOverlay) {
196188
* and inserts it into the pixel list. Only basic scaling is performed at this
197189
* stage. Advanced preprocessing—such as color balancing, palette matching,
198190
* or pixel extraction—is applied only when using {@link MapRendererDataCache}
199-
* together with {@link BuildMapView#addAllCachedRenderers(MapRendererDataCache)}.
191+
* together with {@link BuildMapView#addCachedPixels(int, MapRendererDataCache)}.
200192
* </p>
201193
*
202194
* <p>When using the cache, all images are preprocessed asynchronously and then
@@ -316,6 +308,15 @@ public void clear() {
316308
this.pixels.clear();
317309
}
318310

311+
/**
312+
* Returns {@code true} if this list contains no elements.
313+
*
314+
* @return {@code true} if this list of pixels contains no elements
315+
*/
316+
public boolean isPixelsEmpty() {
317+
return this.pixels.isEmpty();
318+
}
319+
319320
/**
320321
* Applies the global {@link ColorParser} to the text at the specified index.
321322
* <p>

0 commit comments

Comments
 (0)