11package org .broken .arrow .library .itemcreator .meta ;
22
3+ import org .broken .arrow .library .itemcreator .ItemCreator ;
34import org .broken .arrow .library .itemcreator .meta .map .BuildMapView ;
5+ import org .bukkit .Bukkit ;
46import org .bukkit .World ;
7+ import org .bukkit .inventory .ItemStack ;
58import org .bukkit .inventory .meta .ItemMeta ;
69import org .bukkit .inventory .meta .MapMeta ;
710import org .bukkit .map .MapView ;
811
912import javax .annotation .Nonnull ;
1013import javax .annotation .Nullable ;
14+ import java .util .function .Consumer ;
1115
1216/**
1317 * A wrapper class for managing a {@link BuildMapView} and applying it to item metadata.
@@ -25,45 +29,133 @@ public class MapWrapperMeta {
2529 */
2630 @ Nullable
2731 public MapView getMapView () {
32+ if (this .mapView == null )
33+ return null ;
2834 return this .mapView .build ();
2935 }
3036
37+ /**
38+ * Returns the {@link BuildMapView} that also populate the {@link MapView}.
39+ *
40+ * @return The built {@link BuildMapView}, or null if no {@code BuildMapView} is set.
41+ */
42+ @ Nullable
43+ public BuildMapView getMapViewBuilder () {
44+ this .getMapView ();
45+ return this .mapView ;
46+ }
47+
3148 /**
3249 * Creates and sets a new {@link BuildMapView} instance based on the given world.
3350 *
3451 * @param world the {@link World} from which to create a new map view (non-null)
3552 * @return the newly created {@link BuildMapView} instance.
3653 */
3754 public BuildMapView createMapView (@ Nonnull final World world ) {
38- this .mapView = new BuildMapView ( world );
39- return this . mapView ;
55+ return this .createOrRetrieveMapView ( world , - 1 , ( view ) -> {
56+ }) ;
4057 }
4158
4259 /**
43- * Creates and sets a new {@link BuildMapView} instance by wrapping the provided {@link MapView}.
60+ * Creates and sets a new {@link BuildMapView} instance, allowing configuration via a lambda.
61+ * <p>
62+ * This method always creates a new map using the provided {@link World}.
63+ * </p>
4464 *
45- * @param mapView the existing {@link MapView} to wrap (non-null)
46- * @return the newly created {@link BuildMapView} instance wrapping the given map view.
65+ * @param world the world to associate the map with. This does not affect the rendering of the map.
66+ * @param action a consumer to configure the resulting {@link BuildMapView}.
67+ * @return the created {@link BuildMapView}.
68+ */
69+ public BuildMapView createMapView (@ Nonnull final World world , @ Nonnull final Consumer <BuildMapView > action ) {
70+ return this .createOrRetrieveMapView (world , -1 , action );
71+ }
72+
73+ /**
74+ * Attempts to retrieve an existing map by its ID and wraps it in a {@link BuildMapView}.
75+ * <p>
76+ * Unlike {@link #createOrRetrieveMapView(World, int, Consumer)} and {@link #createMapView(World, Consumer)},
77+ * the difference is that the first method always creates a new map when one cannot be found, while the second
78+ * always creates a new map regardless. This method, however, does not create a new map if the ID is missing,
79+ * it simply returns {@code null}.
80+ * </p>
81+ *
82+ * @param id the map ID to retrieve can't be below zero.
83+ * @param action a consumer to configure the resulting {@link BuildMapView}, if found.
84+ * @return the retrieved {@link BuildMapView}, or {@code null} if no map exists for the given ID.
85+ */
86+ @ Nullable
87+ public BuildMapView getExistingMapView (final int id , @ Nonnull final Consumer <BuildMapView > action ) {
88+ return this .createOrRetrieveMapView (null , id , action );
89+ }
90+
91+ /**
92+ * Creates or retrieves a {@link BuildMapView} instance, allowing configuration via a lambda.
93+ * <p>
94+ * If {@code id >= 0}, this method attempts to retrieve an existing map with that ID.
95+ * If no such map exists or {@code id < 0}, a new map is created using the provided {@link World}.
96+ * </p>
97+ *
98+ * @param world the world to associate the map with. This does not affect the rendering of the map.
99+ * May be {@code null} only when {@code id} is provided and exists.
100+ * @param id the map ID to retrieve, or -1 to create a new one.
101+ * @param action a consumer to configure the resulting {@link BuildMapView}.
102+ * @return the created or retrieved {@link BuildMapView}, or {@code null} if retrieval failed and no world was provided.
47103 */
48- public BuildMapView createMapView (@ Nonnull final MapView mapView ) {
104+ @ Nullable
105+ public BuildMapView createOrRetrieveMapView (@ Nullable final World world , final int id , @ Nonnull final Consumer <BuildMapView > action ) {
106+ MapView mapView = null ;
107+
108+ if (id >= 0 ) {
109+ mapView = ItemCreator .getMapById (id );
110+ }
111+ if (mapView == null ) {
112+ if (world == null ) return null ;
113+ mapView = Bukkit .createMap (world );
114+ }
115+
49116 this .mapView = new BuildMapView (mapView );
117+ action .accept (this .mapView );
50118 return this .mapView ;
51119 }
52120
121+ /**
122+ * It is using your crated {@link BuildMapView} instance by wrapping the provided {@link MapView}.
123+ *
124+ * @param buildMapView new instance of {@link BuildMapView} to wrap (non-null)
125+ * @return the newly created {@link BuildMapView} instance wrapping the given map view.
126+ */
127+ public BuildMapView createMapView (@ Nonnull final BuildMapView buildMapView ) {
128+ this .mapView = buildMapView ;
129+ return buildMapView ;
130+ }
131+
53132 /**
54133 * Applies the stored {@link BuildMapView} to the given {@link ItemMeta} if it is a {@link MapMeta}.
55134 *
56135 * <p>If the {@code itemMeta} is not an instance of {@link MapMeta}, this method does nothing.
57136 * Otherwise, it builds the {@link MapView} from the stored {@link BuildMapView} and sets it on the {@link MapMeta}.</p>
58137 *
138+ * @param item the itemStack to apply the data.
59139 * @param itemMeta The {@link ItemMeta} to apply the map view to.
60140 */
61- public void applyMapMeta (@ Nonnull final ItemMeta itemMeta ) {
141+ public void applyMapMeta (final ItemStack item , @ Nonnull final ItemMeta itemMeta ) {
62142 if (!(itemMeta instanceof MapMeta )) return ;
63143 final MapMeta mapMeta = (MapMeta ) itemMeta ;
144+
145+ if (ItemCreator .getServerVersion () < 13.0F ) {
146+ final BuildMapView mapViewBuilder = getMapViewBuilder ();
147+ short durability = mapViewBuilder == null ? -1 : (short ) mapViewBuilder .getId ();
148+ if (durability >= 0 ) {
149+ item .setDurability (durability );
150+ }
151+ return ;
152+ }
153+
64154 if (mapView != null ) {
65155 MapView builtMap = mapView .build ();
66156 mapMeta .setMapView (builtMap );
67157 }
68158 }
159+
160+
69161}
0 commit comments