1+ /*
2+ * This file is part of Skript.
3+ *
4+ * Skript is free software: you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation, either version 3 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * Skript is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with Skript. If not, see <http://www.gnu.org/licenses/>.
16+ *
17+ *
18+ * Copyright 2011-2016 Peter Güttinger and contributors
19+ *
20+ */
21+
22+ package ch .njol .skript .expressions ;
23+
24+ import java .util .ArrayList ;
25+ import java .util .Arrays ;
26+ import java .util .Collections ;
27+ import java .util .Comparator ;
28+
29+ import org .bukkit .event .Event ;
30+ import org .eclipse .jdt .annotation .Nullable ;
31+
32+ import ch .njol .skript .Skript ;
33+ import ch .njol .skript .doc .Description ;
34+ import ch .njol .skript .doc .Events ;
35+ import ch .njol .skript .doc .Examples ;
36+ import ch .njol .skript .doc .Name ;
37+ import ch .njol .skript .doc .Since ;
38+ import ch .njol .skript .lang .Expression ;
39+ import ch .njol .skript .lang .ExpressionType ;
40+ import ch .njol .skript .lang .SkriptParser .ParseResult ;
41+ import ch .njol .skript .lang .util .SimpleExpression ;
42+ import ch .njol .util .Kleenean ;
43+
44+ /**
45+ * Sorts a list of texts/stirngs alphabetically
46+ * @author xXAndrew28Xx
47+ */
48+ @ Name ("Alphabetical Sort" )
49+ @ Description ("Sort a list alphabetically" )
50+ @ Examples ({"set {_list::*} to alphabetically sorted {_list::*" })
51+ @ Since ("dev-18" )
52+ public class ExprAlphabetList extends SimpleExpression <String >{
53+ static {
54+ Skript .registerExpression (ExprAlphabetList .class , String .class , ExpressionType .COMBINED , "alphabetically sorted %strings%" );
55+ }
56+ private Expression <String > exprTexts ;
57+ @ Override
58+ public boolean isSingle () {
59+ return false ;
60+ }
61+
62+ @ Override
63+ public Class <? extends String > getReturnType () {
64+ return String .class ;
65+ }
66+
67+ @ SuppressWarnings ("unchecked" )
68+ @ Override
69+ public boolean init (Expression <?>[] exprs , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
70+ exprTexts = (Expression <String >) exprs [0 ];
71+ return true ;
72+ }
73+
74+ @ Override
75+ public String toString (@ Nullable Event e , boolean debug ) {
76+ return "alphabetically sorted %strings%" ;
77+ }
78+
79+ @ Override
80+ @ Nullable
81+ protected String [] get (Event e ) {
82+ String [] texts = exprTexts .getArray (e );
83+ ArrayList <String > alTexts = new ArrayList <String >(Arrays .asList (texts ));
84+ Collections .sort (alTexts , new Comparator <String >(){
85+
86+ @ Override
87+ public int compare (String o1 , String o2 ) {
88+ return o1 .compareTo (o2 );
89+ }});
90+ return alTexts .toArray (new String [alTexts .size ()]);
91+ }
92+
93+ }
0 commit comments