Skip to content

Commit 21491be

Browse files
small rework on the constructors
1 parent 24d3ec6 commit 21491be

1 file changed

Lines changed: 67 additions & 43 deletions

File tree

core/src/main/java/com/wizardlybump17/wlib/item/ItemBuilder.java

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,61 +41,79 @@
4141
@SerializableAs("item-builder")
4242
public class ItemBuilder implements ConfigurationSerializable, Cloneable {
4343

44-
private @NotNull Material type;
45-
private int amount;
46-
private final @NotNull Map<Object, Object> customData;
44+
private @NotNull Material type = Material.AIR;
45+
private int amount = 1;
46+
private final @NotNull Map<Object, Object> customData = new TreeMap<>();
4747
private @Nullable ItemMetaHandler<?> metaHandler;
4848
private @Nullable ItemMeta itemMeta;
4949

50-
public ItemBuilder(@NotNull Material type, int amount, @NotNull Map<Object, Object> customData, @Nullable ItemMeta itemMeta) {
51-
this.type = type;
52-
this.amount = amount;
53-
this.customData = customData;
54-
55-
ItemMetaHandlerModel<?> metaHandlerModel = ItemMetaHandlerModel.getApplicableModel(type);
56-
if (metaHandlerModel != null)
57-
this.metaHandler = metaHandlerModel.createHandler(this);
58-
59-
this.itemMeta = itemMeta;
50+
public ItemBuilder() {
6051
}
6152

62-
public ItemBuilder(@NotNull Material type, int amount, @NotNull Map<Object, Object> customData) {
63-
this(type, amount, customData, null);
53+
public ItemBuilder(@NotNull Material type) {
54+
this.type = type;
55+
itemMeta = Bukkit.getItemFactory().getItemMeta(type);
56+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
57+
.map(model -> model.createHandler(itemMeta))
58+
.orElse(null);
6459
}
6560

6661
public ItemBuilder(@NotNull Material type, int amount) {
67-
this(type, amount, new HashMap<>());
62+
this.type = type;
63+
this.amount = amount;
64+
itemMeta = Bukkit.getItemFactory().getItemMeta(type);
65+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
66+
.map(model -> model.createHandler(itemMeta))
67+
.orElse(null);
6868
}
6969

70-
public ItemBuilder(@NotNull Material type) {
71-
this(type, 1);
70+
public ItemBuilder(@NotNull Material type, int amount, @NotNull Map<Object, Object> customData) {
71+
this.type = type;
72+
this.amount = amount;
73+
itemMeta = Bukkit.getItemFactory().getItemMeta(type);
74+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
75+
.map(model -> model.createHandler(itemMeta))
76+
.orElse(null);
77+
this.customData.putAll(customData);
7278
}
7379

74-
public ItemBuilder(@Nullable ItemStack item, @NotNull Map<Object, Object> customData) {
75-
this(
76-
item == null ? Material.AIR : item.getType(),
77-
item == null ? 1 : item.getAmount(),
78-
customData,
79-
item == null ? null : item.getItemMeta()
80-
);
80+
public ItemBuilder(@NotNull Material type, int amount, @NotNull Map<Object, Object> customData, @Nullable ItemMeta itemMeta) {
81+
this.type = type;
82+
this.amount = amount;
83+
this.itemMeta = itemMeta;
84+
if (itemMeta != null) {
85+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
86+
.map(model -> model.createHandler(itemMeta))
87+
.orElse(null);
88+
}
89+
this.customData.putAll(customData);
8190
}
8291

83-
public ItemBuilder(@Nullable ItemStack item) {
84-
this(
85-
item == null ? Material.AIR : item.getType(),
86-
item == null ? 1 : item.getAmount(),
87-
new HashMap<>(),
88-
item == null ? null : item.getItemMeta()
89-
);
92+
/**
93+
* @deprecated use {@link #fromItemStack(ItemStack)} instead
94+
*/
95+
@Deprecated(forRemoval = true)
96+
public ItemBuilder(@NotNull ItemStack item) {
97+
type = item.getType();
98+
amount = item.getAmount();
99+
itemMeta = item.getItemMeta();
100+
if (itemMeta != null) {
101+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
102+
.map(model -> model.createHandler(itemMeta))
103+
.orElse(null);
104+
}
90105
}
91106

92-
public ItemBuilder() {
93-
this(
94-
Material.AIR,
95-
1,
96-
new HashMap<>(),
97-
null
98-
);
107+
public ItemBuilder(@NotNull ItemStack item, @NotNull Map<Object, Object> customData) {
108+
type = item.getType();
109+
amount = item.getAmount();
110+
itemMeta = item.getItemMeta();
111+
if (itemMeta != null) {
112+
metaHandler = ItemMetaHandlerModel.getApplicableModelOptional(type)
113+
.map(model -> model.createHandler(itemMeta))
114+
.orElse(null);
115+
}
116+
this.customData.putAll(customData);
99117
}
100118

101119
@SuppressWarnings("unchecked")
@@ -145,7 +163,7 @@ public ItemBuilder type(@NonNull Material material) {
145163
itemMeta = itemMeta == null ? itemFactory.getItemMeta(type) : itemFactory.asMetaFor(itemMeta, type);
146164

147165
ItemMetaHandlerModel<?> model = ItemMetaHandlerModel.getApplicableModel(type);
148-
metaHandler = model == null ? null : model.createHandler(this);
166+
metaHandler = model == null || itemMeta == null ? null : model.createHandler(itemMeta);
149167

150168
return this;
151169
}
@@ -495,7 +513,7 @@ public ItemBuilder clone() {
495513
return new ItemBuilder(
496514
type,
497515
amount,
498-
new HashMap<>(customData),
516+
customData,
499517
itemMeta == null ? null : itemMeta.clone()
500518
);
501519
}
@@ -521,7 +539,13 @@ public ItemBuilder clone() {
521539
* @return a new builder with the data from the given item
522540
*/
523541
public static ItemBuilder fromItemStack(@Nullable ItemStack item) {
524-
return new ItemBuilder(item, new HashMap<>());
542+
if (item == null)
543+
return empty();
544+
return new ItemBuilder(item.getType(), item.getAmount(), Map.of(), item.getItemMeta());
545+
}
546+
547+
public static @NotNull ItemBuilder empty() {
548+
return new ItemBuilder();
525549
}
526550

527551
public static ItemBuilder deserialize(Map<String, Object> map) {
@@ -559,7 +583,7 @@ public static ItemBuilder deserialize(Map<String, Object> map) {
559583
.ifPresent(result::itemCustomData);
560584

561585
ItemMetaHandlerModel<?> metaHandlerModel = ItemMetaHandlerModel.getApplicableModel(result.type());
562-
result.metaHandler(metaHandlerModel == null ? null : metaHandlerModel.createHandler(result));
586+
result.metaHandler(metaHandlerModel == null || result.itemMeta == null ? null : metaHandlerModel.createHandler(result.itemMeta));
563587

564588
if (result.metaHandler != null)
565589
result.metaHandler.deserialize(map);

0 commit comments

Comments
 (0)