2011-12-05 23:25:54 +01:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2022-02-13 21:33:51 +01:00
|
|
|
import com.earth2me.essentials.utils.MaterialUtil;
|
2021-02-05 21:27:33 +01:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2018-12-31 12:57:15 +01:00
|
|
|
import com.earth2me.essentials.utils.VersionUtil;
|
2012-06-10 19:31:28 +02:00
|
|
|
import org.bukkit.Material;
|
2011-12-05 23:25:54 +01:00
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2021-07-06 06:29:24 +02:00
|
|
|
import java.util.ArrayList;
|
2017-06-11 02:17:43 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public class Commanditemdb extends EssentialsCommand {
|
|
|
|
public Commanditemdb() {
|
|
|
|
super("itemdb");
|
|
|
|
}
|
2011-12-05 23:25:54 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
@Override
|
2020-10-03 19:46:05 +02:00
|
|
|
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
|
2015-04-15 06:06:16 +02:00
|
|
|
ItemStack itemStack = null;
|
|
|
|
boolean itemHeld = false;
|
2020-08-11 20:09:22 +02:00
|
|
|
if (args.length == 0) {
|
2017-12-13 07:06:25 +01:00
|
|
|
if (sender.isPlayer() && sender.getPlayer() != null) {
|
2015-04-15 06:06:16 +02:00
|
|
|
itemHeld = true;
|
2019-01-03 21:32:35 +01:00
|
|
|
itemStack = ess.getUser(sender.getPlayer()).getItemInHand();
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
if (itemStack == null) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
itemStack = ess.getItemDb().get(args[0]);
|
|
|
|
}
|
2017-12-13 07:06:25 +01:00
|
|
|
|
2019-01-05 17:28:53 +01:00
|
|
|
String itemId = "none";
|
2012-09-08 19:41:21 +02:00
|
|
|
|
2022-02-13 21:33:51 +01:00
|
|
|
if (VersionUtil.PRE_FLATTENING) {
|
2019-01-05 17:28:53 +01:00
|
|
|
itemId = itemStack.getType().getId() + ":" + itemStack.getDurability();
|
|
|
|
}
|
|
|
|
|
2024-02-03 21:38:14 +01:00
|
|
|
sender.sendTl("itemType", itemStack.getType().toString(), itemId);
|
2019-01-05 17:28:53 +01:00
|
|
|
|
|
|
|
// Don't send IDs twice
|
2024-02-03 21:38:14 +01:00
|
|
|
if (!sender.tl("itemType").contains("{1}") && !itemId.equals("none")) {
|
|
|
|
sender.sendTl("itemId", itemId);
|
2018-12-31 12:57:15 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
if (itemHeld && itemStack.getType() != Material.AIR) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final int maxuses = itemStack.getType().getMaxDurability();
|
2022-02-13 21:33:51 +01:00
|
|
|
final int durability = (maxuses + 1) - MaterialUtil.getDamage(itemStack);
|
2015-04-15 06:06:16 +02:00
|
|
|
if (maxuses != 0) {
|
2024-02-03 21:38:14 +01:00
|
|
|
sender.sendTl("durability", Integer.toString(durability));
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-05 21:27:33 +01:00
|
|
|
|
|
|
|
List<String> nameList = ess.getItemDb().nameList(itemStack);
|
2021-07-06 06:29:24 +02:00
|
|
|
nameList = nameList != null ? new ArrayList<>(nameList) : new ArrayList<>();
|
2021-02-05 21:27:33 +01:00
|
|
|
nameList.addAll(ess.getCustomItemResolver().getAliasesFor(ess.getItemDb().name(itemStack)));
|
2021-07-06 06:29:24 +02:00
|
|
|
if (nameList.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-05 21:27:33 +01:00
|
|
|
|
2021-07-06 06:29:24 +02:00
|
|
|
Collections.sort(nameList);
|
2021-02-05 21:27:33 +01:00
|
|
|
if (nameList.size() > 15) {
|
|
|
|
nameList = nameList.subList(0, 14);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2021-02-05 21:27:33 +01:00
|
|
|
final String itemNameList = StringUtil.joinList(", ", nameList);
|
2024-02-03 21:38:14 +01:00
|
|
|
sender.sendTl("itemNames", itemNameList);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
2017-06-11 02:17:43 +02:00
|
|
|
|
|
|
|
@Override
|
2020-10-03 19:46:05 +02:00
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
2017-06-11 02:17:43 +02:00
|
|
|
if (args.length == 1) {
|
|
|
|
return getItems();
|
|
|
|
} else {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2011-12-05 23:25:54 +01:00
|
|
|
}
|