Skip to content

Commit

Permalink
new packetRegistry system, removed unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikos3k committed Mar 17, 2021
1 parent 8cbbb2f commit 41c1469
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import me.ANONIMUS.proxy.protocol.objects.Player;
import me.ANONIMUS.proxy.protocol.objects.Session;
import me.ANONIMUS.proxy.protocol.packet.Packet;
import me.ANONIMUS.proxy.protocol.packet.PacketBuffer;
import me.ANONIMUS.proxy.protocol.packet.PacketDirection;
import me.ANONIMUS.proxy.protocol.packet.Protocol;
import me.ANONIMUS.proxy.protocol.packet.impl.CustomPacket;
import me.ANONIMUS.proxy.protocol.packet.impl.client.HandshakePacket;
import me.ANONIMUS.proxy.protocol.packet.impl.client.login.ClientLoginStartPacket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import me.ANONIMUS.proxy.protocol.packet.Protocol;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum ConnectionState {
HANDSHAKE, LOGIN, PLAY, STATUS;

private final Map<Protocol, Packet> clientPackets;
private final Map<Protocol, Packet> serverPackets;
private final Map<Packet, List<Protocol>> clientPackets;
private final Map<Packet, List<Protocol>> serverPackets;

ConnectionState() {
this.clientPackets = new HashMap<>();
this.serverPackets = new HashMap<>();
}

public Map<Protocol, Packet> getPacketsByDirection(PacketDirection direction) {
public Map<Packet, List<Protocol>> getPacketsByDirection(PacketDirection direction) {
switch (direction) {
case SERVERBOUND:
return clientPackets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.UUID;
import java.util.stream.IntStream;

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

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PacketRegistry {
public void init() {
Arrays.asList(PacketDirection.values()).forEach(direction -> Arrays.stream(ConnectionState.values()).filter(connectionState -> connectionState != ConnectionState.HANDSHAKE).forEach(state -> new Reflections("me.ANONIMUS.proxy.protocol.packet.impl." + direction.packetsPackageName.toLowerCase() + "." + state.name().toLowerCase()).getSubTypesOf(Packet.class).forEach(p -> {
try {
final Packet packet = p.newInstance();
packet.getProtocolList().forEach(protocol -> state.getPacketsByDirection(direction).put(protocol, packet));
packet.getProtocolList().forEach(protocol -> {
if(state.getPacketsByDirection(direction).get(packet) == null) {
List<Protocol> protocols = new ArrayList<>();
protocols.add(protocol);
state.getPacketsByDirection(direction).put(packet, protocols);
} else {
state.getPacketsByDirection(direction).get(packet).add(protocol);
}
});
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -45,6 +55,15 @@ private Packet getPacket(ConnectionState connectionState, PacketDirection direct
if(connectionState == ConnectionState.HANDSHAKE) {
return new HandshakePacket();
}
return connectionState.getPacketsByDirection(direction).get(protocol);

for(Packet packet : connectionState.getPacketsByDirection(direction).keySet()) {
for(Protocol protocol1 : connectionState.getPacketsByDirection(direction).get(packet)) {
if(protocol1.equals(protocol)) {
return packet;
}
}
}

return null;
}
}

0 comments on commit 41c1469

Please sign in to comment.