public static void registerPacket(Class packet) { if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(packet)) { Logger.warn("This packet does not implement the Message Interface - " + packet.getName()); return; } // Create the Packet once try { net.cubespace.ComuCator.API.Message.Message message = (net.cubespace.ComuCator.API.Message.Message) packet.newInstance(); // Find the Channel to which this Packet belongs String channel; if (packet.isAnnotationPresent(Channel.class)) { channel = ((Channel) packet.getAnnotation(Channel.class)).value(); } else { channel = message.getChannel(); } // If there is no channel for this Packet => Outta here if (channel == null) { Logger.warn( "The packet has no Channel annotation and did gave null back on getChannel() - " + packet.getName()); return; } Long key = StringCode.getStringCode(channel); Long classKey = StringCode.getStringCode(packet.getName()); synchronized (packets) { // Check if Channel exists if (packets.containsKey(key)) { packets.get(key).put(classKey, packet); } else { LinkedHashMap<Long, Class> classes = new LinkedHashMap<>(); classes.put(classKey, packet); packets.put(key, classes); ChannelKeyCache.addToCache(channel); } ClassKeyCache.addToCache(packet); } } catch (Exception e) { Logger.warn( "The packet could not be build. Make sure it has a default Constructor - " + packet.getName()); } }
public static void removePacket(Class packet) { if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(packet)) { Logger.warn("This packet does not implement the Message Interface - " + packet.getName()); return; } // Create the Packet once try { net.cubespace.ComuCator.API.Message.Message message = (net.cubespace.ComuCator.API.Message.Message) packet.newInstance(); // Find the Channel to which this Packet belongs String channel; if (packet.isAnnotationPresent(Channel.class)) { channel = ((Channel) packet.getAnnotation(Channel.class)).value(); } else { channel = message.getChannel(); } // If there is no channel for this Packet => Outta here if (channel == null) { Logger.warn( "The packet has no Channel annotation and did gave null back on getChannel() - " + packet.getName()); return; } Long key = StringCode.getStringCode(channel); Long classKey = StringCode.getStringCode(packet.getName()); synchronized (packets) { // Check if Channel exists if (packets.containsKey(key)) { packets.get(key).remove(classKey); ClassKeyCache.removeFromCache(packet); } if (packets.get(key).size() == 0) { ChannelKeyCache.removeFromCache(channel); } } synchronized (listeners) { if (listeners.containsKey(key)) { listeners.get(key).remove(classKey); PacketRegister packetRegister = new PacketRegister(); packetRegister.setMode((byte) 1); packetRegister.setChannel(key); packetRegister.setPacket(classKey); P2PServers.broadCastToAll(packetRegister); P2PServers.removePacket(key, classKey); } } } catch (Exception e) { Logger.warn( "The packet could not be build. Make sure it has a default Constructor - " + packet.getName()); } }