Skip to content

Commit

Permalink
added redesign placeholder itemstack
Browse files Browse the repository at this point in the history
  • Loading branch information
Draww committed Mar 15, 2019
1 parent c1432ea commit ea66347
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/main/java/me/draww/superrup/RankMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public RankMenu(Player player) {
ranks.forEach(rank -> {
if (!rank.getGroup().equals(group)) {
if (finalIndexPlayer > rank.getQueue()) {
addLast(ElementUtil.emptyElement(rank.getIconLow()));
addLast(ElementUtil.emptyElement(ItemUtil.redesignPlaceholderItemStack(player, rank.getIconLow())));
} else if (finalIndexPlayer < rank.getQueue()) {
if (rank.getQueue().equals(finalIndexPlayer + 1)) {
addLast(new BasicElement(rank.getIconJump(),
addLast(new BasicElement(ItemUtil.redesignPlaceholderItemStack(player, rank.getIconJump()),
new BasicTarget(e -> {
e.cancel();
boolean controlConditions = ConditionProvider.testAllConditions(player, rank);
Expand All @@ -100,13 +100,13 @@ public RankMenu(Player player) {
e.closeView();
})));
} else {
addLast(ElementUtil.emptyElement(rank.getIconHigh()));
addLast(ElementUtil.emptyElement(ItemUtil.redesignPlaceholderItemStack(player, rank.getIconHigh())));
}
} else if (rank.getGroup().equals(group)) {
addLast(ElementUtil.emptyElement(rank.getIconEqual()));
addLast(ElementUtil.emptyElement(ItemUtil.redesignPlaceholderItemStack(player, rank.getIconEqual())));
}
} else {
addLast(ElementUtil.emptyElement(rank.getIconEqual()));
addLast(ElementUtil.emptyElement(ItemUtil.redesignPlaceholderItemStack(player, rank.getIconEqual())));
}
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/me/draww/superrup/utils/ItemStackBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public ItemStackBuilder lore(String... lines) {
});
}

public ItemStackBuilder newLore(String... lines) {
return transformMeta(meta -> {
List<String> lore = new ArrayList<>();
for (String line : lines) {
lore.add(Text.colorize(line));
}
meta.setLore(lore);
});
}

public ItemStackBuilder lore(Iterable<String> lines) {
return transformMeta(meta -> {
List<String> lore = meta.getLore() == null ? new ArrayList<>() : meta.getLore();
Expand All @@ -87,6 +97,16 @@ public ItemStackBuilder lore(Iterable<String> lines) {
});
}

public ItemStackBuilder newLore(Iterable<String> lines) {
return transformMeta(meta -> {
List<String> lore = new ArrayList<>();
for (String line : lines) {
lore.add(Text.colorize(line));
}
meta.setLore(lore);
});
}

public ItemStackBuilder clearLore() {
return transformMeta(meta -> meta.setLore(new ArrayList<>()));
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/me/draww/superrup/utils/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.potion.PotionData;
Expand All @@ -21,6 +22,28 @@

public class ItemUtil {

public static ItemStack redesignPlaceholderItemStack(Player player, ItemStack item, String... variables) {
ItemMeta meta = item.getItemMeta();
String newName = StringUtil.replacePlayerPlaceholders(player, meta.getDisplayName());
if (variables.length > 1) {
for (int i = 0; i < variables.length; i += 2) {
newName = newName.replace(variables[i], variables[i + 1]);
}
}
List<String> oldLores = meta.getLore();
List<String> newLores = new ArrayList<>();
for (String lore : oldLores) {
String newLore = StringUtil.replacePlayerPlaceholders(player, lore);
if (variables.length > 1) {
for (int i = 0; i < variables.length; i += 2) {
newLore = newLore.replace(variables[i], variables[i + 1]);
}
}
newLores.add(newLore);
}
return ItemStackBuilder.of(item).name(Text.colorize(newName)).newLore(Text.colorizeList(newLores)).build();
}

public static ItemStack deserializeItemStack(ConfigurationSection section, Rank rank) { //TODO: remove the return null effects
if (section.contains("template") && section.isString("template")) {
ConfigurationSection templateSection = Main.getInstance().getTemplateConfig().getConfigurationSection("icons." + section.getString("template"));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/ranks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ example_rank_1:
jump:
material: DIAMOND_BLOCK:0
amount: 1
name: '&b%rank%'
name: '&b%rank% - %player_name%'
lores:
- "&aYou need $500 to raise your rank."
high:
Expand Down

0 comments on commit ea66347

Please sign in to comment.