Skip to content

Commit

Permalink
feat(icon): Support custom-model-data in residences' icon
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Sep 17, 2024
1 parent f749375 commit 8051b1e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.3</version>
<version>1.3.0</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.3</version>
<version>1.3.0</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> description;

Expand All @@ -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");

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</properties>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.3</version>
<version>1.3.0</version>
<packaging>pom</packaging>
<modules>
<module>api</module>
Expand Down

0 comments on commit 8051b1e

Please sign in to comment.