Skip to content

Commit c76c7f7

Browse files
committed
First public version of the Mod API
1 parent 15f0ca7 commit c76c7f7

17 files changed

+721
-1
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/
8+
*.iws
9+
*.iml
10+
*.ipr
11+
12+
### Eclipse ###
13+
.apt_generated
14+
.classpath
15+
.factorypath
16+
.project
17+
.settings
18+
.springBeans
19+
.sts4-cache
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
33+
34+
### Mac OS ###
35+
.DS_Store

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# ModAPI
1+
# Hypixel Mod API
2+
3+
The Hypixel Mod API is an implementation of custom packets for communicating with the Hypixel Server via plugin messages.
4+
5+
At this time the API is in an early preview state to obtain feedback from the community. The API is subject to change and may be changed or disabled at any time. You can read more about on the [announcement forum thread](https://hypixel.net/threads/hypixel-mod-api-developer-preview-feedback.5635119/).

pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.hypixel</groupId>
8+
<artifactId>mod-api</artifactId>
9+
<version>0.1.6</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<distributionManagement>
18+
<repository>
19+
<id>Hypixel</id>
20+
<url>https://repo.hypixel.net/repository/Hypixel/</url>
21+
</repository>
22+
</distributionManagement>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.jetbrains</groupId>
27+
<artifactId>annotations</artifactId>
28+
<version>24.0.0</version>
29+
<scope>provided</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.netty</groupId>
33+
<artifactId>netty-all</artifactId>
34+
<version>4.0.56.Final</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-engine</artifactId>
40+
<version>5.9.2</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>2.3.2</version>
51+
<configuration>
52+
<encoding>UTF-8</encoding>
53+
<source>1.8</source>
54+
<target>1.8</target>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package net.hypixel.modapi;
2+
3+
import net.hypixel.modapi.handler.ClientboundPacketHandler;
4+
import net.hypixel.modapi.packet.HypixelPacket;
5+
import net.hypixel.modapi.packet.HypixelPacketType;
6+
import net.hypixel.modapi.serializer.PacketSerializer;
7+
8+
import java.util.List;
9+
import java.util.concurrent.CopyOnWriteArrayList;
10+
11+
public class HypixelModAPI {
12+
private static final HypixelModAPI INSTANCE = new HypixelModAPI();
13+
14+
public static HypixelModAPI getInstance() {
15+
return INSTANCE;
16+
}
17+
18+
private final List<ClientboundPacketHandler> packetHandlers = new CopyOnWriteArrayList<>();
19+
20+
private HypixelModAPI() {
21+
}
22+
23+
public void registerHandler(ClientboundPacketHandler handler) {
24+
packetHandlers.add(handler);
25+
}
26+
27+
public void handle(String identifier, PacketSerializer serializer) {
28+
if (packetHandlers.isEmpty()) {
29+
return;
30+
}
31+
32+
HypixelPacketType packetType = HypixelPacketType.getByIdentifier(identifier);
33+
if (packetType == null) {
34+
return;
35+
}
36+
37+
// All responses contain a boolean of if the response is a success, if not then a string is included with the error message
38+
if (!serializer.readBoolean()) {
39+
String errorMessage = serializer.readString();
40+
throw new RuntimeException("Received error response for packet " + packetType + ": " + errorMessage);
41+
}
42+
43+
HypixelPacket packet = packetType.getPacketFactory().apply(serializer);
44+
for (ClientboundPacketHandler handler : packetHandlers) {
45+
handler.handle(packet);
46+
}
47+
}
48+
49+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.hypixel.modapi.data;
2+
3+
public enum Environment {
4+
PRODUCTION, // The Production Hypixel Network - "mc.hypixel.net"
5+
BETA, // The Alpha Hypixel Network - "alpha.hypixel.net"
6+
TEST, // The Test Hypixel Network - For Hypixel Developers
7+
;
8+
9+
public static final Environment[] VALUES = values();
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.hypixel.modapi.handler;
2+
3+
import net.hypixel.modapi.packet.HypixelPacket;
4+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundLocationPacket;
5+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
6+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
7+
8+
public interface ClientboundPacketHandler extends PacketHandler {
9+
10+
default void handle(HypixelPacket packet) {
11+
if (packet instanceof ClientboundPingPacket) {
12+
onPingPacket((ClientboundPingPacket) packet);
13+
}
14+
15+
if (packet instanceof ClientboundLocationPacket) {
16+
onLocationPacket((ClientboundLocationPacket) packet);
17+
}
18+
19+
if (packet instanceof ClientboundPartyInfoPacket) {
20+
onPartyInfoPacket((ClientboundPartyInfoPacket) packet);
21+
}
22+
}
23+
24+
default void onPingPacket(ClientboundPingPacket packet) {
25+
}
26+
27+
default void onLocationPacket(ClientboundLocationPacket packet) {
28+
}
29+
30+
default void onPartyInfoPacket(ClientboundPartyInfoPacket packet) {
31+
}
32+
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.hypixel.modapi.handler;
2+
3+
import net.hypixel.modapi.packet.HypixelPacket;
4+
5+
public interface PacketHandler {
6+
7+
void handle(HypixelPacket packet);
8+
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.hypixel.modapi.packet;
2+
3+
import net.hypixel.modapi.serializer.PacketSerializer;
4+
5+
public interface HypixelPacket {
6+
7+
HypixelPacketType getType();
8+
9+
void write(PacketSerializer serializer);
10+
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.hypixel.modapi.packet;
2+
3+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundLocationPacket;
4+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
5+
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
6+
import net.hypixel.modapi.serializer.PacketSerializer;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import java.util.function.Function;
13+
14+
public enum HypixelPacketType {
15+
PING(ClientboundPingPacket::new),
16+
LOCATION(ClientboundLocationPacket::new),
17+
PARTY_INFO(ClientboundPartyInfoPacket::new),
18+
;
19+
private static final String IDENTIFIER_PREFIX = "hypixel:";
20+
private static final Map<String, HypixelPacketType> BY_IDENTIFIER = Arrays.stream(values()).collect(HashMap::new, (map, type) -> map.put(type.getIdentifier(), type), HashMap::putAll);
21+
private final Function<PacketSerializer, HypixelPacket> packetFactory;
22+
23+
@Nullable
24+
public static HypixelPacketType getByIdentifier(String identifier) {
25+
return BY_IDENTIFIER.get(identifier);
26+
}
27+
28+
private final String identifier;
29+
30+
HypixelPacketType(Function<PacketSerializer, HypixelPacket> packetFactory) {
31+
this.identifier = IDENTIFIER_PREFIX + name().toLowerCase();
32+
this.packetFactory = packetFactory;
33+
}
34+
35+
public String getIdentifier() {
36+
return identifier;
37+
}
38+
39+
public Function<PacketSerializer, HypixelPacket> getPacketFactory() {
40+
return packetFactory;
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.hypixel.modapi.packet.impl;
2+
3+
import net.hypixel.modapi.packet.HypixelPacket;
4+
import net.hypixel.modapi.serializer.PacketSerializer;
5+
6+
public abstract class VersionedPacket implements HypixelPacket {
7+
private final byte version;
8+
9+
public VersionedPacket(byte version) {
10+
this.version = version;
11+
}
12+
13+
public VersionedPacket(PacketSerializer byteBuf) {
14+
this.version = byteBuf.readByte();
15+
}
16+
17+
@Override
18+
public void write(PacketSerializer serializer) {
19+
serializer.writeByte(version);
20+
}
21+
22+
public byte getVersion() {
23+
return version;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "VersionedPacket{" +
29+
"version=" + version +
30+
'}';
31+
}
32+
}

0 commit comments

Comments
 (0)