-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added option to register packets to multiple groups with different IDs
- Loading branch information
Showing
15 changed files
with
217 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
paklet-api/src/main/java/org/machinemc/paklet/PacketRegistrationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.machinemc.paklet; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Allows to access the additional information during dynamic packet registration | ||
* for packets with {@link Packet#DYNAMIC_PACKET} ID. | ||
*/ | ||
public class PacketRegistrationContext { | ||
|
||
protected static final ThreadLocal<PacketRegistrationContext> threadLocal = ThreadLocal.withInitial(PacketRegistrationContext::new); | ||
|
||
private final String group; | ||
|
||
/** | ||
* Returns the current packet registration context. | ||
* <p> | ||
* This can be called only within methods annotated with {@link PacketID}, resolving | ||
* packet IDs for packets with {@link Packet#DYNAMIC_PACKET} ID. | ||
* | ||
* @return current packet registration context | ||
*/ | ||
public static PacketRegistrationContext get() { | ||
PacketRegistrationContext context = threadLocal.get(); | ||
if (context.group == null) throw new RuntimeException("Called outside of dynamic packet registration context"); | ||
return context; | ||
} | ||
|
||
private PacketRegistrationContext() { | ||
group = null; | ||
} | ||
|
||
/** | ||
* Creates new packet registration context with given group. | ||
* | ||
* @param group group | ||
*/ | ||
protected PacketRegistrationContext(String group) { | ||
this.group = Objects.requireNonNull(group, "Packet group can not be null"); | ||
} | ||
|
||
/** | ||
* Returns packet group used to register the packet. | ||
* | ||
* @return current packet group | ||
*/ | ||
public String getPacketGroup() { | ||
return group; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
paklet-core/src/test/java/org/machinemc/paklet/test/DynamicPacketTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.machinemc.paklet.test; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import org.junit.jupiter.api.Test; | ||
import org.machinemc.paklet.DataVisitor; | ||
import org.machinemc.paklet.PacketFactory; | ||
import org.machinemc.paklet.netty.NettyDataVisitor; | ||
import org.machinemc.paklet.serialization.VarIntSerializer; | ||
import org.machinemc.paklet.test.packet.DynamicPacket; | ||
|
||
public class DynamicPacketTest { | ||
|
||
@Test | ||
public void testDynamicPacketIDs() { | ||
PacketFactory factory = TestUtil.createFactory(); | ||
|
||
assert factory.getPacketID(DynamicPacket.class, "one") == 21; | ||
assert factory.getPacketID(DynamicPacket.class, "two") == 22; | ||
assert factory.getPacketID(DynamicPacket.class, "three") == 23; | ||
|
||
DataVisitor visitor = new NettyDataVisitor(Unpooled.buffer()); | ||
|
||
DynamicPacket packet = new DynamicPacket(); | ||
packet.value = 15; | ||
|
||
factory.write(packet, "two", visitor); | ||
|
||
assert visitor.read(null, new VarIntSerializer()) == 22; | ||
|
||
visitor.readerIndex(0); | ||
|
||
DynamicPacket packetClone = factory.create("two", visitor); | ||
|
||
assert packetClone.value == packet.value; | ||
} | ||
|
||
} |
Oops, something went wrong.