Skip to content

Commit a93f70c

Browse files
authored
Merge pull request #231 from 1robie/developement
Fix CustomModelData for paper
2 parents 049c7dc + c1a1831 commit a93f70c

6 files changed

Lines changed: 132 additions & 205 deletions

File tree

API/src/main/java/fr/maxlego08/menu/api/itemstack/components/CustomModelDataComponent.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,34 @@
1010
import org.jetbrains.annotations.Nullable;
1111

1212
import java.util.List;
13-
import java.util.Optional;
1413

1514
public class CustomModelDataComponent extends ItemComponent {
16-
private final @NotNull Optional<@NotNull List<@NotNull Color>> colors;
17-
private final @NotNull Optional<@NotNull List<@NotNull Boolean>> flags;
18-
private final @NotNull Optional<@NotNull List<@NotNull Float>> floats;
19-
private final @NotNull Optional<@NotNull List<@NotNull String>> string;
15+
private final @NotNull List<@NotNull Color> colors;
16+
private final @NotNull List<@NotNull Boolean> flags;
17+
private final @NotNull List<@NotNull Float> floats;
18+
private final @NotNull List<@NotNull String> strings;
2019

21-
public CustomModelDataComponent(@NotNull Optional<@NotNull List<@NotNull Color>> colors, @NotNull Optional<@NotNull List<@NotNull Boolean>> flags, @NotNull Optional<@NotNull List<@NotNull Float>> floats, @NotNull Optional<@NotNull List<@NotNull String>> string) {
20+
public CustomModelDataComponent(@NotNull List<@NotNull Color> colors, @NotNull List<@NotNull Boolean> flags, @NotNull List<@NotNull Float> floats, @NotNull List<@NotNull String> strings) {
2221
this.colors = colors;
2322
this.flags = flags;
2423
this.floats = floats;
25-
this.string = string;
24+
this.strings = strings;
2625
}
2726

28-
public @NotNull Optional<@NotNull List<@NotNull Color>> getColors() {
27+
public @NotNull List<@NotNull Color> getColors() {
2928
return colors;
3029
}
3130

32-
public @NotNull Optional<@NotNull List<@NotNull Boolean>> getFlags() {
33-
return flags;
31+
public @NotNull List<@NotNull Boolean> getFlags() {
32+
return this.flags;
3433
}
3534

36-
public @NotNull Optional<@NotNull List<@NotNull Float>> getFloats() {
37-
return floats;
35+
public @NotNull List<@NotNull Float> getFloats() {
36+
return this.floats;
3837
}
3938

40-
public @NotNull Optional<@NotNull List<@NotNull String>> getString() {
41-
return string;
39+
public @NotNull List<@NotNull String> getStrings() {
40+
return this.strings;
4241
}
4342

4443
@Override
@@ -47,10 +46,18 @@ public void apply(@NotNull BuildContext context, @NotNull ItemStack itemStack, @
4746
if (itemMeta != null) {
4847
org.bukkit.inventory.meta.components.CustomModelDataComponent customModelDataComponent = itemMeta.getCustomModelDataComponent();
4948

50-
this.colors.ifPresent(customModelDataComponent::setColors);
51-
this.flags.ifPresent(customModelDataComponent::setFlags);
52-
this.floats.ifPresent(customModelDataComponent::setFloats);
53-
this.string.ifPresent(customModelDataComponent::setStrings);
49+
if (!this.colors.isEmpty()) {
50+
customModelDataComponent.setColors(this.colors);
51+
}
52+
if (!this.flags.isEmpty()) {
53+
customModelDataComponent.setFlags(this.flags);
54+
}
55+
if (!this.floats.isEmpty()) {
56+
customModelDataComponent.setFloats(this.floats);
57+
}
58+
if (!this.strings.isEmpty()) {
59+
customModelDataComponent.setStrings(this.strings);
60+
}
5461

5562
itemStack.setItemMeta(itemMeta);
5663
}

Common/src/main/java/fr/maxlego08/menu/common/utils/itemstack/MenuItemStackFormMap.java

Lines changed: 0 additions & 158 deletions
This file was deleted.

src/main/java/fr/maxlego08/menu/loader/components/spigot/SpigotCustomModelDataItemComponentLoader.java renamed to Common/src/main/java/fr/maxlego08/menu/loader/components/spigot/SpigotCustomModelDataItemComponentLoader.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.File;
1414
import java.util.ArrayList;
1515
import java.util.List;
16-
import java.util.Optional;
1716

1817
public class SpigotCustomModelDataItemComponentLoader extends AbstractColorItemComponentLoader {
1918

@@ -24,44 +23,50 @@ public SpigotCustomModelDataItemComponentLoader(){
2423
@Override
2524
public @Nullable ItemComponent load(@NotNull MenuItemStackContext context, @NotNull File file, @NotNull YamlConfiguration configuration, @NotNull String path, @Nullable ConfigurationSection componentSection) {
2625
if (componentSection == null) return null;
27-
Optional<List<Float>> floatList;
28-
List<Float> floats = componentSection.getFloatList("floats");
29-
floatList = floats.isEmpty() ? Optional.empty() : Optional.of(floats);
26+
List<Float> floats = getFloats(componentSection);
3027

31-
Optional<List<Boolean>> booleanList;
32-
List<Boolean> booleans = componentSection.getBooleanList("flags");
33-
booleanList = booleans.isEmpty() ? Optional.empty() : Optional.of(booleans);
28+
List<Boolean> booleans = getBooleans(componentSection);
3429

35-
Optional<List<String>> stringList;
36-
List<String> strings = componentSection.getStringList("strings");
37-
stringList = strings.isEmpty() ? Optional.empty() : Optional.of(strings);
30+
List<String> strings = getStrings(componentSection);
3831

39-
Optional<List<Color>> colorList;
40-
List<?> colorsList = componentSection.getList("colors");
41-
if (colorsList == null || colorsList.isEmpty()) {
42-
colorList = Optional.empty();
43-
} else {
44-
List<Color> colors = new ArrayList<>();
45-
for (Object obj : colorsList) {
46-
Color color = parseColor(obj);
47-
if (color != null) {
48-
colors.add(color);
49-
}
50-
}
51-
colorList = colors.isEmpty() ? Optional.empty() : Optional.of(colors);
52-
}
32+
List<Color> colorList = getColors(componentSection);
5333

54-
if (colorList.isEmpty() && booleanList.isEmpty() && floatList.isEmpty() && stringList.isEmpty()) {
34+
if (colorList.isEmpty() && booleans.isEmpty() && floats.isEmpty() && strings.isEmpty()) {
5535
return null;
5636
}
5737

5838
return new CustomModelDataComponent(
5939
colorList,
60-
booleanList,
61-
floatList,
62-
stringList
40+
booleans,
41+
floats,
42+
strings
6343
);
6444
}
6545

46+
protected @NotNull List<Float> getFloats(@NotNull ConfigurationSection section) {
47+
return section.getFloatList("floats");
48+
}
49+
50+
protected @NotNull List<Boolean> getBooleans(@NotNull ConfigurationSection section) {
51+
return section.getBooleanList("flags");
52+
}
53+
54+
protected @NotNull List<String> getStrings(@NotNull ConfigurationSection section) {
55+
return section.getStringList("strings");
56+
}
57+
58+
protected @NotNull List<Color> getColors(@NotNull ConfigurationSection section) {
59+
List<Color> colors = new ArrayList<>();
60+
List<?> colorsList = section.getList("colors");
61+
if (colorsList != null) {
62+
for (Object obj : colorsList) {
63+
Color color = parseColor(obj);
64+
if (color != null) {
65+
colors.add(color);
66+
}
67+
}
68+
}
69+
return colors;
70+
}
6671

6772
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fr.maxlego08.menu.itemstack.components.paper;
2+
3+
import fr.maxlego08.menu.api.context.BuildContext;
4+
import fr.maxlego08.menu.api.itemstack.ItemComponent;
5+
import io.papermc.paper.datacomponent.DataComponentTypes;
6+
import io.papermc.paper.datacomponent.item.CustomModelData;
7+
import org.bukkit.entity.Player;
8+
import org.bukkit.inventory.ItemStack;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
public class PaperCustomModelDataComponent extends ItemComponent {
13+
private final CustomModelData customModelData;
14+
15+
public PaperCustomModelDataComponent(@NotNull CustomModelData customModelData) {
16+
this.customModelData = customModelData;
17+
}
18+
19+
@Override
20+
public void apply(@NotNull BuildContext context, @NotNull ItemStack itemStack, @Nullable Player player) {
21+
22+
itemStack.setData(DataComponentTypes.CUSTOM_MODEL_DATA, this.customModelData);
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package fr.maxlego08.menu.loader.components.paper;
2+
3+
import fr.maxlego08.menu.api.context.MenuItemStackContext;
4+
import fr.maxlego08.menu.api.itemstack.ItemComponent;
5+
import fr.maxlego08.menu.itemstack.components.paper.PaperCustomModelDataComponent;
6+
import fr.maxlego08.menu.loader.components.spigot.SpigotCustomModelDataItemComponentLoader;
7+
import io.papermc.paper.datacomponent.item.CustomModelData;
8+
import org.bukkit.Color;
9+
import org.bukkit.configuration.ConfigurationSection;
10+
import org.bukkit.configuration.file.YamlConfiguration;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.io.File;
15+
import java.util.List;
16+
17+
public class PaperCustomModelDataComponentLoader extends SpigotCustomModelDataItemComponentLoader {
18+
19+
@Override
20+
public @Nullable ItemComponent load(@NotNull MenuItemStackContext context, @NotNull File file, @NotNull YamlConfiguration configuration, @NotNull String path, @Nullable ConfigurationSection componentSection) {
21+
if (componentSection == null) return null;
22+
List<Float> floats = getFloats(componentSection);
23+
24+
List<Boolean> booleans = getBooleans(componentSection);
25+
26+
List<String> strings = getStrings(componentSection);
27+
28+
List<Color> colorList = getColors(componentSection);
29+
30+
if (colorList.isEmpty() && booleans.isEmpty() && floats.isEmpty() && strings.isEmpty()) {
31+
return null;
32+
}
33+
34+
CustomModelData.Builder builder = CustomModelData.customModelData();
35+
if (!colorList.isEmpty()) {
36+
builder.addColors(colorList);
37+
}
38+
if (!booleans.isEmpty()) {
39+
builder.addFlags(booleans);
40+
}
41+
if (!floats.isEmpty()) {
42+
builder.addFloats(floats);
43+
}
44+
if (!strings.isEmpty()) {
45+
builder.addStrings(strings);
46+
}
47+
return new PaperCustomModelDataComponent(builder.build());
48+
}
49+
}

0 commit comments

Comments
 (0)