diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java index 279e92f..25d29ea 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java @@ -48,17 +48,7 @@ public class ItemInfo implements CommandExecutor { } iteminfo.send(sender); - try { - Map replacementMap = ImmutableMap.of("item", ItemUtil.getName(item)); - if (!Properties.SHOWITEM_MESSAGE || !(sender instanceof Player) - || !MaterialUtil.Show.sendMessage((Player) sender, sender.getName(), Messages.iteminfo_fullname, false, new ItemStack[]{item}, replacementMap)) { - Messages.iteminfo_fullname.send(sender, replacementMap); - } - } catch (IllegalArgumentException e) { - sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!"); - ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e); - return true; - } + if (!sendItemName(sender, item, Messages.iteminfo_fullname)) return true; try { iteminfo_shopname.send(sender, "item", ItemUtil.getSignName(item)); @@ -73,4 +63,19 @@ public class ItemInfo implements CommandExecutor { return true; } + + public static boolean sendItemName(CommandSender sender, ItemStack item, Messages.Message message) { + try { + Map replacementMap = ImmutableMap.of("item", ItemUtil.getName(item)); + if (!Properties.SHOWITEM_MESSAGE || !(sender instanceof Player) + || !MaterialUtil.Show.sendMessage((Player) sender, sender.getName(), message, false, new ItemStack[]{item}, replacementMap)) { + message.send(sender, replacementMap); + } + } catch (IllegalArgumentException e) { + sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!"); + ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e); + return false; + } + return true; + } } diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java index a5acb9d..be4492b 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java @@ -34,6 +34,15 @@ public class Messages { public static Message iteminfo_repaircost; public static Message iteminfo_book; public static Message iteminfo_book_generation; + public static Message iteminfo_leather_color; + public static Message iteminfo_bundle_items; + public static Message iteminfo_axolotl_variant; + public static Message iteminfo_recipes; + public static Message iteminfo_map_view; + public static Message iteminfo_map_location; + public static Message iteminfo_tropical_fish; + public static Message iteminfo_crossbow_projectiles; + public static Message iteminfo_crossbow_projectile; public static Message iteminfo_lore; public static Message METRICS; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/ItemInfoListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/ItemInfoListener.java index aef257c..27e124b 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/ItemInfoListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/ItemInfoListener.java @@ -1,19 +1,30 @@ package com.Acrobot.ChestShop.Listeners; import com.Acrobot.Breeze.Utils.StringUtil; +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Commands.ItemInfo; import com.Acrobot.ChestShop.Events.ItemInfoEvent; import org.bukkit.ChatColor; -import org.bukkit.Material; +import org.bukkit.Color; +import org.bukkit.NamespacedKey; import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.AxolotlBucketMeta; import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.BundleMeta; +import org.bukkit.inventory.meta.CrossbowMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.KnowledgeBookMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.MapMeta; +import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.Repairable; -import org.bukkit.potion.Potion; +import org.bukkit.inventory.meta.TropicalFishBucketMeta; +import org.bukkit.map.MapView; import org.bukkit.potion.PotionEffect; import java.util.Map; @@ -21,34 +32,99 @@ import java.util.Map; 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 static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_axolotl_variant; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_book; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_book_generation; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_bundle_items; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_crossbow_projectile; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_crossbow_projectiles; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_leather_color; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_lore; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_map_location; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_map_view; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_recipes; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_repaircost; +import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_tropical_fish; /** * @author Acrobot */ public class ItemInfoListener implements Listener { + // Register version dependent listeners + static { + try { + Class.forName("org.bukkit.inventory.meta.AxolotlBucketMeta"); + ChestShop.registerListener(new Listener() { + @EventHandler + public void addAxolotlInfo(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof AxolotlBucketMeta) { + iteminfo_axolotl_variant.send(event.getSender(), "variant", capitalizeFirstLetter(((AxolotlBucketMeta) meta).getVariant().name(), '_')); + } + } + } + }); + } catch (ClassNotFoundException | NoClassDefFoundError ignored) {} + try { + Class.forName("org.bukkit.inventory.meta.BundleMeta"); + ChestShop.registerListener(new Listener() { + @EventHandler + public void addAxolotlInfo(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof BundleMeta) { + iteminfo_bundle_items.send(event.getSender(), "itemcount", String.valueOf(((BundleMeta) meta).getItems().size())); + } + } + } + }); + } catch (ClassNotFoundException | NoClassDefFoundError ignored) {} + try { + Class.forName("org.bukkit.inventory.meta.CrossbowMeta"); + ChestShop.registerListener(new Listener() { + @EventHandler + public void addCrossBowInfo(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof CrossbowMeta && ((CrossbowMeta) meta).hasChargedProjectiles()) { + iteminfo_crossbow_projectiles.send(event.getSender()); + for (ItemStack chargedProjectile : ((CrossbowMeta) meta).getChargedProjectiles()) { + ItemInfo.sendItemName(event.getSender(), chargedProjectile, iteminfo_crossbow_projectile); + ChestShop.callEvent(new ItemInfoEvent(event.getSender(), chargedProjectile)); + event.getSender().sendMessage(ChatColor.GRAY + "---"); + } + } + } + } + }); + } catch (ClassNotFoundException | NoClassDefFoundError ignored) {} + } + @EventHandler public static void addRepairCost(ItemInfoEvent event) { - ItemMeta meta = event.getItem().getItemMeta(); - if (meta instanceof Repairable && ((Repairable) meta).getRepairCost() > 0) { - iteminfo_repaircost.send(event.getSender(), "cost", String.valueOf(((Repairable) meta).getRepairCost())); + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof Repairable && ((Repairable) meta).getRepairCost() > 0) { + iteminfo_repaircost.send(event.getSender(), "cost", String.valueOf(((Repairable) meta).getRepairCost())); + } } } @EventHandler public static void addEnchantment(ItemInfoEvent event) { ItemStack item = event.getItem(); - ItemMeta meta = item.getItemMeta(); CommandSender sender = event.getSender(); for (Map.Entry enchantment : item.getEnchantments().entrySet()) { sender.sendMessage(ChatColor.AQUA + capitalizeFirstLetter(enchantment.getKey().getName(), '_') + ' ' + toRoman(enchantment.getValue())); } + if (!item.hasItemMeta()) { + return; + } + ItemMeta meta = item.getItemMeta(); if (meta instanceof EnchantmentStorageMeta) { for (Map.Entry enchantment : ((EnchantmentStorageMeta) meta).getStoredEnchants().entrySet()) { sender.sendMessage(ChatColor.YELLOW + capitalizeFirstLetter(enchantment.getKey().getName(), '_') + ' ' + toRoman(enchantment.getValue())); @@ -56,49 +132,112 @@ public class ItemInfoListener implements Listener { } } + @EventHandler + public static void addLeatherColor(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof LeatherArmorMeta) { + Color color = ((LeatherArmorMeta) meta).getColor(); + iteminfo_leather_color.send(event.getSender(), + "colorred", String.valueOf(color.getRed()), + "colorgreen", String.valueOf(color.getGreen()), + "colorblue", String.valueOf(color.getBlue()), + "colorhex", Integer.toHexString(color.asRGB()) + ); + } + } + } + + @EventHandler + public static void addRecipes(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof KnowledgeBookMeta && !((KnowledgeBookMeta) meta).getRecipes().isEmpty()) { + iteminfo_recipes.send(event.getSender()); + for (NamespacedKey recipe : ((KnowledgeBookMeta) meta).getRecipes()) { + event.getSender().sendMessage(ChatColor.GRAY + recipe.toString()); + } + } + } + } + + @EventHandler + public static void addTropicalFishInfo(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof TropicalFishBucketMeta && ((TropicalFishBucketMeta) meta).hasVariant()) { + iteminfo_tropical_fish.send(event.getSender(), + "pattern", capitalizeFirstLetter(((TropicalFishBucketMeta) meta).getPattern().name()), + "patterncolor", capitalizeFirstLetter(((TropicalFishBucketMeta) meta).getPatternColor().name()), + "bodycolor", capitalizeFirstLetter(((TropicalFishBucketMeta) meta).getBodyColor().name()) + ); + } + } + } + + @EventHandler + public static void addMapInfo(ItemInfoEvent event) { + if (event.getItem().hasItemMeta()) { + ItemMeta meta = event.getItem().getItemMeta(); + if (meta instanceof MapMeta) { + if (((MapMeta) meta).getMapView() != null) { + MapView mapView = ((MapMeta) meta).getMapView(); + iteminfo_map_view.send(event.getSender(), + "id", String.valueOf(mapView.getId()), + "x", String.valueOf(mapView.getCenterX()), + "z", String.valueOf(mapView.getCenterZ()), + "world", mapView.getWorld() != null ? String.valueOf(mapView.getWorld().getName()) : "unknown", + "scale", capitalizeFirstLetter(mapView.getScale().name(), '_'), + "locked", String.valueOf(mapView.isLocked()) + ); + } + if (((MapMeta) meta).hasLocationName()) { + iteminfo_map_location.send(event.getSender(), "location", String.valueOf(((MapMeta) meta).getLocationName())); + } + } + } + } + @EventHandler public static void addPotionInfo(ItemInfoEvent event) { ItemStack item = event.getItem(); - - if (item.getType() != Material.POTION || item.getDurability() == 0) { + if (!item.hasItemMeta()) { return; } - Potion potion; - - try { - potion = Potion.fromItemStack(item); - } catch (IllegalArgumentException ex) { + ItemMeta meta = item.getItemMeta(); + if (!(meta instanceof PotionMeta)) { return; } + PotionMeta potionMeta = (PotionMeta) meta; + StringBuilder message = new StringBuilder(50); message.append(ChatColor.GRAY); - if (potion.getType() == null) { - return; + message.append(capitalizeFirstLetter(item.getType().name(), '_') + " of "); + message.append(capitalizeFirstLetter(potionMeta.getBasePotionData().getType().name(), '_')).append(' '); + if (potionMeta.getBasePotionData().isUpgraded()) { + message.append("II"); + } else if (potionMeta.getBasePotionData().isExtended()) { + message.append("+"); } - if (potion.isSplash()) { - message.append("Splash "); - } - - message.append("Potion of "); - message.append(capitalizeFirstLetter(potion.getType().name(), '_')).append(' '); - message.append(toRoman(potion.getLevel())); - CommandSender sender = event.getSender(); sender.sendMessage(message.toString()); - for (PotionEffect effect : potion.getEffects()) { + for (PotionEffect effect : potionMeta.getCustomEffects()) { sender.sendMessage(ChatColor.DARK_GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20)); } } @EventHandler public static void addBookInfo(ItemInfoEvent event) { + if (!event.getItem().hasItemMeta()) { + return; + } ItemMeta meta = event.getItem().getItemMeta(); if (meta instanceof BookMeta) { BookMeta book = (BookMeta) meta; diff --git a/src/main/resources/languages/lang.en.yml b/src/main/resources/languages/lang.en.yml index 9615029..6a1cb6a 100644 --- a/src/main/resources/languages/lang.en.yml +++ b/src/main/resources/languages/lang.en.yml @@ -18,6 +18,24 @@ iteminfo_book: |- &fBook Pages: &7%pages iteminfo_book_generation: "&fBook Generation: &7%generation" +iteminfo_leather_color: "&fLeather Color: &7Red: %colorred, Green: %colorgreen, Blue: %colorblue (Hex: #%colorhex)" +iteminfo_bundle_items: "&fBundle Items: &7%itemcount" +iteminfo_axolotl_variant: "&fAxolotl Variant: &7%variant" +iteminfo_recipes: "&fRecipe Knowledge:" +iteminfo_map_view: |- + &fMap ID: &7%id + &fMap World: &7%world + &fMap Center: &7%x/%z + &fMap Locked: &7locked + +iteminfo_map_location: "&fMap Location: &7%location" +iteminfo_tropical_fish: |- + &fFish Pattern: &7%pattern + &fFish Pattern Color: &7%patterncolor + &fFish Body Color: &7%bodycolor + +iteminfo_crossbow_projectiles: "&fCharged Crossbow Projectile:" +iteminfo_crossbow_projectile: "&f%item" iteminfo_lore: |- &fLore: &r%lore