/**
   * Initial handling of the comment sub-command.
   *
   * @param sender player that sent the command
   * @param args arguments
   * @return true if command handled correctly
   */
  public static boolean handleCommand(CommandSender sender, String[] args) {

    if (!RTSPermissions.canComment(sender)) return true;

    if (args.length < 3) return false;

    if (!RTSFunctions.isNumber(args[1])) {
      sender.sendMessage(Message.errorTicketNaN(args[1]));
      return true;
    }

    int ticketId = Integer.parseInt(args[1]);

    // Ticket has to be open in order for us to comment on it.
    if (!plugin.tickets.containsKey(ticketId)) {
      sender.sendMessage(Message.ticketNotOpen(ticketId));
      return true;
    }

    User user =
        sender instanceof Player
            ? data.getUser(((Player) sender).getUniqueId(), 0, true)
            : data.getConsole();
    if (user.getUsername() == null) {
      sender.sendMessage(
          Message.error(
              "user.getUsername() returned NULL! Are you using plugins to modify names?"));
      return true;
    }

    if (sender instanceof Player
        && plugin.tickets.get(ticketId).getUUID() != ((Player) sender).getUniqueId()
        && !RTSPermissions.isStaff((Player) sender)) {
      sender.sendMessage(Message.errorTicketOwner());
      return true;
    }

    Ticket ticket = plugin.tickets.get(ticketId);
    TreeSet<Comment> comments = ticket.getComments();

    // Clean up arguments before combining the remaining into a comment.
    args[0] = null;
    args[1] = null;

    String comment = RTSFunctions.implode(args, " ").trim();

    String name =
        sender instanceof Player
            ? plugin.staff.contains(user.getUuid())
                ? sender.getName() + " - Staff"
                : sender.getName()
            : sender.getName();

    long timestamp = System.currentTimeMillis() / 1000;

    // Create a comment and store the comment ID.
    int commentId = data.createComment(name, timestamp, comment, ticketId);
    // If less than 1, then the creation of the comment failed.
    if (commentId < 1) {
      sender.sendMessage(Message.error("Comment could not be created."));
      return true;
    }

    sender.sendMessage(Message.ticketCommentUser(Integer.toString(ticketId)));

    // Notify staff members about the new comment.
    try {
      // Attempt to notify all servers connected to BungeeCord that run ReportRTS.
      BungeeCord.globalNotify(
          Message.ticketComment(Integer.toString(ticketId), user.getUsername()),
          ticketId,
          NotificationType.NOTIFYONLY);
    } catch (IOException e) {
      e.printStackTrace();
    }

    RTSFunctions.messageStaff(
        Message.ticketComment(Integer.toString(ticketId), user.getUsername()), true);

    // Add a comment to the comment set.
    comments.add(new Comment(timestamp, ticketId, commentId, sender.getName(), comment));
    // Update the comments on the ticket.
    ticket.setComments(comments);
    plugin.tickets.put(ticketId, ticket);

    plugin
        .getServer()
        .getPluginManager()
        .callEvent(new TicketCommentEvent(plugin.tickets.get(ticketId), sender, comment));

    return true;
  }
Example #2
0
  public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (args.length == 0) return false;
    if (!(sender instanceof Player)) {
      sender.sendMessage("[ReportRTS] Some information will not be correct, such as location.");
      int userId = dbManager.getUserId("CONSOLE", true);
      String message = RTSFunctions.implode(args, " ");
      Location location = plugin.getServer().getWorlds().get(0).getSpawnLocation();
      String world = plugin.getServer().getWorlds().get(0).getName();
      if (!dbManager.fileRequest("CONSOLE", world, location, message, userId)) {
        sender.sendMessage(Message.parse("generalInternalError", "Request could not be filed."));
        return true;
      }
      int ticketId = dbManager.getLatestTicketIdByUser(userId);
      HelpRequest request =
          new HelpRequest(
              "CONSOLE",
              ticketId,
              System.currentTimeMillis() / 1000,
              message,
              0,
              location.getBlockX(),
              location.getBlockY(),
              location.getBlockZ(),
              location.getYaw(),
              location.getPitch(),
              world,
              BungeeCord.getServer(),
              "");
      plugin.getServer().getPluginManager().callEvent(new ReportCreateEvent(request));
      plugin.requestMap.put(ticketId, request);
      if (plugin.notifyStaffOnNewRequest) {
        try {
          BungeeCord.globalNotify(
              Message.parse("modreqFiledMod", "CONSOLE", ticketId), ticketId, NotificationType.NEW);
        } catch (IOException e) {
          e.printStackTrace();
        }
        RTSFunctions.messageMods(Message.parse("modreqFiledMod", "CONSOLE", ticketId), true);
      }
      return true;
    }
    if (!RTSPermissions.canFileRequest(sender)) return true;
    if (plugin.requestMinimumWords > args.length) {
      sender.sendMessage(Message.parse("modreqTooShort", plugin.requestMinimumWords));
      return true;
    }
    if (RTSFunctions.getOpenRequestsByUser(sender) >= plugin.maxRequests
        && !(ReportRTS.permission != null
            ? ReportRTS.permission.has(sender, "reportrts.command.modreq.unlimited")
            : sender.hasPermission("reportrts.command.modreq.unlimited"))) {
      sender.sendMessage(Message.parse("modreqTooManyOpen"));
      return true;
    }
    if (plugin.requestDelay > 0) {
      if (!(ReportRTS.permission != null
          ? ReportRTS.permission.has(sender, "reportrts.command.modreq.unlimited")
          : sender.hasPermission("reportrts.command.modreq.unlimited"))) {
        long timeBetweenRequest = RTSFunctions.checkTimeBetweenRequests(sender);
        if (timeBetweenRequest > 0) {
          sender.sendMessage(Message.parse("modreqTooFast", timeBetweenRequest));
          return true;
        }
      }
    }
    double start = 0;
    if (plugin.debugMode) start = System.nanoTime();

    Player player = (Player) sender;
    String message = RTSFunctions.implode(args, " ");
    int userId = dbManager.getUserId(player.getName(), true);
    if (!dbManager.fileRequest(
        player.getName(), player.getWorld().getName(), player.getLocation(), message, userId)) {
      sender.sendMessage(Message.parse("generalInternalError", "Request could not be filed."));
      return true;
    }
    int ticketId = dbManager.getLatestTicketIdByUser(userId);

    Location location = player.getLocation();

    sender.sendMessage(Message.parse("modreqFiledUser"));
    plugin.getLogger().log(Level.INFO, "" + player.getName() + " filed a request.");
    if (plugin.notifyStaffOnNewRequest) {
      try {
        BungeeCord.globalNotify(
            Message.parse("modreqFiledMod", player.getName(), ticketId),
            ticketId,
            NotificationType.NEW);
      } catch (IOException e) {
        e.printStackTrace();
      }
      RTSFunctions.messageMods(Message.parse("modreqFiledMod", player.getName(), ticketId), true);
    }

    HelpRequest request =
        new HelpRequest(
            player.getName(),
            ticketId,
            System.currentTimeMillis() / 1000,
            message,
            0,
            location.getBlockX(),
            location.getBlockY(),
            location.getBlockZ(),
            location.getYaw(),
            location.getPitch(),
            player.getWorld().getName(),
            BungeeCord.getServer(),
            "");
    plugin.getServer().getPluginManager().callEvent(new ReportCreateEvent(request));
    plugin.requestMap.put(ticketId, request);
    if (plugin.debugMode)
      Message.debug(sender.getName(), this.getClass().getSimpleName(), start, cmd.getName(), args);
    return true;
  }