• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / CyclopsCore / #479033809

11 Oct 2025 08:52AM UTC coverage: 22.269% (-0.002%) from 22.271%
#479033809

push

github

rubensworks
Bump mod version

2328 of 10454 relevant lines covered (22.27%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/cyclopscore/network/PacketHandler.java
1
package org.cyclops.cyclopscore.network;
2

3
import com.google.common.base.Predicates;
4
import io.netty.channel.ChannelHandler.Sharable;
5
import net.minecraft.client.Minecraft;
6
import net.minecraft.client.player.LocalPlayer;
7
import net.minecraft.resources.ResourceKey;
8
import net.minecraft.resources.ResourceLocation;
9
import net.minecraft.server.level.ServerPlayer;
10
import net.minecraft.world.level.Level;
11
import net.minecraftforge.api.distmarker.Dist;
12
import net.minecraftforge.api.distmarker.OnlyIn;
13
import net.minecraftforge.network.NetworkDirection;
14
import net.minecraftforge.network.NetworkEvent;
15
import net.minecraftforge.network.NetworkRegistry;
16
import net.minecraftforge.network.PacketDistributor;
17
import net.minecraftforge.network.simple.SimpleChannel;
18
import org.cyclops.cyclopscore.CyclopsCore;
19
import org.cyclops.cyclopscore.helper.Helpers;
20
import org.cyclops.cyclopscore.helper.Helpers.IDType;
21
import org.cyclops.cyclopscore.init.ModBase;
22

23
import java.lang.reflect.Constructor;
24
import java.lang.reflect.InvocationTargetException;
25

26
/**
27
 * Advanced packet handler of {@link PacketBase} instances.
28
 * An alternative would be {@link SimpleChannel}.
29
 * @author rubensworks
30
 */
31
@Sharable
32
public final class PacketHandler {
33

34
    private final ModBase mod;
35

36
    private SimpleChannel networkChannel = null;
×
37

38
    public PacketHandler(ModBase mod) {
×
39
        this.mod = mod;
×
40
    }
×
41

42
    public void init() {
43
        if(networkChannel == null) {
×
44
            networkChannel = NetworkRegistry.newSimpleChannel(
×
45
                    new ResourceLocation(mod.getModId(), "channel_main"), () -> "1.0.0",
×
46
                    Predicates.alwaysTrue(), Predicates.alwaysTrue());
×
47
        }
48
    }
×
49

50
    /**
51
     * Register a new packet.
52
     * @param packetType The class of the packet.
53
     * @param <P> The packet type.
54
     */
55
    public <P extends PacketBase> void register(Class<P> packetType) {
56
        int discriminator = Helpers.getNewId(mod, IDType.PACKET);
×
57
        try {
58
            Constructor<P> constructor = packetType.getConstructor();
×
59
            networkChannel.registerMessage(discriminator, packetType,
×
60
                    (packet, packetBuffer) -> {
61
                        try {
62
                            packet.encode(packetBuffer);
×
63
                        } catch (Throwable e) {
×
64
                            throw new PacketCodecException("An exception occurred during encoding of packet " + packet.toString(), e);
×
65
                        }
×
66
                    },
×
67
                    (packetBuffer) -> {
68
                        P packet = null;
×
69
                        try {
70
                            packet = constructor.newInstance();
×
71
                        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
×
72
                            e.printStackTrace();
×
73
                        }
×
74
                        try {
75
                            packet.decode(packetBuffer);
×
76
                        } catch (Throwable e) {
×
77
                            throw new PacketCodecException("An exception occurred during decoding of packet " + packet.toString(), e);
×
78
                        }
×
79
                        return packet;
×
80
                    },
81
                    (packet, contextSupplier) -> {
82
                        NetworkEvent.Context context = contextSupplier.get();
×
83
                        if (context.getDirection().getReceptionSide().isClient()) {
×
84
                            if (packet.isAsync()) {
×
85
                                handlePacketClient(context, packet);
×
86
                            } else {
87
                                context.enqueueWork(() -> handlePacketClient(context, packet));
×
88
                            }
89
                        } else {
90
                            if (packet.isAsync()) {
×
91
                                handlePacketServer(context, packet);
×
92
                            } else {
93
                                context.enqueueWork(() -> handlePacketServer(context, packet));
×
94
                            }
95
                        }
96
                        context.setPacketHandled(true);
×
97
                    });
×
98
        } catch (NoSuchMethodException e) {
×
99
            e.printStackTrace();
×
100
            CyclopsCore.clog(org.apache.logging.log4j.Level.ERROR, "Could not find a default constructor for packet " + packetType.getName());
×
101
        }
×
102
    }
×
103

104
    @OnlyIn(Dist.CLIENT)
105
    public void handlePacketClient(NetworkEvent.Context context, PacketBase packet) {
106
        LocalPlayer player = Minecraft.getInstance().player;
×
107
        packet.actionClient(player != null ? player.level() : null, player);
×
108
    }
×
109

110
    public void handlePacketServer(NetworkEvent.Context context, PacketBase packet) {
111
        packet.actionServer(context.getSender().level(), context.getSender());
×
112
    }
×
113

114
    /**
115
     * Send a packet to the server.
116
     * @param packet The packet.
117
     */
118
    public void sendToServer(PacketBase packet) {
119
        networkChannel.sendToServer(packet);
×
120
    }
×
121

122
    /**
123
     * Send a packet to the player.
124
     * @param packet The packet.
125
     * @param player The player.
126
     */
127
    public void sendToPlayer(PacketBase packet, ServerPlayer player) {
128
        networkChannel.sendTo(packet, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT);
×
129
    }
×
130

131
    /**
132
     * Send a packet to all in the target range.
133
     * @param packet The packet.
134
     * @param point The area to send to.
135
     */
136
    public void sendToAllAround(PacketBase packet, PacketDistributor.TargetPoint point) {
137
        PacketDistributor.PacketTarget target = PacketDistributor.NEAR.with(() -> point);
×
138
        target.send(networkChannel.toVanillaPacket(packet, target.getDirection()));
×
139
    }
×
140

141
    /**
142
     * Send a packet to everything in the given dimension.
143
     * @param packet The packet.
144
     * @param dimension The dimension to send to.
145
     */
146
    public void sendToDimension(PacketBase packet, ResourceKey<Level> dimension) {
147
        PacketDistributor.PacketTarget target = PacketDistributor.DIMENSION.with(() -> dimension);
×
148
        target.send(networkChannel.toVanillaPacket(packet, target.getDirection()));
×
149
    }
×
150

151
    /**
152
     * Send a packet to everything.
153
     * @param packet The packet.
154
     */
155
    public void sendToAll(PacketBase packet) {
156
        PacketDistributor.PacketTarget target = PacketDistributor.ALL.with(() -> null);
×
157
        target.send(networkChannel.toVanillaPacket(packet, target.getDirection()));
×
158
    }
×
159

160
    public static class PacketCodecException extends RuntimeException {
161
        public PacketCodecException(String message, Throwable cause) {
162
            super(message, cause);
×
163
        }
×
164
    }
165

166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc