Skip to content

Commit 949beeb

Browse files
committed
3.5.0 - 1.18 Support
Added support for 1.18 altho there weren't many changes. Started working on unit tests. It's kinda a mess right now. XMaterial Fixed a few version detection issues. ParticleDisplay Removed some unnecessary code. The whole class is based on Builder Pattern now. XItemStack Fixed a few issues related to spawn eggs. Fixed proper cross version material name serialization. Fixed issues with items that don't have meta. ReflectionUtils Added a simple if branching method v(version, item)...orElse(item) as a cleaner expression-oriented alternative to if statements.
1 parent 1e513da commit 949beeb

File tree

17 files changed

+1024
-180
lines changed

17 files changed

+1024
-180
lines changed

.github/ISSUE_TEMPLATE/feature-request.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ assignees: CryptoMorin
88

99
**Description**
1010
A clear and concise description of what the feature is.
11-
Support for versions older than 1.8 will not be added. Some projects might not even support 1.8
11+
Support for versions older than 1.8 will not be added. Some projects might not even support 1.8
12+
13+
Do not make requests regarding support for newly released Minecraft versions (read [contributing guidelines](/CONTRIBUTING.md) for more info).

CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ All the pull requests are merged directly into the master branch.
88
This project imports the full Spigot JAR from an unofficial repo as SkullUtils uses `com.mojang.authlib`
99
It's also used for JavaX nullability annotations.
1010

11-
Do not make PRs relating to adding support when a new version of Minecraft comes out. I'll be finishing the update within the first week of Spigot release. Multiple developers
12-
working on the same thing will be just a waste of time and resources.
11+
Do not make any PRs/issues regarding adding support for new Minecraft versions. I'll be usually finishing the update within the first week of Spigot release.
12+
Having multiple developers work on the same issue will be just a waste of time and resources.
13+
You should also not make any PRs before support is added when a new Minecraft version comes out even
14+
if it's unrelated to adding support for that version since your changes are likely to conflict with the update.
1315

1416
### Rules
1517
* Only Java 8 should be used. All the functions in the latest version of Java 8 can be used.

pom.xml

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.cryptomorin</groupId>
88
<artifactId>XSeries</artifactId>
9-
<version>8.4.0</version>
9+
<version>8.5.0</version>
1010

1111
<name>XSeries</name>
1212
<description>A set of utilities for Minecraft plugins</description>
@@ -65,9 +65,15 @@
6565
<dependency>
6666
<groupId>org.spigotmc</groupId>
6767
<artifactId>spigot</artifactId>
68-
<version>1.17-R0.1-SNAPSHOT</version>
68+
<version>1.17.1-R0.1-SNAPSHOT</version>
6969
<scope>provided</scope>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.junit.jupiter</groupId>
73+
<artifactId>junit-jupiter</artifactId>
74+
<version>5.8.2</version>
75+
<scope>test</scope>
76+
</dependency>
7177
</dependencies>
7278

7379
<build>
@@ -150,7 +156,124 @@
150156
<exclude>com/cryptomorin/xseries/SkullCacheListener.java</exclude>
151157
</excludes>
152158
</configuration>
159+
<executions>
160+
<execution>
161+
<id>default-testCompile</id>
162+
<phase>test-compile</phase>
163+
<goals>
164+
<goal>testCompile</goal>
165+
</goals>
166+
<configuration>
167+
<skip>true</skip>
168+
</configuration>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
<plugin>
173+
<groupId>org.apache.maven.plugins</groupId>
174+
<artifactId>maven-surefire-plugin</artifactId>
175+
<version>3.0.0-M5</version>
176+
<configuration>
177+
<workingDirectory>${basedir}/target/tests</workingDirectory>
178+
<includes>
179+
<include>**/Spigot*.java</include>
180+
</includes>
181+
<excludes>
182+
<exclude>com/cryptomorin/xseries/unused/</exclude>
183+
<exclude>com/cryptomorin/xseries/XMaterialUtil.java</exclude>
184+
<exclude>com/cryptomorin/xseries/SkullCacheListener.java</exclude>
185+
</excludes>
186+
</configuration>
153187
</plugin>
154188
</plugins>
189+
190+
<testSourceDirectory>src/test</testSourceDirectory>
191+
<testResources>
192+
<testResource>
193+
<directory>src/test/resources</directory>
194+
</testResource>
195+
</testResources>
155196
</build>
197+
198+
<profiles>
199+
<profile>
200+
<id>1_18</id>
201+
<build>
202+
<plugins>
203+
<plugin>
204+
<groupId>org.apache.maven.plugins</groupId>
205+
<artifactId>maven-compiler-plugin</artifactId>
206+
<version>3.8.1</version>
207+
<configuration>
208+
<source>17</source>
209+
<target>17</target>
210+
</configuration>
211+
<executions>
212+
<execution>
213+
<id>default-testCompile</id>
214+
<phase>test-compile</phase>
215+
<goals>
216+
<goal>testCompile</goal>
217+
</goals>
218+
<configuration>
219+
<skip>false</skip>
220+
<!-- <testExcludes>-->
221+
<!-- <exclude>**/**1_18.java</exclude>-->
222+
<!-- </testExcludes>-->
223+
</configuration>
224+
</execution>
225+
</executions>
226+
</plugin>
227+
</plugins>
228+
</build>
229+
<dependencies>
230+
<dependency>
231+
<groupId>org.spigotmc</groupId>
232+
<artifactId>spigot</artifactId>
233+
<!-- For some reasons codemc's 1.18 doesn't include NMS -->
234+
<version>1.17.1-R0.1-SNAPSHOT</version>
235+
<scope>compile</scope>
236+
</dependency>
237+
</dependencies>
238+
</profile>
239+
<profile>
240+
<id>1_16R2</id>
241+
<build>
242+
<plugins>
243+
<plugin>
244+
<groupId>org.apache.maven.plugins</groupId>
245+
<artifactId>maven-compiler-plugin</artifactId>
246+
<version>3.8.1</version>
247+
<configuration>
248+
<source>8</source>
249+
<target>8</target>
250+
</configuration>
251+
<executions>
252+
<execution>
253+
<id>default-testCompile</id>
254+
<phase>test-compile</phase>
255+
<goals>
256+
<goal>testCompile</goal>
257+
</goals>
258+
<configuration>
259+
<skip>false</skip>
260+
<testExcludes>
261+
<exclude>**/**1_16R3.java</exclude>
262+
</testExcludes>
263+
</configuration>
264+
</execution>
265+
</executions>
266+
</plugin>
267+
</plugins>
268+
</build>
269+
<dependencies>
270+
<dependency>
271+
<groupId>org.spigotmc</groupId>
272+
<artifactId>spigot</artifactId>
273+
<version>1.16.3-R0.1-SNAPSHOT</version>
274+
<scope>provided</scope>
275+
</dependency>
276+
</dependencies>
277+
</profile>
278+
</profiles>
156279
</project>

src/main/java/com/cryptomorin/xseries/ReflectionUtils.java

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.lang.invoke.MethodHandles;
3131
import java.lang.invoke.MethodType;
3232
import java.util.Objects;
33+
import java.util.concurrent.Callable;
3334
import java.util.concurrent.CompletableFuture;
3435

3536
/**
@@ -46,7 +47,7 @@
4647
* A useful resource used to compare mappings is <a href="https://minidigger.github.io/MiniMappingViewer/#/spigot">Mini's Mapping Viewer</a>
4748
*
4849
* @author Crypto Morin
49-
* @version 4.0.0
50+
* @version 5.0.0
5051
*/
5152
public final class ReflectionUtils {
5253
/**
@@ -73,7 +74,7 @@ public final class ReflectionUtils {
7374
*/
7475
public static final String
7576
CRAFTBUKKIT = "org.bukkit.craftbukkit." + VERSION + '.',
76-
NMS = supports(17) ? "net.minecraft." : "net.minecraft.server." + VERSION + '.';
77+
NMS = v(17, "net.minecraft.").orElse("net.minecraft.server." + VERSION + '.');
7778
/**
7879
* A nullable public accessible field only available in {@code EntityPlayer}.
7980
* This can be null if the player is offline.
@@ -103,9 +104,13 @@ public final class ReflectionUtils {
103104
MethodHandle getHandle = null;
104105
MethodHandle connection = null;
105106
try {
106-
connection = lookup.findGetter(entityPlayer, supports(17) ? "b" : "playerConnection", playerConnection);
107+
connection = lookup.findGetter(entityPlayer,
108+
v(18, "b").orElse("playerConnection"),
109+
playerConnection);
107110
getHandle = lookup.findVirtual(craftPlayer, "getHandle", MethodType.methodType(entityPlayer));
108-
sendPacket = lookup.findVirtual(playerConnection, "sendPacket", MethodType.methodType(void.class, getNMSClass("network.protocol", "Packet")));
111+
sendPacket = lookup.findVirtual(playerConnection,
112+
v(18, "a").orElse("sendPacket"),
113+
MethodType.methodType(void.class, getNMSClass("network.protocol", "Packet")));
109114
} catch (NoSuchMethodException | NoSuchFieldException | IllegalAccessException ex) {
110115
ex.printStackTrace();
111116
}
@@ -117,6 +122,20 @@ public final class ReflectionUtils {
117122

118123
private ReflectionUtils() {}
119124

125+
/**
126+
* This method is purely for readability.
127+
* No performance is gained.
128+
*
129+
* @since 5.0.0
130+
*/
131+
public static <T> VersionHandler<T> v(int version, T handle) {
132+
return new VersionHandler<>(version, handle);
133+
}
134+
135+
public static <T> CallableVersionHandler<T> v(int version, Callable<T> handle) {
136+
return new CallableVersionHandler<>(version, handle);
137+
}
138+
120139
/**
121140
* Checks whether the server version is equal or greater than the given version.
122141
*
@@ -262,4 +281,59 @@ public static Class<?> toArrayClass(Class<?> clazz) {
262281
return null;
263282
}
264283
}
284+
285+
public static final class VersionHandler<T> {
286+
private int version;
287+
private T handle;
288+
289+
private VersionHandler(int version, T handle) {
290+
if (supports(version)) {
291+
this.version = version;
292+
this.handle = handle;
293+
}
294+
}
295+
296+
public VersionHandler<T> v(int version, T handle) {
297+
if (version == this.version) throw new IllegalArgumentException("Cannot have duplicate version handles for version: " + version);
298+
if (version > this.version && supports(version)) {
299+
this.version = version;
300+
this.handle = handle;
301+
}
302+
return this;
303+
}
304+
305+
public T orElse(T handle) {
306+
return this.version == 0 ? handle : this.handle;
307+
}
308+
}
309+
310+
public static final class CallableVersionHandler<T> {
311+
private int version;
312+
private Callable<T> handle;
313+
314+
private CallableVersionHandler(int version, Callable<T> handle) {
315+
if (supports(version)) {
316+
this.version = version;
317+
this.handle = handle;
318+
}
319+
}
320+
321+
public CallableVersionHandler<T> v(int version, Callable<T> handle) {
322+
if (version == this.version) throw new IllegalArgumentException("Cannot have duplicate version handles for version: " + version);
323+
if (version > this.version && supports(version)) {
324+
this.version = version;
325+
this.handle = handle;
326+
}
327+
return this;
328+
}
329+
330+
public T orElse(Callable<T> handle) {
331+
try {
332+
return (this.version == 0 ? handle : this.handle).call();
333+
} catch (Exception e) {
334+
e.printStackTrace();
335+
return null;
336+
}
337+
}
338+
}
265339
}

0 commit comments

Comments
 (0)