11package pro .cloudnode .smp .enchantbookplus ;
22
33import org .bukkit .NamespacedKey ;
4+ import org .bukkit .Registry ;
45import org .bukkit .enchantments .Enchantment ;
56import org .jetbrains .annotations .NotNull ;
67import org .jetbrains .annotations .Nullable ;
78
89import java .util .ArrayList ;
9- import java .util .HashMap ;
1010import java .util .List ;
11+ import java .util .Map ;
1112import java .util .Objects ;
1213import java .util .Optional ;
1314import java .util .stream .Collectors ;
@@ -38,19 +39,16 @@ public class ConfigEnchantmentEntry {
3839 */
3940 protected final boolean multiplyCostByLevel ;
4041
41- /**
42- * Name of the enchantment.
43- */
44- public final @ NotNull String getName () {
45- return name ;
46- }
47-
4842 /**
4943 * Maximum level of the enchantment.
5044 */
51- public final @ NotNull Optional <@ NotNull Integer > getMaxLevel () {
52- if (Optional .ofNullable (maxLevel ).isEmpty ()) return Optional .empty ();
53- if (maxLevelRelative ) return Optional .of (getEnchantment ().getMaxLevel () + maxLevel );
45+ public final @ NotNull Optional <Integer > getMaxLevel () {
46+ if (Optional .ofNullable (maxLevel ).isEmpty ())
47+ return Optional .empty ();
48+
49+ if (maxLevelRelative )
50+ return Optional .of (getEnchantment ().getMaxLevel () + maxLevel );
51+
5452 return Optional .of (maxLevel );
5553 }
5654
@@ -72,7 +70,7 @@ public final boolean getMultiplyCostByLevel() {
7270 * Get enchantment
7371 */
7472 public final Enchantment getEnchantment () {
75- return Enchantment . getByKey (NamespacedKey .minecraft (name ));
73+ return Registry . ENCHANTMENT . get (NamespacedKey .minecraft (name ));
7674 }
7775
7876 /**
@@ -91,7 +89,13 @@ public final boolean isEnchantment(final @NotNull Enchantment enchantment) {
9189 * @param cost Cost of the enchantment.
9290 * @param multiplyCostByLevel Multiply cost by level.
9391 */
94- public ConfigEnchantmentEntry (final @ NotNull String name , final @ Nullable Integer maxLevel , final boolean maxLevelRelative , final int cost , final boolean multiplyCostByLevel ) {
92+ public ConfigEnchantmentEntry (
93+ final @ NotNull String name ,
94+ final @ Nullable Integer maxLevel ,
95+ final boolean maxLevelRelative ,
96+ final int cost ,
97+ final boolean multiplyCostByLevel
98+ ) {
9599 this .name = name ;
96100 this .maxLevel = maxLevel ;
97101 this .maxLevelRelative = maxLevelRelative ;
@@ -104,12 +108,27 @@ public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Intege
104108 *
105109 * @param configValue Config object
106110 */
107- public static @ NotNull ConfigEnchantmentEntry configValue (final @ NotNull HashMap <@ NotNull String , @ NotNull Object > configValue ) {
108- final @ NotNull String name = (String ) Objects .requireNonNull (configValue .get ("name" ));
111+ public static @ NotNull ConfigEnchantmentEntry configValue (
112+ final @ NotNull Map <@ NotNull String , @ NotNull Object > configValue
113+ ) throws NumberFormatException , IndexOutOfBoundsException , ClassCastException {
114+ final String name = (String ) Objects .requireNonNull (configValue .get ("name" ));
115+
109116 final @ Nullable Integer maxLevel ;
117+
110118 final boolean maxLevelRelative ;
111- if (configValue .containsKey ("max-level" )) {
112- if (configValue .get ("max-level" ) instanceof final @ NotNull String string ) {
119+
120+ if (!configValue .containsKey ("max-level" )) {
121+ maxLevel = null ;
122+ maxLevelRelative = false ;
123+ }
124+
125+ else {
126+ if (!(configValue .get ("max-level" ) instanceof final String string )) {
127+ maxLevel = (Integer ) configValue .get ("max-level" );
128+ maxLevelRelative = false ;
129+ }
130+
131+ else {
113132 if (string .startsWith ("+" )) {
114133 maxLevel = Integer .parseInt (string .substring (1 ));
115134 maxLevelRelative = true ;
@@ -119,38 +138,36 @@ public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Intege
119138 maxLevelRelative = false ;
120139 }
121140 }
122- else {
123- maxLevel = (Integer ) configValue .get ("max-level" );
124- maxLevelRelative = false ;
125- }
126- }
127- else {
128- maxLevel = null ;
129- maxLevelRelative = false ;
130- }
131- final boolean multiplyCostByLevel ;
132- final int cost ;
133- if (configValue .containsKey ("cost" )) {
134- if (configValue .get ("cost" ) instanceof final @ NotNull String costString ) {
135- if (costString .startsWith ("*" )) {
136- multiplyCostByLevel = true ;
137- cost = Integer .parseInt (costString .substring (1 ));
138- }
139- else {
140- multiplyCostByLevel = false ;
141- cost = Integer .parseInt (costString );
142- }
143- }
144- else {
145- multiplyCostByLevel = false ;
146- cost = (Integer ) configValue .get ("cost" );
147- }
148- }
149- else {
150- multiplyCostByLevel = false ;
151- cost = 0 ;
152141 }
153- return new ConfigEnchantmentEntry (name , maxLevel , maxLevelRelative , cost , multiplyCostByLevel );
142+
143+ if (!configValue .containsKey ("cost" ))
144+ return new ConfigEnchantmentEntry (name , maxLevel , maxLevelRelative , 0 , false );
145+
146+ if (!(configValue .get ("cost" ) instanceof final @ NotNull String costString ))
147+ return new ConfigEnchantmentEntry (
148+ name ,
149+ maxLevel ,
150+ maxLevelRelative ,
151+ (Integer ) configValue .get ("cost" ),
152+ false
153+ );
154+
155+ if (costString .startsWith ("*" ))
156+ return new ConfigEnchantmentEntry (
157+ name ,
158+ maxLevel ,
159+ maxLevelRelative ,
160+ Integer .parseInt (costString .substring (1 )),
161+ true
162+ );
163+
164+ return new ConfigEnchantmentEntry (
165+ name ,
166+ maxLevel ,
167+ maxLevelRelative ,
168+ Integer .parseInt (costString ),
169+ false
170+ );
154171 }
155172
156173
@@ -159,7 +176,9 @@ public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Intege
159176 *
160177 * @param configValue Config object array
161178 */
162- public static @ NotNull List <@ NotNull ConfigEnchantmentEntry > configArray (final @ NotNull ArrayList <@ NotNull HashMap <@ NotNull String , @ NotNull Object >> configValue ) {
179+ public static @ NotNull List <@ NotNull ConfigEnchantmentEntry > configArray (
180+ final @ NotNull ArrayList <@ NotNull Map <@ NotNull String , @ NotNull Object >> configValue
181+ ) throws NumberFormatException , IndexOutOfBoundsException , ClassCastException {
163182 return configValue .stream ().map (ConfigEnchantmentEntry ::configValue ).collect (Collectors .toList ());
164183 }
165184
@@ -169,17 +188,33 @@ public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Intege
169188 * @param configValue Config object
170189 */
171190 private static boolean isValidConfigValue (final @ Nullable Object configValue ) {
172- if (configValue == null ) return false ;
173- if (!(configValue instanceof final @ NotNull ArrayList <?> arrayList )) return false ;
174- for (final @ NotNull Object object : arrayList ) {
175- if (!(object instanceof final @ NotNull HashMap <?, ?> hashMap )) return false ;
176- if (!hashMap .containsKey ("name" )) return false ;
177- if (!(hashMap .get ("name" ) instanceof String )) return false ;
178- if (hashMap .containsKey ("max-level" ) && !(hashMap .get ("max-level" ) instanceof String ) && !(hashMap .get ("max-level" ) instanceof Integer ))
191+ if (configValue == null )
192+ return false ;
193+
194+ if (!(configValue instanceof final ArrayList <?> arrayList ))
195+ return false ;
196+
197+ for (final Object object : arrayList ) {
198+ if (!(object instanceof final Map <?, ?> hashMap ))
179199 return false ;
180- if (hashMap .containsKey ("cost" ) && !(hashMap .get ("cost" ) instanceof String ) && !(hashMap .get ("cost" ) instanceof Integer ))
200+
201+ if (!hashMap .containsKey ("name" ))
202+ return false ;
203+
204+ if (!(hashMap .get ("name" ) instanceof String ))
205+ return false ;
206+
207+ if (hashMap .containsKey ("max-level" ) &&
208+ !(hashMap .get ("max-level" ) instanceof String ) &&
209+ !(hashMap .get ("max-level" ) instanceof Integer ))
210+ return false ;
211+
212+ if (hashMap .containsKey ("cost" ) &&
213+ !(hashMap .get ("cost" ) instanceof String ) &&
214+ !(hashMap .get ("cost" ) instanceof Integer ))
181215 return false ;
182216 }
217+
183218 return true ;
184219 }
185220
@@ -188,22 +223,45 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
188223 *
189224 * @param configValue Config object
190225 */
191- public static @ NotNull List <@ NotNull ConfigEnchantmentEntry > config (final @ Nullable Object configValue ) throws IllegalArgumentException {
192- if (!isValidConfigValue (configValue )) throw new IllegalArgumentException ("Invalid config value" );
193- return configArray ((ArrayList <HashMap <String , Object >>) configValue );
226+ public static @ NotNull List <@ NotNull ConfigEnchantmentEntry > config (
227+ final @ Nullable Object configValue
228+ ) throws IllegalArgumentException , IndexOutOfBoundsException , ClassCastException {
229+ if (!isValidConfigValue (configValue ))
230+ throw new IllegalArgumentException ("Invalid config value" );
231+
232+ //noinspection unchecked
233+ return configArray ((ArrayList <Map <String , Object >>) configValue );
194234 }
195235
196236 public static final class AllConfigEnchantmentEntry extends ConfigEnchantmentEntry {
197- private AllConfigEnchantmentEntry (final @ Nullable Integer maxLevel , final boolean maxLevelRelative , final int cost , final boolean multiplyCostByLevel ) {
237+ private AllConfigEnchantmentEntry (
238+ final @ Nullable Integer maxLevel ,
239+ final boolean maxLevelRelative ,
240+ final int cost ,
241+ final boolean multiplyCostByLevel
242+ ) {
198243 super ("ALL" , maxLevel , maxLevelRelative , cost , multiplyCostByLevel );
199244 }
200245
201- public static @ NotNull AllConfigEnchantmentEntry from (final @ NotNull ConfigEnchantmentEntry configEnchantmentEntry ) {
202- return new AllConfigEnchantmentEntry (configEnchantmentEntry .maxLevel , configEnchantmentEntry .maxLevelRelative , configEnchantmentEntry .cost , configEnchantmentEntry .multiplyCostByLevel );
246+ public static @ NotNull AllConfigEnchantmentEntry from (
247+ final @ NotNull ConfigEnchantmentEntry configEnchantmentEntry
248+ ) {
249+ return new AllConfigEnchantmentEntry (
250+ configEnchantmentEntry .maxLevel ,
251+ configEnchantmentEntry .maxLevelRelative ,
252+ configEnchantmentEntry .cost ,
253+ configEnchantmentEntry .multiplyCostByLevel
254+ );
203255 }
204256
205257 public @ NotNull ConfigEnchantmentEntry enchant (final @ NotNull Enchantment enchantment ) {
206- return new ConfigEnchantmentEntry (enchantment .getKey ().getKey (), this .maxLevel , maxLevelRelative , cost , multiplyCostByLevel );
258+ return new ConfigEnchantmentEntry (
259+ enchantment .getKey ().getKey (),
260+ this .maxLevel ,
261+ maxLevelRelative ,
262+ cost ,
263+ multiplyCostByLevel
264+ );
207265 }
208266 }
209267}
0 commit comments