forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ItemInfoListener.java
318 lines (292 loc) · 14.3 KB
/
ItemInfoListener.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.Acrobot.ChestShop.Listeners;
import static com.Acrobot.Breeze.Utils.NumberUtil.toRoman;
import static com.Acrobot.Breeze.Utils.NumberUtil.toTime;
import static com.Acrobot.Breeze.Utils.StringUtil.capitalizeFirstLetter;
import com.Acrobot.Breeze.Utils.EnchantmentNames;
import com.Acrobot.Breeze.Utils.FireworkEffectTypeNames;
import com.Acrobot.Breeze.Utils.PotionNames;
import com.Acrobot.ChestShop.Events.ItemInfoEvent;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.Beehive;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.AxolotlBucketMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BookMeta.Generation;
import org.bukkit.inventory.meta.CrossbowMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.inventory.meta.TropicalFishBucketMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
/**
* @author Acrobot
*/
public class ItemInfoListener implements Listener {
@EventHandler
public static void addInfo(ItemInfoEvent event) {
CommandSender sender = event.getSender();
ItemStack item = event.getItem();
Material type = item.getType();
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasDisplayName() && !(meta instanceof BookMeta)) {
sender.sendMessage(" " + ChatColor.GRAY + "Name: " + meta.getDisplayName());
}
int maxdurability = type.getMaxDurability();
if (maxdurability > 0 && meta instanceof Damageable) {
int remainingDurability = maxdurability - ((Damageable) meta).getDamage();
sender.sendMessage(" " + ChatColor.RED + "Durability: " + remainingDurability + "/" + maxdurability);
}
if (meta instanceof Repairable) {
Repairable repairable = (Repairable) meta;
if (repairable.hasRepairCost()) {
sender.sendMessage(" " + ChatColor.RED + "Additional Repair Cost: " + repairable.getRepairCost());
}
}
if (meta instanceof BookMeta) {
BookMeta book = (BookMeta) meta;
if (meta.hasDisplayName()) {
sender.sendMessage(" " + ChatColor.GRAY + "Title: " + meta.getDisplayName());
} else if (book.hasTitle()) {
sender.sendMessage(" " + ChatColor.GRAY + "Title: " + book.getTitle());
}
if (book.hasAuthor()) {
sender.sendMessage(" " + ChatColor.GRAY + "Author: " + book.getAuthor());
}
if (book.hasGeneration() && book.getGeneration() != Generation.ORIGINAL) {
sender.sendMessage(" " + ChatColor.RED + capitalizeFirstLetter(book.getGeneration().name(), '_'));
}
}
Map<Enchantment, Integer> enchantments = item.getEnchantments();
for (Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
sender.sendMessage(" " + ChatColor.GRAY + EnchantmentNames.getName(enchantment.getKey()) + ((enchantment.getKey().getMaxLevel() > 1 || enchantment.getValue() != 1) ? (' ' + toRoman(enchantment.getValue())) : ""));
}
if (meta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta ench = (EnchantmentStorageMeta) meta;
if (ench.hasStoredEnchants()) {
for (Map.Entry<Enchantment, Integer> enchantment : ench.getStoredEnchants().entrySet()) {
sender.sendMessage(" " + ChatColor.GRAY + EnchantmentNames.getName(enchantment.getKey()) + ((enchantment.getKey().getMaxLevel() > 1 || enchantment.getValue() != 1) ? (' ' + toRoman(enchantment.getValue())) : ""));
}
}
}
if (meta instanceof FireworkMeta) {
FireworkMeta firework = (FireworkMeta) meta;
sender.sendMessage(" " + ChatColor.GRAY + "Flight Duration: " + firework.getPower());
for (FireworkEffect effect : firework.getEffects()) {
sendFireworkEffect(sender, effect);
}
}
if (meta instanceof FireworkEffectMeta) {
FireworkEffectMeta fireworkEffect = (FireworkEffectMeta) meta;
if (fireworkEffect.hasEffect()) {
FireworkEffect effect = fireworkEffect.getEffect();
sendFireworkEffect(sender, effect);
}
}
if (meta instanceof SkullMeta) {
SkullMeta skull = (SkullMeta) meta;
if (skull.hasOwner()) {
@SuppressWarnings("deprecation")
String owner = skull.getOwner();
if (owner != null) {
sender.sendMessage(" " + ChatColor.GRAY + "Skull Owner: " + owner);
}
}
}
if (meta instanceof PotionMeta) {
PotionMeta potion = (PotionMeta) meta;
StringBuilder message = new StringBuilder(50).append(" ").append(ChatColor.GRAY);
PotionData base = potion.getBasePotionData();
if (base != null) {
if (base.isExtended()) {
message.append("Extended ");
}
if (type == Material.SPLASH_POTION) {
message.append("Splash ");
}
if (type == Material.LINGERING_POTION) {
message.append("Lingering ");
}
message.append(PotionNames.getName(base.getType())).append(' ');
if (base.isUpgraded()) {
message.append("II ");
}
sender.sendMessage(message.toString());
}
if (potion.hasCustomEffects()) {
for (PotionEffect effect : potion.getCustomEffects()) {
sender.sendMessage(" " + ChatColor.GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20));
}
}
}
if (meta instanceof BlockStateMeta) {
BlockStateMeta blockStateMeta = (BlockStateMeta) meta;
if (blockStateMeta.hasBlockState()) {
BlockState blockState = blockStateMeta.getBlockState();
if (blockState != null) {
if (blockState instanceof Container) {
Inventory inv = ((Container) blockState).getInventory();
int stacks = 0;
for (ItemStack stack : inv.getContents()) {
if (stack != null && stack.getType() != Material.AIR) {
stacks++;
}
}
if (stacks > 0) {
sender.sendMessage(" " + ChatColor.GRAY + "Content: " + stacks + " Stacks");
}
}
if (blockState instanceof Beehive) {
Beehive beehive = (Beehive) blockState;
// int honeyLevel = ((org.bukkit.block.data.type.Beehive) beehive.getBlockData()).getHoneyLevel();
// sender.sendMessage(" " + ChatColor.GRAY + "Honey Level: " + honeyLevel);
int bees = beehive.getEntityCount();
sender.sendMessage(" " + ChatColor.GRAY + "Bees: " + bees);
}
}
}
}
if (meta instanceof TropicalFishBucketMeta) {
TropicalFishBucketMeta tropicalFishBucketMeta = (TropicalFishBucketMeta) meta;
if (tropicalFishBucketMeta.hasVariant()) {
String pattern = capitalizeFirstLetter(Objects.toString(tropicalFishBucketMeta.getPattern()), '_');
String basecolor = capitalizeFirstLetter(Objects.toString(tropicalFishBucketMeta.getBodyColor()), '_');
String patterncolor = capitalizeFirstLetter(Objects.toString(tropicalFishBucketMeta.getPatternColor()), '_');
sender.sendMessage(" " + ChatColor.GRAY + "Variant: " + pattern + " " + basecolor + "/" + patterncolor);
}
}
if (meta instanceof AxolotlBucketMeta axolotlBucketMeta) {
if (axolotlBucketMeta.hasVariant()) {
String variant = capitalizeFirstLetter(Objects.toString(axolotlBucketMeta.getVariant()), '_');
sender.sendMessage(" " + ChatColor.GRAY + "Variant: " + variant);
}
}
if (meta instanceof LeatherArmorMeta) {
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) meta;
Color color = leatherArmorMeta.getColor();
if (color != null) {
sender.sendMessage(" " + ChatColor.GRAY + "Color: " + getColorHexCode(color));
}
}
if (meta instanceof CrossbowMeta crossbowMeta) {
if (crossbowMeta.hasChargedProjectiles()) {
for (ItemStack stack : crossbowMeta.getChargedProjectiles()) {
if (stack != null && !stack.getType().isAir()) {
String arrow = capitalizeFirstLetter(Objects.toString(stack.getType()), '_');
ItemMeta arrowMeta = stack.getItemMeta();
if (arrowMeta instanceof PotionMeta potion) {
StringBuilder message = new StringBuilder(50).append(" (");
boolean first = true;
PotionData base = potion.getBasePotionData();
if (base != null) {
if (base.isExtended()) {
message.append("Extended ");
}
if (type == Material.SPLASH_POTION) {
message.append("Splash ");
}
if (type == Material.LINGERING_POTION) {
message.append("Lingering ");
}
message.append(PotionNames.getName(base.getType()));
if (base.isUpgraded()) {
message.append(" II");
}
first = false;
}
if (potion.hasCustomEffects()) {
for (PotionEffect effect : potion.getCustomEffects()) {
if (!first) {
message.append(", ");
}
first = false;
message.append(ChatColor.GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20));
}
}
if (!first) {
message.append(")");
arrow = arrow + message.toString();
}
}
sender.sendMessage(" " + ChatColor.GRAY + "Projectile: " + arrow);
}
}
}
}
if (meta != null && meta.hasLore() && !(meta instanceof BookMeta)) {
List<String> lore = meta.getLore();
if (lore != null && lore.size() > 0) {
if (lore.size() == 1) {
sender.sendMessage(" " + ChatColor.GRAY + "Lore: " + lore.get(0));
} else {
sender.sendMessage(" " + ChatColor.GRAY + "Lore:");
for (String loreLine : lore) {
sender.sendMessage(" " + ChatColor.GRAY + loreLine);
}
}
}
}
}
private static void sendFireworkEffect(CommandSender sender, FireworkEffect effect) {
sender.sendMessage(" " + ChatColor.GRAY + FireworkEffectTypeNames.getName(effect.getType()));
List<Color> colors = effect.getColors();
if (colors != null && !colors.isEmpty()) {
sender.sendMessage(" " + ChatColor.GRAY + colorListToString(colors));
}
colors = effect.getFadeColors();
if (colors != null && !colors.isEmpty()) {
sender.sendMessage(" " + ChatColor.GRAY + "Fade to " + colorListToString(colors));
}
if (effect.hasTrail()) {
sender.sendMessage(" " + ChatColor.GRAY + "Trail");
}
if (effect.hasFlicker()) {
sender.sendMessage(" " + ChatColor.GRAY + "Twinkle");
}
}
private static String colorListToString(List<Color> colors) {
StringBuilder sb = new StringBuilder();
for (Color c : colors) {
if (sb.length() > 0) {
sb.append(", ");
}
DyeColor dyeColor = DyeColor.getByFireworkColor(c);
sb.append(dyeColor != null ? capitalizeFirstLetter(dyeColor.name(), '_') : "Custom");
}
return sb.toString();
}
private static String getTwoCharacterColorHexCode(int color) {
String s = Integer.toHexString(color).toUpperCase();
if (s.length() < 2) {
s = "0" + s;
}
return s;
}
private static String getColorHexCode(Color color) {
String red = getTwoCharacterColorHexCode(color.getRed());
String green = getTwoCharacterColorHexCode(color.getGreen());
String blue = getTwoCharacterColorHexCode(color.getBlue());
return "#" + red + green + blue;
}
}