diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditemlore.java b/Essentials/src/com/earth2me/essentials/commands/Commanditemlore.java new file mode 100644 index 000000000..2780cccc6 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditemlore.java @@ -0,0 +1,75 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FormatUtil; +import com.earth2me.essentials.utils.NumberUtil; +import com.google.common.collect.Lists; +import org.bukkit.Server; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static com.earth2me.essentials.I18n.tl; + +public class Commanditemlore extends EssentialsCommand { + + public Commanditemlore() { + super("itemlore"); + } + + @Override + protected void run(Server server, User user, String commandLabel, String[] args) throws Exception { + ItemStack item = user.getBase().getItemInHand(); + if (item.getType().name().contains("AIR")) { + throw new Exception(tl("itemloreInvalidItem")); + } + + if (args.length == 0) { + throw new NotEnoughArgumentsException(); + } + + ItemMeta im = item.getItemMeta(); + if (args[0].equalsIgnoreCase("add") && args.length > 1) { + String line = FormatUtil.formatString(user, "essentials.itemlore", getFinalArg(args, 1)).trim(); + List lore = im.hasLore() ? im.getLore() : new ArrayList<>(); + lore.add(line); + im.setLore(lore); + item.setItemMeta(im); + user.sendMessage(tl("itemloreSuccess", line)); + } else if (args[0].equalsIgnoreCase("clear")) { + im.setLore(new ArrayList<>()); + item.setItemMeta(im); + user.sendMessage(tl("itemloreClear")); + } else if (args[0].equalsIgnoreCase("set") && args.length > 2 && NumberUtil.isInt(args[1])) { + if (!im.hasLore()) { + throw new Exception(tl("itemloreNoLore")); + } + + int line = Integer.parseInt(args[1]); + String newLine = FormatUtil.formatString(user, "essentials.itemlore", getFinalArg(args, 2)).trim(); + List lore = im.getLore(); + try { + lore.set(line - 1, newLine); + } catch (Exception e) { + throw new Exception(tl("itemloreNoLine", line), e); + } + im.setLore(lore); + item.setItemMeta(im); + user.sendMessage(tl("itemloreSuccessLore", line, newLine)); + } else { + throw new NotEnoughArgumentsException(); + } + } + + @Override + protected List getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) { + if (args.length == 1) { + return Lists.newArrayList("add", "set", "clear"); + } else { + return Collections.emptyList(); + } + } +} diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties index 0a0336820..aa10f4d57 100644 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -326,6 +326,14 @@ itemCannotBeSold=\u00a74That item cannot be sold to the server. itemCommandDescription=Spawn an item. itemCommandUsage=/ [amount [itemmeta...]] itemId=\u00a76ID\:\u00a7c {0} +itemloreClear=\u00a76You have cleared this item''s lore. +itemloreCommandDescription=Edit the lore of an item. +itemloreCommandUsage=/ [text/line] [text] +itemloreInvalidItem=\u00a74You need to hold an item to edit its lore. +itemloreNoLine=\u00a74Your held item does not have lore text on line \u00a7c{0}\u00a74. +itemloreNoLore=\u00a74Your held item does not have any lore text. +itemloreSuccess=\u00a76You have added "\u00a7c{0}\u00a76" to your held item''s lore. +itemloreSuccessLore=\u00a76You have set line \u00a7c{0}\u00a76 of your held item''s lore to "\u00a7c{1}\u00a76". itemMustBeStacked=\u00a74Item must be traded in stacks. A quantity of 2s would be two stacks, etc. itemNames=\u00a76Item short names\:\u00a7r {0} itemnameClear=\u00a76You have cleared this item''s name. diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index e7fcba4b2..3f2103cf7 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -216,6 +216,10 @@ commands: description: Searches for an item. usage: / aliases: [dura,edura,durability,edurability,eitemdb,itemno,eitemno] + itemlore: + description: Edit the lore of an item. + usage: / [text/line] [text] + aliases: [lore, elore, ilore, eilore, eitemlore] itemname: description: Names an item. usage: / [name]