Skip to content

Commit 818e6ac

Browse files
Merge branch 'item-builder-small-rework' into new-command-system-1.20.6-2
# Conflicts: # core/src/main/java/com/wizardlybump17/wlib/WLib.java # gradle/wrapper/gradle-wrapper.properties
2 parents e49c5df + 21491be commit 818e6ac

11 files changed

Lines changed: 251 additions & 131 deletions

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);
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
11
package com.wizardlybump17.wlib.item.handler;
22

3-
import com.wizardlybump17.wlib.item.ItemBuilder;
43
import com.wizardlybump17.wlib.item.handler.model.FireworkMetaHandlerModel;
54
import org.bukkit.FireworkEffect;
65
import org.bukkit.inventory.meta.FireworkMeta;
6+
import org.jetbrains.annotations.NotNull;
77

8-
import java.util.Collections;
98
import java.util.List;
109
import java.util.Map;
1110

1211
public class FireworkMetaHandler extends ItemMetaHandler<FireworkMetaHandlerModel> {
1312

14-
public FireworkMetaHandler(FireworkMetaHandlerModel model, ItemBuilder builder) {
15-
super(model, builder);
13+
public FireworkMetaHandler(FireworkMetaHandlerModel model, @NotNull FireworkMeta itemMeta) {
14+
super(model, itemMeta);
15+
}
16+
17+
@Override
18+
public @NotNull FireworkMeta getItemMeta() {
19+
return (FireworkMeta) super.getItemMeta();
1620
}
1721

1822
@Override
1923
public void serialize(Map<String, Object> map) {
20-
List<FireworkEffect> effects = getBuilder().getFromMeta(FireworkMeta::getEffects, Collections.emptyList());
21-
map.put("effects", effects.isEmpty() ? null : effects);
22-
int power = getBuilder().<Integer, FireworkMeta>getFromMeta(FireworkMeta::getPower, 0);
23-
map.put("power", power == 0 ? null : power);
24+
FireworkMeta itemMeta = getItemMeta();
25+
26+
List<FireworkEffect> effects = itemMeta.getEffects();
27+
if (!effects.isEmpty())
28+
map.put("effects", effects);
29+
30+
int power = itemMeta.getPower();
31+
map.put("power", power);
2432
}
2533

26-
@SuppressWarnings("unchecked")
2734
@Override
2835
public void deserialize(Map<String, Object> map) {
29-
getBuilder().<FireworkMeta>consumeMeta(meta -> {
30-
meta.addEffects((List<FireworkEffect>) map.getOrDefault("effects", Collections.emptyList()));
31-
meta.setPower((Integer) map.getOrDefault("power", 0));
32-
});
36+
FireworkMeta itemMeta = getItemMeta();
37+
38+
itemMeta.addEffects((FireworkEffect) map.getOrDefault("effects", List.of()));
39+
itemMeta.setPower((int) map.getOrDefault("power", 0));
3340
}
3441

3542
public FireworkMetaHandler effects(FireworkEffect... effects) {
36-
getBuilder().<FireworkMeta>consumeMeta(meta -> meta.addEffects(effects));
43+
FireworkMeta itemMeta = getItemMeta();
44+
itemMeta.clearEffects();
45+
itemMeta.addEffects(effects);
3746
return this;
3847
}
3948

4049
public FireworkMetaHandler power(int power) {
41-
getBuilder().<FireworkMeta>consumeMeta(meta -> meta.setPower(power));
50+
getItemMeta().setPower(power);
4251
return this;
4352
}
4453

4554
public FireworkMetaHandler clearEffects() {
46-
getBuilder().consumeMeta(FireworkMeta::clearEffects);
55+
getItemMeta().clearEffects();
4756
return this;
4857
}
4958

5059
public List<FireworkEffect> effects() {
51-
return getBuilder().getFromMeta(FireworkMeta::getEffects, Collections.emptyList());
60+
return getItemMeta().getEffects();
5261
}
5362

5463
public int power() {
55-
return getBuilder().<Integer, FireworkMeta>getFromMeta(FireworkMeta::getPower, 0);
64+
return getItemMeta().getPower();
5665
}
5766
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package com.wizardlybump17.wlib.item.handler;
22

3-
import com.wizardlybump17.wlib.item.ItemBuilder;
43
import com.wizardlybump17.wlib.item.handler.model.ItemMetaHandlerModel;
54
import lombok.Data;
5+
import org.bukkit.inventory.meta.ItemMeta;
6+
import org.jetbrains.annotations.NotNull;
67

78
import java.util.Map;
89

910
@Data
1011
public abstract class ItemMetaHandler<M extends ItemMetaHandlerModel<?>> {
1112

1213
private final M model;
13-
private final ItemBuilder builder;
14+
private final @NotNull ItemMeta itemMeta;
1415

1516
public abstract void serialize(Map<String, Object> map);
1617

1718
public abstract void deserialize(Map<String, Object> map);
19+
20+
public @NotNull ItemMeta getItemMeta() {
21+
return itemMeta;
22+
}
1823
}

core/src/main/java/com/wizardlybump17/wlib/item/handler/LeatherArmorMetaHandler.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
package com.wizardlybump17.wlib.item.handler;
22

3-
import com.wizardlybump17.wlib.item.ItemBuilder;
43
import com.wizardlybump17.wlib.item.handler.model.LeatherArmorMetaHandlerModel;
54
import org.bukkit.Color;
65
import org.bukkit.inventory.meta.LeatherArmorMeta;
6+
import org.jetbrains.annotations.NotNull;
77

88
import java.util.Map;
99

1010
public class LeatherArmorMetaHandler extends ItemMetaHandler<LeatherArmorMetaHandlerModel> {
1111

12-
public LeatherArmorMetaHandler(LeatherArmorMetaHandlerModel model, ItemBuilder builder) {
13-
super(model, builder);
12+
public LeatherArmorMetaHandler(LeatherArmorMetaHandlerModel model, LeatherArmorMeta itemMeta) {
13+
super(model, itemMeta);
1414
}
1515

1616
@Override
17-
public void serialize(Map<String, Object> map) {
18-
map.put("color", getBuilder().getFromMeta(LeatherArmorMeta::getColor, (Color) null));
17+
public @NotNull LeatherArmorMeta getItemMeta() {
18+
return (LeatherArmorMeta) super.getItemMeta();
1919
}
2020

2121
@Override
22-
public void deserialize(Map<String, Object> map) {
23-
getBuilder().<LeatherArmorMeta>consumeMeta(meta -> meta.setColor(getColor(map.get("color"))));
22+
public void serialize(@NotNull Map<String, Object> map) {
23+
LeatherArmorMeta itemMeta = getItemMeta();
24+
25+
map.put("color", itemMeta.getColor());
26+
}
27+
28+
@Override
29+
public void deserialize(@NotNull Map<String, Object> map) {
30+
LeatherArmorMeta itemMeta = getItemMeta();
31+
32+
itemMeta.setColor(getColor(map.get("color")));
2433
}
2534

26-
public LeatherArmorMetaHandler color(Color color) {
27-
getBuilder().<LeatherArmorMeta>consumeMeta(meta -> meta.setColor(color));
35+
public @NotNull LeatherArmorMetaHandler color(@NotNull Color color) {
36+
getItemMeta().setColor(color);
2837
return this;
2938
}
3039

31-
public Color color() {
32-
return getBuilder().getFromMeta(LeatherArmorMeta::getColor, (Color) null);
40+
public @NotNull Color color() {
41+
return getItemMeta().getColor();
3342
}
3443

3544
private static Color getColor(Object object) {

0 commit comments

Comments
 (0)