Add itemlore command (#3331)

Co-authored-by: pop4959 <pop4959@gmail.com>
Co-authored-by: MD <1917406+md678685@users.noreply.github.com>

Adds a command to add to or clear an item's lore.

Closes #1911.
This commit is contained in:
Josh Roy 2020-07-06 14:46:57 -04:00 committed by GitHub
parent 016a1b3421
commit 711bfed557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 0 deletions

View File

@ -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<String> 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<String> 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<String> getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) {
if (args.length == 1) {
return Lists.newArrayList("add", "set", "clear");
} else {
return Collections.emptyList();
}
}
}

View File

@ -326,6 +326,14 @@ itemCannotBeSold=\u00a74That item cannot be sold to the server.
itemCommandDescription=Spawn an item.
itemCommandUsage=/<command> <item|numeric> [amount [itemmeta...]]
itemId=\u00a76ID\:\u00a7c {0}
itemloreClear=\u00a76You have cleared this item''s lore.
itemloreCommandDescription=Edit the lore of an item.
itemloreCommandUsage=/<command> <add/set/clear> [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.

View File

@ -216,6 +216,10 @@ commands:
description: Searches for an item.
usage: /<command> <item>
aliases: [dura,edura,durability,edurability,eitemdb,itemno,eitemno]
itemlore:
description: Edit the lore of an item.
usage: /<command> <add/set/clear> [text/line] [text]
aliases: [lore, elore, ilore, eilore, eitemlore]
itemname:
description: Names an item.
usage: /<command> [name]