Skip to content

Commit d5479db

Browse files
committed
fix sending packets with direction BOTH
1 parent 3088e29 commit d5479db

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

api/src/main/java/net/labymod/serverapi/api/Protocol.java

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,13 @@ private void sendPacketInternal(
358358
+ this.identifier + "found");
359359
}
360360

361-
if (this.protocolSide.isAcceptingDirection(protocolPacket.direction)) {
361+
Direction direction = protocolPacket.direction;
362+
if (direction != Direction.BOTH && this.protocolSide.isAcceptingDirection(direction)) {
363+
this.protocolService.logger().warn(
364+
"Attempted to send packet " + packet.getClass().getSimpleName()
365+
+ " in the wrong direction (packet direction: " + direction + ", protocol side: "
366+
+ this.protocolSide + ")"
367+
);
362368
return;
363369
}
364370

@@ -383,6 +389,61 @@ private void sendPacketInternal(
383389
this.protocolService.afterPacketSent(this, packet, recipient);
384390
}
385391

392+
private static class ProtocolPacket {
393+
394+
private static final UnsafeProvider UNSAFE_PROVIDER = new UnsafeProvider();
395+
396+
private final int id;
397+
private final Class<? extends Packet> packet;
398+
private final Set<PacketHandler> handlers;
399+
private final Direction direction;
400+
401+
private boolean fromConstructor = true;
402+
private Constructor<? extends Packet> constructor;
403+
404+
private ProtocolPacket(int id, Class<? extends Packet> packet, Direction direction) {
405+
this.id = id;
406+
this.packet = packet;
407+
this.direction = direction;
408+
this.handlers = new HashSet<>();
409+
}
410+
411+
private Packet createPacket() throws Exception {
412+
if (this.constructor == null && this.fromConstructor) {
413+
try {
414+
this.constructor = this.packet.getDeclaredConstructor();
415+
this.constructor.setAccessible(true);
416+
this.fromConstructor = !this.constructor.isAnnotationPresent(
417+
OnlyWriteConstructor.class
418+
);
419+
} catch (Exception e) {
420+
this.fromConstructor = false;
421+
}
422+
}
423+
424+
if (this.fromConstructor) {
425+
return this.constructor.newInstance();
426+
}
427+
428+
return (Packet) UNSAFE_PROVIDER.unsafe.allocateInstance(this.packet);
429+
}
430+
}
431+
432+
private static final class UnsafeProvider {
433+
434+
private final Unsafe unsafe;
435+
436+
private UnsafeProvider() {
437+
try {
438+
Field field = Unsafe.class.getDeclaredField("theUnsafe");
439+
field.setAccessible(true);
440+
this.unsafe = (Unsafe) field.get(null);
441+
} catch (Exception exception) {
442+
throw new IllegalStateException("Failed to get unsafe instance", exception);
443+
}
444+
}
445+
}
446+
386447
private final class AwaitingResponse {
387448

388449
private final UUID recipient;
@@ -457,59 +518,4 @@ public String toString() {
457518
"responseCallback=" + this.responseCallback + ']';
458519
}
459520
}
460-
461-
private static class ProtocolPacket {
462-
463-
private static final UnsafeProvider UNSAFE_PROVIDER = new UnsafeProvider();
464-
465-
private final int id;
466-
private final Class<? extends Packet> packet;
467-
private final Set<PacketHandler> handlers;
468-
private final Direction direction;
469-
470-
private boolean fromConstructor = true;
471-
private Constructor<? extends Packet> constructor;
472-
473-
private ProtocolPacket(int id, Class<? extends Packet> packet, Direction direction) {
474-
this.id = id;
475-
this.packet = packet;
476-
this.direction = direction;
477-
this.handlers = new HashSet<>();
478-
}
479-
480-
private Packet createPacket() throws Exception {
481-
if (this.constructor == null && this.fromConstructor) {
482-
try {
483-
this.constructor = this.packet.getDeclaredConstructor();
484-
this.constructor.setAccessible(true);
485-
this.fromConstructor = !this.constructor.isAnnotationPresent(
486-
OnlyWriteConstructor.class
487-
);
488-
} catch (Exception e) {
489-
this.fromConstructor = false;
490-
}
491-
}
492-
493-
if (this.fromConstructor) {
494-
return this.constructor.newInstance();
495-
}
496-
497-
return (Packet) UNSAFE_PROVIDER.unsafe.allocateInstance(this.packet);
498-
}
499-
}
500-
501-
private static final class UnsafeProvider {
502-
503-
private final Unsafe unsafe;
504-
505-
private UnsafeProvider() {
506-
try {
507-
Field field = Unsafe.class.getDeclaredField("theUnsafe");
508-
field.setAccessible(true);
509-
this.unsafe = (Unsafe) field.get(null);
510-
} catch (Exception exception) {
511-
throw new IllegalStateException("Failed to get unsafe instance", exception);
512-
}
513-
}
514-
}
515521
}

0 commit comments

Comments
 (0)