Skip to content

Commit e66cf66

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

7 files changed

Lines changed: 206 additions & 187 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 & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
import org.bukkit.Registry;
55
import org.bukkit.enchantments.Enchantment;
66
import org.jetbrains.annotations.NotNull;
7-
import org.jetbrains.annotations.Nullable;
7+
import org.jspecify.annotations.NullMarked;
8+
import org.jspecify.annotations.Nullable;
89

910
import java.util.ArrayList;
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.Objects;
13-
import java.util.Optional;
14+
import java.util.OptionalInt;
1415
import java.util.stream.Collectors;
1516

17+
@NullMarked
1618
public class ConfigEnchantmentEntry {
1719
/**
1820
* Name of the enchantment.
1921
*/
20-
public final @NotNull String name;
22+
public final String name;
2123

2224
/**
2325
* Maximum level of the enchantment.
@@ -40,57 +42,14 @@ public class ConfigEnchantmentEntry {
4042
protected final boolean multiplyCostByLevel;
4143

4244
/**
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.
45+
* @param name Name of the enchantment.
46+
* @param maxLevel Maximum level of the enchantment.
47+
* @param maxLevelRelative Max level relative
48+
* @param cost Cost of the enchantment.
9049
* @param multiplyCostByLevel Multiply cost by level.
9150
*/
9251
public ConfigEnchantmentEntry(
93-
final @NotNull String name,
52+
final String name,
9453
final @Nullable Integer maxLevel,
9554
final boolean maxLevelRelative,
9655
final int cost,
@@ -108,77 +67,67 @@ public ConfigEnchantmentEntry(
10867
*
10968
* @param configValue Config object
11069
*/
111-
public static @NotNull ConfigEnchantmentEntry configValue(
112-
final @NotNull Map<@NotNull String, @NotNull Object> configValue
70+
public static ConfigEnchantmentEntry configValue(
71+
final Map<String, Object> configValue
11372
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
11473
final String name = (String) Objects.requireNonNull(configValue.get("name"));
11574

116-
final @Nullable Integer maxLevel;
75+
final Integer maxLevel;
11776

11877
final boolean maxLevelRelative;
11978

12079
if (!configValue.containsKey("max-level")) {
12180
maxLevel = null;
12281
maxLevelRelative = false;
123-
}
124-
125-
else {
82+
} else {
12683
if (!(configValue.get("max-level") instanceof final String string)) {
12784
maxLevel = (Integer) configValue.get("max-level");
12885
maxLevelRelative = false;
129-
}
130-
131-
else {
86+
} else {
13287
if (string.startsWith("+")) {
13388
maxLevel = Integer.parseInt(string.substring(1));
13489
maxLevelRelative = true;
135-
}
136-
else {
90+
} else {
13791
maxLevel = Integer.parseInt(string);
13892
maxLevelRelative = false;
13993
}
14094
}
14195
}
14296

143-
if (!configValue.containsKey("cost"))
97+
if (!configValue.containsKey("cost")) {
14498
return new ConfigEnchantmentEntry(name, maxLevel, maxLevelRelative, 0, false);
99+
}
145100

146-
if (!(configValue.get("cost") instanceof final @NotNull String costString))
101+
if (!(configValue.get("cost") instanceof final @NotNull String costString)) {
147102
return new ConfigEnchantmentEntry(
148103
name,
149104
maxLevel,
150105
maxLevelRelative,
151106
(Integer) configValue.get("cost"),
152107
false
153108
);
109+
}
154110

155-
if (costString.startsWith("*"))
111+
if (costString.startsWith("*")) {
156112
return new ConfigEnchantmentEntry(
157113
name,
158114
maxLevel,
159115
maxLevelRelative,
160116
Integer.parseInt(costString.substring(1)),
161117
true
162118
);
119+
}
163120

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

173-
174124
/**
175125
* From config object array
176126
*
177127
* @param configValue Config object array
178128
*/
179-
public static @NotNull List<@NotNull ConfigEnchantmentEntry> configArray(
180-
final @NotNull ArrayList<@NotNull Map<@NotNull String, @NotNull Object>> configValue
181-
) throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
129+
public static List<ConfigEnchantmentEntry> configArray(final ArrayList<Map<String, Object>> configValue)
130+
throws NumberFormatException, IndexOutOfBoundsException, ClassCastException {
182131
return configValue.stream().map(ConfigEnchantmentEntry::configValue).collect(Collectors.toList());
183132
}
184133

@@ -188,31 +137,36 @@ public ConfigEnchantmentEntry(
188137
* @param configValue Config object
189138
*/
190139
private static boolean isValidConfigValue(final @Nullable Object configValue) {
191-
if (configValue == null)
140+
if (configValue == null) {
192141
return false;
142+
}
193143

194-
if (!(configValue instanceof final ArrayList<?> arrayList))
144+
if (!(configValue instanceof final ArrayList<?> arrayList)) {
195145
return false;
146+
}
196147

197148
for (final Object object : arrayList) {
198-
if (!(object instanceof final Map<?, ?> hashMap))
149+
if (!(object instanceof final Map<?, ?> hashMap)) {
199150
return false;
151+
}
200152

201-
if (!hashMap.containsKey("name"))
153+
if (!hashMap.containsKey("name")) {
202154
return false;
155+
}
203156

204-
if (!(hashMap.get("name") instanceof String))
157+
if (!(hashMap.get("name") instanceof String)) {
205158
return false;
159+
}
206160

207-
if (hashMap.containsKey("max-level") &&
208-
!(hashMap.get("max-level") instanceof String) &&
209-
!(hashMap.get("max-level") instanceof Integer))
161+
if (hashMap.containsKey("max-level") && !(hashMap.get("max-level") instanceof String) && !(hashMap.get(
162+
"max-level") instanceof Integer)) {
210163
return false;
164+
}
211165

212-
if (hashMap.containsKey("cost") &&
213-
!(hashMap.get("cost") instanceof String) &&
214-
!(hashMap.get("cost") instanceof Integer))
166+
if (hashMap.containsKey("cost") && !(hashMap.get("cost") instanceof String)
167+
&& !(hashMap.get("cost") instanceof Integer)) {
215168
return false;
169+
}
216170
}
217171

218172
return true;
@@ -223,16 +177,61 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
223177
*
224178
* @param configValue Config object
225179
*/
226-
public static @NotNull List<@NotNull ConfigEnchantmentEntry> config(
227-
final @Nullable Object configValue
228-
) throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
229-
if (!isValidConfigValue(configValue))
180+
public static List<ConfigEnchantmentEntry> config(final @Nullable Object configValue)
181+
throws IllegalArgumentException, IndexOutOfBoundsException, ClassCastException {
182+
if (!isValidConfigValue(configValue)) {
230183
throw new IllegalArgumentException("Invalid config value");
184+
}
231185

232186
//noinspection unchecked
233187
return configArray((ArrayList<Map<String, Object>>) configValue);
234188
}
235189

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

246-
public static @NotNull AllConfigEnchantmentEntry from(
247-
final @NotNull ConfigEnchantmentEntry configEnchantmentEntry
248-
) {
245+
public static AllConfigEnchantmentEntry from(final ConfigEnchantmentEntry configEnchantmentEntry) {
249246
return new AllConfigEnchantmentEntry(
250247
configEnchantmentEntry.maxLevel,
251248
configEnchantmentEntry.maxLevelRelative,
@@ -254,7 +251,7 @@ private AllConfigEnchantmentEntry(
254251
);
255252
}
256253

257-
public @NotNull ConfigEnchantmentEntry enchant(final @NotNull Enchantment enchantment) {
254+
public ConfigEnchantmentEntry enchant(final Enchantment enchantment) {
258255
return new ConfigEnchantmentEntry(
259256
enchantment.getKey().getKey(),
260257
this.maxLevel,

0 commit comments

Comments
 (0)