Skip to content

Commit a18e6e2

Browse files
committed
Switch annotations to JSpecify and reformat code
1 parent a897d88 commit a18e6e2

7 files changed

Lines changed: 206 additions & 188 deletions

File tree

.idea/dictionaries/project.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@
5151
<scope>provided</scope>
5252
</dependency>
5353
<dependency>
54-
<groupId>org.jetbrains</groupId>
55-
<artifactId>annotations</artifactId>
56-
<version>26.0.2-1</version>
57-
<scope>compile</scope>
54+
<groupId>org.jspecify</groupId>
55+
<artifactId>jspecify</artifactId>
56+
<version>1.0.0</version>
5857
</dependency>
5958
</dependencies>
6059
</project>

src/main/java/pro/cloudnode/smp/enchantbookplus/ConfigEnchantmentEntry.java

Lines changed: 92 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
import org.bukkit.NamespacedKey;
44
import org.bukkit.Registry;
55
import org.bukkit.enchantments.Enchantment;
6-
import org.jetbrains.annotations.NotNull;
7-
import org.jetbrains.annotations.Nullable;
6+
import org.jspecify.annotations.NullMarked;
7+
import org.jspecify.annotations.Nullable;
88

99
import java.util.ArrayList;
1010
import java.util.List;
1111
import java.util.Map;
1212
import java.util.Objects;
13-
import java.util.Optional;
13+
import java.util.OptionalInt;
1414
import java.util.stream.Collectors;
1515

16+
@NullMarked
1617
public class ConfigEnchantmentEntry {
1718
/**
1819
* Name of the enchantment.
1920
*/
20-
public final @NotNull String name;
21+
public final String name;
2122

2223
/**
2324
* Maximum level of the enchantment.
@@ -40,57 +41,14 @@ public class ConfigEnchantmentEntry {
4041
protected final boolean multiplyCostByLevel;
4142

4243
/**
43-
* Maximum level of the enchantment.
44-
*/
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-
52-
return Optional.of(maxLevel);
53-
}
54-
55-
/**
56-
* Cost of the enchantment.
57-
*/
58-
public final int getCost() {
59-
return cost;
60-
}
61-
62-
/**
63-
* Multiply cost by level.
64-
*/
65-
public final boolean getMultiplyCostByLevel() {
66-
return multiplyCostByLevel;
67-
}
68-
69-
/**
70-
* Get enchantment
71-
*/
72-
public final Enchantment getEnchantment() {
73-
return Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name));
74-
}
75-
76-
/**
77-
* Is enchantment
78-
*
79-
* @param enchantment The enchantment
80-
*/
81-
public final boolean isEnchantment(final @NotNull Enchantment enchantment) {
82-
return name.equalsIgnoreCase(enchantment.getKey().getKey());
83-
}
84-
85-
/**
86-
* @param name Name of the enchantment.
87-
* @param maxLevel Maximum level of the enchantment.
88-
* @param maxLevelRelative Max level relative
89-
* @param cost Cost of the enchantment.
44+
* @param name Name of the enchantment.
45+
* @param maxLevel Maximum level of the enchantment.
46+
* @param maxLevelRelative Max level relative
47+
* @param cost Cost of the enchantment.
9048
* @param multiplyCostByLevel Multiply cost by level.
9149
*/
9250
public ConfigEnchantmentEntry(
93-
final @NotNull String name,
51+
final String name,
9452
final @Nullable Integer maxLevel,
9553
final boolean maxLevelRelative,
9654
final int cost,
@@ -108,77 +66,67 @@ public ConfigEnchantmentEntry(
10866
*
10967
* @param configValue Config object
11068
*/
111-
public static @NotNull ConfigEnchantmentEntry configValue(
112-
final @NotNull Map<@NotNull String, @NotNull Object> configValue
69+
public static ConfigEnchantmentEntry configValue(
70+
final Map<String, Object> configValue
11371
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
11472
final String name = (String) Objects.requireNonNull(configValue.get("name"));
11573

116-
final @Nullable Integer maxLevel;
74+
final Integer maxLevel;
11775

11876
final boolean maxLevelRelative;
11977

12078
if (!configValue.containsKey("max-level")) {
12179
maxLevel = null;
12280
maxLevelRelative = false;
123-
}
124-
125-
else {
81+
} else {
12682
if (!(configValue.get("max-level") instanceof final String string)) {
12783
maxLevel = (Integer) configValue.get("max-level");
12884
maxLevelRelative = false;
129-
}
130-
131-
else {
85+
} else {
13286
if (string.startsWith("+")) {
13387
maxLevel = Integer.parseInt(string.substring(1));
13488
maxLevelRelative = true;
135-
}
136-
else {
89+
} else {
13790
maxLevel = Integer.parseInt(string);
13891
maxLevelRelative = false;
13992
}
14093
}
14194
}
14295

143-
if (!configValue.containsKey("cost"))
96+
if (!configValue.containsKey("cost")) {
14497
return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, 0, false);
98+
}
14599

146-
if (!(configValue.get("cost") instanceof final @NotNull String costString))
100+
if (!(configValue.get("cost") instanceof final String costString)) {
147101
return new ConfigEnchantmentEntry(
148102
name,
149103
maxLevel,
150104
maxLevelRelative,
151105
(Integer) configValue.get("cost"),
152106
false
153107
);
108+
}
154109

155-
if (costString.startsWith("*"))
110+
if (costString.startsWith("*")) {
156111
return new ConfigEnchantmentEntry(
157112
name,
158113
maxLevel,
159114
maxLevelRelative,
160115
Integer.parseInt(costString.substring(1)),
161116
true
162117
);
118+
}
163119

164-
return new ConfigEnchantmentEntry(
165-
name,
166-
maxLevel,
167-
maxLevelRelative,
168-
Integer.parseInt(costString),
169-
false
170-
);
120+
return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, Integer.parseInt(costString), false);
171121
}
172122

173-
174123
/**
175124
* From config object array
176125
*
177126
* @param configValue Config object array
178127
*/
179-
public static @NotNull List<@NotNull ConfigEnchantmentEntry> configArray(
180-
final @NotNull ArrayList<@NotNull Map<@NotNull String, @NotNull Object>> configValue
181-
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
128+
public static List<ConfigEnchantmentEntry> configArray(final ArrayList<Map<String, Object>> configValue)
129+
throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
182130
return configValue.stream().map(ConfigEnchantmentEntry::configValue).collect(Collectors.toList());
183131
}
184132

@@ -188,31 +136,36 @@ public ConfigEnchantmentEntry(
188136
* @param configValue Config object
189137
*/
190138
private static boolean isValidConfigValue(final @Nullable Object configValue) {
191-
if (configValue == null)
139+
if (configValue == null) {
192140
return false;
141+
}
193142

194-
if (!(configValue instanceof final ArrayList<?> arrayList))
143+
if (!(configValue instanceof final ArrayList<?> arrayList)) {
195144
return false;
145+
}
196146

197147
for (final Object object : arrayList) {
198-
if (!(object instanceof final Map<?, ?> hashMap))
148+
if (!(object instanceof final Map<?, ?> hashMap)) {
199149
return false;
150+
}
200151

201-
if (!hashMap.containsKey("name"))
152+
if (!hashMap.containsKey("name")) {
202153
return false;
154+
}
203155

204-
if (!(hashMap.get("name") instanceof String))
156+
if (!(hashMap.get("name") instanceof String)) {
205157
return false;
158+
}
206159

207-
if (hashMap.containsKey("max-level") &&
208-
!(hashMap.get("max-level") instanceof String) &&
209-
!(hashMap.get("max-level") instanceof Integer))
160+
if (hashMap.containsKey("max-level") && !(hashMap.get("max-level") instanceof String) && !(hashMap.get(
161+
"max-level") instanceof Integer)) {
210162
return false;
163+
}
211164

212-
if (hashMap.containsKey("cost") &&
213-
!(hashMap.get("cost") instanceof String) &&
214-
!(hashMap.get("cost") instanceof Integer))
165+
if (hashMap.containsKey("cost") && !(hashMap.get("cost") instanceof String)
166+
&& !(hashMap.get("cost") instanceof Integer)) {
215167
return false;
168+
}
216169
}
217170

218171
return true;
@@ -223,16 +176,61 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
223176
*
224177
* @param configValue Config object
225178
*/
226-
public static @NotNull List<@NotNull ConfigEnchantmentEntry> config(
227-
final @Nullable Object configValue
228-
) throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
229-
if (!isValidConfigValue(configValue))
179+
public static List<ConfigEnchantmentEntry> config(final @Nullable Object configValue)
180+
throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
181+
if (!isValidConfigValue(configValue)) {
230182
throw new IllegalArgumentException("Invalid config value");
183+
}
231184

232185
//noinspection unchecked
233186
return configArray((ArrayList<Map<String, Object>>) configValue);
234187
}
235188

189+
/**
190+
* Maximum level of the enchantment.
191+
*/
192+
public final OptionalInt getMaxLevel() {
193+
if (maxLevel == null) {
194+
return OptionalInt.empty();
195+
}
196+
197+
if (maxLevelRelative) {
198+
return OptionalInt.of(getEnchantment().getMaxLevel() + maxLevel);
199+
}
200+
201+
return OptionalInt.of(maxLevel);
202+
}
203+
204+
/**
205+
* Cost of the enchantment.
206+
*/
207+
public final int getCost() {
208+
return cost;
209+
}
210+
211+
/**
212+
* Multiply cost by level.
213+
*/
214+
public final boolean getMultiplyCostByLevel() {
215+
return multiplyCostByLevel;
216+
}
217+
218+
/**
219+
* Get enchantment
220+
*/
221+
public final Enchantment getEnchantment() {
222+
return Objects.requireNonNull(Registry.ENCHANTMENT.get(NamespacedKey.minecraft(name)));
223+
}
224+
225+
/**
226+
* Is enchantment
227+
*
228+
* @param enchantment The enchantment
229+
*/
230+
public final boolean isEnchantment(final Enchantment enchantment) {
231+
return name.equalsIgnoreCase(enchantment.getKey().getKey());
232+
}
233+
236234
public static final class AllConfigEnchantmentEntry extends ConfigEnchantmentEntry {
237235
private AllConfigEnchantmentEntry(
238236
final @Nullable Integer maxLevel,
@@ -243,9 +241,7 @@ private AllConfigEnchantmentEntry(
243241
super("ALL", maxLevel, maxLevelRelative, cost, multiplyCostByLevel);
244242
}
245243

246-
public static @NotNull AllConfigEnchantmentEntry from(
247-
final @NotNull ConfigEnchantmentEntry configEnchantmentEntry
248-
) {
244+
public static AllConfigEnchantmentEntry from(final ConfigEnchantmentEntry configEnchantmentEntry) {
249245
return new AllConfigEnchantmentEntry(
250246
configEnchantmentEntry.maxLevel,
251247
configEnchantmentEntry.maxLevelRelative,
@@ -254,7 +250,7 @@ private AllConfigEnchantmentEntry(
254250
);
255251
}
256252

257-
public @NotNull ConfigEnchantmentEntry enchant(final @NotNull Enchantment enchantment) {
253+
public ConfigEnchantmentEntry enchant(final Enchantment enchantment) {
258254
return new ConfigEnchantmentEntry(
259255
enchantment.getKey().getKey(),
260256
this.maxLevel,

0 commit comments

Comments
 (0)