|
11 | 11 | import net.minecraft.network.PacketByteBuf; |
12 | 12 | import net.minecraft.network.codec.PacketCodec; |
13 | 13 | import net.minecraft.network.packet.CustomPayload; |
14 | | -import org.apache.commons.lang3.Validate; |
15 | 14 | import org.slf4j.Logger; |
16 | 15 |
|
17 | 16 | public class FabricModAPI implements ClientModInitializer { |
18 | 17 | private static final Logger LOGGER = LogUtils.getLogger(); |
19 | 18 |
|
20 | 19 | @Override |
21 | 20 | public void onInitializeClient() { |
22 | | - registerPayloads(); |
| 21 | + reloadRegistrations(); |
23 | 22 | registerPacketSender(); |
24 | 23 | } |
25 | 24 |
|
26 | | - private void registerPayloads() { |
27 | | - for (String identifier : HypixelModAPI.getInstance().getRegistry().getIdentifiers()) { |
28 | | - registerClientbound(identifier); |
29 | | - registerServerbound(identifier); |
| 25 | + /** |
| 26 | + * Reloads the identifiers that are registered in the Hypixel Mod API and makes sure that the packets are registered. |
| 27 | + * <p> |
| 28 | + * This method is available for internal use by Hypixel to add new packets externally, and is not intended for use by other developers. |
| 29 | + */ |
| 30 | + public static void reloadRegistrations() { |
| 31 | + for (String identifier : HypixelModAPI.getInstance().getRegistry().getClientboundIdentifiers()) { |
| 32 | + try { |
| 33 | + registerClientbound(identifier); |
| 34 | + } catch (Exception e) { |
| 35 | + LOGGER.error("Failed to register clientbound packet with identifier '{}'", identifier, e); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + for (String identifier : HypixelModAPI.getInstance().getRegistry().getServerboundIdentifiers()) { |
| 40 | + try { |
| 41 | + registerServerbound(identifier); |
| 42 | + } catch (Exception e) { |
| 43 | + LOGGER.error("Failed to register serverbound packet with identifier '{}'", identifier, e); |
| 44 | + } |
30 | 45 | } |
31 | 46 | } |
32 | 47 |
|
33 | | - private void registerPacketSender() { |
| 48 | + private static void registerPacketSender() { |
34 | 49 | HypixelModAPI.getInstance().setPacketSender((packet) -> { |
35 | 50 | ServerboundHypixelPayload payload = new ServerboundHypixelPayload(packet); |
36 | 51 | ClientPlayNetworking.send(payload); |
37 | 52 | }); |
38 | 53 | } |
39 | 54 |
|
40 | | - public void registerClientbound(String identifier) { |
41 | | - Validate.isTrue(HypixelModAPI.getInstance().getRegistry().isRegistered(identifier), "Identifier %s is not registered", identifier); |
| 55 | + private static void registerClientbound(String identifier) { |
| 56 | + try { |
| 57 | + CustomPayload.Id<ClientboundHypixelPayload> clientboundId = CustomPayload.id(identifier); |
| 58 | + PacketCodec<PacketByteBuf, ClientboundHypixelPayload> codec = ClientboundHypixelPayload.buildCodec(clientboundId); |
| 59 | + PayloadTypeRegistry.playS2C().register(clientboundId, codec); |
42 | 60 |
|
43 | | - CustomPayload.Id<ClientboundHypixelPayload> clientboundId = CustomPayload.id(identifier); |
44 | | - PacketCodec<PacketByteBuf, ClientboundHypixelPayload> codec = ClientboundHypixelPayload.buildCodec(clientboundId); |
45 | | - PayloadTypeRegistry.playS2C().register(clientboundId, codec); |
46 | | - |
47 | | - // Also register the global receiver for handling incoming packets |
48 | | - ClientPlayNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> { |
49 | | - if (!payload.isSuccess()) { |
50 | | - LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason()); |
51 | | - return; |
52 | | - } |
| 61 | + // Also register the global receiver for handling incoming packets |
| 62 | + ClientPlayNetworking.registerGlobalReceiver(clientboundId, (payload, context) -> { |
| 63 | + if (!payload.isSuccess()) { |
| 64 | + LOGGER.warn("Received an error response for packet {}: {}", identifier, payload.getErrorReason()); |
| 65 | + return; |
| 66 | + } |
53 | 67 |
|
54 | | - try { |
55 | | - HypixelModAPI.getInstance().handle(payload.getPacket()); |
56 | | - } catch (Exception e) { |
57 | | - LOGGER.error("An error occurred while handling packet {}", identifier, e); |
58 | | - } |
| 68 | + try { |
| 69 | + HypixelModAPI.getInstance().handle(payload.getPacket()); |
| 70 | + } catch (Exception e) { |
| 71 | + LOGGER.error("An error occurred while handling packet {}", identifier, e); |
| 72 | + } |
59 | 73 |
|
60 | | - try { |
61 | | - HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket()); |
62 | | - } catch (Exception e) { |
63 | | - LOGGER.error("An error occurred while handling packet {}", identifier, e); |
64 | | - } |
65 | | - }); |
| 74 | + try { |
| 75 | + HypixelModAPICallback.EVENT.invoker().onPacketReceived(payload.getPacket()); |
| 76 | + } catch (Exception e) { |
| 77 | + LOGGER.error("An error occurred while handling packet {}", identifier, e); |
| 78 | + } |
| 79 | + }); |
| 80 | + } catch (IllegalArgumentException ignored) { |
| 81 | + // Ignored as this is fired when we reload the registrations and the packet is already registered |
| 82 | + } |
66 | 83 | } |
67 | 84 |
|
68 | | - public void registerServerbound(String identifier) { |
69 | | - Validate.isTrue(HypixelModAPI.getInstance().getRegistry().isRegistered(identifier), "Identifier %s is not registered", identifier); |
70 | | - |
71 | | - CustomPayload.Id<ServerboundHypixelPayload> serverboundId = CustomPayload.id(identifier); |
72 | | - PacketCodec<PacketByteBuf, ServerboundHypixelPayload> codec = ServerboundHypixelPayload.buildCodec(serverboundId); |
73 | | - PayloadTypeRegistry.playC2S().register(serverboundId, codec); |
| 85 | + private static void registerServerbound(String identifier) { |
| 86 | + try { |
| 87 | + CustomPayload.Id<ServerboundHypixelPayload> serverboundId = CustomPayload.id(identifier); |
| 88 | + PacketCodec<PacketByteBuf, ServerboundHypixelPayload> codec = ServerboundHypixelPayload.buildCodec(serverboundId); |
| 89 | + PayloadTypeRegistry.playC2S().register(serverboundId, codec); |
| 90 | + } catch (IllegalArgumentException ignored) { |
| 91 | + // Ignored as this is fired when we reload the registrations and the packet is already registered |
| 92 | + } |
74 | 93 | } |
75 | 94 | } |
0 commit comments