From b767f4a9a135bfeb9093f46b5b95dc6fb51cc95d Mon Sep 17 00:00:00 2001 From: AyhamAl-Ali Date: Tue, 7 Sep 2021 03:21:55 +0300 Subject: [PATCH 01/16] =?UTF-8?q?=F0=9F=9A=80=20Add=20regex=20replace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ch/njol/skript/effects/EffReplace.java | 72 +++++++++++-------- .../java/ch/njol/skript/lang/Expression.java | 2 +- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 5d945b0b8fd..83e63e6cd82 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -39,42 +39,47 @@ import ch.njol.util.Kleenean; import ch.njol.util.StringUtils; -/** - * @author Peter Güttinger - */ @Name("Replace") -@Description({"Replaces all occurrences of a given text with another text. Please note that you can only change variables and a few expressions, e.g. a message or a line of a sign.", - "Starting with 2.2-dev24, you can replace items in a inventory too."}) -@Examples({"replace \"\" in {textvar} with \"%item%\"", - "replace every \"&\" with \"§\" in line 1", - "# The following acts as a simple chat censor, but it will e.g. censor mass, hassle, assassin, etc. as well:", +@Description("Replaces all occurrences of a given text/regex with another text.") +@Examples({"replace \"\" in {_msg} with \"[%name of player's tool%]\"", + "replace every \"&\" with \"§\" in line 1 of targeted block", + "", + "# Very simple chat censor", "on chat:", " replace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", - " ", + " replace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", + "", "replace all stone and dirt in player's inventory and player's top inventory with diamond"}) -@Since("2.0, 2.2-dev24 (replace in muliple strings and replace items in inventory), 2.5 (replace first, case sensitivity)") +@Since("2.0, 2.2-dev24 (replace in multiple strings, replace items in inventory), 2.5 (replace first, case sensitivity), INSERT VERSION (regex)") public class EffReplace extends Effect { static { Skript.registerEffect(EffReplace.class, "replace (all|every|) %strings% in %strings% with %string% [(1¦with case sensitivity)]", "replace (all|every|) %strings% with %string% in %strings% [(1¦with case sensitivity)]", "replace first %strings% in %strings% with %string% [(1¦with case sensitivity)]", - "replace first %strings% with %string% in %string% [(1¦with case sensitivity)]", + "replace first %strings% with %string% in %strings% [(1¦with case sensitivity)]", + "(replace [using] regex|regex replace) %strings% in %strings% with %string%", + "(replace [using] regex|regex replace) %strings% with %string% in %strings%", "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", "replace (all|every|) %itemtypes% with %itemtype% in %inventories%"); } - + @SuppressWarnings("null") private Expression haystack, needles, replacement; - private boolean replaceString = true; - private boolean replaceFirst = false; + private boolean replaceString; + private boolean replaceRegex; + private boolean replaceItems; + private boolean replaceFirst; private boolean caseSensitive = false; - @SuppressWarnings({"null"}) + @Override - public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { + @SuppressWarnings("null") + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { haystack = exprs[1 + matchedPattern % 2]; replaceString = matchedPattern < 4; - replaceFirst = matchedPattern > 1 && matchedPattern < 4; + replaceFirst = matchedPattern == 2 || matchedPattern == 3; + replaceRegex = matchedPattern == 4 || matchedPattern == 5; + replaceItems = matchedPattern == 6 || matchedPattern == 7; if (replaceString && !ChangerUtils.acceptsChange(haystack, ChangeMode.SET, String.class)) { Skript.error(haystack + " cannot be changed and can thus not have parts replaced."); return false; @@ -86,31 +91,39 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final replacement = exprs[2 - matchedPattern % 2]; return true; } - - @SuppressWarnings("null") + @Override - protected void execute(final Event e) { + @SuppressWarnings("null") + protected void execute(Event e) { Object[] haystack = this.haystack.getAll(e); Object[] needles = this.needles.getAll(e); Object replacement = this.replacement.getSingle(e); if (replacement == null || haystack == null || haystack.length == 0 || needles == null || needles.length == 0) return; - if (replaceString) { + if (replaceString || replaceRegex) { if (replaceFirst) { for (int x = 0; x < haystack.length; x++) - for (final Object n : needles) { + for (Object n : needles) { assert n != null; - haystack[x] = StringUtils.replaceFirst((String)haystack[x], (String)n, Matcher.quoteReplacement((String)replacement), caseSensitive); + haystack[x] = StringUtils.replaceFirst((String) haystack[x], (String) n, Matcher.quoteReplacement((String) replacement), caseSensitive); + } + } else if (replaceRegex) { + for (int x = 0; x < haystack.length; x++) + for (Object n : needles) { + assert n != null; + try { + haystack[x] = ((String) haystack[x]).replaceAll((String) n, (String) replacement); + } catch (Exception ignored) {} } } else { for (int x = 0; x < haystack.length; x++) - for (final Object n : needles) { + for (Object n : needles) { assert n != null; haystack[x] = StringUtils.replace((String) haystack[x], (String) n, (String) replacement, caseSensitive); } } this.haystack.change(e, haystack, ChangeMode.SET); - } else { + } else if (replaceItems) { for (Inventory inv : (Inventory[]) haystack) for (ItemType item : (ItemType[]) needles) for (Integer slot : inv.all(item.getRandom()).keySet()) { @@ -118,13 +131,10 @@ protected void execute(final Event e) { } } } - + @Override - public String toString(final @Nullable Event e, final boolean debug) { - if (replaceFirst) - return "replace first " + needles.toString(e, debug) + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) - + "(case sensitive: " + caseSensitive + ")"; - return "replace " + needles.toString(e, debug) + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) + public String toString(@Nullable Event e, boolean debug) { + return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(e, debug) + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) + "(case sensitive: " + caseSensitive + ")"; } diff --git a/src/main/java/ch/njol/skript/lang/Expression.java b/src/main/java/ch/njol/skript/lang/Expression.java index 41f6b18df38..4f66e8e6493 100644 --- a/src/main/java/ch/njol/skript/lang/Expression.java +++ b/src/main/java/ch/njol/skript/lang/Expression.java @@ -53,7 +53,7 @@ public interface Expression extends SyntaxElement, Debuggable { /** * Get the single value of this expression. *

- * This method may only return null if it always returns null for the given event, i.e. it is equivalent to getting a random element out of {@link #getAll(Event)} or null iff + * This method may only return null if it always returns null for the given event, i.e. it is equivalent to getting a random element out of {@link #getAll(Event)} or null if * that array is empty. *

* Do not use this in conditions, use {@link #check(Event, Checker, boolean)} instead. From fbf31126adab00bc11cd9cd1d90ecd9c00ea80ef Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Fri, 24 Jun 2022 18:43:51 +0300 Subject: [PATCH 02/16] RC --- .../ch/njol/skript/effects/EffReplace.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 0a3ab1d4ddd..d3372a0ff7f 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -41,27 +41,29 @@ import java.util.regex.Matcher; @Name("Replace") -@Description("Replaces all occurrences of a given text/regex with another text. Please note that you can only change variables and a few expressions, e.g. a message or a line of a sign.") -@Examples({"replace \"\" in {_msg} with \"[%name of player's tool%]\"", - "replace every \"&\" with \"§\" in line 1 of targeted block", - "", - "# Very simple chat censor", - "on chat:", - " replace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", - " replace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", - "", - "replace all stone and dirt in player's inventory and player's top inventory with diamond"}) -@Since("2.0, 2.2-dev24 (replace in multiple strings, replace items in inventory), 2.5 (replace first, case sensitivity), INSERT VERSION (regex)") +@Description("Replaces all occurrences of a given text/regex with another text. Please note that you can only change " + + "variables and a few expressions, e.g. a message or a line of a sign.") +@Examples({ + "replace \"\" in {_msg} with \"[%name of player's tool%]\"", + "replace every \"&\" with \"§\" in line 1 of targeted block", + "", + "# Very simple chat censor", + "on chat:", + "\treplace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", + "\treplace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", + "", + "replace all stone and dirt in player's inventory and player's top inventory with diamond" +}) +@Since("2.0, 2.2-dev24 (multiple strings, items in inventory), 2.5 (replace first, case sensitivity)," + + "INSERT VERSION (regex)") public class EffReplace extends Effect { static { Skript.registerEffect(EffReplace.class, - "replace (all|every|) %strings% in %strings% with %string% [(1¦with case sensitivity)]", - "replace (all|every|) %strings% with %string% in %strings% [(1¦with case sensitivity)]", - "replace first %strings% in %strings% with %string% [(1¦with case sensitivity)]", - "replace first %strings% with %string% in %strings% [(1¦with case sensitivity)]", - "(replace [using] regex|regex replace) %strings% in %strings% with %string%", - "(replace [using] regex|regex replace) %strings% with %string% in %strings%", + "replace (all:(all|every)|:first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", + "replace (all:(all|every)|:first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", + "(regex:(replace [using] regex|regex replace)) %strings% in %strings% with %string%", + "(regex:(replace [using] regex|regex replace)) %strings% with %string% in %strings%", "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", "replace (all|every|) %itemtypes% with %itemtype% in %inventories%"); } @@ -78,15 +80,15 @@ public class EffReplace extends Effect { @SuppressWarnings("null") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { haystack = exprs[1 + matchedPattern % 2]; - replaceString = matchedPattern < 4; - replaceFirst = matchedPattern == 2 || matchedPattern == 3; - replaceRegex = matchedPattern == 4 || matchedPattern == 5; - replaceItems = matchedPattern == 6 || matchedPattern == 7; + replaceString = matchedPattern < 2; + replaceFirst = parseResult.hasTag("first"); + replaceRegex = parseResult.hasTag("regex"); + replaceItems = matchedPattern == 4 || matchedPattern == 5; if (replaceString && !ChangerUtils.acceptsChange(haystack, ChangeMode.SET, String.class)) { Skript.error(haystack + " cannot be changed and can thus not have parts replaced."); return false; } - if (SkriptConfig.caseSensitive.value() || parseResult.mark == 1) { + if (SkriptConfig.caseSensitive.value() || parseResult.hasTag("case")) { caseSensitive = true; } needles = exprs[0]; @@ -104,30 +106,33 @@ protected void execute(Event e) { return; if (replaceString || replaceRegex) { if (replaceFirst) { - for (int x = 0; x < haystack.length; x++) + for (int x = 0; x < haystack.length; x++) { for (Object n : needles) { assert n != null; haystack[x] = StringUtils.replaceFirst((String) haystack[x], (String) n, Matcher.quoteReplacement((String) replacement), caseSensitive); } + } } else if (replaceRegex) { - for (int x = 0; x < haystack.length; x++) + for (int x = 0; x < haystack.length; x++) { for (Object n : needles) { assert n != null; try { haystack[x] = ((String) haystack[x]).replaceAll((String) n, (String) replacement); } catch (Exception ignored) {} } + } } else { - for (int x = 0; x < haystack.length; x++) + for (int x = 0; x < haystack.length; x++) { for (Object n : needles) { assert n != null; haystack[x] = StringUtils.replace((String) haystack[x], (String) n, (String) replacement, caseSensitive); } + } } this.haystack.change(e, haystack, ChangeMode.SET); } else if (replaceItems) { - for (Inventory inv : (Inventory[]) haystack) - for (ItemType needle : (ItemType[]) needles) + for (Inventory inv : (Inventory[]) haystack) { + for (ItemType needle : (ItemType[]) needles) { for (Map.Entry entry : inv.all(needle.getMaterial()).entrySet()) { int slot = entry.getKey(); ItemStack itemStack = entry.getValue(); @@ -135,17 +140,19 @@ protected void execute(Event e) { if (new ItemType(itemStack).isSimilar(needle)) { ItemStack newItemStack = ((ItemType) replacement).getRandom(); newItemStack.setAmount(itemStack.getAmount()); - inv.setItem(slot, newItemStack); } } + } + } } } @Override public String toString(@Nullable Event e, boolean debug) { - return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(e, debug) + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) - + "(case sensitive: " + caseSensitive + ")"; + return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(e, debug) + + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) + + "(case sensitive: " + caseSensitive + ")"; } } From 58ef65402b5f125d7eafd49408e2542e10db51f4 Mon Sep 17 00:00:00 2001 From: Ayham Al Ali Date: Sat, 6 Aug 2022 03:04:28 +0300 Subject: [PATCH 03/16] Update src/main/java/ch/njol/skript/effects/EffReplace.java Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> --- src/main/java/ch/njol/skript/effects/EffReplace.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index d3372a0ff7f..68e3dadc3c9 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -152,7 +152,7 @@ protected void execute(Event e) { public String toString(@Nullable Event e, boolean debug) { return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(e, debug) + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) - + "(case sensitive: " + caseSensitive + ")"; + + (caseSensitive ? " with case sensitivity" : ""); } } From 94be1284ef639aeb993153d626a1f18ad32a8f2e Mon Sep 17 00:00:00 2001 From: Ayham Al Ali Date: Sat, 6 Aug 2022 03:57:06 +0300 Subject: [PATCH 04/16] Update src/main/java/ch/njol/skript/effects/EffReplace.java Co-authored-by: APickledWalrus --- src/main/java/ch/njol/skript/effects/EffReplace.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 68e3dadc3c9..4503a765553 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -54,8 +54,7 @@ "", "replace all stone and dirt in player's inventory and player's top inventory with diamond" }) -@Since("2.0, 2.2-dev24 (multiple strings, items in inventory), 2.5 (replace first, case sensitivity)," + - "INSERT VERSION (regex)") +@Since("2.0, 2.2-dev24 (multiple strings, items in inventory), 2.5 (replace first, case sensitivity), INSERT VERSION (regex)") public class EffReplace extends Effect { static { From 9f6b2df6f4b3f3ba76ffd7587bd9fcdde301ee88 Mon Sep 17 00:00:00 2001 From: Ayham Al Ali Date: Sat, 6 Aug 2022 03:57:29 +0300 Subject: [PATCH 05/16] Update src/main/java/ch/njol/skript/effects/EffReplace.java Co-authored-by: TPGamesNL <29547183+TPGamesNL@users.noreply.github.com> --- src/main/java/ch/njol/skript/effects/EffReplace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 4503a765553..4c938c66d22 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -61,8 +61,8 @@ public class EffReplace extends Effect { Skript.registerEffect(EffReplace.class, "replace (all:(all|every)|:first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", "replace (all:(all|every)|:first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", - "(regex:(replace [using] regex|regex replace)) %strings% in %strings% with %string%", - "(regex:(replace [using] regex|regex replace)) %strings% with %string% in %strings%", + "regex:(replace [using] regex|regex replace) %strings% in %strings% with %string%", + "regex:(replace [using] regex|regex replace) %strings% with %string% in %strings%", "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", "replace (all|every|) %itemtypes% with %itemtype% in %inventories%"); } From 785ef9467a671e58db0b00fd418692ddf11202c1 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Sat, 6 Aug 2022 03:57:49 +0300 Subject: [PATCH 06/16] Auto stash before merge of "ench/regex-replace" and "AyhamAl-Ali/ench/regex-replace" --- .../ch/njol/skript/effects/EffReplace.java | 26 ++++++++++++------- .../java/ch/njol/skript/lang/Expression.java | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 4c938c66d22..3fffed9161f 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -37,8 +37,11 @@ import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; +import java.util.regex.Pattern; @Name("Replace") @Description("Replaces all occurrences of a given text/regex with another text. Please note that you can only change " + @@ -67,7 +70,7 @@ public class EffReplace extends Effect { "replace (all|every|) %itemtypes% with %itemtype% in %inventories%"); } - @SuppressWarnings("null") + @SuppressWarnings("NotNullFieldNotInitialized") private Expression haystack, needles, replacement; private boolean replaceString; private boolean replaceRegex; @@ -79,12 +82,12 @@ public class EffReplace extends Effect { @SuppressWarnings("null") public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { haystack = exprs[1 + matchedPattern % 2]; - replaceString = matchedPattern < 2; + replaceString = matchedPattern < 4; replaceFirst = parseResult.hasTag("first"); replaceRegex = parseResult.hasTag("regex"); replaceItems = matchedPattern == 4 || matchedPattern == 5; if (replaceString && !ChangerUtils.acceptsChange(haystack, ChangeMode.SET, String.class)) { - Skript.error(haystack + " cannot be changed and can thus not have parts replaced."); + Skript.error(haystack + " cannot be changed and can thus not have parts replaced"); return false; } if (SkriptConfig.caseSensitive.value() || parseResult.hasTag("case")) { @@ -103,7 +106,7 @@ protected void execute(Event e) { Object replacement = this.replacement.getSingle(e); if (replacement == null || haystack == null || haystack.length == 0 || needles == null || needles.length == 0) return; - if (replaceString || replaceRegex) { + if (replaceString) { if (replaceFirst) { for (int x = 0; x < haystack.length; x++) { for (Object n : needles) { @@ -112,12 +115,17 @@ protected void execute(Event e) { } } } else if (replaceRegex) { + List patterns = new ArrayList<>(needles.length); + for (Object n : needles) { + assert n != null; + try { + patterns.add(Pattern.compile((String) n)); + } catch (Exception ignored) {} + } for (int x = 0; x < haystack.length; x++) { - for (Object n : needles) { - assert n != null; - try { - haystack[x] = ((String) haystack[x]).replaceAll((String) n, (String) replacement); - } catch (Exception ignored) {} + for (Pattern p : patterns) { + assert p != null; + haystack[x] = p.matcher((String) haystack[x]).replaceAll((String) replacement); } } } else { diff --git a/src/main/java/ch/njol/skript/lang/Expression.java b/src/main/java/ch/njol/skript/lang/Expression.java index 4476a654228..c0b58f166b0 100644 --- a/src/main/java/ch/njol/skript/lang/Expression.java +++ b/src/main/java/ch/njol/skript/lang/Expression.java @@ -53,7 +53,7 @@ public interface Expression extends SyntaxElement, Debuggable { /** * Get the single value of this expression. *

- * This method may only return null if it always returns null for the given event, i.e. it is equivalent to getting a random element out of {@link #getAll(Event)} or null if + * This method may only return null if it always returns null for the given event, i.e. it is equivalent to getting a random element out of {@link #getAll(Event)} or null iff * that array is empty. *

* Do not use this in conditions, use {@link #check(Event, Checker, boolean)} instead. From 5820bd98c07cc2dcf12ed4f444eff18177a3b7e2 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Sat, 6 Aug 2022 03:58:42 +0300 Subject: [PATCH 07/16] Add [the] --- src/main/java/ch/njol/skript/effects/EffReplace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 3fffed9161f..55d0b9bb1d8 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -62,8 +62,8 @@ public class EffReplace extends Effect { static { Skript.registerEffect(EffReplace.class, - "replace (all:(all|every)|:first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", - "replace (all:(all|every)|:first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", + "replace (all:(all|every)|:[the] first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", + "replace (all:(all|every)|:[the] first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", "regex:(replace [using] regex|regex replace) %strings% in %strings% with %string%", "regex:(replace [using] regex|regex replace) %strings% with %string% in %strings%", "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", From fbeb4ba60bf11b7cba38221d40f6bf2d79f53b10 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Sat, 6 Aug 2022 13:30:02 +0300 Subject: [PATCH 08/16] Fix build --- src/main/java/ch/njol/skript/effects/EffReplace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 55d0b9bb1d8..2ddabca3e89 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -62,8 +62,8 @@ public class EffReplace extends Effect { static { Skript.registerEffect(EffReplace.class, - "replace (all:(all|every)|:[the] first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", - "replace (all:(all|every)|:[the] first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", + "replace (all:(all|every)|first:[the] first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", + "replace (all:(all|every)|first:[the] first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", "regex:(replace [using] regex|regex replace) %strings% in %strings% with %string%", "regex:(replace [using] regex|regex replace) %strings% with %string% in %strings%", "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", From e603f6ced1e7840d68cd121c337415a380930db1 Mon Sep 17 00:00:00 2001 From: Ayham Al Ali Date: Mon, 2 Jan 2023 02:58:34 +0300 Subject: [PATCH 09/16] Update src/main/java/ch/njol/skript/effects/EffReplace.java Co-authored-by: Patrick Miller --- src/main/java/ch/njol/skript/effects/EffReplace.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 2ddabca3e89..2ab2a036dff 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -62,12 +62,12 @@ public class EffReplace extends Effect { static { Skript.registerEffect(EffReplace.class, - "replace (all:(all|every)|first:[the] first|) %strings% in %strings% with %string% [(case:with case sensitivity)]", - "replace (all:(all|every)|first:[the] first|) %strings% with %string% in %strings% [(case:with case sensitivity)]", + "replace [all:(all|every)|first:[the] first] %strings% in %strings% with %string% [case:with case sensitivity]", + "replace [all:(all|every)|first:[the] first] %strings% with %string% in %strings% [case:with case sensitivity]", "regex:(replace [using] regex|regex replace) %strings% in %strings% with %string%", "regex:(replace [using] regex|regex replace) %strings% with %string% in %strings%", - "replace (all|every|) %itemtypes% in %inventories% with %itemtype%", - "replace (all|every|) %itemtypes% with %itemtype% in %inventories%"); + "replace [all|every] %itemtypes% in %inventories% with %itemtype%", + "replace [all|every] %itemtypes% with %itemtype% in %inventories%"); } @SuppressWarnings("NotNullFieldNotInitialized") From 6f6241c236897f3934bcbdc6c29598fca6ee47b5 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 2 Jan 2023 03:00:51 +0300 Subject: [PATCH 10/16] Better variable naming --- .../ch/njol/skript/effects/EffReplace.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 2ab2a036dff..2a98d5ada7d 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -100,54 +100,54 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @SuppressWarnings("null") - protected void execute(Event e) { - Object[] haystack = this.haystack.getAll(e); - Object[] needles = this.needles.getAll(e); - Object replacement = this.replacement.getSingle(e); + protected void execute(Event event) { + Object[] haystack = this.haystack.getAll(event); + Object[] needles = this.needles.getAll(event); + Object replacement = this.replacement.getSingle(event); if (replacement == null || haystack == null || haystack.length == 0 || needles == null || needles.length == 0) return; if (replaceString) { if (replaceFirst) { - for (int x = 0; x < haystack.length; x++) { - for (Object n : needles) { - assert n != null; - haystack[x] = StringUtils.replaceFirst((String) haystack[x], (String) n, Matcher.quoteReplacement((String) replacement), caseSensitive); + for (int i = 0; i < haystack.length; i++) { + for (Object needle : needles) { + assert needle != null; + haystack[i] = StringUtils.replaceFirst((String) haystack[i], (String) needle, Matcher.quoteReplacement((String) replacement), caseSensitive); } } } else if (replaceRegex) { List patterns = new ArrayList<>(needles.length); - for (Object n : needles) { - assert n != null; + for (Object needle : needles) { + assert needle != null; try { - patterns.add(Pattern.compile((String) n)); + patterns.add(Pattern.compile((String) needle)); } catch (Exception ignored) {} } - for (int x = 0; x < haystack.length; x++) { + for (int i = 0; i < haystack.length; i++) { for (Pattern p : patterns) { assert p != null; - haystack[x] = p.matcher((String) haystack[x]).replaceAll((String) replacement); + haystack[i] = p.matcher((String) haystack[i]).replaceAll((String) replacement); } } } else { - for (int x = 0; x < haystack.length; x++) { - for (Object n : needles) { - assert n != null; - haystack[x] = StringUtils.replace((String) haystack[x], (String) n, (String) replacement, caseSensitive); + for (int i = 0; i < haystack.length; i++) { + for (Object needle : needles) { + assert needle != null; + haystack[i] = StringUtils.replace((String) haystack[i], (String) needle, (String) replacement, caseSensitive); } } } - this.haystack.change(e, haystack, ChangeMode.SET); + this.haystack.change(event, haystack, ChangeMode.SET); } else if (replaceItems) { - for (Inventory inv : (Inventory[]) haystack) { + for (Inventory inventory : (Inventory[]) haystack) { for (ItemType needle : (ItemType[]) needles) { - for (Map.Entry entry : inv.all(needle.getMaterial()).entrySet()) { + for (Map.Entry entry : inventory.all(needle.getMaterial()).entrySet()) { int slot = entry.getKey(); ItemStack itemStack = entry.getValue(); if (new ItemType(itemStack).isSimilar(needle)) { ItemStack newItemStack = ((ItemType) replacement).getRandom(); newItemStack.setAmount(itemStack.getAmount()); - inv.setItem(slot, newItemStack); + inventory.setItem(slot, newItemStack); } } } @@ -156,9 +156,9 @@ protected void execute(Event e) { } @Override - public String toString(@Nullable Event e, boolean debug) { - return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(e, debug) - + " in " + haystack.toString(e, debug) + " with " + replacement.toString(e, debug) + public String toString(@Nullable Event event, boolean debug) { + return "replace " + (replaceFirst ? "first " : (replaceRegex ? "regex " : "")) + needles.toString(event, debug) + + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) + (caseSensitive ? " with case sensitivity" : ""); } From 26f50142767c16e38570ce963be343b5240cd0d4 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali Date: Mon, 2 Jan 2023 03:01:32 +0300 Subject: [PATCH 11/16] p -> pattern --- src/main/java/ch/njol/skript/effects/EffReplace.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 2a98d5ada7d..1831bcd0685 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -123,9 +123,9 @@ protected void execute(Event event) { } catch (Exception ignored) {} } for (int i = 0; i < haystack.length; i++) { - for (Pattern p : patterns) { - assert p != null; - haystack[i] = p.matcher((String) haystack[i]).replaceAll((String) replacement); + for (Pattern pattern : patterns) { + assert pattern != null; + haystack[i] = pattern.matcher((String) haystack[i]).replaceAll((String) replacement); } } } else { From f55124d238fa6d73b388786723f3e75f89aa0987 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Tue, 22 Aug 2023 09:41:00 +0300 Subject: [PATCH 12/16] Fix merge to master conflicts --- src/main/java/ch/njol/skript/effects/EffReplace.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 69dad7c50ff..8865870fc18 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -101,7 +101,6 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @SuppressWarnings("null") - @Override protected void execute(Event event) { Object[] needles = this.needles.getAll(event); if (haystack instanceof ExpressionList) { @@ -170,9 +169,9 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) @Override public String toString(@Nullable Event event, boolean debug) { if (replaceFirst) - return "replace first " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) + return "replace first " + needles.toString(event, debug) + (replaceRegex ? " (regex) " : "") + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) + "(case sensitive: " + caseSensitive + ")"; - return "replace " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) + return "replace " + needles.toString(event, debug) + (replaceRegex ? " (regex) " : "") + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) + "(case sensitive: " + caseSensitive + ")"; } From bf321ab27feb1945d4f4f2437649ab4ca45a73ce Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Tue, 22 Aug 2023 09:54:12 +0300 Subject: [PATCH 13/16] Fix tests --- src/main/java/ch/njol/skript/effects/EffReplace.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 8865870fc18..18c318b246a 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -147,7 +147,7 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) } } } - this.haystack.change(event, haystack, ChangeMode.SET); + haystackExpr.change(event, haystack, ChangeMode.SET); } else if (replaceItems) { for (Inventory inventory : (Inventory[]) haystack) { for (ItemType needle : (ItemType[]) needles) { From d78fe227bfa229fdcf2efad0f986d43752535e70 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 6 Apr 2024 01:13:54 +0300 Subject: [PATCH 14/16] Address reviews - Add regex replace first - removed unused vars --- .../ch/njol/skript/effects/EffReplace.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 1831bcd0685..d966a9d2acb 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -52,8 +52,8 @@ "", "# Very simple chat censor", "on chat:", - "\treplace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", - "\treplace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", + "\treplace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", + "\treplace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", "", "replace all stone and dirt in player's inventory and player's top inventory with diamond" }) @@ -74,7 +74,6 @@ public class EffReplace extends Effect { private Expression haystack, needles, replacement; private boolean replaceString; private boolean replaceRegex; - private boolean replaceItems; private boolean replaceFirst; private boolean caseSensitive = false; @@ -85,7 +84,6 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye replaceString = matchedPattern < 4; replaceFirst = parseResult.hasTag("first"); replaceRegex = parseResult.hasTag("regex"); - replaceItems = matchedPattern == 4 || matchedPattern == 5; if (replaceString && !ChangerUtils.acceptsChange(haystack, ChangeMode.SET, String.class)) { Skript.error(haystack + " cannot be changed and can thus not have parts replaced"); return false; @@ -107,37 +105,42 @@ protected void execute(Event event) { if (replacement == null || haystack == null || haystack.length == 0 || needles == null || needles.length == 0) return; if (replaceString) { - if (replaceFirst) { - for (int i = 0; i < haystack.length; i++) { - for (Object needle : needles) { - assert needle != null; - haystack[i] = StringUtils.replaceFirst((String) haystack[i], (String) needle, Matcher.quoteReplacement((String) replacement), caseSensitive); - } - } - } else if (replaceRegex) { + String stringReplacement = (String) replacement; + if (replaceRegex) { // replace all/first - regex List patterns = new ArrayList<>(needles.length); for (Object needle : needles) { assert needle != null; try { patterns.add(Pattern.compile((String) needle)); - } catch (Exception ignored) {} + } catch (Exception ignored) { } } for (int i = 0; i < haystack.length; i++) { for (Pattern pattern : patterns) { assert pattern != null; - haystack[i] = pattern.matcher((String) haystack[i]).replaceAll((String) replacement); + Matcher matcher = pattern.matcher((String) haystack[i]); + if (replaceFirst) // first + haystack[i] = matcher.replaceFirst(stringReplacement); + else // all + haystack[i] = matcher.replaceAll(stringReplacement); + } + } + } else if (replaceFirst) { // replace first - string + for (int i = 0; i < haystack.length; i++) { + for (Object needle : needles) { + assert needle != null; + haystack[i] = StringUtils.replaceFirst((String) haystack[i], (String) needle, Matcher.quoteReplacement(stringReplacement), caseSensitive); } } - } else { + } else { // replace all - string for (int i = 0; i < haystack.length; i++) { for (Object needle : needles) { assert needle != null; - haystack[i] = StringUtils.replace((String) haystack[i], (String) needle, (String) replacement, caseSensitive); + haystack[i] = StringUtils.replace((String) haystack[i], (String) needle, stringReplacement, caseSensitive); } } } this.haystack.change(event, haystack, ChangeMode.SET); - } else if (replaceItems) { + } else { // replace items for (Inventory inventory : (Inventory[]) haystack) { for (ItemType needle : (ItemType[]) needles) { for (Map.Entry entry : inventory.all(needle.getMaterial()).entrySet()) { From 255696eeddc2962f9fcc39cbd54f93328cc54645 Mon Sep 17 00:00:00 2001 From: Ayham Al-Ali <20037329+AyhamAl-Ali@users.noreply.github.com> Date: Sat, 6 Apr 2024 01:18:33 +0300 Subject: [PATCH 15/16] Fix build --- src/main/java/ch/njol/skript/effects/EffReplace.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index e1b8a45b819..c4e36f95b68 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -151,7 +151,7 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) } } haystackExpr.change(event, haystack, ChangeMode.SET); - } else if (replaceItems) { + } else { for (Inventory inventory : (Inventory[]) haystack) { for (ItemType needle : (ItemType[]) needles) { for (Map.Entry entry : inventory.all(needle.getMaterial()).entrySet()) { From 026655e5b21b075af96408264649926f0fd5599d Mon Sep 17 00:00:00 2001 From: sovdee <10354869+sovdeeth@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:19:36 +0200 Subject: [PATCH 16/16] Update src/main/java/ch/njol/skript/effects/EffReplace.java Co-authored-by: Patrick Miller --- src/main/java/ch/njol/skript/effects/EffReplace.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index c4e36f95b68..c3458548041 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -53,8 +53,8 @@ "", "# Very simple chat censor", "on chat:", - "\treplace all \"kys\", \"idiot\" and \"noob\" with \"****\" in the message", - "\treplace using regex \"\\b(kys|idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", + "\treplace all \"idiot\" and \"noob\" with \"****\" in the message", + "\treplace using regex \"\\b(idiot|noob)\\b\" with \"****\" in the message # Regex version for better results", "", "replace all stone and dirt in player's inventory and player's top inventory with diamond" })