Skip to content

Commit

Permalink
Apply new line (%nr%) in item lore correctly (#3636)
Browse files Browse the repository at this point in the history
* Apply new line in item lore

* Add test case for item with lore

* Remove needless clone()

* fix test case of item with lore

* Use Expression#stream instead of Arrays.stream
  • Loading branch information
yukukotani authored Jan 11, 2021
1 parent 2610d30 commit 0841a4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package ch.njol.skript.expressions;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.event.Event;
import org.bukkit.inventory.meta.ItemMeta;
Expand Down Expand Up @@ -62,10 +64,13 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean kleenean

@Override
protected ItemType[] get(Event e, ItemType[] source) {
String[] lore = this.lore.getArray(e);
List<String> lore = this.lore.stream(e)
.flatMap(l -> Arrays.stream(l.split("\n")))
.collect(Collectors.toList());

return get(source, item -> {
ItemMeta meta = item.getItemMeta();
meta.setLore(Arrays.asList(lore));
meta.setLore(lore);
item.setItemMeta(meta);
return item;
});
Expand Down
19 changes: 19 additions & 0 deletions src/test/skript/tests/syntaxes/expressions/ExprItemWithLore.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test "item with lore as one string without newline":
set {_i1} to 1 of stone with lore "first line"
assert the line 1 of lore of {_i1} is "first line" with "has correct first line"

test "item with lore as multiple string without newline":
set {_i2} to 1 of stone with lore "first line" and "second line"
assert the line 1 of lore of {_i2} is "first line" with "has correct first line"
assert the line 2 of lore of {_i2} is "second line" with "has correct second line"

test "item with lore as one string with newline":
set {_i3} to 1 of stone with lore "first line%nl%second line"
assert the line 1 of lore of {_i3} is "first line" with "has correct first line"
assert the line 2 of lore of {_i3} is "second line" with "has correct second line"

test "item with lore as multiple string with newline":
set {_i4} to 1 of stone with lore "first line%nl%second line" and "third line"
assert the line 1 of lore of {_i4} is "first line" with "has correct first line"
assert the line 2 of lore of {_i4} is "second line" with "has correct second line"
assert the line 3 of lore of {_i4} is "third line" with "has correct third line"

0 comments on commit 0841a4f

Please sign in to comment.