Skip to content

Commit

Permalink
Improve item info
Browse files Browse the repository at this point in the history
Add firework info
Add book info
Add skull info
Add basic block state info
  • Loading branch information
Brokkonaut committed Mar 6, 2019
1 parent 4364c24 commit 9dead2c
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 58 deletions.
@@ -0,0 +1,31 @@
package com.Acrobot.Breeze.Utils;

import java.util.HashMap;
import org.bukkit.FireworkEffect;

public class FireworkEffectTypeNames {
private static final HashMap<FireworkEffect.Type, String> typeToName;
static {
typeToName = new HashMap<FireworkEffect.Type, String>();
addType(FireworkEffect.Type.BALL, "Small Ball");
addType(FireworkEffect.Type.BALL_LARGE, "Large Ball");
addType(FireworkEffect.Type.BURST, "Burst");
addType(FireworkEffect.Type.STAR, "Star-shaped");
addType(FireworkEffect.Type.CREEPER, "Creeper-shaped");
}

private static void addType(FireworkEffect.Type type, String name) {
typeToName.put(type, name);
}

public static String getName(FireworkEffect.Type type) {
if (type == null) {
return null;
}
String name = typeToName.get(type);
if (name != null) {
return name;
}
return StringUtil.capitalizeFirstLetter(type.name(), '_');
}
}
187 changes: 129 additions & 58 deletions src/main/java/com/Acrobot/ChestShop/Listeners/ItemInfoListener.java
Expand Up @@ -4,40 +4,56 @@
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 org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.DyeColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
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.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.BookMeta.Generation;
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.PotionMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;

import com.Acrobot.Breeze.Utils.EnchantmentNames;
import com.Acrobot.Breeze.Utils.PotionNames;
import com.Acrobot.ChestShop.Events.ItemInfoEvent;

/**
* @author Acrobot
*/
public class ItemInfoListener implements Listener {

@EventHandler(priority = EventPriority.LOWEST)
public static void addDamage(ItemInfoEvent event) {
ItemStack item = event.getItem();
@EventHandler
public static void addInfo(ItemInfoEvent event) {
CommandSender sender = event.getSender();
ItemStack item = event.getItem();
Material type = item.getType();
ItemMeta meta = item.getItemMeta();

int maxdurability = item.getType().getMaxDurability();
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);
Expand All @@ -49,24 +65,21 @@ public static void addDamage(ItemInfoEvent event) {
sender.sendMessage(" " + ChatColor.RED + "Additional Repair Cost: " + repairable.getRepairCost());
}
}
}

@EventHandler(priority = EventPriority.HIGH)
public static void addName(ItemInfoEvent event) {
ItemStack item = event.getItem();
CommandSender sender = event.getSender();
ItemMeta meta = item.getItemMeta();

if (meta.hasDisplayName()) {
sender.sendMessage(" " + ChatColor.GRAY + "Name: " + meta.getDisplayName());
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.getGeneration() != Generation.ORIGINAL) {
sender.sendMessage(" " + ChatColor.RED + capitalizeFirstLetter(book.getGeneration().name(), '_'));
}
}
}

@EventHandler
public static void addEnchantment(ItemInfoEvent event) {
ItemStack item = event.getItem();
CommandSender sender = event.getSender();
ItemMeta meta = item.getItemMeta();

Map<Enchantment, Integer> enchantments = item.getEnchantments();
for (Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
Expand All @@ -81,53 +94,111 @@ public static void addEnchantment(ItemInfoEvent event) {
}
}
}
}

@EventHandler
public static void addPotionInfo(ItemInfoEvent event) {
ItemStack item = event.getItem();
Material t = item.getType();
if (t != Material.POTION && t != Material.SPLASH_POTION && t != Material.LINGERING_POTION && t != Material.TIPPED_ARROW) {
return;
if (meta instanceof FireworkMeta) {
FireworkMeta firework = (FireworkMeta) meta;
sender.sendMessage(" " + ChatColor.GRAY + "Flight Duration: " + firework.getPower());
for (FireworkEffect effect : firework.getEffects()) {
sendFireworkEffect(sender, effect);
}
}

ItemMeta meta = item.getItemMeta();
if (!(meta instanceof PotionMeta)) {
return;
if (meta instanceof FireworkEffectMeta) {
FireworkEffectMeta fireworkEffect = (FireworkEffectMeta) meta;
if (fireworkEffect.hasEffect()) {
FireworkEffect effect = fireworkEffect.getEffect();
sendFireworkEffect(sender, effect);
}
}

PotionMeta potion = (PotionMeta) meta;

StringBuilder message = new StringBuilder(50);
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);
}
}
}

message.append(ChatColor.GRAY);
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));
}
}
}

PotionData base = potion.getBasePotionData();
if (base == null) {
return;
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 (base.isExtended()) {
message.append("Extended ");
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));
}
if (t == Material.SPLASH_POTION) {
message.append("Splash ");
colors = effect.getFadeColors();
if (colors != null && !colors.isEmpty()) {
sender.sendMessage(" " + ChatColor.GRAY + "Fade to " + colorListToString(colors));
}
if (t == Material.LINGERING_POTION) {
message.append("Lingering ");
if (effect.hasTrail()) {
sender.sendMessage(" " + ChatColor.GRAY + "Trail");
}

message.append(PotionNames.getName(base.getType())).append(' ');
if (base.isUpgraded()) {
message.append("II ");
if (effect.hasFlicker()) {
sender.sendMessage(" " + ChatColor.GRAY + "Twinkle");
}
CommandSender sender = event.getSender();

sender.sendMessage(" " + message.toString());
if (potion.hasCustomEffects()) {
for (PotionEffect effect : potion.getCustomEffects()) {
sender.sendMessage(" " + ChatColor.GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20));
}

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();
}
}

0 comments on commit 9dead2c

Please sign in to comment.