Skip to content

Commit

Permalink
Log exceptions that occur whilst handling packet responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Dec 30, 2022
1 parent 7cf099d commit 24bcf1e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
32 changes: 25 additions & 7 deletions src/main/java/com/sollace/fabwork/impl/ReceiverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import java.util.function.BiPredicate;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.sollace.fabwork.api.packets.Packet;
import com.sollace.fabwork.api.packets.Receiver;
import com.sollace.fabwork.api.packets.*;

import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;

public class ReceiverImpl<P extends PlayerEntity, T extends Packet<P>> implements Receiver<P, T> {
private final ListenerList persistentListeners = new ListenerList();
private final ListenerList listeners = new ListenerList();
private static final Logger LOGGER = LogManager.getLogger();

public static <P extends PlayerEntity, T extends Packet<P>> Receiver<P, T> empty(Identifier id) {
return new Receiver<>() {
Expand All @@ -31,13 +31,24 @@ public void addTemporaryListener(BiPredicate<P, T> callback) {
};
}

private final ListenerList persistentListeners = new ListenerList();
private final ListenerList listeners = new ListenerList();

private final Identifier id;

public ReceiverImpl(Identifier id) {
this.id = id;
}

@Override
public void addPersistentListener(BiConsumer<P, T> callback) {
persistentListeners.enqueue((p, t) -> {
try {
callback.accept(p, t);
return false;
} catch (Exception e) {}
} catch (Exception e) {
LOGGER.error("Exception whilst handling packet callback for packet " + id, e);
}
return true;
});
}
Expand All @@ -47,16 +58,23 @@ public void addTemporaryListener(BiPredicate<P, T> callback) {
listeners.enqueue((p, t) -> {
try {
return callback.test(p, t);
} catch (Exception e) {}
} catch (Exception e) {
LOGGER.error("Exception whilst handling packet callback for packet " + id, e);
}
return true;
});
}

@SuppressWarnings("unchecked")
public void onReceive(P sender, T packet) {
persistentListeners.fire(sender, packet);
listeners.fire(sender, packet);
if (packet instanceof HandledPacket) {
((HandledPacket<P>)packet).handle(sender);
try {
((HandledPacket<P>)packet).handle(sender);
} catch (Exception e) {
LOGGER.error("Exception whilst handling packet callback for handled packet " + id, e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class ClientSimpleNetworkingImpl {
public static <T extends Packet<PlayerEntity>> S2CPacketType<T> register(Identifier id, Function<PacketByteBuf, T> factory) {
ReceiverImpl<PlayerEntity, T> receiver = new ReceiverImpl<>();
ReceiverImpl<PlayerEntity, T> receiver = new ReceiverImpl<>(id);
S2CPacketType<T> type = new S2CPacketType<>(id, factory, receiver);
ClientPlayNetworking.registerGlobalReceiver(type.id(), (client, handler, buffer, responder) -> {
T packet = type.constructor().apply(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public class ServerSimpleNetworkingImpl {
public static <T extends Packet<ServerPlayerEntity>> C2SPacketType<T> register(Identifier id, Function<PacketByteBuf, T> factory) {
ReceiverImpl<ServerPlayerEntity, T> receiver = new ReceiverImpl<>();
ReceiverImpl<ServerPlayerEntity, T> receiver = new ReceiverImpl<>(id);
C2SPacketType<T> type = new C2SPacketType<>(id, factory, receiver);
ServerPlayNetworking.registerGlobalReceiver(type.id(), (server, player, handler, buffer, responder) -> {
T packet = type.constructor().apply(buffer);
Expand Down

0 comments on commit 24bcf1e

Please sign in to comment.