From 11b3deb5fc0d25659ebc8fc18ce302fe05f774b3 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Fri, 25 Feb 2022 20:12:40 +0300 Subject: [PATCH 01/16] =?UTF-8?q?=E2=9A=92=EF=B8=8F=20Replace=20color=20ch?= =?UTF-8?q?ar=20in=20debug=20statements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ch/njol/skript/ScriptLoader.java | 7 ++++--- .../java/ch/njol/skript/command/Commands.java | 11 ++++++----- .../java/ch/njol/skript/lang/TriggerItem.java | 3 ++- .../java/ch/njol/skript/util/SkriptColor.java | 15 ++++++++++++++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/njol/skript/ScriptLoader.java b/src/main/java/ch/njol/skript/ScriptLoader.java index b552e7b9531..a0a987b96f7 100644 --- a/src/main/java/ch/njol/skript/ScriptLoader.java +++ b/src/main/java/ch/njol/skript/ScriptLoader.java @@ -57,6 +57,7 @@ import ch.njol.skript.sections.SecLoop; import ch.njol.skript.util.Date; import ch.njol.skript.util.ExceptionUtils; +import ch.njol.skript.util.SkriptColor; import ch.njol.skript.util.Task; import ch.njol.skript.variables.TypeHints; import ch.njol.skript.variables.Variables; @@ -741,7 +742,7 @@ private static ScriptInfo loadScript(@Nullable Config config) { continue; if (Skript.debug() || node.debug()) - Skript.debug(event + " (" + parsedEvent.getSecond().toString(null, true) + "):"); + Skript.debug(SkriptColor.replaceColorChar(event + " (" + parsedEvent.getSecond().toString(null, true) + "):")); Class[] eventClasses = parsedEvent.getSecond().getEventClasses(); if (eventClasses == null) @@ -1115,7 +1116,7 @@ public static ArrayList loadItems(SectionNode node) { continue; if (Skript.debug() || n.debug()) - Skript.debug(getParser().getIndentation() + stmt.toString(null, true)); + Skript.debug(SkriptColor.replaceColorChar(getParser().getIndentation() + stmt.toString(null, true))); items.add(stmt); } else if (n instanceof SectionNode) { @@ -1129,7 +1130,7 @@ public static ArrayList loadItems(SectionNode node) { continue; if (Skript.debug() || n.debug()) - Skript.debug(getParser().getIndentation() + section.toString(null, true)); + Skript.debug(SkriptColor.replaceColorChar(getParser().getIndentation() + section.toString(null, true))); items.add(section); diff --git a/src/main/java/ch/njol/skript/command/Commands.java b/src/main/java/ch/njol/skript/command/Commands.java index 064a88e60ca..6a1cac04bde 100644 --- a/src/main/java/ch/njol/skript/command/Commands.java +++ b/src/main/java/ch/njol/skript/command/Commands.java @@ -33,6 +33,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import ch.njol.skript.util.SkriptColor; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -277,15 +278,15 @@ static boolean handleEffectCommand(final CommandSender sender, String command) { log.clear(); // ignore warnings and stuff log.printLog(); - sender.sendMessage(ChatColor.GRAY + "executing '" + ChatColor.stripColor(command) + "'"); + sender.sendMessage(ChatColor.GRAY + "executing '" + SkriptColor.replaceColorChar(command) + "'"); if (SkriptConfig.logPlayerCommands.value() && !(sender instanceof ConsoleCommandSender)) - Skript.info(sender.getName() + " issued effect command: " + command); + Skript.info(sender.getName() + " issued effect command: " + SkriptColor.replaceColorChar(command)); TriggerItem.walk(e, new EffectCommandEvent(sender, command)); } else { if (sender == Bukkit.getConsoleSender()) // log as SEVERE instead of INFO like printErrors below - SkriptLogger.LOGGER.severe("Error in: " + ChatColor.stripColor(command)); + SkriptLogger.LOGGER.severe("Error in: " + SkriptColor.replaceColorChar(command)); else - sender.sendMessage(ChatColor.RED + "Error in: " + ChatColor.GRAY + ChatColor.stripColor(command)); + sender.sendMessage(ChatColor.RED + "Error in: " + ChatColor.GRAY + SkriptColor.replaceColorChar(command)); log.printErrors(sender, "(No specific information is available)"); } } finally { @@ -293,7 +294,7 @@ static boolean handleEffectCommand(final CommandSender sender, String command) { } return true; } catch (final Exception e) { - Skript.exception(e, "Unexpected error while executing effect command '" + command + "' by '" + sender.getName() + "'"); + Skript.exception(e, "Unexpected error while executing effect command '" + SkriptColor.replaceColorChar(command) + "' by '" + sender.getName() + "'"); sender.sendMessage(ChatColor.RED + "An internal error occurred while executing this effect. Please refer to the server log for details."); return true; } diff --git a/src/main/java/ch/njol/skript/lang/TriggerItem.java b/src/main/java/ch/njol/skript/lang/TriggerItem.java index 997d89aca4e..a3a45289936 100644 --- a/src/main/java/ch/njol/skript/lang/TriggerItem.java +++ b/src/main/java/ch/njol/skript/lang/TriggerItem.java @@ -20,6 +20,7 @@ import java.io.File; +import ch.njol.skript.util.SkriptColor; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; @@ -124,7 +125,7 @@ public String getIndentation() { protected final void debug(final Event e, final boolean run) { if (!Skript.debug()) return; - Skript.debug(getIndentation() + (run ? "" : "-") + toString(e, true)); + Skript.debug(SkriptColor.replaceColorChar(getIndentation() + (run ? "" : "-") + toString(e, true))); } @Override diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index b1052c445cb..e1dc9713064 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -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; @@ -66,6 +67,7 @@ public enum SkriptColor implements Color { private final static Map names = new HashMap<>(); private final static Set colors = new HashSet<>(); private final static String LANGUAGE_NODE = "colors"; + private final static Pattern COLOR_CHAR_PATTERN = Pattern.compile("§"); static { colors.addAll(Arrays.asList(values())); @@ -219,7 +221,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 COLOR_CHAR_PATTERN.matcher(s).replaceAll("&"); + } + @Override public String toString() { return adjective == null ? "" + name() : adjective.toString(-1, 0); From 39d209770344a68017d72194053a0a0a570d5321 Mon Sep 17 00:00:00 2001 From: Ayham Al Ali Date: Wed, 9 Mar 2022 00:07:45 +0300 Subject: [PATCH 02/16] Update src/main/java/ch/njol/skript/util/SkriptColor.java Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> --- src/main/java/ch/njol/skript/util/SkriptColor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index e1dc9713064..ccafad83f9a 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -230,7 +230,7 @@ public static SkriptColor fromWoolData(short data) { * @return String with replaced chat color character */ public static String replaceColorChar(String s) { - return COLOR_CHAR_PATTERN.matcher(s).replaceAll("&"); + return s.replace('\u00A7', '&'); } @Override From b3a3fa754e54f9dfa82234427962ea40d506dc26 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Wed, 9 Mar 2022 00:08:45 +0300 Subject: [PATCH 03/16] Ready --- src/main/java/ch/njol/skript/util/SkriptColor.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index ccafad83f9a..7f3935fa3f5 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -63,11 +63,10 @@ public enum SkriptColor implements Color { DARK_PURPLE(DyeColor.PURPLE, ChatColor.DARK_PURPLE), LIGHT_PURPLE(DyeColor.MAGENTA, ChatColor.LIGHT_PURPLE); - + private final static Map names = new HashMap<>(); private final static Set colors = new HashSet<>(); private final static String LANGUAGE_NODE = "colors"; - private final static Pattern COLOR_CHAR_PATTERN = Pattern.compile("§"); static { colors.addAll(Arrays.asList(values())); From 7952529fa11382f91ac000e2a614e4a022541191 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 28 Mar 2022 00:30:08 +0300 Subject: [PATCH 04/16] =?UTF-8?q?=F0=9F=9A=80=20Improve=20toString=20to=20?= =?UTF-8?q?ItemData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ch/njol/skript/aliases/ItemData.java | 105 +++++++++++++----- .../java/ch/njol/skript/aliases/ItemType.java | 41 +------ src/main/resources/lang/default.lang | 1 + 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/ItemData.java b/src/main/java/ch/njol/skript/aliases/ItemData.java index 8dcb8060b5f..7d9dcfa9d5d 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemData.java +++ b/src/main/java/ch/njol/skript/aliases/ItemData.java @@ -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; @@ -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 { @@ -116,7 +120,8 @@ public static class OldItemData { } private final static Message m_named = new Message("aliases.named"); - + private final static Message m_withLore = new Message("aliases.with lore"); + /** * Before 1.13, data values ("block states") are applicable to items. */ @@ -271,13 +276,53 @@ 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 enchs = stack.getEnchantments(); + if (!enchs.isEmpty()) { + b.append(Language.getSpaced("enchantments.of")); + int i = 0; + for (Entry 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_withLore).append(" "); + List 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; } /** diff --git a/src/main/java/ch/njol/skript/aliases/ItemType.java b/src/main/java/ch/njol/skript/aliases/ItemType.java index 3d2e88519e4..f6fc4c1dd5c 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemType.java +++ b/src/main/java/ch/njol/skript/aliases/ItemType.java @@ -980,16 +980,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 + " "); @@ -1007,36 +997,7 @@ private String toString(final boolean debug, final int flags, final @Nullable Ad } b.append(types.get(i).toString(debug, plural)); } -// final Map enchs = enchantments; -// if (enchs == null) -// return "" + b.toString(); -// b.append(Language.getSpaced("enchantments.of")); -// int i = 0; -// for (final Entry 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; } public static String toString(final ItemStack i) { diff --git a/src/main/resources/lang/default.lang b/src/main/resources/lang/default.lang index 8ffe537e750..bca01db9e74 100644 --- a/src/main/resources/lang/default.lang +++ b/src/main/resources/lang/default.lang @@ -44,6 +44,7 @@ aliases: of: of(?: any)? named: named + with lore: with lore # -- Enchantments -- enchantments: From 2953706b738bf7d8340899cc005d1a6fbdf5e51d Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 28 Mar 2022 00:31:04 +0300 Subject: [PATCH 05/16] Remove unused imports --- src/main/java/ch/njol/skript/aliases/ItemType.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/aliases/ItemType.java b/src/main/java/ch/njol/skript/aliases/ItemType.java index f6fc4c1dd5c..40653881f7d 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemType.java +++ b/src/main/java/ch/njol/skript/aliases/ItemType.java @@ -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; From a4ca252e1fd6d49f582b46d143ff2ec1b3912c94 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 28 Mar 2022 00:22:44 +0300 Subject: [PATCH 06/16] Revert "Merge branch 'ench/debug-replace-chat-color' into ench/itemdata-tostring" This reverts commit 743987c030a02f727f23dc6c2271e61a00c6b46a. --- src/main/java/ch/njol/skript/ScriptLoader.java | 7 +++---- .../java/ch/njol/skript/command/Commands.java | 11 +++++------ .../java/ch/njol/skript/lang/TriggerItem.java | 3 +-- .../java/ch/njol/skript/util/SkriptColor.java | 16 ++-------------- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/main/java/ch/njol/skript/ScriptLoader.java b/src/main/java/ch/njol/skript/ScriptLoader.java index a0a987b96f7..b552e7b9531 100644 --- a/src/main/java/ch/njol/skript/ScriptLoader.java +++ b/src/main/java/ch/njol/skript/ScriptLoader.java @@ -57,7 +57,6 @@ import ch.njol.skript.sections.SecLoop; import ch.njol.skript.util.Date; import ch.njol.skript.util.ExceptionUtils; -import ch.njol.skript.util.SkriptColor; import ch.njol.skript.util.Task; import ch.njol.skript.variables.TypeHints; import ch.njol.skript.variables.Variables; @@ -742,7 +741,7 @@ private static ScriptInfo loadScript(@Nullable Config config) { continue; if (Skript.debug() || node.debug()) - Skript.debug(SkriptColor.replaceColorChar(event + " (" + parsedEvent.getSecond().toString(null, true) + "):")); + Skript.debug(event + " (" + parsedEvent.getSecond().toString(null, true) + "):"); Class[] eventClasses = parsedEvent.getSecond().getEventClasses(); if (eventClasses == null) @@ -1116,7 +1115,7 @@ public static ArrayList loadItems(SectionNode node) { continue; if (Skript.debug() || n.debug()) - Skript.debug(SkriptColor.replaceColorChar(getParser().getIndentation() + stmt.toString(null, true))); + Skript.debug(getParser().getIndentation() + stmt.toString(null, true)); items.add(stmt); } else if (n instanceof SectionNode) { @@ -1130,7 +1129,7 @@ public static ArrayList loadItems(SectionNode node) { continue; if (Skript.debug() || n.debug()) - Skript.debug(SkriptColor.replaceColorChar(getParser().getIndentation() + section.toString(null, true))); + Skript.debug(getParser().getIndentation() + section.toString(null, true)); items.add(section); diff --git a/src/main/java/ch/njol/skript/command/Commands.java b/src/main/java/ch/njol/skript/command/Commands.java index 6a1cac04bde..064a88e60ca 100644 --- a/src/main/java/ch/njol/skript/command/Commands.java +++ b/src/main/java/ch/njol/skript/command/Commands.java @@ -33,7 +33,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import ch.njol.skript.util.SkriptColor; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -278,15 +277,15 @@ static boolean handleEffectCommand(final CommandSender sender, String command) { log.clear(); // ignore warnings and stuff log.printLog(); - sender.sendMessage(ChatColor.GRAY + "executing '" + SkriptColor.replaceColorChar(command) + "'"); + sender.sendMessage(ChatColor.GRAY + "executing '" + ChatColor.stripColor(command) + "'"); if (SkriptConfig.logPlayerCommands.value() && !(sender instanceof ConsoleCommandSender)) - Skript.info(sender.getName() + " issued effect command: " + SkriptColor.replaceColorChar(command)); + Skript.info(sender.getName() + " issued effect command: " + command); TriggerItem.walk(e, new EffectCommandEvent(sender, command)); } else { if (sender == Bukkit.getConsoleSender()) // log as SEVERE instead of INFO like printErrors below - SkriptLogger.LOGGER.severe("Error in: " + SkriptColor.replaceColorChar(command)); + SkriptLogger.LOGGER.severe("Error in: " + ChatColor.stripColor(command)); else - sender.sendMessage(ChatColor.RED + "Error in: " + ChatColor.GRAY + SkriptColor.replaceColorChar(command)); + sender.sendMessage(ChatColor.RED + "Error in: " + ChatColor.GRAY + ChatColor.stripColor(command)); log.printErrors(sender, "(No specific information is available)"); } } finally { @@ -294,7 +293,7 @@ static boolean handleEffectCommand(final CommandSender sender, String command) { } return true; } catch (final Exception e) { - Skript.exception(e, "Unexpected error while executing effect command '" + SkriptColor.replaceColorChar(command) + "' by '" + sender.getName() + "'"); + Skript.exception(e, "Unexpected error while executing effect command '" + command + "' by '" + sender.getName() + "'"); sender.sendMessage(ChatColor.RED + "An internal error occurred while executing this effect. Please refer to the server log for details."); return true; } diff --git a/src/main/java/ch/njol/skript/lang/TriggerItem.java b/src/main/java/ch/njol/skript/lang/TriggerItem.java index a3a45289936..997d89aca4e 100644 --- a/src/main/java/ch/njol/skript/lang/TriggerItem.java +++ b/src/main/java/ch/njol/skript/lang/TriggerItem.java @@ -20,7 +20,6 @@ import java.io.File; -import ch.njol.skript.util.SkriptColor; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; @@ -125,7 +124,7 @@ public String getIndentation() { protected final void debug(final Event e, final boolean run) { if (!Skript.debug()) return; - Skript.debug(SkriptColor.replaceColorChar(getIndentation() + (run ? "" : "-") + toString(e, true))); + Skript.debug(getIndentation() + (run ? "" : "-") + toString(e, true)); } @Override diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index 7f3935fa3f5..b1052c445cb 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -25,7 +25,6 @@ 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; @@ -63,7 +62,7 @@ public enum SkriptColor implements Color { DARK_PURPLE(DyeColor.PURPLE, ChatColor.DARK_PURPLE), LIGHT_PURPLE(DyeColor.MAGENTA, ChatColor.LIGHT_PURPLE); - + private final static Map names = new HashMap<>(); private final static Set colors = new HashSet<>(); private final static String LANGUAGE_NODE = "colors"; @@ -220,18 +219,7 @@ 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); From 230f5c01428789efb6f202f4ddeaa3cf6c77fcc6 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 28 Mar 2022 00:38:40 +0300 Subject: [PATCH 07/16] Details --- src/main/java/ch/njol/skript/aliases/ItemData.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/ItemData.java b/src/main/java/ch/njol/skript/aliases/ItemData.java index 7d9dcfa9d5d..dc1ba4d8349 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemData.java +++ b/src/main/java/ch/njol/skript/aliases/ItemData.java @@ -120,7 +120,7 @@ public static class OldItemData { } private final static Message m_named = new Message("aliases.named"); - private final static Message m_withLore = new Message("aliases.with lore"); + private final static Message m_with_lore = new Message("aliases.with lore"); /** * Before 1.13, data values ("block states") are applicable to items. @@ -306,7 +306,7 @@ public String toString(final boolean debug, final boolean plural) { b.append("\"").append(SkriptColor.replaceColorChar(meta.getDisplayName())).append("\""); } if (meta.hasLore()) { - b.append(" ").append(m_withLore).append(" "); + b.append(" ").append(m_with_lore).append(" "); List lore = meta.getLore(); int i = 0; for (String l : lore) { From d915a216a623e12e0927b094071c9b7db2612cff Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Thu, 31 Mar 2022 22:50:31 +0300 Subject: [PATCH 08/16] Temporarily add replaceColorChar method until the other pr is merged --- .../java/ch/njol/skript/util/SkriptColor.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/SkriptColor.java b/src/main/java/ch/njol/skript/util/SkriptColor.java index b1052c445cb..7f3935fa3f5 100644 --- a/src/main/java/ch/njol/skript/util/SkriptColor.java +++ b/src/main/java/ch/njol/skript/util/SkriptColor.java @@ -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; @@ -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 names = new HashMap<>(); private final static Set colors = new HashSet<>(); private final static String LANGUAGE_NODE = "colors"; @@ -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); From faac01573826e2f295ec11dd0e84784e60fae4f9 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Sun, 27 Nov 2022 15:47:49 +0300 Subject: [PATCH 09/16] Requested Changes - formatting --- src/main/java/ch/njol/skript/aliases/ItemData.java | 12 +++++++----- src/main/java/ch/njol/skript/aliases/ItemType.java | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/ItemData.java b/src/main/java/ch/njol/skript/aliases/ItemData.java index dc1ba4d8349..14dba25331c 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemData.java +++ b/src/main/java/ch/njol/skript/aliases/ItemData.java @@ -285,10 +285,11 @@ public String toString(final boolean debug, final boolean plural) { int i = 0; for (Entry e : enchs.entrySet()) { if (i != 0) { - if (i != enchs.size() - 1) + if (i != enchs.size() - 1) { b.append(", "); - else + } else { b.append(" " + GeneralWords.and + " "); + } } Enchantment ench = e.getKey(); if (ench == null) @@ -311,10 +312,11 @@ public String toString(final boolean debug, final boolean plural) { int i = 0; for (String l : lore) { if (i != 0) { - if (i != lore.size() - 1) + if (i != lore.size() - 1) { b.append(", "); - else + } else { b.append(" " + GeneralWords.and + " "); + } } b.append("\"").append(SkriptColor.replaceColorChar(l)).append("\""); i++; @@ -322,7 +324,7 @@ public String toString(final boolean debug, final boolean plural) { } } - return "" + b; + return b.toString(); } /** diff --git a/src/main/java/ch/njol/skript/aliases/ItemType.java b/src/main/java/ch/njol/skript/aliases/ItemType.java index 40653881f7d..940864ba6e7 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemType.java +++ b/src/main/java/ch/njol/skript/aliases/ItemType.java @@ -996,7 +996,7 @@ private String toString(final boolean debug, final int flags, final @Nullable Ad } b.append(types.get(i).toString(debug, plural)); } - return "" + b; + return b.toString(); } public static String toString(final ItemStack i) { From 2d56dc6d0c91967e0ec7d1b0ab1fb0e6a20785a7 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Fri, 21 Apr 2023 09:00:06 +0300 Subject: [PATCH 10/16] Add support for parsing itemtype after named and with lore addition --- .../java/ch/njol/skript/aliases/Aliases.java | 146 +++++++++++------- .../java/ch/njol/skript/aliases/ItemData.java | 39 ++--- 2 files changed, 110 insertions(+), 75 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 58aa0c9faeb..1314264dc6e 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -25,7 +25,6 @@ import ch.njol.skript.config.Node; import ch.njol.skript.config.SectionNode; import ch.njol.skript.entity.EntityData; -import org.skriptlang.skript.lang.script.Script; import ch.njol.skript.lang.parser.ParserInstance; import ch.njol.skript.localization.ArgsMessage; import ch.njol.skript.localization.Language; @@ -39,7 +38,9 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.inventory.meta.ItemMeta; import org.eclipse.jdt.annotation.Nullable; +import org.skriptlang.skript.lang.script.Script; import java.io.IOException; import java.io.UncheckedIOException; @@ -50,11 +51,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public abstract class Aliases { @@ -229,95 +232,126 @@ public static ItemType parseAlias(final String s) { private final static RegexMessage p_every = new RegexMessage("aliases.every", "", " (.+)", Pattern.CASE_INSENSITIVE); private final static RegexMessage p_of_every = new RegexMessage("aliases.of every", "(\\d+) ", " (.+)", Pattern.CASE_INSENSITIVE); private final static RegexMessage p_of = new RegexMessage("aliases.of", "(\\d+) (?:", " )?(.+)", Pattern.CASE_INSENSITIVE); - + 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"); + /** * Parses an ItemType. *

* Prints errors. * - * @param s + * @param value * @return The parsed ItemType or null if the input is invalid. */ @Nullable - public static ItemType parseItemType(String s) { - if (s.isEmpty()) + public static ItemType parseItemType(String value) { + if (value.isEmpty()) return null; - s = "" + s.trim(); + value = "" + value.trim(); - final ItemType t = new ItemType(); - - Matcher m; - if ((m = p_of_every.matcher(s)).matches()) { - t.setAmount(Utils.parseInt("" + m.group(1))); - t.setAll(true); - s = "" + m.group(m.groupCount()); - } else if ((m = p_of.matcher(s)).matches()) { - t.setAmount(Utils.parseInt("" + m.group(1))); - s = "" + m.group(m.groupCount()); - } else if ((m = p_every.matcher(s)).matches()) { - t.setAll(true); - s = "" + m.group(m.groupCount()); + ItemType itemtype = new ItemType(); + + // Amount + Matcher matcher; + if ((matcher = p_of_every.matcher(value)).matches()) { + 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()); } else { - final int l = s.length(); - s = Noun.stripIndefiniteArticle(s); - if (s.length() != l) // had indefinite article - t.setAmount(1); + int length = value.length(); + value = Noun.stripIndefiniteArticle(value); + if (value.length() != length) // had indefinite article + itemtype.setAmount(1); } - - String lc = s.toLowerCase(Locale.ENGLISH); - String of = Language.getSpaced("enchantments.of").toLowerCase(); - int c = -1; - outer: while ((c = lc.indexOf(of, c + 1)) != -1) { - ItemType t2 = t.clone(); + + String lowercase = value.toLowerCase(Locale.ENGLISH); + + // Enchantments + String of = Language.getSpaced("enchantments.of").toLowerCase(Locale.ENGLISH); + int character = -1; + outer: while ((character = lowercase.indexOf(of, character + 1)) != -1) { + ItemType clonedItemtype = itemtype.clone(); try (BlockingLogHandler ignored = new BlockingLogHandler().start()) { - if (parseType("" + s.substring(0, c), t2, false) == null) + if (parseType("" + value.substring(0, character), clonedItemtype, false) == null) continue; } - if (t2.numTypes() == 0) + if (clonedItemtype.numTypes() == 0) continue; - String[] enchs = lc.substring(c + of.length()).split("\\s*(,|" + Pattern.quote(Language.get("and")) + ")\\s*"); - for (final String ench : enchs) { - EnchantmentType e = EnchantmentType.parse("" + ench); - if (e == null) + + boolean hasName = lowercase.contains(" " + m_named + " "); + boolean hasLore = lowercase.contains(" " + m_with_lore + " "); + + // Name + matcher = p_named.matcher(value.substring(character, hasLore ? lowercase.indexOf(" " + m_with_lore + " ") : lowercase.length())); + if (matcher.matches()) { + ItemMeta meta = clonedItemtype.getItemMeta(); + meta.setDisplayName(matcher.group(2)); + clonedItemtype.setItemMeta(meta); + } + + // Lore + matcher = p_with_lore.matcher(value); + if (matcher.matches()) { + ItemMeta meta = clonedItemtype.getItemMeta(); + meta.setLore(Arrays.stream(MULTIPLE_VALUE_SPLIT_PATTERN.split(matcher.group(2))).map(line -> line.substring(1, line.length() -1)).collect(Collectors.toList())); // strip quotes + clonedItemtype.setItemMeta(meta); + } + int endOfSubstring = hasName ? lowercase.indexOf(" " + m_named + " ") : hasLore ? lowercase.indexOf(" " + m_with_lore + " ") : lowercase.length(); + String[] enchantments = ENCHANTMENTS_PATTERN.split(lowercase.substring(character + of.length(), endOfSubstring)); + for (String enchantment : enchantments) { + EnchantmentType enchantmentType = EnchantmentType.parse("" + enchantment); + if (enchantmentType == null) continue outer; - t2.addEnchantments(e); + clonedItemtype.addEnchantments(enchantmentType); } - return t2; + return clonedItemtype; } - - if (parseType(s, t, false) == null) + + if (parseType(value, itemtype, false) == null) return null; - - if (t.numTypes() == 0) + + if (itemtype.numTypes() == 0) return null; - - return t; + + return itemtype; } /** * Prints errors. * - * @param s The string holding the type, can be either a number or an alias, plus an optional data part. Case does not matter. - * @param t The ItemType to add the parsed ItemData(s) to (i.e. this ItemType will be modified) + * @param value The string holding the type, can be either a number or an alias, plus an optional data part. Case does not matter. + * @param itemtype The ItemType to add the parsed ItemData(s) to (i.e. this ItemType will be modified) * @param isAlias Whether this type is parsed for an alias. * @return The given item type or null if the input couldn't be parsed. */ @Nullable - private static ItemType parseType(final String s, final ItemType t, final boolean isAlias) { - ItemType i; - if (s.isEmpty()) { - t.add(new ItemData(Material.AIR)); - return t; - } else if (s.matches("\\d+")) { + private static ItemType parseType(String value, ItemType itemtype, boolean isAlias) { + ItemType itemtypeCompare; + if (value.isEmpty()) { + itemtype.add(new ItemData(Material.AIR)); + return itemtype; + } else if (value.matches("\\d+")) { return null; - } else if ((i = getAlias(s)) != null) { - for (ItemData d : i) { - t.add(d.clone()); + } else if ((itemtypeCompare = getAlias(value)) != null) { + for (ItemData itemData : itemtypeCompare) { + itemtype.add(itemData.clone()); } - return t; + return itemtype; } if (isAlias) - Skript.error(m_invalid_item_type.toString(s)); + Skript.error(m_invalid_item_type.toString(value)); return null; } diff --git a/src/main/java/ch/njol/skript/aliases/ItemData.java b/src/main/java/ch/njol/skript/aliases/ItemData.java index 7cc85ad31cb..188a20f5d42 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemData.java +++ b/src/main/java/ch/njol/skript/aliases/ItemData.java @@ -53,6 +53,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -248,56 +249,56 @@ public String toString() { return toString(false, false); } - public String toString(final boolean debug, final boolean plural) { - StringBuilder b = new StringBuilder(Aliases.getMaterialName(this, plural)); + public String toString(boolean debug, boolean plural) { + StringBuilder builder = new StringBuilder(Aliases.getMaterialName(this, plural)); ItemMeta meta = stack.getItemMeta(); - Map enchs = stack.getEnchantments(); - if (!enchs.isEmpty()) { - b.append(Language.getSpaced("enchantments.of")); + Map enchantments = stack.getEnchantments(); + if (!enchantments.isEmpty()) { + builder.append(Language.getSpaced("enchantments.of").toLowerCase(Locale.ENGLISH)); int i = 0; - for (Entry e : enchs.entrySet()) { + for (Entry e : enchantments.entrySet()) { if (i != 0) { - if (i != enchs.size() - 1) { - b.append(", "); + if (i != enchantments.size() - 1) { + builder.append(", "); } else { - b.append(" " + GeneralWords.and + " "); + builder.append(" " + GeneralWords.and + " "); } } Enchantment ench = e.getKey(); if (ench == null) continue; - b.append(EnchantmentType.toString(ench)); - b.append(" "); - b.append(e.getValue()); + builder.append(EnchantmentType.toString(ench)); + builder.append(" "); + builder.append(e.getValue()); i++; } } if (meta != null) { if (meta.hasDisplayName()) { - b.append(" ").append(m_named).append(" "); - b.append("\"").append(SkriptColor.replaceColorChar(meta.getDisplayName())).append("\""); + builder.append(" ").append(m_named).append(" "); + builder.append("\"").append(SkriptColor.replaceColorChar(meta.getDisplayName())).append("\""); } if (meta.hasLore()) { - b.append(" ").append(m_with_lore).append(" "); + builder.append(" ").append(m_with_lore).append(" "); List lore = meta.getLore(); int i = 0; for (String l : lore) { if (i != 0) { if (i != lore.size() - 1) { - b.append(", "); + builder.append(", "); } else { - b.append(" " + GeneralWords.and + " "); + builder.append(" " + GeneralWords.and + " "); } } - b.append("\"").append(SkriptColor.replaceColorChar(l)).append("\""); + builder.append("\"").append(SkriptColor.replaceColorChar(l)).append("\""); i++; } } } - return b.toString(); + return builder.toString(); } /** From c5adbc49ad48ce8164b48b35367b13311122c1d0 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 00:57:32 +0300 Subject: [PATCH 11/16] Add itemtype parsing tests --- .../regressions/4707-itemtype parsing.sk | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/test/skript/tests/regressions/4707-itemtype parsing.sk diff --git a/src/test/skript/tests/regressions/4707-itemtype parsing.sk b/src/test/skript/tests/regressions/4707-itemtype parsing.sk new file mode 100644 index 00000000000..62e8aab0890 --- /dev/null +++ b/src/test/skript/tests/regressions/4707-itemtype parsing.sk @@ -0,0 +1,83 @@ +test "itemtype parsing": + + # -- ASSIGNMENTS + + # amount, enchantment, name, multi line lore + set {_i1} to 3 of stone pickaxe of power 1 and unbreaking 2 named "Hello ""World!" with lore "test1", "test2" and "test3%nl%tes""t4" + # amount, enchantment, name, single line lore + set {_i2} to 5 of stone of power 10 and efficiency 5 named "Hello World!" with lore "test1" + # amount, enchantment, name + set {_i3} to dirt of power 4 and unbreaking 5 and efficiency 6 named "&aGreen Color" + # amount, enchantment + set {_i4} to 20 of diamond pickaxe of power 1 and unbreaking 2 + + # name + set {_i5} to dirt named "Hello ""World!" + # multi line lore + set {_i6} to stone pickaxe with lore "test1", "test2" and "test3%nl%tes""t4" + # multi line lore, name (order) + set {_i7} to stone pickaxe of power 1 with lore "test1", "test2" and "test3%nl%tes""t4" named "Hello ""World!" + # plain + set {_i8} to stone pickaxe + set {_i9} to "%{_i1}%" parsed as itemtype + + + # -- CHECKS + + assert name of {_i1} is "Hello ""World!" with "Itemtype test ##1.1 failed" + assert size of lore of {_i1} is 4 with "Itemtype test ##1.2 failed" + assert size of enchantments of {_i1} is 2 with "Itemtype test ##1.3 failed" + assert itemstack size of {_i1} is 3 with "Itemtype test ##1.4 failed" + assert type of {_i1} is stone pickaxe with "Itemtype test ##1.5 failed" + + assert name of {_i2} is "Hello World!" with "Itemtype test ##2.1 failed" + assert lore of {_i2} is "test1" with "Itemtype test ##2.2 failed" + assert level of power of {_i2} is 10 with "Itemtype test ##2.3 failed" + assert itemstack size of {_i2} is 5 with "Itemtype test ##2.4 failed" + assert type of {_i2} is stone with "Itemtype test ##2.5 failed" + + assert name of {_i3} is "&aGreen Color" with "Itemtype test ##3.1 failed" + assert lore of {_i3} is not set with "Itemtype test ##3.2 failed" + assert size of lore of {_i3} is 0 with "Itemtype test ##3.3 failed" + assert enchantments of {_i3} are power 4 and unbreaking 5 and efficiency 6 with "Itemtype test ##3.4 failed" + assert itemstack size of {_i3} is 1 with "Itemtype test ##3.5 failed" + assert type of {_i3} is dirt with "Itemtype test ##3.6 failed" + + assert name of {_i4} is not set with "Itemtype test ##4.1 failed" + assert lore of {_i4} is not set with "Itemtype test ##4.2 failed" + assert enchantments of {_i4} are power 1 and unbreaking 2 with "Itemtype test ##4.3 failed" + assert itemstack size of {_i4} is 1 with "Itemtype test ##4.4 failed" + assert type of {_i4} is dirt with "Itemtype test ##4.5 failed" + + assert name of {_i5} is "Hello ""World!" with "Itemtype test ##5.1 failed" + assert lore of {_i5} is not set with "Itemtype test ##5.2 failed" + assert enchantments of {_i5} is not set with "Itemtype test ##5.3 failed" + assert itemstack size of {_i5} is 20 with "Itemtype test ##5.4 failed" + assert type of {_i5} is diamond pickaxe with "Itemtype test ##5.5 failed" + + assert name of {_i6} is not set with "Itemtype test ##6.1 failed" + assert 4th element of lore of {_i6} is "tes""t4" with "Itemtype test ##6.2 failed" + assert enchantments of {_i6} is not set with "Itemtype test ##6.3 failed" + assert itemstack size of {_i6} is 1 with "Itemtype test ##6.4 failed" + assert type of {_i6} is stone pickaxe with "Itemtype test ##6.5 failed" + + assert name of {_i7} is "Hello ""World!" with "Itemtype test ##7.1 failed" + assert size of lore of {_i7} is 4 with "Itemtype test ##7.2 failed" + assert size of enchantments of {_i7} is 1 with "Itemtype test ##7.3 failed" + assert itemstack size of {_i7} is 1 with "Itemtype test ##7.4 failed" + assert type of {_i7} is stone pickaxe with "Itemtype test ##7.5 failed" + + assert name of {_i8} is not set with "Itemtype test ##8.1 failed" + assert lore of {_i8} is not set with "Itemtype test ##8.2 failed" + assert enchantments of {_i8} is not set with "Itemtype test ##8.3 failed" + assert itemstack size of {_i8} is 1 with "Itemtype test ##8.4 failed" + assert type of {_i8} is stone pickaxe with "Itemtype test ##8.5 failed" + + assert name of {_i9} is "Hello ""World!" with "Itemtype test ##9.1 failed" + assert size of lore of {_i9} is 4 with "Itemtype test ##9.2 failed" + assert size of enchantments of {_i9} is 2 with "Itemtype test ##9.3 failed" + assert itemstack size of {_i9} is 3 with "Itemtype test ##9.4 failed" + assert type of {_i9} is stone pickaxe with "Itemtype test ##9.5 failed" + + + From 28eec80116d0eea72cc6ebb36146d7de8ea014b4 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 01:10:57 +0300 Subject: [PATCH 12/16] Fix tests --- src/test/skript/tests/regressions/4707-itemtype parsing.sk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/skript/tests/regressions/4707-itemtype parsing.sk b/src/test/skript/tests/regressions/4707-itemtype parsing.sk index 62e8aab0890..3b442aec65f 100644 --- a/src/test/skript/tests/regressions/4707-itemtype parsing.sk +++ b/src/test/skript/tests/regressions/4707-itemtype parsing.sk @@ -39,13 +39,13 @@ test "itemtype parsing": assert name of {_i3} is "&aGreen Color" with "Itemtype test ##3.1 failed" assert lore of {_i3} is not set with "Itemtype test ##3.2 failed" assert size of lore of {_i3} is 0 with "Itemtype test ##3.3 failed" - assert enchantments of {_i3} are power 4 and unbreaking 5 and efficiency 6 with "Itemtype test ##3.4 failed" + assert enchantments of {_i3} contains power 4 and unbreaking 5 and efficiency 6 with "Itemtype test ##3.4 failed" assert itemstack size of {_i3} is 1 with "Itemtype test ##3.5 failed" assert type of {_i3} is dirt with "Itemtype test ##3.6 failed" assert name of {_i4} is not set with "Itemtype test ##4.1 failed" assert lore of {_i4} is not set with "Itemtype test ##4.2 failed" - assert enchantments of {_i4} are power 1 and unbreaking 2 with "Itemtype test ##4.3 failed" + assert enchantments of {_i4} contains power 1 and unbreaking 2 with "Itemtype test ##4.3 failed" assert itemstack size of {_i4} is 1 with "Itemtype test ##4.4 failed" assert type of {_i4} is dirt with "Itemtype test ##4.5 failed" From 405e0f3eb6549cb758288dd43b34d9dd74cdce7b Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 01:30:17 +0300 Subject: [PATCH 13/16] Fix tests 2 --- .../skript/tests/regressions/4707-itemtype parsing.sk | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/skript/tests/regressions/4707-itemtype parsing.sk b/src/test/skript/tests/regressions/4707-itemtype parsing.sk index 3b442aec65f..8b5683b03a6 100644 --- a/src/test/skript/tests/regressions/4707-itemtype parsing.sk +++ b/src/test/skript/tests/regressions/4707-itemtype parsing.sk @@ -46,14 +46,14 @@ test "itemtype parsing": assert name of {_i4} is not set with "Itemtype test ##4.1 failed" assert lore of {_i4} is not set with "Itemtype test ##4.2 failed" assert enchantments of {_i4} contains power 1 and unbreaking 2 with "Itemtype test ##4.3 failed" - assert itemstack size of {_i4} is 1 with "Itemtype test ##4.4 failed" - assert type of {_i4} is dirt with "Itemtype test ##4.5 failed" + assert itemstack size of {_i4} is 20 with "Itemtype test ##4.4 failed" + assert type of {_i4} is diamond pickaxe with "Itemtype test ##4.5 failed" assert name of {_i5} is "Hello ""World!" with "Itemtype test ##5.1 failed" assert lore of {_i5} is not set with "Itemtype test ##5.2 failed" assert enchantments of {_i5} is not set with "Itemtype test ##5.3 failed" - assert itemstack size of {_i5} is 20 with "Itemtype test ##5.4 failed" - assert type of {_i5} is diamond pickaxe with "Itemtype test ##5.5 failed" + assert itemstack size of {_i5} is 1 with "Itemtype test ##5.4 failed" + assert type of {_i5} is dirt with "Itemtype test ##5.5 failed" assert name of {_i6} is not set with "Itemtype test ##6.1 failed" assert 4th element of lore of {_i6} is "tes""t4" with "Itemtype test ##6.2 failed" From d44793b5c70398fed1f6dcb10001a34e30b4f1df Mon Sep 17 00:00:00 2001 From: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:40:23 +0300 Subject: [PATCH 14/16] Update src/main/java/ch/njol/skript/aliases/Aliases.java Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- src/main/java/ch/njol/skript/aliases/Aliases.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index 1314264dc6e..a3484e2001f 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -311,7 +311,7 @@ public static ItemType parseItemType(String value) { int endOfSubstring = hasName ? lowercase.indexOf(" " + m_named + " ") : hasLore ? lowercase.indexOf(" " + m_with_lore + " ") : lowercase.length(); String[] enchantments = ENCHANTMENTS_PATTERN.split(lowercase.substring(character + of.length(), endOfSubstring)); for (String enchantment : enchantments) { - EnchantmentType enchantmentType = EnchantmentType.parse("" + enchantment); + EnchantmentType enchantmentType = EnchantmentType.parse(enchantment); if (enchantmentType == null) continue outer; clonedItemtype.addEnchantments(enchantmentType); From c4c5ffdadc6f4c42751ea9a89fdfcaef3d0c96be Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:30:08 +0300 Subject: [PATCH 15/16] Adding some helpful comments --- src/main/java/ch/njol/skript/aliases/Aliases.java | 12 +++++++++--- src/main/java/ch/njol/skript/aliases/ItemData.java | 12 ++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/aliases/Aliases.java b/src/main/java/ch/njol/skript/aliases/Aliases.java index a3484e2001f..58a3a78e1f8 100644 --- a/src/main/java/ch/njol/skript/aliases/Aliases.java +++ b/src/main/java/ch/njol/skript/aliases/Aliases.java @@ -277,10 +277,9 @@ public static ItemType parseItemType(String value) { } String lowercase = value.toLowerCase(Locale.ENGLISH); - - // Enchantments String of = Language.getSpaced("enchantments.of").toLowerCase(Locale.ENGLISH); int character = -1; + outer: while ((character = lowercase.indexOf(of, character + 1)) != -1) { ItemType clonedItemtype = itemtype.clone(); try (BlockingLogHandler ignored = new BlockingLogHandler().start()) { @@ -293,6 +292,11 @@ public static ItemType parseItemType(String value) { boolean hasName = lowercase.contains(" " + m_named + " "); boolean hasLore = lowercase.contains(" " + m_with_lore + " "); + // Order of ItemType output matters here + // Order: AMOUNT TYPE ENCHANTMENTS NAME LORE + // e.g. Name is followed my Lore, so we need to check for name from its beginning to the beginning of `with lore` part + // 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())); if (matcher.matches()) { @@ -302,12 +306,14 @@ public static ItemType parseItemType(String value) { } // Lore - matcher = p_with_lore.matcher(value); + matcher = p_with_lore.matcher(value); // since no more values are printed after the lore we don't need to substring it like name if (matcher.matches()) { ItemMeta meta = clonedItemtype.getItemMeta(); meta.setLore(Arrays.stream(MULTIPLE_VALUE_SPLIT_PATTERN.split(matcher.group(2))).map(line -> line.substring(1, line.length() -1)).collect(Collectors.toList())); // strip quotes clonedItemtype.setItemMeta(meta); } + + // Substring to avoid including name and lore as enchantments int endOfSubstring = hasName ? lowercase.indexOf(" " + m_named + " ") : hasLore ? lowercase.indexOf(" " + m_with_lore + " ") : lowercase.length(); String[] enchantments = ENCHANTMENTS_PATTERN.split(lowercase.substring(character + of.length(), endOfSubstring)); for (String enchantment : enchantments) { diff --git a/src/main/java/ch/njol/skript/aliases/ItemData.java b/src/main/java/ch/njol/skript/aliases/ItemData.java index 188a20f5d42..d55236654d6 100644 --- a/src/main/java/ch/njol/skript/aliases/ItemData.java +++ b/src/main/java/ch/njol/skript/aliases/ItemData.java @@ -257,20 +257,20 @@ public String toString(boolean debug, boolean plural) { if (!enchantments.isEmpty()) { builder.append(Language.getSpaced("enchantments.of").toLowerCase(Locale.ENGLISH)); int i = 0; - for (Entry e : enchantments.entrySet()) { + for (Entry entry : enchantments.entrySet()) { if (i != 0) { if (i != enchantments.size() - 1) { builder.append(", "); } else { - builder.append(" " + GeneralWords.and + " "); + builder.append(" ").append(GeneralWords.and).append(" "); } } - Enchantment ench = e.getKey(); + Enchantment ench = entry.getKey(); if (ench == null) continue; builder.append(EnchantmentType.toString(ench)); builder.append(" "); - builder.append(e.getValue()); + builder.append(entry.getValue()); i++; } } @@ -284,12 +284,12 @@ public String toString(boolean debug, boolean plural) { builder.append(" ").append(m_with_lore).append(" "); List lore = meta.getLore(); int i = 0; - for (String l : lore) { + for (String l : lore) { // hasLore handles NPE if (i != 0) { if (i != lore.size() - 1) { builder.append(", "); } else { - builder.append(" " + GeneralWords.and + " "); + builder.append(" ").append(GeneralWords.and).append(" "); } } builder.append("\"").append(SkriptColor.replaceColorChar(l)).append("\""); From 05befaabc7e9ab282c91d545edb44d6314107186 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:31:05 +0300 Subject: [PATCH 16/16] Remove file ending empty lines from test script --- src/test/skript/tests/regressions/4707-itemtype parsing.sk | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/skript/tests/regressions/4707-itemtype parsing.sk b/src/test/skript/tests/regressions/4707-itemtype parsing.sk index 8b5683b03a6..e7f9b08f6bd 100644 --- a/src/test/skript/tests/regressions/4707-itemtype parsing.sk +++ b/src/test/skript/tests/regressions/4707-itemtype parsing.sk @@ -78,6 +78,3 @@ test "itemtype parsing": assert size of enchantments of {_i9} is 2 with "Itemtype test ##9.3 failed" assert itemstack size of {_i9} is 3 with "Itemtype test ##9.4 failed" assert type of {_i9} is stone pickaxe with "Itemtype test ##9.5 failed" - - -