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 ;
4540import net .fabricmc .fabric .api .client .command .v1 .FabricClientCommandSource ;
4641import net .minecraft .client .network .ClientPlayNetworkHandler ;
4742import net .minecraft .client .sound .PositionedSoundInstance ;
43+ import net .minecraft .enchantment .Enchantment ;
44+ import net .minecraft .enchantment .Enchantments ;
4845import net .minecraft .item .Item ;
4946import net .minecraft .item .ItemStack ;
5047import net .minecraft .item .Items ;
@@ -1216,12 +1213,29 @@ public enum ScriptActionType {
12161213 .arg ("Last Index" ,ScriptActionArgumentType .NUMBER )
12171214 .action (ctx -> {
12181215 String text = ctx .value ("Text" ).asText ();
1219- int start = (int )ctx .value ("First Index" ).asNumber ()+ 1 ;
1216+ int start = (int )ctx .value ("First Index" ).asNumber ()- 1 ;
12201217 int end = (int )ctx .value ("Last Index" ).asNumber ();
12211218 String result = text .substring (start , end );
12221219 ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
12231220 })),
12241221
1222+ TEXT_SUBTEXT_V1 (builder -> builder .name ("Get Subtext OLD" )
1223+ .description ("Gets a piece of text within another text." )
1224+ .icon (Items .KNOWLEDGE_BOOK )
1225+ .category (ScriptActionCategory .TEXTS )
1226+ .arg ("Result" ,ScriptActionArgumentType .VARIABLE )
1227+ .arg ("Text" ,ScriptActionArgumentType .TEXT )
1228+ .arg ("First Index" ,ScriptActionArgumentType .NUMBER )
1229+ .arg ("Last Index" ,ScriptActionArgumentType .NUMBER )
1230+ .deprecate (TEXT_SUBTEXT )
1231+ .action (ctx -> {
1232+ String text = ctx .value ("Text" ).asText ();
1233+ int start = (int )ctx .value ("First Index" ).asNumber ()+1 ;
1234+ int end = (int )ctx .value ("Last Index" ).asNumber ();
1235+ String result = text .substring (start , end );
1236+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1237+ })),
1238+
12251239 TEXT_LENGTH (builder -> builder .name ("Get Text Length" )
12261240 .description ("Get the length of a text value." )
12271241 .icon (Items .BOOKSHELF )
@@ -1512,7 +1526,7 @@ public enum ScriptActionType {
15121526
15131527 if (io .github .techstreet .dfscript .DFScript .MC .currentScreen instanceof ScriptMenu menu ) {
15141528 if (menu .ownedBy (ctx .script ())) {
1515- menu .widgets .add (new ScriptMenuTextField ("" ,x ,y ,width ,height ,false ,identifier ));
1529+ menu .widgets .add (new ScriptMenuTextField ("" ,x ,y ,width ,height ,true ,identifier ));
15161530 } else {
15171531 ChatUtil .error ("Unable to add text field to menu! (Not owned by script)" );
15181532 }
@@ -1626,29 +1640,162 @@ public enum ScriptActionType {
16261640 .hasChildren (true )
16271641 .action (ctx -> {
16281642 ctx .setLastIfResult (!ctx .lastIfResult ());
1643+ })),
1644+
1645+ SORT_LIST (builder -> builder .name ("Sort List" )
1646+ .description ("Sorts a list in ascending order." )
1647+ .icon (Items .REPEATING_COMMAND_BLOCK )
1648+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1649+ .arg ("List" , ScriptActionArgumentType .LIST , b -> b .optional (true ))
1650+ .category (ScriptActionCategory .LISTS )
1651+ .action (ctx -> {
1652+ List <ScriptValue > list = null ;
1653+
1654+ if (ctx .argMap ().containsKey ("List" ))
1655+ {
1656+ list = ctx .value ("List" ).asList ();
1657+ }
1658+ else
1659+ {
1660+ list = ctx .value ("Result" ).asList ();
1661+ }
1662+
1663+ list .sort (new ScriptValueComparator ());
1664+
1665+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptListValue (list ));
1666+ })),
1667+
1668+ REPLACE_TEXT (builder -> builder .name ("Replace Text" )
1669+ .description ("Searches for part of a text and replaces it." )
1670+ .icon (Items .LEAD )
1671+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1672+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1673+ .arg ("Text part to replace" , ScriptActionArgumentType .TEXT )
1674+ .arg ("Replacement" , ScriptActionArgumentType .TEXT )
1675+ .category (ScriptActionCategory .TEXTS )
1676+ .action (ctx -> {
1677+ String result = ctx .value ("Text to change" ).asText ();
1678+
1679+ result = result .replace (ctx .value ("Text part to replace" ).asText (), ctx .value ("Replacement" ).asText ());
1680+
1681+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1682+ })),
1683+
1684+ REGEX_REPLACE_TEXT (builder -> builder .name ("Replace Text using Regex" )
1685+ .description (new String []{"Searches for part of a text" , "using a regex and replaces it." })
1686+ .icon (Items .LEAD , true )
1687+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1688+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1689+ .arg ("Regex" , ScriptActionArgumentType .TEXT )
1690+ .arg ("Replacement" , ScriptActionArgumentType .TEXT )
1691+ .category (ScriptActionCategory .TEXTS )
1692+ .action (ctx -> {
1693+ String result = ctx .value ("Text to change" ).asText ();
1694+
1695+ result = result .replaceAll (ctx .value ("Regex" ).asText (), ctx .value ("Replacement" ).asText ());
1696+
1697+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1698+ })),
1699+
1700+ REMOVE_TEXT (builder -> builder .name ("Remove Text" )
1701+ .description ("Searches for part of a text and replaces it." )
1702+ .icon (Items .WRITABLE_BOOK )
1703+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1704+ .arg ("Text to change" , ScriptActionArgumentType .TEXT )
1705+ .arg ("Text to remove" , ScriptActionArgumentType .TEXT , b -> b .plural (true ))
1706+ .category (ScriptActionCategory .TEXTS )
1707+ .action (ctx -> {
1708+ String result = ctx .value ("Text to change" ).asText ();
1709+
1710+ List <ScriptValue > textsToRemove = ctx .pluralValue ("Text to remove" );
1711+
1712+ for (int i = 0 ; i < textsToRemove .size (); i ++) {
1713+ result = result .replace (textsToRemove .get (i ).asText (), "" );
1714+ }
1715+
1716+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1717+ })),
1718+
1719+ STRIP_COLOR (builder -> builder .name ("Strip Color from Text" )
1720+ .description ("Searches for color codes in a text and removes them." )
1721+ .icon (Items .CYAN_DYE )
1722+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1723+ .arg ("Text" , ScriptActionArgumentType .TEXT , b -> b .optional (true ))
1724+ .category (ScriptActionCategory .TEXTS )
1725+ .action (ctx -> {
1726+ String result = null ;
1727+
1728+ if (ctx .argMap ().containsKey ("Text" )) {
1729+ result = ctx .value ("Text" ).asText ();
1730+ } else {
1731+ result = ctx .value ("Result" ).asText ();
1732+ }
1733+
1734+ result = result .replaceAll ("&x(&[0-9a-fA-F]){6}" , "" );
1735+ result = result .replaceAll ("&[0-9a-fA-FlonmkrLONMKR]" , "" );
1736+
1737+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
1738+ })),
1739+
1740+ REPEAT_TEXT (builder -> builder .name ("Repeat Text" )
1741+ .description ("Repeats a text the given number of times." )
1742+ .icon (Items .REPEATING_COMMAND_BLOCK )
1743+ .arg ("Result" , ScriptActionArgumentType .VARIABLE )
1744+ .arg ("Text to repeat" , ScriptActionArgumentType .TEXT )
1745+ .arg ("Times to repeat" , ScriptActionArgumentType .NUMBER )
1746+ .category (ScriptActionCategory .TEXTS )
1747+ .action (ctx -> {
1748+ String result = "" ;
1749+ String input = ctx .value ("Text to repeat" ).asText ();
1750+ int times = (int ) ctx .value ("Times to repeat" ).asNumber ();
1751+
1752+ for (int i = 0 ; i < times ; i ++)
1753+ {
1754+ result += input ;
1755+ }
1756+
1757+ ctx .context ().setVariable (ctx .variable ("Result" ).name (), new ScriptTextValue (result ));
16291758 }));
16301759
16311760 private Consumer <ScriptActionContext > action = (ctx ) -> {
16321761 };
1762+
1763+ private boolean glow = false ;
16331764 private Item icon = Items .STONE ;
16341765 private String name = "Unnamed Action" ;
16351766 private boolean hasChildren = false ;
16361767 private ScriptActionCategory category = ScriptActionCategory .MISC ;
16371768 private List <String > description = new ArrayList ();
16381769 private ScriptGroup group = ScriptGroup .ACTION ;
1770+
1771+ private ScriptActionType deprecated = null ; //if deprecated == null, the action is not deprecated
16391772 private final List <ScriptActionArgument > arguments = new ArrayList <>();
16401773 ScriptActionType (Consumer <ScriptActionType > builder ) {
16411774 description .add ("No description provided." );
16421775 builder .accept (this );
16431776 }
16441777 public ItemStack getIcon () {
16451778 ItemStack item = new ItemStack (icon );
1779+
16461780 item .setCustomName (((LiteralText ) Text .of (name ))
16471781 .fillStyle (Style .EMPTY
16481782 .withColor (Formatting .WHITE )
16491783 .withItalic (false )));
16501784
16511785 NbtList lore = new NbtList ();
1786+
1787+ if (isDeprecated ())
1788+ {
1789+ lore .add (NbtString .of (Text .Serializer .toJson (((LiteralText ) Text .of ("This action is deprecated!" ))
1790+ .fillStyle (Style .EMPTY
1791+ .withColor (Formatting .RED )
1792+ .withItalic (false )))));
1793+ lore .add (NbtString .of (Text .Serializer .toJson (((LiteralText ) Text .of ("Use '" + deprecated .getName () + "'" ))
1794+ .fillStyle (Style .EMPTY
1795+ .withColor (Formatting .RED )
1796+ .withItalic (false )))));
1797+ }
1798+
16521799 for (String descriptionLine : description ) {
16531800 lore .add (NbtString .of (Text .Serializer .toJson (((LiteralText ) Text .of (descriptionLine ))
16541801 .fillStyle (Style .EMPTY
@@ -1665,12 +1812,22 @@ public ItemStack getIcon() {
16651812 item .getSubNbt ("display" )
16661813 .put ("Lore" , lore );
16671814
1815+ if (glow )
1816+ {
1817+ item .addEnchantment (Enchantments .UNBREAKING , 1 );
1818+ item .addHideFlag (ItemStack .TooltipSection .ENCHANTMENTS );
1819+ }
1820+
16681821 return item ;
16691822 }
16701823 public String getName () {
16711824 return name ;
16721825 }
16731826
1827+ public boolean isDeprecated () {
1828+ return deprecated != null ;
1829+ }
1830+
16741831 public boolean hasChildren () {
16751832 return hasChildren ;
16761833 }
@@ -1684,8 +1841,14 @@ private ScriptActionType action(Consumer<ScriptActionContext> action) {
16841841 return this ;
16851842 }
16861843
1687- private ScriptActionType icon (Item icon ) {
1844+ private ScriptActionType icon (Item icon , boolean glow ) {
16881845 this .icon = icon ;
1846+ this .glow = glow ;
1847+ return this ;
1848+ }
1849+
1850+ private ScriptActionType icon (Item icon ) {
1851+ icon (icon , false );
16891852 return this ;
16901853 }
16911854
@@ -1738,6 +1901,12 @@ public ScriptActionType arg(String name, ScriptActionArgumentType type) {
17381901 });
17391902 }
17401903
1904+ public ScriptActionType deprecate (ScriptActionType newScriptActionType ) {
1905+ deprecated = newScriptActionType ;
1906+
1907+ return this ;
1908+ }
1909+
17411910 public void run (ScriptActionContext ctx ) {
17421911 List <List <ScriptActionArgument >> possibilities = new ArrayList <>();
17431912
0 commit comments