Skip to content

Commit 002042b

Browse files
committed
Improve comparability with legacy
Also added option to retrieve the wrapped type, to give better potion support.
1 parent 1f533f7 commit 002042b

File tree

9 files changed

+100
-35
lines changed

9 files changed

+100
-35
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ public PotionType getPotionType() {
139139
return (this.potionTypeWrapper == null ? null : this.potionTypeWrapper.getPotionType());
140140
}
141141

142+
/**
143+
* Gets the predefined {@link PotionType} to apply to the potion, if any.
144+
* <p>
145+
* If a type is set, it overrides any custom potion effects and color.
146+
*
147+
* @return the safe potion type name, or {@code null} if using custom effects instead
148+
*/
149+
@Nullable
150+
public String getPotionName() {
151+
return (this.potionTypeWrapper == null ? null : this.potionTypeWrapper.name());
152+
}
153+
142154
/**
143155
* Sets the predefined {@link PotionTypeWrapper} to apply to this potion item.
144156
* <p>

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class MapWrapperMeta {
3131
public MapView getMapView() {
3232
if (this.mapView == null)
3333
return null;
34-
return this.mapView.build();
34+
return this.mapView.finilazeMapView();
3535
}
3636

3737
/**
@@ -143,7 +143,7 @@ public void applyMapMeta(final ItemStack item, @Nonnull final ItemMeta itemMeta)
143143
final MapMeta mapMeta = (MapMeta) itemMeta;
144144

145145
if (ItemCreator.getServerVersion() < 13.0F) {
146-
final BuildMapView mapViewBuilder = getMapViewBuilder();
146+
final BuildMapView mapViewBuilder = this.getMapViewBuilder();
147147
short durability = mapViewBuilder == null ? -1 : (short) mapViewBuilder.getId();
148148
if (durability >= 0) {
149149
item.setDurability(durability);
@@ -152,10 +152,13 @@ public void applyMapMeta(final ItemStack item, @Nonnull final ItemMeta itemMeta)
152152
}
153153

154154
if (mapView != null) {
155-
MapView builtMap = mapView.build();
155+
MapView builtMap = mapView.finilazeMapView();
156156
mapMeta.setMapView(builtMap);
157157
}
158158
}
159159

160-
160+
@Override
161+
public String toString() {
162+
return mapView + "";
163+
}
161164
}

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public class BuildMapView {
3232
private final World world;
3333
private final Set<MapRendererData> renderers = new HashSet<>();
3434

35+
private final boolean virtual;
3536
private boolean locked;
3637
private boolean unlimited;
3738
private boolean trackingPosition;
38-
private boolean virtual;
39-
40-
private int mapId;
4139

40+
private final int mapId;
4241

4342
/**
4443
* Constructs a new {@code BuildMapView} for the given {@link World},
@@ -47,10 +46,7 @@ public class BuildMapView {
4746
* @param world the {@link World} where the map view is created (non-null)
4847
*/
4948
public BuildMapView(@Nonnull final World world) {
50-
this.mapView = Bukkit.createMap(world);
51-
this.virtual = mapView.isVirtual();
52-
this.mapId = retrieveMapId(mapView);
53-
this.world = mapView.getWorld();
49+
this(Bukkit.createMap(world));
5450
}
5551

5652
/**
@@ -62,6 +58,7 @@ public BuildMapView(@Nonnull final World world) {
6258
public BuildMapView(@Nonnull final MapView mapView) {
6359
this.mapView = mapView;
6460
this.virtual = mapView.isVirtual();
61+
this.mapId = retrieveMapId(mapView);
6562
this.world = mapView.getWorld();
6663
}
6764

@@ -195,6 +192,17 @@ public MapRendererData addRenderer(@Nonnull final Consumer<MapRendererData> data
195192
return mapRenderer;
196193
}
197194

195+
/**
196+
* Adds a map list of renderer instance's.
197+
*
198+
* @param renderers the render data list to be added.
199+
*/
200+
public void addAllRenderers(@Nonnull final List<MapRenderer> renderers) {
201+
renderers.forEach(mapRenderer -> {
202+
this.renderers.add(new MapRendererData(mapRenderer));
203+
});
204+
}
205+
198206
/**
199207
* Remove a renderer from this map.
200208
*
@@ -280,7 +288,7 @@ public void setLocked(final boolean locked) {
280288
*
281289
* @return a new MapView instance with your settings.
282290
*/
283-
public MapView build() {
291+
public MapView finilazeMapView() {
284292
if (world == null)
285293
throw new IllegalStateException("World must be set before building MapView.");
286294

@@ -298,8 +306,8 @@ public MapView build() {
298306
for (MapRenderer renderer : mapView.getRenderers()) {
299307
mapView.removeRenderer(renderer);
300308
}
301-
302-
// Add renderers from your data
309+
System.out.println("mapId " + mapId);
310+
System.out.println("mapView.getRenderers() " + renderers.size());
303311
if (!renderers.isEmpty()) {
304312
for (MapRendererData data : renderers) {
305313
mapView.addRenderer(data.getMapRenderer());
@@ -332,5 +340,16 @@ public static int retrieveMapId(@Nonnull final MapView mapView) {
332340
return -1;
333341
}
334342
}
343+
344+
@Override
345+
public String toString() {
346+
return "id: " + getId() +
347+
",\n world: " + (world == null ? "" : world.getName()) +
348+
",\n x: " + getCenterX() +
349+
",\n z: " + getCenterZ() +
350+
",\n renderers:" + renderers
351+
;
352+
}
353+
335354
}
336355

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,18 @@ public List<MapPixel> getPixels() {
226226
* @return The {@link MapRenderer} instance.
227227
*/
228228
public MapRenderer getMapRenderer() {
229-
if (this.mapRenderer != null)
229+
/* if (this.mapRenderer != null) {
230+
mapRenderer.render();
230231
return mapRenderer;
232+
}*/
231233
return new MapRenderer() {
232234
@Override
233235
public void render(@Nonnull MapView map, @Nonnull MapCanvas canvas, @Nonnull Player player) {
234236
if (dynamicRenderer != null && dynamicRenderer.render(map, canvas, player))
235237
return;
238+
if (mapRenderer != null) {
239+
mapRenderer.render(map, canvas, player);
240+
}
236241
canvas.setCursors(mapCursors.getMapCursorCollection());
237242
if (!getPixels().isEmpty()) {
238243
setPixels(canvas);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ public MapCursorWrapper(final byte x, final byte y, final byte direction, final
4848
public MapCursorWrapper(final byte x, final byte y, final byte direction, @Nonnull final MapCursor.Type type, final boolean visible, @Nullable final String caption) {
4949
this.cursorId = id++;
5050
this.rawType = retrieveRawType(type);
51-
if (ItemCreator.getServerVersion() < 12.1f)
52-
this.cursor = new MapCursor(x, y, direction, rawType, visible, caption);
51+
if (ItemCreator.getServerVersion() < 13.0f) {
52+
if (ItemCreator.getServerVersion() < 12.0f)
53+
this.cursor = new MapCursor(x, y, direction, rawType, visible, caption);
54+
else
55+
this.cursor = new MapCursor(x, y, direction, rawType, visible);
56+
}
5357
else
5458
this.cursor = new MapCursor(x, y, direction, type, visible, caption);
5559
setDirection(direction);

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/potion/PotionTypeWrapper.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,25 @@ public enum PotionTypeWrapper {
172172
/**
173173
* Turtle Master potion, normal duration.
174174
*/
175-
TURTLE_MASTER(PotionType.TURTLE_MASTER, PotionModifier.NORMAL),
175+
TURTLE_MASTER(getPotionType("TURTLE_MASTER"), PotionModifier.NORMAL),
176176
/**
177177
* Turtle Master potion, long duration.
178178
*/
179-
LONG_TURTLE_MASTER(PotionType.TURTLE_MASTER, PotionModifier.LONG),
179+
LONG_TURTLE_MASTER(getPotionType("TURTLE_MASTER"), PotionModifier.LONG),
180180
/**
181181
* Turtle Master potion, strong effect.
182182
*/
183-
STRONG_TURTLE_MASTER(PotionType.TURTLE_MASTER, PotionModifier.STRONG),
183+
STRONG_TURTLE_MASTER(getPotionType("TURTLE_MASTER"), PotionModifier.STRONG),
184184
/**
185185
* Slow Falling potion, normal duration.
186186
*/
187-
SLOW_FALLING(PotionType.SLOW_FALLING, PotionModifier.NORMAL),
187+
SLOW_FALLING(getPotionType("SLOW_FALLING"), PotionModifier.NORMAL),
188188
/**
189189
* Slow Falling potion, long duration.
190190
*/
191-
LONG_SLOW_FALLING(PotionType.SLOW_FALLING, PotionModifier.LONG);
191+
LONG_SLOW_FALLING(getPotionType("SLOW_FALLING"), PotionModifier.LONG);
192192

193-
private static final Map<String, PotionTypeWrapper> POTION_TYPE_NAME = new HashMap<>();
193+
private static final Map<String, PotionTypeWrapper> POTION_TYPE_NAME = loadPotions();
194194
private final PotionType potionType;
195195
private final PotionModifier potionModifier;
196196
private final float serverVersion = ItemCreator.getServerVersion();
@@ -205,12 +205,15 @@ public enum PotionTypeWrapper {
205205
PotionTypeWrapper(@Nonnull final PotionType potionType, @Nonnull final PotionModifier potionModifier) {
206206
this.potionType = potionType;
207207
this.potionModifier = potionModifier;
208+
208209
}
209210

210-
static {
211+
private static Map<String, PotionTypeWrapper> loadPotions() {
212+
Map<String, PotionTypeWrapper> typeWrapperHashMap = new HashMap<>();
211213
for (PotionTypeWrapper data : values()) {
212-
POTION_TYPE_NAME.put(data.getPotionType().name(), data);
214+
typeWrapperHashMap.put(data.getPotionType().name(), data);
213215
}
216+
return typeWrapperHashMap ;
214217
}
215218

216219
/**
@@ -242,14 +245,16 @@ public PotionType getPotionType() {
242245
*/
243246
@Nullable
244247
public static PotionTypeWrapper findPotionByType(final PotionType bukkitPotionType) {
248+
if(bukkitPotionType == null)
249+
return null;
245250
return POTION_TYPE_NAME.get(bukkitPotionType.name());
246251
}
247252

248253
/**
249254
* Finds the {@link PotionTypeWrapper} associated with the given potion name.
250255
* <p>
251256
* This method supports both modern and legacy potion naming. If the provided name matches
252-
* an entry in {@code POTION_TYPE_NAME}, that entry will be returned first.
257+
* an entry in {@code POTION_TYPE_NAME} cache, that entry will be returned first.
253258
* Otherwise, it attempts to resolve the name using {@link PotionTypeWrapper#valueOf(String)}.
254259
* </p>
255260
*
@@ -264,7 +269,6 @@ public static PotionTypeWrapper findPotionByType(final PotionType bukkitPotionTy
264269
@Nullable
265270
public static PotionTypeWrapper findPotionByName(final String name) {
266271
if (name == null) return null;
267-
268272
String bukkitPortion = name.toUpperCase();
269273
PotionTypeWrapper potionTypeWrapper = POTION_TYPE_NAME.get(bukkitPortion);
270274
if (potionTypeWrapper != null)
@@ -377,8 +381,19 @@ private PotionType getPotionMapping() {
377381
*/
378382
@Nonnull
379383
private static PotionType getUncraftable() {
380-
PotionType potion = findPotionTypeByName("UNCRAFTABLE");
381-
return potion != null ? potion : PotionType.MUNDANE;
384+
try {
385+
return PotionType.valueOf("UNCRAFTABLE");
386+
} catch (IllegalArgumentException | NoSuchFieldError ex) {
387+
return PotionType.MUNDANE;
388+
}
389+
}
390+
391+
private static PotionType getPotionType(String name) {
392+
try {
393+
return PotionType.valueOf(name);
394+
} catch (IllegalArgumentException | NoSuchFieldError ignored) {
395+
return getUncraftable();
396+
}
382397
}
383398

384399
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/itemstack/SerializeItem.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public ItemStack toItemStack() {
154154

155155
if (meta instanceof MapMeta && mapViewMeta != null) {
156156
MapMeta fwm = (MapMeta) meta;
157-
mapViewMeta.applyMapMeta(item,fwm);
157+
mapViewMeta.applyMapMeta(item, fwm);
158158
}
159159

160160
item.setItemMeta(meta);
@@ -513,15 +513,21 @@ private static void retrieveMapData(@Nonnull final ItemStack item, @Nonnull fina
513513
final MapView mapView = ItemCreator.getMapById(durability);
514514
if (mapView == null) return;
515515
final MapWrapperMeta mapMetaWrapper = new MapWrapperMeta();
516-
mapMetaWrapper.createMapView(new BuildMapView(mapView));
516+
final BuildMapView buildMapView = new BuildMapView(mapView);
517+
buildMapView.addAllRenderers(mapView.getRenderers());
518+
//buildMapView.finilazeMapView();
519+
mapMetaWrapper.createMapView(buildMapView);
517520
data.mapViewMeta = mapMetaWrapper;
518521
}
519522
} else {
520523
if (mapMeta.hasMapView()) {
521524
final MapView mapView = mapMeta.getMapView();
522525
if (mapView != null) {
523526
final MapWrapperMeta mapMetaWrapper = new MapWrapperMeta();
524-
mapMetaWrapper.createMapView(new BuildMapView(mapView));
527+
final BuildMapView buildMapView = new BuildMapView(mapView);
528+
buildMapView.addAllRenderers(mapView.getRenderers());
529+
// buildMapView.finilazeMapView();
530+
mapMetaWrapper.createMapView(buildMapView);
525531
data.mapViewMeta = mapMetaWrapper;
526532
}
527533
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/typeadapter/BottleEffectMetaAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public void write(final JsonWriter out, final BottleEffectMeta value) throws IOE
3737
return;
3838
}
3939
final JsonWriterHelper json = new JsonWriterHelper(out);
40-
final PotionType potionType = value.getPotionType();
40+
final String potionType = value.getPotionName();
4141

42-
json.value("potion_type", potionType != null ? potionType.name() : "");
42+
json.value("potion_type", potionType != null ? potionType : "");
4343
json.value("is_water_bottle", value.isWaterBottle());
4444
json.value("is_upgraded", value.isUpgraded());
4545
json.value("is_extended", value.isExtended());

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/serialization/typeadapter/MapMetaAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.TypeAdapter;
44
import com.google.gson.stream.JsonReader;
55
import com.google.gson.stream.JsonWriter;
6+
import org.broken.arrow.library.itemcreator.ItemCreator;
67
import org.broken.arrow.library.itemcreator.meta.MapWrapperMeta;
78
import org.broken.arrow.library.itemcreator.meta.map.BuildMapView;
89
import org.broken.arrow.library.itemcreator.serialization.jsonhelper.JsonReaderHelper;
@@ -105,7 +106,7 @@ public MapWrapperMeta read(final JsonReader in) throws IOException {
105106
});
106107
final World world = mapViewValues.world;
107108
if(world != null) {
108-
MapView mapView = Bukkit.getMap(mapViewValues.id);
109+
MapView mapView = ItemCreator.getMapById(mapViewValues.id);
109110
if (mapView == null) mapView = Bukkit.createMap(world);
110111
BuildMapView buildMapView = new BuildMapView(mapView);
111112
buildMapView.setCenterX(mapViewValues.centerX);

0 commit comments

Comments
 (0)