Add more information to /iteminfo command and make messages configurable

This commit is contained in:
Phoenix616 2020-05-03 15:05:24 +01:00
parent 0d08880ed2
commit 432c505806
4 changed files with 72 additions and 23 deletions

View File

@ -2,10 +2,12 @@ package com.Acrobot.Breeze.Configuration;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author Acrobot * @author Acrobot
@ -26,6 +28,13 @@ public class ValueParser {
sb.append("\n- ").append(parseToYAML(o)); sb.append("\n- ").append(parseToYAML(o));
} }
return sb.toString(); return sb.toString();
} else if (object instanceof String) {
String[] lines = ((String) object).split("\\R");
if (lines.length == 1) {
return '\"' + String.valueOf(object) + '\"';
} else {
return "|-\n" + Arrays.stream(lines).map(s -> " " + s).collect(Collectors.joining("\n"));
}
} else { } else {
return '\"' + String.valueOf(object) + '\"'; return '\"' + String.valueOf(object) + '\"';
} }

View File

@ -17,6 +17,9 @@ import org.bukkit.inventory.ItemStack;
import java.util.logging.Level; import java.util.logging.Level;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_fullname;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_shopname;
import static com.Acrobot.ChestShop.Configuration.Messages.replace;
/** /**
* @author Acrobot * @author Acrobot
@ -41,9 +44,9 @@ public class ItemInfo implements CommandExecutor {
return false; return false;
} }
sender.sendMessage(Messages.prefix(iteminfo)); sender.sendMessage(iteminfo);
try { try {
sender.sendMessage(ChatColor.WHITE + "Full Name: " + ChatColor.GRAY + MaterialUtil.getName(item)); sender.sendMessage(replace(iteminfo_fullname, "item", MaterialUtil.getName(item)));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!"); 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); ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e);
@ -51,7 +54,7 @@ public class ItemInfo implements CommandExecutor {
} }
try { try {
sender.sendMessage(ChatColor.WHITE + "Shop Sign: " + ChatColor.GRAY + MaterialUtil.getSignName(item)); sender.sendMessage(Messages.replace(iteminfo_shopname, "item", MaterialUtil.getSignName(item)));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!"); sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!");
ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e); ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e);
@ -63,24 +66,4 @@ public class ItemInfo implements CommandExecutor {
return true; return true;
} }
private static String getNameAndID(ItemStack item) {
return MaterialUtil.getName(item);
}
private static String getDurability(ItemStack item) {
if (item.getDurability() != 0) {
return ChatColor.DARK_GREEN + ":" + Integer.toString(item.getDurability());
} else {
return "";
}
}
private static String getMetadata(ItemStack item) {
if (!item.hasItemMeta()) {
return "";
}
return ChatColor.GOLD + "#" + MaterialUtil.Metadata.getItemCode(item);
}
} }

View File

@ -9,6 +9,12 @@ import com.Acrobot.Breeze.Configuration.Configuration;
public class Messages { public class Messages {
public static String prefix = "&a[Shop] &r"; public static String prefix = "&a[Shop] &r";
public static String iteminfo = "&aItem Information: &r"; public static String iteminfo = "&aItem Information: &r";
public static String iteminfo_fullname = "&fFull Name: &7%item";
public static String iteminfo_shopname = "&fShop Sign: &7%item";
public static String iteminfo_repaircost = "&fRepair Cost: &7%cost";
public static String iteminfo_book = "&fBook Title: &7%title\n&fBook Author: &7%author\n&fBook Pages: &7%pages";
public static String iteminfo_book_generatopm = "&fBook Generation: &7%generation";
public static String iteminfo_lore = "&fLore: \n&r%lore";
@PrecededBySpace @PrecededBySpace
public static String ACCESS_DENIED = "You don't have permission to access that shop's storage container!"; public static String ACCESS_DENIED = "You don't have permission to access that shop's storage container!";
@ -93,4 +99,11 @@ public class Messages {
public static String prefix(String message) { public static String prefix(String message) {
return Configuration.getColoured(prefix + message); return Configuration.getColoured(prefix + message);
} }
public static String replace(String message, String... replacements) {
for (int i = 0; i + 1 < replacements.length; i+=2) {
message = message.replace("%" + replacements[i], replacements[i+1]);
}
return message;
}
} }

View File

@ -1,5 +1,7 @@
package com.Acrobot.ChestShop.Listeners; package com.Acrobot.ChestShop.Listeners;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Events.ItemInfoEvent; import com.Acrobot.ChestShop.Events.ItemInfoEvent;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@ -8,8 +10,10 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.potion.Potion; import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
@ -18,11 +22,25 @@ import java.util.Map;
import static com.Acrobot.Breeze.Utils.NumberUtil.toRoman; import static com.Acrobot.Breeze.Utils.NumberUtil.toRoman;
import static com.Acrobot.Breeze.Utils.NumberUtil.toTime; import static com.Acrobot.Breeze.Utils.NumberUtil.toTime;
import static com.Acrobot.Breeze.Utils.StringUtil.capitalizeFirstLetter; import static com.Acrobot.Breeze.Utils.StringUtil.capitalizeFirstLetter;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_book;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_book_generatopm;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_lore;
import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_repaircost;
import static com.Acrobot.ChestShop.Configuration.Messages.replace;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class ItemInfoListener implements Listener { public class ItemInfoListener implements Listener {
@EventHandler
public static void addRepairCost(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta instanceof Repairable && ((Repairable) meta).getRepairCost() > 0) {
event.getSender().sendMessage(replace(iteminfo_repaircost, "cost", String.valueOf(((Repairable) meta).getRepairCost())));
}
}
@EventHandler @EventHandler
public static void addEnchantment(ItemInfoEvent event) { public static void addEnchantment(ItemInfoEvent event) {
ItemStack item = event.getItem(); ItemStack item = event.getItem();
@ -80,4 +98,30 @@ public class ItemInfoListener implements Listener {
sender.sendMessage(ChatColor.DARK_GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20)); sender.sendMessage(ChatColor.DARK_GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20));
} }
} }
@EventHandler
public static void addBookInfo(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta instanceof BookMeta) {
BookMeta book = (BookMeta) meta;
event.getSender().sendMessage(replace(iteminfo_book,
"title", book.getTitle(),
"author", book.getAuthor(),
"pages", String.valueOf(book.getPageCount())
));
if (book.hasGeneration()) {
event.getSender().sendMessage(replace(iteminfo_book_generatopm,
"generation", StringUtil.capitalizeFirstLetter(book.getGeneration().name(), '_')
));
}
}
}
@EventHandler
public static void addLoreInfo(ItemInfoEvent event) {
ItemMeta meta = event.getItem().getItemMeta();
if (meta.hasLore()) {
event.getSender().sendMessage(Messages.replace(iteminfo_lore, "lore", String.join("\n", meta.getLore())));
}
}
} }