Skip to content

Annotate internal methods as so, remove custom Experimental annotation #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2024
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
38 changes: 21 additions & 17 deletions src/main/java/net/hypixel/modapi/HypixelModAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.hypixel.modapi.packet.impl.serverbound.ServerboundPlayerInfoPacket;
import net.hypixel.modapi.packet.impl.serverbound.ServerboundRegisterPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -74,21 +75,6 @@ private void registerDefaultHandler() {
registerHandler(ClientboundHelloPacket.class, p -> sendRegisterPacket(true));
}

public PacketRegistry getRegistry() {
return registry;
}

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) {
if (subscribedEvents.add(getRegistry().getIdentifier(packet))) {
sendRegisterPacket(false);
}
}

private void sendRegisterPacket(boolean alwaysSendIfNotEmpty) {
if (packetSender == null) {
// Allow registering events before the mod has fully initialized
Expand All @@ -100,12 +86,17 @@ private void sendRegisterPacket(boolean alwaysSendIfNotEmpty) {
}

Set<String> lastSubscribedEvents = new HashSet<>(subscribedEvents);
Map<String, Integer> versionsMap = getRegistry().getEventVersions(lastSubscribedEvents);
if (sendPacket(new ServerboundRegisterPacket(versionsMap))) {
if (sendPacket(new ServerboundRegisterPacket(registry, lastSubscribedEvents))) {
this.lastSubscribedEvents = lastSubscribedEvents;
}
}

@ApiStatus.Internal
public PacketRegistry getRegistry() {
return registry;
}

@ApiStatus.Internal
public void handle(String identifier, PacketSerializer serializer) {
if (handlers.isEmpty()) {
return;
Expand All @@ -130,6 +121,7 @@ public void handle(String identifier, PacketSerializer serializer) {
handle(packet);
}

@ApiStatus.Internal
@SuppressWarnings("unchecked")
public void handle(ClientboundHypixelPacket packet) {
Collection<ClientboundPacketHandler<?>> typedHandlers = handlers.get(packet.getClass());
Expand All @@ -141,13 +133,25 @@ public void handle(ClientboundHypixelPacket packet) {
}
}

@ApiStatus.Internal
public void setPacketSender(Predicate<HypixelPacket> packetSender) {
if (this.packetSender != null) {
throw new IllegalArgumentException("Packet sender already set");
}
this.packetSender = packetSender;
}

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) {
if (subscribedEvents.add(getRegistry().getIdentifier(packet))) {
sendRegisterPacket(false);
}
}

/**
* @return whether the packet was sent successfully
*/
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/net/hypixel/modapi/annotation/Experimental.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/net/hypixel/modapi/error/ModAPIException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.hypixel.modapi.error;

import org.jetbrains.annotations.ApiStatus;

public class ModAPIException extends RuntimeException {
private final String identifier;
private final ErrorReason reason;

@ApiStatus.Internal
public ModAPIException(String identifier, ErrorReason reason) {
super(String.format("Received error response '%s' from packet '%s'", reason, identifier));
this.identifier = identifier;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/hypixel/modapi/packet/HypixelPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import net.hypixel.modapi.HypixelModAPI;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

public interface HypixelPacket {

@ApiStatus.Internal
void write(PacketSerializer serializer);

@ApiStatus.Internal
default String getIdentifier() {
return HypixelModAPI.getInstance().getRegistry().getIdentifier(getClass());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/hypixel/modapi/packet/PacketRegistry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hypixel.modapi.packet;

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -10,6 +11,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

@ApiStatus.Internal
public class PacketRegistry {

private final Map<String, Registration> registrations = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import net.hypixel.modapi.packet.HypixelPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

/**
* Represents a packet that is backed by a version. Versioned packets will only be handled if the incoming packet matches the version of the packet known.
*/
public abstract class VersionedPacket implements HypixelPacket {
protected int version;

@ApiStatus.Internal
public VersionedPacket(int version) {
this.version = version;
}

@ApiStatus.Internal
public VersionedPacket(PacketSerializer serializer) {
read(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.hypixel.data.region.Environment;
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

/**
* This packet is automatically sent on every join to Hypixel to indicate that the client has connected to a Hypixel server.
Expand All @@ -11,10 +12,12 @@
public class ClientboundHelloPacket implements ClientboundHypixelPacket {
private final Environment environment;

@ApiStatus.Internal
public ClientboundHelloPacket(Environment environment) {
this.environment = environment;
}

@ApiStatus.Internal
public ClientboundHelloPacket(PacketSerializer serializer) {
this.environment = Environment.getById(serializer.readVarInt()).orElseThrow(() -> new IllegalArgumentException("Invalid environment ID"));

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

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

import java.util.*;

Expand All @@ -10,6 +11,7 @@ public class ClientboundPartyInfoPacket extends ClientboundVersionedPacket {
private boolean inParty;
private Map<UUID, PartyMember> memberMap;

@ApiStatus.Internal
public ClientboundPartyInfoPacket(int version, boolean inParty, Map<UUID, PartyMember> memberMap) {
super(version);
if (version > CURRENT_VERSION) {
Expand All @@ -20,6 +22,7 @@ public ClientboundPartyInfoPacket(int version, boolean inParty, Map<UUID, PartyM
this.memberMap = memberMap;
}

@ApiStatus.Internal
public ClientboundPartyInfoPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down Expand Up @@ -117,11 +120,13 @@ public static class PartyMember {
private final UUID uuid;
private final PartyRole role;

@ApiStatus.Internal
public PartyMember(UUID uuid, PartyRole role) {
this.uuid = uuid;
this.role = role;
}

@ApiStatus.Internal
PartyMember(PacketSerializer serializer) {
this.uuid = serializer.readUuid();
this.role = PartyRole.VALUES[serializer.readVarInt()];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package net.hypixel.modapi.packet.impl.clientbound;

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

public class ClientboundPingPacket extends ClientboundVersionedPacket {
private static final int CURRENT_VERSION = 1;

private String response;

@ApiStatus.Internal
public ClientboundPingPacket(String response) {
super(CURRENT_VERSION);
this.response = response;
}

@ApiStatus.Internal
public ClientboundPingPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.hypixel.data.rank.PackageRank;
import net.hypixel.data.rank.PlayerRank;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand All @@ -17,6 +18,7 @@ public class ClientboundPlayerInfoPacket extends ClientboundVersionedPacket {
@Nullable
private String prefix;

@ApiStatus.Internal
public ClientboundPlayerInfoPacket(PlayerRank playerRank, PackageRank packageRank, MonthlyPackageRank monthlyPackageRank, @Nullable String prefix) {
super(CURRENT_VERSION);
this.playerRank = playerRank;
Expand All @@ -25,6 +27,7 @@ public ClientboundPlayerInfoPacket(PlayerRank playerRank, PackageRank packageRan
this.prefix = prefix;
}

@ApiStatus.Internal
public ClientboundPlayerInfoPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import net.hypixel.modapi.packet.ClientboundHypixelPacket;
import net.hypixel.modapi.packet.impl.VersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

/**
* Represents a packet that is backed by a version. Clientbound versioned packets will only be handled if the incoming packet matches the version of the packet known.
*/
public abstract class ClientboundVersionedPacket extends VersionedPacket implements ClientboundHypixelPacket {

@ApiStatus.Internal
public ClientboundVersionedPacket(int version) {
super(version);
}

@ApiStatus.Internal
public ClientboundVersionedPacket(PacketSerializer serializer) {
super(serializer);
}
Expand All @@ -32,6 +35,7 @@ public void write(PacketSerializer serializer) {

protected abstract int getLatestVersion();

@ApiStatus.Internal
public boolean isExpectedVersion() {
return getVersion() == getLatestVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.hypixel.modapi.packet.EventPacket;
import net.hypixel.modapi.packet.impl.clientbound.ClientboundVersionedPacket;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand All @@ -21,6 +22,7 @@ public class ClientboundLocationPacket extends ClientboundVersionedPacket implem
@Nullable
private String map;

@ApiStatus.Internal
public ClientboundLocationPacket(String serverName, @Nullable ServerType serverType, @Nullable String lobbyName, @Nullable String mode, @Nullable String map) {
super(CURRENT_VERSION);
this.serverName = serverName;
Expand All @@ -30,6 +32,7 @@ public ClientboundLocationPacket(String serverName, @Nullable ServerType serverT
this.map = map;
}

@ApiStatus.Internal
public ClientboundLocationPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

public class ServerboundPartyInfoPacket extends ServerboundVersionedPacket {
private static final int CURRENT_VERSION = 2;
Expand All @@ -9,6 +10,7 @@ public ServerboundPartyInfoPacket() {
super(CURRENT_VERSION);
}

@ApiStatus.Internal
public ServerboundPartyInfoPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

public class ServerboundPingPacket extends ServerboundVersionedPacket {
private static final int CURRENT_VERSION = 1;
Expand All @@ -9,6 +10,7 @@ public ServerboundPingPacket() {
super(CURRENT_VERSION);
}

@ApiStatus.Internal
public ServerboundPingPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

public class ServerboundPlayerInfoPacket extends ServerboundVersionedPacket {
private static final int CURRENT_VERSION = 1;
Expand All @@ -9,6 +10,7 @@ public ServerboundPlayerInfoPacket() {
super(CURRENT_VERSION);
}

@ApiStatus.Internal
public ServerboundPlayerInfoPacket(PacketSerializer serializer) {
super(serializer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package net.hypixel.modapi.packet.impl.serverbound;

import net.hypixel.modapi.packet.PacketRegistry;
import net.hypixel.modapi.serializer.PacketSerializer;
import org.jetbrains.annotations.ApiStatus;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Notifys the remote server what versions of event packets we want to receive.
* <p>
* You should not use this packet manually, instead, use {@link net.hypixel.modapi.HypixelModAPI#subscribeToEventPacket(Class)} to subscribe to event packets.
*/
@ApiStatus.Internal
public class ServerboundRegisterPacket extends ServerboundVersionedPacket {
private static final int MAX_IDENTIFIER_LENGTH = 20;
private static final int MAX_IDENTIFIERS = 5;
private static final int CURRENT_VERSION = 1;

private Map<String, Integer> subscribedEvents;

public ServerboundRegisterPacket(Map<String, Integer> subscribedEvents) {
public ServerboundRegisterPacket(PacketRegistry registry, Set<String> subscribedEventIdentifiers) {
super(CURRENT_VERSION);
this.subscribedEvents = subscribedEvents;
this.subscribedEvents = registry.getEventVersions(subscribedEventIdentifiers);

if (subscribedEvents.size() > MAX_IDENTIFIERS) {
throw new IllegalArgumentException("wantedPackets cannot contain more than " + MAX_IDENTIFIERS + " identifiers");
Expand Down
Loading