Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Improve toString of ItemData #4707

Closed

Conversation

AyhamAl-Ali
Copy link
Member

@AyhamAl-Ali AyhamAl-Ali commented Mar 27, 2022

Description

This PR should be merged at least after #4630
Enhance toString of items to include enchantments, lore, fix colors and add quotation marks around the name
After PR
image

It's no longer a breaking change as of commit however, I am keeping the label to note in the release changelog that the output of itemtypes has been updated so if someone is depending on that it will be a breaking change.


Target Minecraft Versions: Any
Requirements: None
Related Issues:

@AyhamAl-Ali AyhamAl-Ali added the enhancement Feature request, an issue about something that could be improved, or a PR improving something. label Mar 27, 2022
@AyhamAl-Ali AyhamAl-Ali changed the title Ench/itemdata tostring 🚀 Improve toString to ItemData Mar 27, 2022
@AyhamAl-Ali AyhamAl-Ali changed the title 🚀 Improve toString to ItemData 🚀 Improve toString of ItemData Mar 27, 2022
@pawelkuwa
Copy link

where can i download skript nightly with it?

@AyhamAl-Ali
Copy link
Member Author

where can i download skript nightly with it?

You can locate it now in the checks tab > artifacts or from this link

@pawelkuwa
Copy link

image
doesn't give item :(

@APickledWalrus
Copy link
Member

image doesn't give item :(

It's because the ItemType parser does not support parsing to this extent. We can expand the output here, but it won't be useful for parsing the string version of an item type.

@pawelkuwa
Copy link

why it won't be usefull? in some scripts i save items into string for store items in yaml but i must use my special function for read and for other people it is terrible

@APickledWalrus
Copy link
Member

The issue is you won't be able to parse the string as an item (as you encountered above when you stated that it doesn't give you the item)

Perhaps we could change this in the future, but you should probably use something like skript-yaml which IIRC will serialize items for you (meaning you don't need to store them as strings)

@AyhamAl-Ali
Copy link
Member Author

If you don't want to use the suggestion above you better split its attributes when saving then parse it when needed something like this

itemtype: stone
name: "&aCustom Name"
lore: ["lore 1", "lore 2", "lore 3"]

script would be

# set {_itemtype}, {_name} and {_lore} to the yml values
set {_item} to {_itemtype} parsed as itemtype named {_name} with lore {_lore}

@AyhamAl-Ali AyhamAl-Ali added the breaking changes Pull or feature requests that contain breaking changes (API, syntax, etc.) label Jul 17, 2022
src/main/java/ch/njol/skript/aliases/ItemType.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/aliases/ItemData.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/aliases/ItemData.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/aliases/ItemData.java Outdated Show resolved Hide resolved
Copy link
Collaborator

@TheLimeGlass TheLimeGlass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AyhamAl-Ali conflicts

@TheLimeGlass
Copy link
Collaborator

TheLimeGlass commented Jan 2, 2023

I think Aliases#parseItemType should support parsing of this addition (Just name and lore need to be added from the looks of it), that way parsing will work. I don't think we should completely remove the ability to parse what ItemType returns.

@Moderocky Moderocky force-pushed the master branch 2 times, most recently from bd134d0 to 3f08853 Compare September 16, 2023 16:59
@Moderocky Moderocky changed the base branch from master to dev/feature September 18, 2023 09:18
@AyhamAl-Ali AyhamAl-Ali added the feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version. label Oct 5, 2023
@sovdeeth sovdeeth added the 2.8 Targeting a 2.8.X version release label Dec 30, 2023
@sovdeeth sovdeeth removed 2.8 Targeting a 2.8.X version release feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version. labels Jan 1, 2024
@AyhamAl-Ali AyhamAl-Ali added the feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version. label Mar 15, 2024
@sovdeeth sovdeeth added the 2.9 Targeting a 2.9.X version release label Mar 19, 2024
Comment on lines +237 to +244
private final static RegexMessage p_named = new RegexMessage("aliases.named", "(.+) ", " \"(.+)\"", Pattern.CASE_INSENSITIVE);
private final static RegexMessage p_with_lore = new RegexMessage("aliases.with lore", "(.+) ", " (\".+\")", Pattern.CASE_INSENSITIVE);

private final static Pattern ENCHANTMENTS_PATTERN = Pattern.compile("\\s*(,|" + Pattern.quote(Language.get("and")) + ")\\s*");
private final static Pattern MULTIPLE_VALUE_SPLIT_PATTERN = Pattern.compile("(, ?| " + Pattern.quote(Language.get("and")) + " )");

private final static Message m_named = new Message("aliases.named");
private final static Message m_with_lore = new Message("aliases.with lore");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final static RegexMessage p_named = new RegexMessage("aliases.named", "(.+) ", " \"(.+)\"", Pattern.CASE_INSENSITIVE);
private final static RegexMessage p_with_lore = new RegexMessage("aliases.with lore", "(.+) ", " (\".+\")", Pattern.CASE_INSENSITIVE);
private final static Pattern ENCHANTMENTS_PATTERN = Pattern.compile("\\s*(,|" + Pattern.quote(Language.get("and")) + ")\\s*");
private final static Pattern MULTIPLE_VALUE_SPLIT_PATTERN = Pattern.compile("(, ?| " + Pattern.quote(Language.get("and")) + " )");
private final static Message m_named = new Message("aliases.named");
private final static Message m_with_lore = new Message("aliases.with lore");
private static final RegexMessage p_named = new RegexMessage("aliases.named", "(.+) ", " \"(.+)\"", Pattern.CASE_INSENSITIVE);
private static final RegexMessage p_with_lore = new RegexMessage("aliases.with lore", "(.+) ", " (\".+\")", Pattern.CASE_INSENSITIVE);
private static final Pattern ENCHANTMENTS_PATTERN = Pattern.compile("\\s*(,|" + Pattern.quote(Language.get("and")) + ")\\s*");
private static final Pattern MULTIPLE_VALUE_SPLIT_PATTERN = Pattern.compile("(, ?| " + Pattern.quote(Language.get("and")) + " )");
private static final Message m_named = new Message("aliases.named");
private static final Message m_with_lore = new Message("aliases.with lore");

s = "" + s.trim();

final ItemType t = new ItemType();
value = "" + value.trim();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value = "" + value.trim();
value = value.trim();

These should be removable. IIRC they are leftover from old null analysis

Comment on lines +265 to +273
itemtype.setAmount(Utils.parseInt("" + matcher.group(1)));
itemtype.setAll(true);
value = "" + matcher.group(matcher.groupCount());
} else if ((matcher = p_of.matcher(value)).matches()) {
itemtype.setAmount(Utils.parseInt("" + matcher.group(1)));
value = "" + matcher.group(matcher.groupCount());
} else if ((matcher = p_every.matcher(value)).matches()) {
itemtype.setAll(true);
value = "" + matcher.group(matcher.groupCount());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
itemtype.setAmount(Utils.parseInt("" + matcher.group(1)));
itemtype.setAll(true);
value = "" + matcher.group(matcher.groupCount());
} else if ((matcher = p_of.matcher(value)).matches()) {
itemtype.setAmount(Utils.parseInt("" + matcher.group(1)));
value = "" + matcher.group(matcher.groupCount());
} else if ((matcher = p_every.matcher(value)).matches()) {
itemtype.setAll(true);
value = "" + matcher.group(matcher.groupCount());
itemtype.setAmount(Utils.parseInt(matcher.group(1)));
itemtype.setAll(true);
value = matcher.group(matcher.groupCount());
} else if ((matcher = p_of.matcher(value)).matches()) {
itemtype.setAmount(Utils.parseInt(matcher.group(1)));
value = matcher.group(matcher.groupCount());
} else if ((matcher = p_every.matcher(value)).matches()) {
itemtype.setAll(true);
value = matcher.group(matcher.groupCount());

try (BlockingLogHandler ignored = new BlockingLogHandler().start()) {
if (parseType("" + s.substring(0, c), t2, false) == null)
if (parseType("" + value.substring(0, character), clonedItemtype, false) == null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (parseType("" + value.substring(0, character), clonedItemtype, false) == null)
if (parseType(value.substring(0, character), clonedItemtype, false) == null)

// otherwise the regex will include the lore as a name

// Name
matcher = p_named.matcher(value.substring(character, hasLore ? lowercase.indexOf(" " + m_with_lore + " ") : lowercase.length()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the name contains with lore?

Comment on lines +265 to +267
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
if (i != 0) {
if (i != enchantments.size() - 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
if (i != 0) {
if (i != enchantments.size() - 1) {
int size = enchantments.size();
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
if (i != 0) {
if (i != size - 1) {

if (meta != null) {
if (meta.hasDisplayName()) {
builder.append(" ").append(m_named).append(" ");
builder.append("\"").append(SkriptColor.replaceColorChar(meta.getDisplayName())).append("\"");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should color this by default. IMO it should be up to the user to color the string.

@APickledWalrus
Copy link
Member

This PR also likely needs to account for the fact that some ItemDatas do not represent ItemStacks anymore

@APickledWalrus APickledWalrus removed feature-ready A PR/issue that has been approved, tested and can be merged/closed in the next feature version. 2.9 Targeting a 2.9.X version release labels Jul 1, 2024
@sovdeeth
Copy link
Member

Closing due to inactivity

@sovdeeth sovdeeth closed this Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking changes Pull or feature requests that contain breaking changes (API, syntax, etc.) enhancement Feature request, an issue about something that could be improved, or a PR improving something.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants