Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/main/java/net/hypixel/modapi/HypixelModAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static HypixelModAPI getInstance() {
}

private final PacketRegistry registry = new PacketRegistry();
private final List<ClientboundPacketHandler> handlers = new CopyOnWriteArrayList<>();
private final Map<Class<? extends ClientboundHypixelPacket>, Collection<ClientboundPacketHandler<?>>> handlers = new ConcurrentHashMap<>();
private final Set<String> subscribedEvents = ConcurrentHashMap.newKeySet();
private Set<String> lastSubscribedEvents = Collections.emptySet();
private Predicate<HypixelPacket> packetSender = null;
Expand Down Expand Up @@ -71,20 +71,16 @@ private void registerEventPackets() {
}

private void registerDefaultHandler() {
registerHandler(new ClientboundPacketHandler() {
@Override
public void onHelloEvent(ClientboundHelloPacket packet) {
sendRegisterPacket(true);
}
});
registerHandler(ClientboundHelloPacket.class, p -> sendRegisterPacket(true));
}

public PacketRegistry getRegistry() {
return registry;
}

public void registerHandler(ClientboundPacketHandler handler) {
handlers.add(handler);
public <T extends ClientboundHypixelPacket> void registerHandler(Class<T> packetClass, ClientboundPacketHandler<T> handler) {
if (packetClass == null || handler == null) return;
handlers.computeIfAbsent(packetClass, cls -> new CopyOnWriteArrayList<>()).add(handler);
}

public void subscribeToEventPacket(Class<? extends EventPacket> packet) {
Expand Down Expand Up @@ -134,9 +130,14 @@ public void handle(String identifier, PacketSerializer serializer) {
handle(packet);
}

@SuppressWarnings("unchecked")
public void handle(ClientboundHypixelPacket packet) {
for (ClientboundPacketHandler handler : handlers) {
packet.handle(handler);
Collection<ClientboundPacketHandler<?>> typedHandlers = handlers.get(packet.getClass());
// nothing registered for this packet.
if (typedHandlers == null) return;
for (ClientboundPacketHandler<?> handler : typedHandlers) {
// this cast is safe as we ensure its type when it is added to the handlers list in the first place.
((ClientboundPacketHandler<ClientboundHypixelPacket>) handler).handle(packet);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
package net.hypixel.modapi.handler;

import net.hypixel.modapi.packet.impl.clientbound.ClientboundHelloPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPartyInfoPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPingPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundPlayerInfoPacket;
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;

public interface ClientboundPacketHandler {

default void onHelloEvent(ClientboundHelloPacket packet) {
}

default void onPingPacket(ClientboundPingPacket packet) {
}

default void onPartyInfoPacket(ClientboundPartyInfoPacket packet) {
}

default void onPlayerInfoPacket(ClientboundPlayerInfoPacket packet) {
}

default void onLocationEvent(ClientboundLocationPacket packet) {
}
@FunctionalInterface
public interface ClientboundPacketHandler<T extends ClientboundHypixelPacket> {
void handle(T packet);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
package net.hypixel.modapi.packet;

import net.hypixel.modapi.handler.ClientboundPacketHandler;

public interface ClientboundHypixelPacket extends HypixelPacket {

void handle(ClientboundPacketHandler handler);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.data.region.Environment;
import net.hypixel.modapi.annotation.Experimental;
import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.serializer.PacketSerializer;

Expand All @@ -24,11 +22,6 @@ public ClientboundHelloPacket(PacketSerializer serializer) {
serializer.discardRemaining();
}

@Override
public void handle(ClientboundPacketHandler handler) {
handler.onHelloEvent(this);
}

@Override
public void write(PacketSerializer serializer) {
serializer.writeVarInt(environment.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.serializer.PacketSerializer;

import java.util.*;
Expand Down Expand Up @@ -84,11 +83,6 @@ protected int getLatestVersion() {
return CURRENT_VERSION;
}

@Override
public void handle(ClientboundPacketHandler handler) {
handler.onPartyInfoPacket(this);
}

public boolean isInParty() {
return inParty;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;

public class ClientboundPingPacket extends ClientboundVersionedPacket {
Expand Down Expand Up @@ -40,11 +37,6 @@ protected int getLatestVersion() {
return CURRENT_VERSION;
}

@Override
public void handle(ClientboundPacketHandler handler) {
handler.onPingPacket(this);
}

public String getResponse() {
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import net.hypixel.data.rank.MonthlyPackageRank;
import net.hypixel.data.rank.PackageRank;
import net.hypixel.data.rank.PlayerRank;
import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -59,11 +56,6 @@ protected int getLatestVersion() {
return CURRENT_VERSION;
}

@Override
public void handle(ClientboundPacketHandler handler) {
handler.onPlayerInfoPacket(this);
}

public PlayerRank getPlayerRank() {
return playerRank;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.hypixel.modapi.packet.impl.clientbound.event;

import net.hypixel.data.type.ServerType;
import net.hypixel.modapi.handler.ClientboundPacketHandler;
import net.hypixel.modapi.packet.EventPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundVersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
Expand Down Expand Up @@ -64,11 +63,6 @@ protected int getLatestVersion() {
return CURRENT_VERSION;
}

@Override
public void handle(ClientboundPacketHandler handler) {
handler.onLocationEvent(this);
}

public String getServerName() {
return serverName;
}
Expand Down