From 8051b1eef3f79c627daec0b12780140d85908c3a Mon Sep 17 00:00:00 2001 From: carm Date: Tue, 17 Sep 2024 23:46:55 +0800 Subject: [PATCH] feat(icon): Support custom-model-data in residences' icon --- api/pom.xml | 2 +- .../api/residence/ResidenceData.java | 15 +++++++++- plugin/pom.xml | 2 +- .../storage/yaml/data/YAMLResidenceData.java | 30 +++++++++++++++---- .../residencelist/ui/ResidenceAdminUI.java | 7 ++++- .../residencelist/ui/ResidenceInfoUI.java | 7 ++++- .../residencelist/ui/ResidenceListUI.java | 8 +++-- .../residencelist/ui/ResidenceManageUI.java | 9 ++++-- pom.xml | 2 +- 9 files changed, 67 insertions(+), 15 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 065e8d7..44ae149 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ com.artformgames residencelist-parent - 1.2.3 + 1.3.0 ${project.jdk.version} diff --git a/api/src/main/java/com/artformgames/plugin/residencelist/api/residence/ResidenceData.java b/api/src/main/java/com/artformgames/plugin/residencelist/api/residence/ResidenceData.java index f4da932..f514f70 100644 --- a/api/src/main/java/com/artformgames/plugin/residencelist/api/residence/ResidenceData.java +++ b/api/src/main/java/com/artformgames/plugin/residencelist/api/residence/ResidenceData.java @@ -6,6 +6,8 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -26,7 +28,18 @@ public interface ResidenceData { @Nullable Material getIconMaterial(); - void setIconMaterial(@NotNull Material material); + int getCustomModelData(); + + default void setIconMaterial(@NotNull ItemStack iconItem) { + int customModelData = -1; + ItemMeta meta = iconItem.getItemMeta(); + if (meta != null && meta.hasCustomModelData()) { + customModelData = meta.getCustomModelData(); + } + setIconMaterial(iconItem.getType(), customModelData); + } + + void setIconMaterial(@NotNull Material material, int customModelData); default @NotNull String getDisplayName() { return Optional.ofNullable(getAliasName()).orElse(getName()); diff --git a/plugin/pom.xml b/plugin/pom.xml index 7788b0c..60f7973 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -6,7 +6,7 @@ com.artformgames residencelist-parent - 1.2.3 + 1.3.0 ${project.jdk.version} diff --git a/plugin/src/main/java/com/artformgames/plugin/residencelist/storage/yaml/data/YAMLResidenceData.java b/plugin/src/main/java/com/artformgames/plugin/residencelist/storage/yaml/data/YAMLResidenceData.java index 03bb1c0..ebcc7c6 100644 --- a/plugin/src/main/java/com/artformgames/plugin/residencelist/storage/yaml/data/YAMLResidenceData.java +++ b/plugin/src/main/java/com/artformgames/plugin/residencelist/storage/yaml/data/YAMLResidenceData.java @@ -25,6 +25,8 @@ public class YAMLResidenceData implements ResidenceData { protected final @NotNull ClaimedResidence residence; protected @Nullable Material icon; + protected int customModelData; // <= 0 means no custom model data. + protected @Nullable String aliasName; protected @NotNull List description; @@ -36,9 +38,17 @@ public YAMLResidenceData(@NotNull File file, @NotNull ClaimedResidence residence this.conf = file.exists() ? YamlConfiguration.loadConfiguration(file) : new YamlConfiguration(); this.residence = residence; - this.icon = Optional.ofNullable(conf.getString("icon")) - .flatMap(XMaterial::matchXMaterial) - .map(XMaterial::parseMaterial).orElse(null); + String iconData = conf.getString("icon"); + + if (iconData != null) { + String[] args = iconData.split(":"); + this.icon = XMaterial.matchXMaterial(args[0]).map(XMaterial::parseMaterial).orElse(null); + try { + this.customModelData = args.length > 1 ? Integer.parseInt(args[1]) : null; + } catch (NumberFormatException e) { + } + } + this.aliasName = conf.getString("nickname", residence.getName()); this.description = conf.getStringList("description"); @@ -58,9 +68,19 @@ public YAMLResidenceData(@NotNull File file, @NotNull ClaimedResidence residence return icon; } - public void setIconMaterial(@NotNull Material material) { + public int getCustomModelData() { + return customModelData; + } + + public void setIconMaterial(@NotNull Material material, int customModelData) { this.icon = material; - this.conf.set("icon", XMaterial.matchXMaterial(material).name()); + this.customModelData = customModelData; + + if (customModelData > 0) { + this.conf.set("icon", XMaterial.matchXMaterial(material).name() + ":" + customModelData); + } else { + this.conf.set("icon", XMaterial.matchXMaterial(material).name()); + } } public @NotNull String getDisplayName() { diff --git a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceAdminUI.java b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceAdminUI.java index 2d1ce39..51c9f86 100644 --- a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceAdminUI.java +++ b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceAdminUI.java @@ -138,7 +138,12 @@ protected GUIItem generateIcon(ClaimedResidence residence) { icon.insertLore("click-lore", CONFIG.ADDITIONAL_LORE.NORMAL); } if (!data.getDescription().isEmpty()) icon.insertLore("description", data.getDescription()); - if (data.getIconMaterial() != null) icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getIconMaterial() != null) { + icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getCustomModelData() > 0) { + icon.handleMeta((itemMeta, player) -> itemMeta.setCustomModelData(data.getCustomModelData())); + } + } return new GUIItem(icon.get(viewer)) { @Override public void onClick(Player clicker, ClickType type) { diff --git a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceInfoUI.java b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceInfoUI.java index 34fd324..a4cea63 100644 --- a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceInfoUI.java +++ b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceInfoUI.java @@ -180,7 +180,12 @@ protected GUIItem generateIcon(UserListData userData, ClaimedResidence residence icon.insertLore("click-lore", CONFIG.ADDITIONAL_LORE.CLICK); if (!getData().getDescription().isEmpty()) icon.insertLore("description", getData().getDescription()); if (userData.isPinned(residence.getName())) icon.glow(); - if (getData().getIconMaterial() != null) icon.handleItem((i, p) -> i.setType(getData().getIconMaterial())); + if (data.getIconMaterial() != null) { + icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getCustomModelData() > 0) { + icon.handleMeta((itemMeta, player) -> itemMeta.setCustomModelData(data.getCustomModelData())); + } + } return new GUIItem(icon.get(viewer)) { @Override public void onClick(Player clicker, ClickType type) { diff --git a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceListUI.java b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceListUI.java index 6637ae1..b40f03f 100644 --- a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceListUI.java +++ b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceListUI.java @@ -156,8 +156,12 @@ protected GUIItem generateIcon(UserListData userData, ClaimedResidence residence if (userData.isPinned(residence.getName())) { icon.glow(); } - - if (data.getIconMaterial() != null) icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getIconMaterial() != null) { + icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getCustomModelData() > 0) { + icon.handleMeta((itemMeta, player) -> itemMeta.setCustomModelData(data.getCustomModelData())); + } + } return new GUIItem(icon.get(viewer)) { @Override public void onClick(Player clicker, ClickType type) { diff --git a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceManageUI.java b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceManageUI.java index 26284c7..96a13b9 100644 --- a/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceManageUI.java +++ b/plugin/src/main/java/com/artformgames/plugin/residencelist/ui/ResidenceManageUI.java @@ -114,7 +114,7 @@ public void onClick(Player clicker, ClickType type) { PluginMessages.EDIT.ICON_BLOCKED.send(player, getData().getDisplayName()); PluginMessages.EDIT.FAILED_SOUND.playTo(player); } else { - getData().modify(d -> d.setIconMaterial(itemStack.getType())); + getData().modify(d -> d.setIconMaterial(itemStack)); PluginMessages.EDIT.ICON_UPDATED.send(player, getData().getDisplayName()); PluginMessages.EDIT.SUCCESS_SOUND.playTo(player); } @@ -219,7 +219,12 @@ protected GUIItem generateIcon(UserListData userData, ClaimedResidence residence icon.insertLore("click-lore", CONFIG.ADDITIONAL_LORE.CLICK); if (!getData().getDescription().isEmpty()) icon.insertLore("description", getData().getDescription()); if (userData.isPinned(residence.getName())) icon.glow(); - if (getData().getIconMaterial() != null) icon.handleItem((i, p) -> i.setType(getData().getIconMaterial())); + if (data.getIconMaterial() != null) { + icon.handleItem((i, p) -> i.setType(data.getIconMaterial())); + if (data.getCustomModelData() > 0) { + icon.handleMeta((itemMeta, player) -> itemMeta.setCustomModelData(data.getCustomModelData())); + } + } return new GUIItem(icon.get(viewer)) { @Override public void onClick(Player clicker, ClickType type) { diff --git a/pom.xml b/pom.xml index 8733e30..c6cf3d9 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ com.artformgames residencelist-parent - 1.2.3 + 1.3.0 pom api