2222import io .github .techstreet .dfscript .script .menu .ScriptWidget ;
2323import io .github .techstreet .dfscript .script .util .ScriptValueItem ;
2424import io .github .techstreet .dfscript .script .util .ScriptValueJson ;
25- import io .github .techstreet .dfscript .script .values .ScriptDictionaryValue ;
26- import io .github .techstreet .dfscript .script .values .ScriptListValue ;
27- import io .github .techstreet .dfscript .script .values .ScriptNumberValue ;
28- import io .github .techstreet .dfscript .script .values .ScriptTextValue ;
29- import io .github .techstreet .dfscript .script .values .ScriptUnknownValue ;
30- import io .github .techstreet .dfscript .script .values .ScriptValue ;
25+ import io .github .techstreet .dfscript .script .values .*;
3126import io .github .techstreet .dfscript .util .ComponentUtil ;
3227import io .github .techstreet .dfscript .util .FileUtil ;
3328import io .github .techstreet .dfscript .util .ItemUtil ;
4742import net .fabricmc .fabric .api .client .command .v2 .FabricClientCommandSource ;
4843import net .minecraft .client .network .ClientPlayNetworkHandler ;
4944import net .minecraft .client .sound .PositionedSoundInstance ;
45+ import net .minecraft .enchantment .Enchantment ;
46+ import net .minecraft .enchantment .Enchantments ;
5047import net .minecraft .item .Item ;
5148import net .minecraft .item .ItemStack ;
5249import net .minecraft .item .Items ;
@@ -1221,12 +1218,29 @@ public enum ScriptActionType {
12211218 .arg ("Last Index" ,ScriptActionArgumentType .NUMBER )
12221219 .action (ctx -> {
12231220 String text = ctx .value ("Text" ).asText ();
1224- int start = (int )ctx .value ("First Index" ).asNumber ()+ 1 ;
1221+ int start = (int )ctx .value ("First Index" ).asNumber ()- 1 ;
12251222 int end = (int )ctx .value ("Last Index" ).asNumber ();
12261223 String result = text .substring (start , end );
12271224 ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
12281225 })),
12291226
1227+ TEXT_SUBTEXT_V1 (builder -> builder .name ("Get Subtext OLD" )
1228+ .description ("Gets a piece of text within another text." )
1229+ .icon (Items .KNOWLEDGE_BOOK )
1230+ .category (ScriptActionCategory .TEXTS )
1231+ .arg ("Result" ,ScriptActionArgumentType .VARIABLE )
1232+ .arg ("Text" ,ScriptActionArgumentType .TEXT )
1233+ .arg ("First Index" ,ScriptActionArgumentType .NUMBER )
1234+ .arg ("Last Index" ,ScriptActionArgumentType .NUMBER )
1235+ .deprecate (TEXT_SUBTEXT )
1236+ .action (ctx -> {
1237+ String text = ctx .value ("Text" ).asText ();
1238+ int start = (int )ctx .value ("First Index" ).asNumber ()+1 ;
1239+ int end = (int )ctx .value ("Last Index" ).asNumber ();
1240+ String result = text .substring (start , end );
1241+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1242+ })),
1243+
12301244 TEXT_LENGTH (builder -> builder .name ("Get Text Length" )
12311245 .description ("Get the length of a text value." )
12321246 .icon (Items .BOOKSHELF )
@@ -1517,7 +1531,7 @@ public enum ScriptActionType {
15171531
15181532 if (io .github .techstreet .dfscript .DFScript .MC .currentScreen instanceof ScriptMenu menu ) {
15191533 if (menu .ownedBy (ctx .script ())) {
1520- menu .widgets .add (new ScriptMenuTextField ("" ,x ,y ,width ,height ,false ,identifier ));
1534+ menu .widgets .add (new ScriptMenuTextField ("" ,x ,y ,width ,height ,true ,identifier ));
15211535 } else {
15221536 ChatUtil .error ("Unable to add text field to menu! (Not owned by script)" );
15231537 }
@@ -1631,29 +1645,162 @@ public enum ScriptActionType {
16311645 .hasChildren (true )
16321646 .action (ctx -> {
16331647 ctx .setLastIfResult (!ctx .lastIfResult ());
1648+ })),
1649+
1650+ SORT_LIST (builder -> builder .name ("Sort List" )
1651+ .description ("Sorts a list in ascending order." )
1652+ .icon (Items .REPEATING_COMMAND_BLOCK )
1653+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1654+ .arg ("List" , ScriptActionArgumentType .LIST , b -> b .optional (true ))
1655+ .category (ScriptActionCategory .LISTS )
1656+ .action (ctx -> {
1657+ List <ScriptValue > list = null ;
1658+
1659+ if (ctx .argMap ().containsKey ("List" ))
1660+ {
1661+ list = ctx .value ("List" ).asList ();
1662+ }
1663+ else
1664+ {
1665+ list = ctx .value ("Result" ).asList ();
1666+ }
1667+
1668+ list .sort (new ScriptValueComparator ());
1669+
1670+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptListValue (list ));
1671+ })),
1672+
1673+ REPLACE_TEXT (builder -> builder .name ("Replace Text" )
1674+ .description ("Searches for part of a text and replaces it." )
1675+ .icon (Items .LEAD )
1676+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1677+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1678+ .arg ("Text part to replace" , ScriptActionArgumentType .TEXT )
1679+ .arg ("Replacement" , ScriptActionArgumentType .TEXT )
1680+ .category (ScriptActionCategory .TEXTS )
1681+ .action (ctx -> {
1682+ String result = ctx .value ("Text to change" ).asText ();
1683+
1684+ result = result .replace (ctx .value ("Text part to replace" ).asText (), ctx .value ("Replacement" ).asText ());
1685+
1686+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1687+ })),
1688+
1689+ REGEX_REPLACE_TEXT (builder -> builder .name ("Replace Text using Regex" )
1690+ .description (new String []{"Searches for part of a text" , "using a regex and replaces it." })
1691+ .icon (Items .LEAD , true )
1692+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1693+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1694+ .arg ("Regex" , ScriptActionArgumentType .TEXT )
1695+ .arg ("Replacement" , ScriptActionArgumentType .TEXT )
1696+ .category (ScriptActionCategory .TEXTS )
1697+ .action (ctx -> {
1698+ String result = ctx .value ("Text to change" ).asText ();
1699+
1700+ result = result .replaceAll (ctx .value ("Regex" ).asText (), ctx .value ("Replacement" ).asText ());
1701+
1702+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1703+ })),
1704+
1705+ REMOVE_TEXT (builder -> builder .name ("Remove Text" )
1706+ .description ("Searches for part of a text and replaces it." )
1707+ .icon (Items .WRITABLE_BOOK )
1708+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1709+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1710+ .arg ("Text to remove" , ScriptActionArgumentType .TEXT , b -> b .plural (true ))
1711+ .category (ScriptActionCategory .TEXTS )
1712+ .action (ctx -> {
1713+ String result = ctx .value ("Text to change" ).asText ();
1714+
1715+ List <ScriptValue > textsToRemove = ctx .pluralValue ("Text to remove" );
1716+
1717+ for (int i = 0 ; i < textsToRemove .size (); i ++) {
1718+ result = result .replace (textsToRemove .get (i ).asText (), "" );
1719+ }
1720+
1721+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1722+ })),
1723+
1724+ STRIP_COLOR (builder -> builder .name ("Strip Color from Text" )
1725+ .description ("Searches for color codes in a text and removes them." )
1726+ .icon (Items .CYAN_DYE )
1727+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1728+ .arg ("Text" , ScriptActionArgumentType .TEXT , b -> b .optional (true ))
1729+ .category (ScriptActionCategory .TEXTS )
1730+ .action (ctx -> {
1731+ String result = null ;
1732+
1733+ if (ctx .argMap ().containsKey ("Text" )) {
1734+ result = ctx .value ("Text" ).asText ();
1735+ } else {
1736+ result = ctx .value ("Result" ).asText ();
1737+ }
1738+
1739+ result = result .replaceAll ("&x(&[0-9a-fA-F]){6}" , "" );
1740+ result = result .replaceAll ("&[0-9a-fA-FlonmkrLONMKR]" , "" );
1741+
1742+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1743+ })),
1744+
1745+ REPEAT_TEXT (builder -> builder .name ("Repeat Text" )
1746+ .description ("Repeats a text the given number of times." )
1747+ .icon (Items .REPEATING_COMMAND_BLOCK )
1748+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1749+ .arg ("Text to repeat" , ScriptActionArgumentType .TEXT )
1750+ .arg ("Times to repeat" , ScriptActionArgumentType .NUMBER )
1751+ .category (ScriptActionCategory .TEXTS )
1752+ .action (ctx -> {
1753+ String result = "" ;
1754+ String input = ctx .value ("Text to repeat" ).asText ();
1755+ int times = (int ) ctx .value ("Times to repeat" ).asNumber ();
1756+
1757+ for (int i = 0 ; i < times ; i ++)
1758+ {
1759+ result += input ;
1760+ }
1761+
1762+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
16341763 }));
16351764
16361765 private Consumer <ScriptActionContext > action = (ctx ) -> {
16371766 };
1767+
1768+ private boolean glow = false ;
16381769 private Item icon = Items .STONE ;
16391770 private String name = "Unnamed Action" ;
16401771 private boolean hasChildren = false ;
16411772 private ScriptActionCategory category = ScriptActionCategory .MISC ;
16421773 private List <String > description = new ArrayList ();
16431774 private ScriptGroup group = ScriptGroup .ACTION ;
1775+
1776+ private ScriptActionType deprecated = null ; //if deprecated == null, the action is not deprecated
16441777 private final List <ScriptActionArgument > arguments = new ArrayList <>();
16451778 ScriptActionType (Consumer <ScriptActionType > builder ) {
16461779 description .add ("No description provided." );
16471780 builder .accept (this );
16481781 }
16491782 public ItemStack getIcon () {
16501783 ItemStack item = new ItemStack (icon );
1784+
16511785 item .setCustomName (Text .literal (name )
16521786 .fillStyle (Style .EMPTY
16531787 .withColor (Formatting .WHITE )
16541788 .withItalic (false )));
16551789
16561790 NbtList lore = new NbtList ();
1791+
1792+ if (isDeprecated ())
1793+ {
1794+ lore .add (NbtString .of (Text .Serializer .toJson (Text .literal ("This action is deprecated!" )
1795+ .fillStyle (Style .EMPTY
1796+ .withColor (Formatting .RED )
1797+ .withItalic (false )))));
1798+ lore .add (NbtString .of (Text .Serializer .toJson (Text .literal ("Use '" + deprecated .getName () + "'" )
1799+ .fillStyle (Style .EMPTY
1800+ .withColor (Formatting .RED )
1801+ .withItalic (false )))));
1802+ }
1803+
16571804 for (String descriptionLine : description ) {
16581805 lore .add (NbtString .of (Text .Serializer .toJson (Text .literal (descriptionLine )
16591806 .fillStyle (Style .EMPTY
@@ -1670,12 +1817,22 @@ public ItemStack getIcon() {
16701817 item .getSubNbt ("display" )
16711818 .put ("Lore" , lore );
16721819
1820+ if (glow )
1821+ {
1822+ item .addEnchantment (Enchantments .UNBREAKING , 1 );
1823+ item .addHideFlag (ItemStack .TooltipSection .ENCHANTMENTS );
1824+ }
1825+
16731826 return item ;
16741827 }
16751828 public String getName () {
16761829 return name ;
16771830 }
16781831
1832+ public boolean isDeprecated () {
1833+ return deprecated != null ;
1834+ }
1835+
16791836 public boolean hasChildren () {
16801837 return hasChildren ;
16811838 }
@@ -1689,8 +1846,14 @@ private ScriptActionType action(Consumer<ScriptActionContext> action) {
16891846 return this ;
16901847 }
16911848
1692- private ScriptActionType icon (Item icon ) {
1849+ private ScriptActionType icon (Item icon , boolean glow ) {
16931850 this .icon = icon ;
1851+ this .glow = glow ;
1852+ return this ;
1853+ }
1854+
1855+ private ScriptActionType icon (Item icon ) {
1856+ icon (icon , false );
16941857 return this ;
16951858 }
16961859
@@ -1743,6 +1906,12 @@ public ScriptActionType arg(String name, ScriptActionArgumentType type) {
17431906 });
17441907 }
17451908
1909+ public ScriptActionType deprecate (ScriptActionType newScriptActionType ) {
1910+ deprecated = newScriptActionType ;
1911+
1912+ return this ;
1913+ }
1914+
17461915 public void run (ScriptActionContext ctx ) {
17471916 List <List <ScriptActionArgument >> possibilities = new ArrayList <>();
17481917
0 commit comments