Improve itemlore and itemname tab completion (#4280)

- Improve tab completion for itemname and itemlore
- Improve air type matching
- Fix incorrect /itemlore usage messages
This commit is contained in:
Josh Roy 2021-06-28 15:19:02 -04:00 committed by GitHub
parent 4e4eb35240
commit 9d3bf337e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 9 deletions

View File

@ -9,7 +9,6 @@ import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IEssentials;
import net.ess3.api.events.KitClaimEvent;
import org.bukkit.Bukkit;
@ -292,6 +291,6 @@ public class Kit {
}
private boolean isEmptyStack(ItemStack stack) {
return stack == null || stack.getType() == Material.AIR || (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_14_4_R01) && stack.getType().isAir());
return stack == null || MaterialUtil.isAir(stack.getType());
}
}

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.FormatUtil;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.NumberUtil;
import com.google.common.collect.Lists;
import org.bukkit.Server;
@ -11,6 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import static com.earth2me.essentials.I18n.tl;
@ -23,7 +25,7 @@ public class Commanditemlore extends EssentialsCommand {
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack item = user.getBase().getItemInHand();
if (item.getType().name().contains("AIR")) {
if (MaterialUtil.isAir(item.getType())) {
throw new Exception(tl("itemloreInvalidItem"));
}
@ -68,6 +70,34 @@ public class Commanditemlore extends EssentialsCommand {
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
if (args.length == 1) {
return Lists.newArrayList("add", "set", "clear");
} else if (args.length == 2) {
switch (args[0].toLowerCase(Locale.ENGLISH)) {
case "set": {
final ItemStack item = user.getBase().getItemInHand();
if (!MaterialUtil.isAir(item.getType()) && item.hasItemMeta() && item.getItemMeta().hasLore()) {
final List<String> lineNumbers = new ArrayList<>();
for (int i = 1; i <= item.getItemMeta().getLore().size(); i++) {
lineNumbers.add(String.valueOf(i));
}
return lineNumbers;
}
return Collections.emptyList();
}
case "clear":
case "add":
default: {
return Collections.emptyList();
}
}
} else if (args.length == 3) {
if (args[0].equalsIgnoreCase("set") && NumberUtil.isInt(args[1])) {
final int i = Integer.parseInt(args[1]);
final ItemStack item = user.getBase().getItemInHand();
if (!MaterialUtil.isAir(item.getType()) && item.hasItemMeta() && item.getItemMeta().hasLore() && item.getItemMeta().getLore().size() >= i) {
return Lists.newArrayList(FormatUtil.unformatString(user, "essentials.itemlore", item.getItemMeta().getLore().get(i - 1)));
}
}
return Collections.emptyList();
} else {
return Collections.emptyList();
}

View File

@ -1,13 +1,17 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.TriState;
import com.earth2me.essentials.utils.FormatUtil;
import org.bukkit.Material;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.TriState;
import com.google.common.collect.Lists;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Collections;
import java.util.List;
import static com.earth2me.essentials.I18n.tl;
public class Commanditemname extends EssentialsCommand {
@ -20,7 +24,7 @@ public class Commanditemname extends EssentialsCommand {
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack item = user.getBase().getItemInHand();
if (item.getType() == Material.AIR) {
if (MaterialUtil.isAir(item.getType())) {
user.sendMessage(tl("itemnameInvalidItem"));
return;
}
@ -42,4 +46,15 @@ public class Commanditemname extends EssentialsCommand {
item.setItemMeta(im);
user.sendMessage(name == null ? tl("itemnameClear") : tl("itemnameSuccess", name));
}
@Override
protected List<String> getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) {
if (args.length == 1) {
final ItemStack item = user.getBase().getItemInHand();
if (!MaterialUtil.isAir(item.getType()) && item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
return Lists.newArrayList(FormatUtil.unformatString(user, "essentials.itemname", item.getItemMeta().getDisplayName()));
}
}
return Collections.emptyList();
}
}

View File

@ -169,6 +169,10 @@ public final class MaterialUtil {
return isPlayerHead(material, -1) || isMobHead(material, -1);
}
public static boolean isAir(final Material material) {
return material == Material.AIR || (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_14_4_R01) && material.isAir());
}
public static Material convertFromLegacy(final int id, final byte damage) {
for (final Material material : EnumSet.allOf(Material.class)) {
if (material.getId() == id) {

View File

@ -502,11 +502,11 @@ 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]
itemloreCommandUsage1=/<command> <add> [text]
itemloreCommandUsage1=/<command> add [text]
itemloreCommandUsage1Description=Adds the given text to the end of the held item's lore
itemloreCommandUsage2=/<command> <set> <line number> <text>
itemloreCommandUsage2=/<command> set <line number> <text>
itemloreCommandUsage2Description=Sets the specified line of the held item's lore to the given text
itemloreCommandUsage3=/<command> <clear>
itemloreCommandUsage3=/<command> clear
itemloreCommandUsage3Description=Clears the held item's lore
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.