Skip to content

Commit aa6925a

Browse files
Implement mod implementation interface for use in downstream mods (#40)
This change will provide better expansion moving forward for the downstream mod implementations, for now, the major addition is allowing downstream mods to track if we are connected to Hypixel to prevent sending plugin messages when we're not.
1 parent 9c98e57 commit aa6925a

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/main/java/net/hypixel/modapi/HypixelModAPI.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import net.hypixel.modapi.packet.impl.serverbound.ServerboundRegisterPacket;
1717
import net.hypixel.modapi.serializer.PacketSerializer;
1818
import org.jetbrains.annotations.ApiStatus;
19+
import org.jetbrains.annotations.Nullable;
1920

2021
import java.util.*;
2122
import java.util.concurrent.ConcurrentHashMap;
2223
import java.util.concurrent.CopyOnWriteArrayList;
23-
import java.util.function.Predicate;
2424

2525
public class HypixelModAPI {
2626
private static final HypixelModAPI INSTANCE = new HypixelModAPI();
@@ -33,12 +33,12 @@ public static HypixelModAPI getInstance() {
3333
private final Map<String, Collection<RegisteredHandlerImpl<?>>> handlers = new ConcurrentHashMap<>();
3434
private final Set<String> subscribedEvents = ConcurrentHashMap.newKeySet();
3535
private Set<String> lastSubscribedEvents = Collections.emptySet();
36-
private Predicate<HypixelPacket> packetSender = null;
36+
@Nullable
37+
private HypixelModAPIImplementation modImplementation;
3738

3839
private HypixelModAPI() {
3940
registerHypixelPackets();
4041
registerEventPackets();
41-
registerDefaultHandler();
4242
}
4343

4444
private void registerHypixelPackets() {
@@ -77,8 +77,8 @@ private void registerDefaultHandler() {
7777
}
7878

7979
private void sendRegisterPacket(boolean alwaysSendIfNotEmpty) {
80-
if (packetSender == null) {
81-
// Allow registering events before the mod has fully initialized
80+
if (modImplementation == null || !modImplementation.isConnectedToHypixel()) {
81+
// Allow registering events when not connected to Hypixel
8282
return;
8383
}
8484

@@ -144,11 +144,18 @@ public void handleError(String identifier, ErrorReason reason) {
144144
}
145145

146146
@ApiStatus.Internal
147-
public void setPacketSender(Predicate<HypixelPacket> packetSender) {
148-
if (this.packetSender != null) {
149-
throw new IllegalArgumentException("Packet sender already set");
147+
public void setModImplementation(HypixelModAPIImplementation modImplementation) {
148+
if (this.modImplementation != null) {
149+
throw new IllegalStateException("Mod implementation already set");
150+
}
151+
152+
if (modImplementation == null) {
153+
throw new NullPointerException("modImplementation cannot be null");
150154
}
151-
this.packetSender = packetSender;
155+
156+
this.modImplementation = modImplementation;
157+
this.modImplementation.onInit();
158+
registerDefaultHandler();
152159
}
153160

154161
/**
@@ -184,11 +191,11 @@ public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
184191
* @return whether the packet was sent successfully
185192
*/
186193
public boolean sendPacket(HypixelPacket packet) {
187-
if (packetSender == null) {
188-
throw new IllegalStateException("Packet sender not set");
194+
if (modImplementation == null) {
195+
throw new IllegalStateException("Mod implementation not set");
189196
}
190197

191-
return packetSender.test(packet);
198+
return modImplementation.sendPacket(packet);
192199
}
193200

194201
private static class RegisteredHandlerImpl<T extends ClientboundHypixelPacket> implements RegisteredHandler<T> {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.hypixel.modapi;
2+
3+
import net.hypixel.modapi.packet.HypixelPacket;
4+
import org.jetbrains.annotations.ApiStatus;
5+
6+
@ApiStatus.Internal
7+
public interface HypixelModAPIImplementation {
8+
9+
void onInit();
10+
11+
boolean sendPacket(HypixelPacket packet);
12+
13+
boolean isConnectedToHypixel();
14+
15+
}

0 commit comments

Comments
 (0)