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
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
11b3deb
⚒️ Replace color char in debug statements
AyhamAl-Ali Feb 25, 2022
39d2097
Update src/main/java/ch/njol/skript/util/SkriptColor.java
AyhamAl-Ali Mar 8, 2022
b3a3fa7
Ready
AyhamAl-Ali Mar 8, 2022
743987c
Merge branch 'ench/debug-replace-chat-color' into ench/itemdata-tostring
AyhamAl-Ali Mar 27, 2022
7952529
🚀 Improve toString to ItemData
AyhamAl-Ali Mar 27, 2022
2953706
Remove unused imports
AyhamAl-Ali Mar 27, 2022
a4ca252
Revert "Merge branch 'ench/debug-replace-chat-color' into ench/itemda…
AyhamAl-Ali Mar 27, 2022
230f5c0
Details
AyhamAl-Ali Mar 27, 2022
d915a21
Temporarily add replaceColorChar method until the other pr is merged
AyhamAl-Ali Mar 31, 2022
faac015
Requested Changes - formatting
AyhamAl-Ali Nov 27, 2022
bb80a7c
Merge branch 'master' into ench/itemdata-tostring
AyhamAl-Ali Jan 1, 2023
2d56dc6
Add support for parsing itemtype after named and with lore addition
AyhamAl-Ali Apr 21, 2023
c5adbc4
Add itemtype parsing tests
AyhamAl-Ali Apr 21, 2023
28eec80
Fix tests
AyhamAl-Ali Apr 21, 2023
405e0f3
Fix tests 2
AyhamAl-Ali Apr 21, 2023
d44793b
Update src/main/java/ch/njol/skript/aliases/Aliases.java
AyhamAl-Ali Apr 22, 2023
c4c5ffd
Adding some helpful comments
AyhamAl-Ali Apr 22, 2023
05befaa
Remove file ending empty lines from test script
AyhamAl-Ali Apr 22, 2023
250182b
Merge branch 'dev/feature' into ench/itemdata-tostring
AyhamAl-Ali Oct 5, 2023
c6eb7bc
Merge branch 'dev/feature' into ench/itemdata-tostring
sovdeeth Mar 19, 2024
8ea3fec
Merge branch 'dev/feature' into ench/itemdata-tostring
Moderocky May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 77 additions & 30 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
*/
package ch.njol.skript.aliases;

import java.io.IOException;
import java.io.NotSerializableException;
import java.io.StreamCorruptedException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.BukkitUnsafe;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.bukkitutil.block.BlockCompat;
import ch.njol.skript.bukkitutil.block.BlockValues;
import ch.njol.skript.localization.GeneralWords;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Message;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.SkriptColor;
import ch.njol.skript.variables.Variables;
import ch.njol.util.EnumTypeAdapter;
import ch.njol.yggdrasil.Fields;
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
Expand All @@ -47,18 +51,18 @@
import org.bukkit.potion.PotionData;
import org.eclipse.jdt.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ch.njol.util.EnumTypeAdapter;
import ch.njol.skript.Skript;
import ch.njol.skript.bukkitutil.BukkitUnsafe;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.bukkitutil.block.BlockCompat;
import ch.njol.skript.bukkitutil.block.BlockValues;
import ch.njol.skript.localization.Message;
import ch.njol.skript.variables.Variables;
import ch.njol.yggdrasil.Fields;
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.StreamCorruptedException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

public class ItemData implements Cloneable, YggdrasilExtendedSerializable {

Expand Down Expand Up @@ -116,7 +120,8 @@ public static class OldItemData {
}

private final static Message m_named = new Message("aliases.named");

private final static Message m_with_lore = new Message("aliases.with lore");

/**
* Before 1.13, data values ("block states") are applicable to items.
*/
Expand Down Expand Up @@ -271,13 +276,55 @@ public String toString() {
}

public String toString(final boolean debug, final boolean plural) {
StringBuilder builder = new StringBuilder(Aliases.getMaterialName(this, plural));
StringBuilder b = new StringBuilder(Aliases.getMaterialName(this, plural));
ItemMeta meta = stack.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
builder.append(" ").append(m_named).append(" ");
builder.append(meta.getDisplayName());

Map<Enchantment, Integer> enchs = stack.getEnchantments();
if (!enchs.isEmpty()) {
b.append(Language.getSpaced("enchantments.of"));
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
int i = 0;
for (Entry<Enchantment, Integer> e : enchs.entrySet()) {
if (i != 0) {
if (i != enchs.size() - 1) {
b.append(", ");
} else {
b.append(" " + GeneralWords.and + " ");
}
}
Enchantment ench = e.getKey();
if (ench == null)
continue;
b.append(EnchantmentType.toString(ench));
b.append(" ");
b.append(e.getValue());
i++;
}
}
return builder.toString();

if (meta != null) {
if (meta.hasDisplayName()) {
b.append(" ").append(m_named).append(" ");
b.append("\"").append(SkriptColor.replaceColorChar(meta.getDisplayName())).append("\"");
}
if (meta.hasLore()) {
b.append(" ").append(m_with_lore).append(" ");
List<String> lore = meta.getLore();
int i = 0;
for (String l : lore) {
if (i != 0) {
if (i != lore.size() - 1) {
b.append(", ");
} else {
b.append(" " + GeneralWords.and + " ");
}
}
b.append("\"").append(SkriptColor.replaceColorChar(l)).append("\"");
i++;
}
}
}

return b.toString();
}

/**
Expand Down
42 changes: 1 addition & 41 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.RandomAccess;
import java.util.Set;

import ch.njol.skript.classes.Comparator.Relation;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -980,16 +979,6 @@ public String toString(final int flags, final @Nullable Adjective a) {

private String toString(final boolean debug, final int flags, final @Nullable Adjective a) {
final StringBuilder b = new StringBuilder();
// if (types.size() == 1 && !types.get(0).hasDataRange()) {
// if (getAmount() != 1)
// b.append(amount + " ");
// if (isAll())
// b.append(getAmount() == 1 ? "every " : "of every ");
// } else {
// if (getAmount() != 1)
// b.append(amount + " of ");
// b.append(isAll() ? "every " : "any ");
// }
final boolean plural = amount != 1 && amount != -1 || (flags & Language.F_PLURAL) != 0;
if (amount != -1 && amount != 1) {
b.append(amount + " ");
Expand All @@ -1007,36 +996,7 @@ private String toString(final boolean debug, final int flags, final @Nullable Ad
}
b.append(types.get(i).toString(debug, plural));
}
// final Map<Enchantment, Integer> enchs = enchantments;
// if (enchs == null)
// return "" + b.toString();
// b.append(Language.getSpaced("enchantments.of"));
// int i = 0;
// for (final Entry<Enchantment, Integer> e : enchs.entrySet()) {
// if (i != 0) {
// if (i != enchs.size() - 1)
// b.append(", ");
// else
// b.append(" " + GeneralWords.and + " ");
// }
// final Enchantment ench = e.getKey();
// if (ench == null)
// continue;
// b.append(EnchantmentType.toString(ench));
// b.append(" ");
// b.append(e.getValue());
// i++;
// }
// if (meta != null) {
// final ItemMeta m = (ItemMeta) meta;
// if (m.hasDisplayName()) {
// b.append(" " + m_named.toString() + " ");
// b.append("\"" + m.getDisplayName() + "\"");
// }
// if (debug)
// b.append(" meta=[").append(meta).append("]");
// }
return "" + b.toString();
return b.toString();
}

public static String toString(final ItemStack i) {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/ch/njol/skript/util/SkriptColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
Expand Down Expand Up @@ -62,7 +63,7 @@ public enum SkriptColor implements Color {

DARK_PURPLE(DyeColor.PURPLE, ChatColor.DARK_PURPLE),
LIGHT_PURPLE(DyeColor.MAGENTA, ChatColor.LIGHT_PURPLE);

private final static Map<String, SkriptColor> names = new HashMap<>();
private final static Set<SkriptColor> colors = new HashSet<>();
private final static String LANGUAGE_NODE = "colors";
Expand Down Expand Up @@ -219,7 +220,18 @@ public static SkriptColor fromWoolData(short data) {
}
return null;
}


/**
* Replace chat color character '§' with '&'
* This is an alternative method to {@link ChatColor#stripColor(String)}
* But does not strip the color code.
* @param s string to replace chat color character of.
* @return String with replaced chat color character
*/
public static String replaceColorChar(String s) {
return s.replace('\u00A7', '&');
}

@Override
public String toString() {
return adjective == null ? "" + name() : adjective.toString(-1, 0);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ aliases:
of: of(?: any)?

named: named
with lore: with lore

# -- Enchantments --
enchantments:
Expand Down