检查项目知识是否包含字符串 (loren.contains("§eSigned from "))
Checking if item lore contains contains string (loren.contains("§eSigned from "))
我只想检查:
if (lore.contains("§eSigned of ")) {
但它并不知道它确实包含“§eSigned of”
我写了一个 Minecraft 命令 /sign
你可以给一个物品添加传说(“playerrank | playername 签名”)。
然后我想添加一个 /unsign
命令来删除这个传说。
ItemStack is = p.getItemInHand();
ItemMeta im = is.getItemMeta();
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<String>();
if (lore.contains("§eSigned of " + getChatName(p))) { // this line is important!
for (int i = 0; i < 3; i++) {
int size = lore.size();
lore.remove(size - 1);
}
im.setLore(lore);
is.setItemMeta(im);
p.setItemInHand(is);
sendMessage(p, "§aThis item is no longer signed");
} else {
sendMessage(p, "§aThis item is not signed!");
}
return CommandResult.None;
一切正常,直到您例如改变你的名字。比你不能删除标志因为 getChatName(p)
已经改变了。
要解决这个问题,我只想检查
if (lore.contains("§eSigned of ")) {
但它没有得到它并且 returns 错误。 (它说知识不包含“§eSigned of”)
我尝试了很多,但它只适用于字符串“§eSigned of”和 getChatName(p)
。
由于文档“包含”搜索特定的字符串,所以它应该像我想的那样工作吗?
添加:
getChatName(p)
returns 玩家等级和玩家名,如:“会员 | domi”
sendMessage(p, "")
在 Minecraft 聊天中发送一条简单消息
您 运行 遇到的问题是 contains(String)
寻找匹配的字符串。您搜索的是检查列表中是否有任何字符串以“§eSigned of”开头。
我建议添加这样的函数 isSignedItem
:
private boolean isSignedItem(List<String> lore) {
for (String st : lore)
if (st.startsWith("§eSigned of "))
return true;
return false;
}
然后使用此功能检查项目是否已签名:
[...]
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<String>();
if (isSignedItem(lore)) { // this line is important!
for (int i = 0; i < 3; i++) {
int size = lore.size();
lore.remove(size - 1);
}
[...]
我只想检查:
if (lore.contains("§eSigned of ")) {
但它并不知道它确实包含“§eSigned of”
我写了一个 Minecraft 命令 /sign
你可以给一个物品添加传说(“playerrank | playername 签名”)。
然后我想添加一个 /unsign
命令来删除这个传说。
ItemStack is = p.getItemInHand();
ItemMeta im = is.getItemMeta();
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<String>();
if (lore.contains("§eSigned of " + getChatName(p))) { // this line is important!
for (int i = 0; i < 3; i++) {
int size = lore.size();
lore.remove(size - 1);
}
im.setLore(lore);
is.setItemMeta(im);
p.setItemInHand(is);
sendMessage(p, "§aThis item is no longer signed");
} else {
sendMessage(p, "§aThis item is not signed!");
}
return CommandResult.None;
一切正常,直到您例如改变你的名字。比你不能删除标志因为 getChatName(p)
已经改变了。
要解决这个问题,我只想检查
if (lore.contains("§eSigned of ")) {
但它没有得到它并且 returns 错误。 (它说知识不包含“§eSigned of”)
我尝试了很多,但它只适用于字符串“§eSigned of”和 getChatName(p)
。
由于文档“包含”搜索特定的字符串,所以它应该像我想的那样工作吗?
添加:
getChatName(p)
returns 玩家等级和玩家名,如:“会员 | domi”
sendMessage(p, "")
在 Minecraft 聊天中发送一条简单消息
您 运行 遇到的问题是 contains(String)
寻找匹配的字符串。您搜索的是检查列表中是否有任何字符串以“§eSigned of”开头。
我建议添加这样的函数 isSignedItem
:
private boolean isSignedItem(List<String> lore) {
for (String st : lore)
if (st.startsWith("§eSigned of "))
return true;
return false;
}
然后使用此功能检查项目是否已签名:
[...]
List<String> lore = im.hasLore() ? im.getLore() : new ArrayList<String>();
if (isSignedItem(lore)) { // this line is important!
for (int i = 0; i < 3; i++) {
int size = lore.size();
lore.remove(size - 1);
}
[...]