44import com .earth2me .essentials .config .EssentialsConfiguration ;
55import com .earth2me .essentials .utils .NumberUtil ;
66import org .spongepowered .configurate .CommentedConfigurationNode ;
7- import org .spongepowered .configurate .serialize .SerializationException ;
87
98import java .io .File ;
109import java .math .BigDecimal ;
10+ import java .util .HashMap ;
1111import java .util .List ;
1212import java .util .Locale ;
1313import java .util .Map ;
14+ import java .util .Set ;
1415
1516import static com .earth2me .essentials .I18n .capitalCase ;
1617import static com .earth2me .essentials .I18n .tl ;
1718
1819public class Kits implements IConf {
19- private final EssentialsConfiguration config ;
20- private CommentedConfigurationNode kits ;
20+ private final IEssentials ess ;
21+ private final EssentialsConfiguration rootConfig ;
22+ private final Map <String , EssentialsConfiguration > kitToConfigMap = new HashMap <>();
23+ private final Map <String , Map <String , Object >> kitMap = new HashMap <>();
2124
2225 public Kits (final IEssentials essentials ) {
23- config = new EssentialsConfiguration (new File (essentials .getDataFolder (), "kits.yml" ), "/kits.yml" );
26+ this .ess = essentials ;
27+ this .rootConfig = new EssentialsConfiguration (new File (essentials .getDataFolder (), "kits.yml" ), "/kits.yml" );
2428
2529 reloadConfig ();
2630 }
2731
2832 @ Override
2933 public void reloadConfig () {
30- config .load ();
31- kits = _getKits ();
34+ rootConfig .load ();
35+ parseKits ();
3236 }
3337
3438 public File getFile () {
35- return config .getFile ();
39+ return rootConfig .getFile ();
3640 }
3741
38- private CommentedConfigurationNode _getKits () {
39- final CommentedConfigurationNode section = config .getSection ("kits" );
40- if (section != null ) {
41- final CommentedConfigurationNode newSection = config .newSection ();
42- for (final String kitItem : ConfigurateUtil .getKeys (section )) {
43- final CommentedConfigurationNode kitSection = section .node (kitItem );
44- if (kitSection .isMap ()) {
45- try {
46- newSection .node (kitItem .toLowerCase (Locale .ENGLISH )).set (kitSection );
47- } catch (SerializationException e ) {
48- e .printStackTrace ();
42+ private void parseKit (final String kitName , final CommentedConfigurationNode kitSection , final EssentialsConfiguration parentConfig ) {
43+ if (kitSection .isMap ()) {
44+ final String effectiveKitName = kitName .toLowerCase (Locale .ENGLISH );
45+ kitToConfigMap .put (effectiveKitName , parentConfig );
46+ kitMap .put (effectiveKitName , ConfigurateUtil .getRawMap (kitSection ));
47+ }
48+ }
49+
50+ private void parseKits () {
51+ kitToConfigMap .clear ();
52+ kitMap .clear ();
53+
54+ // Kits from kits.yml file
55+ final CommentedConfigurationNode fileKits = rootConfig .getSection ("kits" );
56+ if (fileKits != null ) {
57+ for (final Map .Entry <String , CommentedConfigurationNode > kitEntry : ConfigurateUtil .getMap (fileKits ).entrySet ()) {
58+ parseKit (kitEntry .getKey (), kitEntry .getValue (), rootConfig );
59+ }
60+ }
61+
62+ // Kits from kits subdirectory
63+ final File kitsFolder = new File (this .ess .getDataFolder (), "kits" );
64+ if (!kitsFolder .exists () || !kitsFolder .isDirectory ()) {
65+ return ;
66+ }
67+
68+ final File [] kitsFiles = kitsFolder .listFiles ();
69+
70+ //noinspection ConstantConditions - will not be null, conditions checked above.
71+ for (final File kitFile : kitsFiles ) {
72+ if (kitFile .getName ().endsWith (".yml" )) {
73+ final EssentialsConfiguration kitConfig = new EssentialsConfiguration (kitFile );
74+ kitConfig .load ();
75+ final CommentedConfigurationNode kits = kitConfig .getSection ("kits" );
76+ if (kits != null ) {
77+ for (final Map .Entry <String , CommentedConfigurationNode > kitEntry : ConfigurateUtil .getMap (kits ).entrySet ()) {
78+ parseKit (kitEntry .getKey (), kitEntry .getValue (), kitConfig );
4979 }
5080 }
5181 }
52- return newSection ;
5382 }
54- return null ;
5583 }
5684
57- public EssentialsConfiguration getConfig () {
58- return config ;
85+ /**
86+ * Should be used for EssentialsUpgrade conversions <b>only</b>.
87+ */
88+ public EssentialsConfiguration getRootConfig () {
89+ return rootConfig ;
5990 }
6091
61- public CommentedConfigurationNode getKits () {
62- return kits ;
92+ public Set < String > getKitKeys () {
93+ return kitMap . keySet () ;
6394 }
6495
65- public Map <String , Object > getKit (String name ) {
66- name = name .replace ('.' , '_' ).replace ('/' , '_' );
67- if (getKits () != null ) {
68- final CommentedConfigurationNode kits = getKits ();
69- // Other parts of the codebase/3rd party plugins expect us to lowercase kit names here.
70- // This isn't strictly needed for the future of Essentials, but for compatibility it's here.
71- final CommentedConfigurationNode kitSection = kits .node (name .toLowerCase ());
72- if (!kitSection .virtual () && kitSection .isMap ()) {
73- return ConfigurateUtil .getRawMap (kitSection );
74- }
96+ public Map <String , Object > getKit (final String name ) {
97+ if (name != null ) {
98+ return kitMap .get (name .replace ('.' , '_' ).replace ('/' , '_' ));
7599 }
76-
77100 return null ;
78101 }
79102
80103 // Tries to find an existing kit name that matches the given name, ignoring case. Returns null if no match.
81104 public String matchKit (final String name ) {
82- final CommentedConfigurationNode section = config .getSection ("kits" );
83- if (section != null ) {
84- for (final String kitName : ConfigurateUtil .getKeys (section )) {
105+ if (name != null ) {
106+ for (final String kitName : kitMap .keySet ()) {
85107 if (kitName .equalsIgnoreCase (name )) {
86108 return kitName ;
87109 }
@@ -90,25 +112,32 @@ public String matchKit(final String name) {
90112 return null ;
91113 }
92114
93- public void addKit (final String name , final List <String > lines , final long delay ) {
115+ public void addKit (String name , final List <String > lines , final long delay ) {
116+ name = name .replace ('.' , '_' ).replace ('/' , '_' ).toLowerCase (Locale .ENGLISH );
94117 // Will overwrite but w/e
95- config .setProperty ("kits." + name + ".delay" , delay );
96- config .setProperty ("kits." + name + ".items" , lines );
97- kits = _getKits ();
98- config .save ();
118+ rootConfig .setProperty ("kits." + name + ".delay" , delay );
119+ rootConfig .setProperty ("kits." + name + ".items" , lines );
120+ parseKits ();
121+ rootConfig .save ();
99122 }
100123
101- public void removeKit (final String name ) {
124+ public void removeKit (String name ) {
125+ name = name .replace ('.' , '_' ).replace ('/' , '_' ).toLowerCase (Locale .ENGLISH );
126+ if (!kitToConfigMap .containsKey (name ) || !kitMap .containsKey (name )) {
127+ return ;
128+ }
129+
130+ final EssentialsConfiguration config = kitToConfigMap .get (name );
102131 config .removeProperty ("kits." + name );
103- kits = _getKits ();
104- config .save ();
132+
133+ config .blockingSave ();
134+ parseKits ();
105135 }
106136
107137 public String listKits (final net .ess3 .api .IEssentials ess , final User user ) throws Exception {
108138 try {
109- final CommentedConfigurationNode kits = config .getSection ("kits" );
110139 final StringBuilder list = new StringBuilder ();
111- for (final String kitItem : ConfigurateUtil . getKeys ( kits )) {
140+ for (final String kitItem : kitMap . keySet ( )) {
112141 if (user == null ) {
113142 list .append (" " ).append (capitalCase (kitItem ));
114143 } else if (user .isAuthorized ("essentials.kits." + kitItem .toLowerCase (Locale .ENGLISH ))) {
0 commit comments