forked from ChestShop-authors/ChestShop-3
/
ItemNamingUtils.java
146 lines (122 loc) · 6.21 KB
/
ItemNamingUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.Acrobot.ChestShop.Utils;
import com.Acrobot.Breeze.Utils.EnchantmentNames;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.Breeze.Utils.PotionNames;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.PreShopCreationItemDisplayNameEvent;
import com.Acrobot.ChestShop.ItemNaming.ChestShopEnchantedBookDisplayNameShortener;
import com.Acrobot.ChestShop.ItemNaming.ChestShopItemDisplayNameShortener;
import com.Acrobot.ChestShop.ItemNaming.ItemDisplayNameShortener;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.MusicInstrument;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MusicInstrumentMeta;
import org.bukkit.inventory.meta.PotionMeta;
public class ItemNamingUtils {
private static final ItemDisplayNameShortener CHEST_SHOP_ITEM_DISPLAY_NAME_SHORTENER = new ChestShopItemDisplayNameShortener();
private static final ItemDisplayNameShortener CHEST_SHOP_ENCHANTED_BOOK_DISPLAY_NAME_SHORTENER = new ChestShopEnchantedBookDisplayNameShortener();
public static String getSignItemName(ItemStack itemStack) {
return getDisplayName(itemStack, 15);
}
public static String getDisplayName(ItemStack itemStack) {
return getDisplayName(itemStack, Integer.MAX_VALUE);
}
public static String getDisplayName(ItemStack itemStack, int length) {
if (itemStack == null) {
return null;
}
ItemDisplayNameShortener extraShortener = null;
Material type = itemStack.getType();
String itemName = StringUtil.capitalizeFirstLetter(type.name(), '_');
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof MusicInstrumentMeta musicInstrumentMeta) {
MusicInstrument instrumentType = musicInstrumentMeta.getInstrument();
if (instrumentType == null) {
instrumentType = MusicInstrument.PONDER_GOAT_HORN;
}
String instrument = StringUtil.capitalizeFirstLetter(instrumentType.getKey().getKey().replace("_goat_horn", ""));
itemName = itemName + " " + instrument;
} else if (itemMeta instanceof PotionMeta potionMeta) {
itemName = PotionNames.getName(potionMeta.getBasePotionType()).replace("Potion", itemName);
} else if (itemMeta instanceof FireworkMeta fireworkMeta) {
if (fireworkMeta.getEffectsSize() == 0) {
itemName = "Rocket Strength " + fireworkMeta.getPower();
}
} else if (itemMeta instanceof EnchantmentStorageMeta enchantmentStorageMeta) {
Map<Enchantment, Integer> enchants = enchantmentStorageMeta.getStoredEnchants();
if (enchants.size() == 1) {
Entry<Enchantment, Integer> enchantmentAndLevel = enchants.entrySet().iterator().next();
Enchantment enchantment = enchantmentAndLevel.getKey();
int level = enchantmentAndLevel.getValue();
itemName = itemName + " " + EnchantmentNames.getName(enchantment) + ((enchantment.getMaxLevel() > 1 || level != 1) ? (' ' + NumberUtil.toRoman(level)) : "");
extraShortener = CHEST_SHOP_ENCHANTED_BOOK_DISPLAY_NAME_SHORTENER;
} else {
itemName = itemName + " " + enchants.size() + " Enchantments";
}
}
boolean needsItalicEffect = false;
if (itemStack.hasItemMeta()) {
if (!itemMeta.equals(new ItemStack(itemStack.getType()).getItemMeta())) {
needsItalicEffect = true;
}
}
PreShopCreationItemDisplayNameEvent preShopCreationItemDisplayNameEvent = new PreShopCreationItemDisplayNameEvent(itemStack, itemName);
ChestShop.callEvent(preShopCreationItemDisplayNameEvent);
itemName = preShopCreationItemDisplayNameEvent.getDisplayName();
ItemDisplayNameShortener itemDisplayNameShortener = preShopCreationItemDisplayNameEvent.getItemDisplayNameShortener();
itemName = shortenDisplayName(itemName, length, itemDisplayNameShortener != null ? itemDisplayNameShortener : extraShortener);
return (needsItalicEffect ? ChatColor.ITALIC : "") + itemName.substring(0, Math.min(itemName.length(), length));
}
public static String shortenDisplayName(String name, int length) {
return shortenDisplayName(name, length, null);
}
public static String shortenDisplayName(String name, int length, ItemDisplayNameShortener customItemDisplayNameShortener) {
String result = name.toLowerCase();
if (result.length() <= length) {
return name;
}
if (customItemDisplayNameShortener != null) {
result = customItemDisplayNameShortener.shorten(result, length);
if (result.length() <= length) {
return result;
}
}
result = CHEST_SHOP_ITEM_DISPLAY_NAME_SHORTENER.shorten(result, length);
if (result.length() <= length) {
return result;
}
String[] words = result.split(" ");
String[] shortenedWords = new String[words.length];
int remainingLetters = length;
int lettersPerWord = length / words.length;
for (int i = 0; i < words.length; i++) {
String word = words[i];
int letters = Math.min(lettersPerWord, word.length());
shortenedWords[i] = word.substring(0, letters);
remainingLetters -= letters;
}
int currentWord = -1;
while (remainingLetters > 0) {
currentWord++;
if (currentWord >= words.length) {
currentWord = 0;
}
String word = words[currentWord];
String shortened = shortenedWords[currentWord];
if (word.equals(shortened)) {
continue;
}
shortenedWords[currentWord] = shortened + word.charAt(shortened.length());
remainingLetters--;
}
return String.join("", shortenedWords);
}
}