Skip to content

Commit

Permalink
improvement: Add methods to expose clientbound and serverbound identi…
Browse files Browse the repository at this point in the history
…fiers
  • Loading branch information
ConnorLinfoot committed May 5, 2024
1 parent 7159410 commit 9695afc
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/main/java/net/hypixel/modapi/packet/PacketRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

public class PacketRegistry {

Expand Down Expand Up @@ -43,10 +44,6 @@ Collection<Registration> getRegistrations() {
return registrations.values();
}

public boolean isRegistered(String identifier) {
return registrations.containsKey(identifier);
}

public ClientboundHypixelPacket createClientboundPacket(String identifier, PacketSerializer serializer) {
return getRegistration(identifier).clientPacketFactory.apply(serializer);
}
Expand All @@ -55,14 +52,44 @@ public HypixelPacket createServerboundPacket(String identifier, PacketSerializer
return getRegistration(identifier).serverPacketFactory.apply(serializer);
}

/**
* @return whether a packet with the given identifier is registered, either for clientbound or serverbound
*/
public boolean isRegistered(String identifier) {
return registrations.containsKey(identifier);
}

public String getIdentifier(Class<? extends HypixelPacket> clazz) {
return classToIdentifier.get(clazz);
}

/**
* @return all registered packet identifiers, this will include both clientbound and serverbound packets
*/
public Set<String> getIdentifiers() {
return Collections.unmodifiableSet(registrations.keySet());
}

/**
* @return all registered clientbound packet identifiers
*/
public Set<String> getClientboundIdentifiers() {
return Collections.unmodifiableSet(registrations.values().stream()
.filter(registration -> registration.getClientboundClazz() != null)
.map(registration -> classToIdentifier.get(registration.getClientboundClazz()))
.collect(Collectors.toSet()));
}

/**
* @return all registered serverbound packet identifiers
*/
public Set<String> getServerboundIdentifiers() {
return Collections.unmodifiableSet(registrations.values().stream()
.filter(registration -> registration.getServerboundClazz() != null)
.map(registration -> classToIdentifier.get(registration.getServerboundClazz()))
.collect(Collectors.toSet()));
}

public static final class RegistrationBuilder {
private final PacketRegistry registry;
private final String identifier;
Expand Down

0 comments on commit 9695afc

Please sign in to comment.