Skip to content

Commit a58b41e

Browse files
authored
Added support for older versions (1.17–1.21.11) and Spigot/Bukkit (#47)
2 parents 8f77236 + b693788 commit a58b41e

File tree

11 files changed

+305
-172
lines changed

11 files changed

+305
-172
lines changed

.github/workflows/codeql.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- uses: actions/setup-java@v4
5353
with:
5454
distribution: 'temurin'
55-
java-version: 17
55+
java-version: 16
5656

5757
# Initializes the CodeQL tools for scanning.
5858
- name: Initialize CodeQL

.github/workflows/maven.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424

2525
steps:
2626
- uses: actions/checkout@v6
27-
- name: Set up JDK 17
27+
- name: Set up JDK 16
2828
uses: actions/setup-java@v4
2929
with:
30-
java-version: '17'
30+
java-version: '16'
3131
distribution: 'temurin'
3232
cache: maven
3333
- name: Build with Maven

.github/workflows/release.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Java
1717
uses: actions/setup-java@v4
1818
with:
19-
java-version: 17
19+
java-version: 16
2020
distribution: temurin
2121
cache: maven
2222

@@ -48,8 +48,14 @@ jobs:
4848
name: ${{ github.event.release.name == github.event.release.tag_name && format('EnchantBookPlus {0}', steps.version.outputs.VERSION) || github.event.release.name }}
4949
version: ${{ steps.version.outputs.VERSION }}
5050
changelog: ${{ github.event.release.body }}
51-
loaders: paper
51+
loaders: |-
52+
paper
53+
spigot
54+
bukkit
5255
game-versions: |-
56+
1.17.x
57+
1.18.x
58+
1.19.x
5359
1.20.x
5460
1.21.x
5561
files: target/EnchantBookPlus-${{ steps.version.outputs.VERSION }}.jar

.idea/jarRepositories.xml

Lines changed: 5 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: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<name>EnchantBookPlus</name>
1313

1414
<properties>
15-
<java.version>17</java.version>
15+
<java.version>16</java.version>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
</properties>
1818

@@ -27,22 +27,6 @@
2727
<target>${java.version}</target>
2828
</configuration>
2929
</plugin>
30-
<plugin>
31-
<groupId>org.apache.maven.plugins</groupId>
32-
<artifactId>maven-shade-plugin</artifactId>
33-
<version>3.6.1</version>
34-
<executions>
35-
<execution>
36-
<phase>package</phase>
37-
<goals>
38-
<goal>shade</goal>
39-
</goals>
40-
<configuration>
41-
<createDependencyReducedPom>false</createDependencyReducedPom>
42-
</configuration>
43-
</execution>
44-
</executions>
45-
</plugin>
4630
</plugins>
4731
<resources>
4832
<resource>
@@ -54,21 +38,23 @@
5438

5539
<repositories>
5640
<repository>
57-
<id>papermc-repo</id>
58-
<url>https://repo.papermc.io/repository/maven-public/</url>
59-
</repository>
60-
<repository>
61-
<id>sonatype</id>
62-
<url>https://oss.sonatype.org/content/groups/public/</url>
41+
<id>spigotmc-repo</id>
42+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
6343
</repository>
6444
</repositories>
6545

6646
<dependencies>
6747
<dependency>
68-
<groupId>io.papermc.paper</groupId>
69-
<artifactId>paper-api</artifactId>
70-
<version>1.20.4-R0.1-SNAPSHOT</version>
48+
<groupId>org.spigotmc</groupId>
49+
<artifactId>spigot-api</artifactId>
50+
<version>1.17-R0.1-SNAPSHOT</version>
7151
<scope>provided</scope>
7252
</dependency>
53+
<dependency>
54+
<groupId>org.jetbrains</groupId>
55+
<artifactId>annotations</artifactId>
56+
<version>26.0.2</version>
57+
<scope>compile</scope>
58+
</dependency>
7359
</dependencies>
7460
</project>
Lines changed: 122 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package pro.cloudnode.smp.enchantbookplus;
22

33
import org.bukkit.NamespacedKey;
4+
import org.bukkit.Registry;
45
import org.bukkit.enchantments.Enchantment;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
78

89
import java.util.ArrayList;
9-
import java.util.HashMap;
1010
import java.util.List;
11+
import java.util.Map;
1112
import java.util.Objects;
1213
import java.util.Optional;
1314
import 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

Comments
 (0)