Example #1
0
  public static void removeListener(PacketListener listener) {
    // Find the Channel
    String channel;
    if (listener.getClass().isAnnotationPresent(Channel.class)) {
      channel = listener.getClass().getAnnotation(Channel.class).value();
    } else {
      channel = listener.getChannel();
    }

    // If there is no channel for this Packet => Outta here
    if (channel == null) {
      Logger.warn(
          "The listener has no Channel annotation and did gave null back on getChannel() - "
              + listener.getClass().getName());
      return;
    }

    Long key = StringCode.getStringCode(channel);

    for (Method method : listener.getClass().getMethods()) {
      if (method.isAnnotationPresent(PacketHandler.class)) {
        Class argument = method.getParameterTypes()[0];

        if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(argument)) {
          continue;
        }

        Long argumentKey = StringCode.getStringCode(argument.getName());

        synchronized (listeners) {
          // Check if Channel exists
          if (listeners.containsKey(key) && listeners.get(key).containsKey(argumentKey)) {
            Iterator<ListenerMethod> iterator = listeners.get(key).get(argumentKey).iterator();

            while (iterator.hasNext()) {
              ListenerMethod method1 = iterator.next();

              if (method1.getPacketListener().equals(listener)) {
                iterator.remove();
              }
            }

            if (listeners.get(key).get(argumentKey).size() == 0) {
              PacketRegister packetRegister = new PacketRegister();
              packetRegister.setMode((byte) 1);
              packetRegister.setChannel(key);
              packetRegister.setPacket(argumentKey);

              P2PServers.broadCastToAll(packetRegister);
              P2PServers.removePacket(key, argumentKey);

              listeners.get(key).remove(argumentKey);
            }
          }
        }
      }
    }
  }
Example #2
0
  public static void registerListener(PacketListener listener) {
    // Find the Channel
    String channel;
    if (listener.getClass().isAnnotationPresent(Channel.class)) {
      channel = listener.getClass().getAnnotation(Channel.class).value();
    } else {
      channel = listener.getChannel();
    }

    // If there is no channel for this Packet => Outta here
    if (channel == null) {
      Logger.warn(
          "The listener has no Channel annotation and did gave null back on getChannel() - "
              + listener.getClass().getName());
      return;
    }

    Long key = StringCode.getStringCode(channel);

    for (Method method : listener.getClass().getMethods()) {
      if (method.isAnnotationPresent(PacketHandler.class)) {
        Class argument = method.getParameterTypes()[0];

        if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(argument)) {
          continue;
        }

        ListenerMethod listenerMethod = new ListenerMethod(method, listener);

        Long argumentKey = StringCode.getStringCode(argument.getName());

        synchronized (packets) {
          if (!packets.containsKey(key) || !packets.get(key).containsKey(argumentKey)) {
            continue;
          }
        }

        synchronized (listeners) {
          // Check if Channel exists
          if (listeners.containsKey(key)) {
            if (listeners.get(key).containsKey(argumentKey)) {
              listeners.get(key).get(argumentKey).add(listenerMethod);
            } else {
              ArrayList<ListenerMethod> listeners1 = new ArrayList<>();
              listeners1.add(listenerMethod);

              listeners.get(key).put(argumentKey, listeners1);

              PacketRegister packetRegister = new PacketRegister();
              packetRegister.setMode((byte) 0);
              packetRegister.setChannel(key);
              packetRegister.setPacket(argumentKey);

              P2PServers.broadCastToAll(packetRegister);
              P2PServers.registerPacket(key, argumentKey);
            }
          } else {
            ArrayList<ListenerMethod> listeners1 = new ArrayList<>();
            listeners1.add(listenerMethod);

            LinkedHashMap<Long, ArrayList<ListenerMethod>> newHashMap = new LinkedHashMap<>();
            newHashMap.put(argumentKey, listeners1);

            listeners.put(key, newHashMap);

            PacketRegister packetRegister = new PacketRegister();
            packetRegister.setMode((byte) 0);
            packetRegister.setChannel(key);
            packetRegister.setPacket(argumentKey);

            P2PServers.broadCastToAll(packetRegister);
            P2PServers.registerPacket(key, argumentKey);
          }
        }
      }
    }
  }