Esempio n. 1
0
 /**
  * If rating categories are enabled, get the average rating by category.<br>
  * - The index corresponds to the index of the category in the config
  *
  * @param plot
  * @return
  */
 public static double[] getAverageRatings(Plot plot) {
   HashMap<UUID, Integer> rating;
   if (plot.getSettings().ratings != null) {
     rating = plot.getSettings().ratings;
   } else if (Settings.Enabled_Components.RATING_CACHE) {
     rating = new HashMap<>();
   } else {
     rating = DBFunc.getRatings(plot);
   }
   int size = 1;
   if (!Settings.Ratings.CATEGORIES.isEmpty()) {
     size = Math.max(1, Settings.Ratings.CATEGORIES.size());
   }
   double[] ratings = new double[size];
   if (rating == null || rating.isEmpty()) {
     return ratings;
   }
   for (Map.Entry<UUID, Integer> entry : rating.entrySet()) {
     int current = entry.getValue();
     if (Settings.Ratings.CATEGORIES.isEmpty()) {
       ratings[0] += current;
     } else {
       for (int i = 0; i < Settings.Ratings.CATEGORIES.size(); i++) {
         ratings[i] += current % 10 - 1;
         current /= 10;
       }
     }
   }
   for (int i = 0; i < size; i++) {
     ratings[i] /= rating.size();
   }
   return ratings;
 }
Esempio n. 2
0
  /**
   * Format a string with plot information.
   *
   * @param info
   * @param plot
   * @param player
   * @param full
   * @param whenDone
   */
  public static void format(
      String info, Plot plot, PlotPlayer player, boolean full, RunnableVal<String> whenDone) {
    int num = plot.getConnectedPlots().size();
    String alias = !plot.getAlias().isEmpty() ? plot.getAlias() : C.NONE.s();
    Location bot = plot.getCorners()[0];
    String biome = WorldUtil.IMP.getBiome(plot.getArea().worldname, bot.getX(), bot.getZ());
    String trusted = getPlayerList(plot.getTrusted());
    String members = getPlayerList(plot.getMembers());
    String denied = getPlayerList(plot.getDenied());
    String seen;
    if (Settings.Enabled_Components.PLOT_EXPIRY) {
      if (plot.isOnline()) {
        seen = C.NOW.s();
      } else {
        int time = (int) (ExpireManager.IMP.getAge(plot) / 1000);
        seen = time != 0 ? secToTime(time) : C.UNKNOWN.s();
      }
    } else {
      seen = C.NEVER.s();
    }
    Optional<String> descriptionFlag = plot.getFlag(Flags.DESCRIPTION);
    String description =
        descriptionFlag.isPresent()
            ? Flags.DESCRIPTION.valueToString(descriptionFlag.get())
            : C.NONE.s();

    StringBuilder flags = new StringBuilder();
    HashMap<Flag<?>, Object> flagMap =
        FlagManager.getPlotFlags(plot.getArea(), plot.getSettings(), true);
    if (flagMap.isEmpty()) {
      flags.append(C.NONE.s());
    } else {
      String prefix = "";
      for (Map.Entry<Flag<?>, Object> entry : flagMap.entrySet()) {
        flags.append(prefix).append(C.PLOT_FLAG_LIST.f(entry.getKey().getName(), entry.getValue()));
        prefix = ", ";
      }
    }
    boolean build = plot.isAdded(player.getUUID());
    String owner = plot.getOwners().isEmpty() ? "unowned" : getPlayerList(plot.getOwners());
    info = info.replace("%id%", plot.getId().toString());
    info = info.replace("%alias%", alias);
    info = info.replace("%num%", String.valueOf(num));
    info = info.replace("%desc%", description);
    info = info.replace("%biome%", biome);
    info = info.replace("%owner%", owner);
    info = info.replace("%members%", members);
    info = info.replace("%player%", player.getName());
    info = info.replace("%trusted%", trusted);
    info = info.replace("%helpers%", members);
    info = info.replace("%denied%", denied);
    info = info.replace("%seen%", seen);
    info = info.replace("%flags%", flags);
    info = info.replace("%build%", String.valueOf(build));
    info = info.replace("%desc%", "No description set.");
    if (info.contains("%rating%")) {
      String newInfo = info;
      TaskManager.runTaskAsync(
          () -> {
            int max = 10;
            if (Settings.Ratings.CATEGORIES != null && !Settings.Ratings.CATEGORIES.isEmpty()) {
              max = 8;
            }
            String info1;
            if (full
                && Settings.Ratings.CATEGORIES != null
                && Settings.Ratings.CATEGORIES.size() > 1) {
              double[] ratings = getAverageRatings(plot);
              String rating = "";
              String prefix = "";
              for (int i = 0; i < ratings.length; i++) {
                rating +=
                    prefix
                        + Settings.Ratings.CATEGORIES.get(i)
                        + '='
                        + String.format("%.1f", ratings[i]);
                prefix = ",";
              }
              info1 = newInfo.replaceAll("%rating%", rating);
            } else {
              info1 =
                  newInfo.replaceAll(
                      "%rating%", String.format("%.1f", plot.getAverageRating()) + '/' + max);
            }
            whenDone.run(info1);
          });
      return;
    }
    whenDone.run(info);
  }
Esempio n. 3
0
 @Override
 public boolean onCommand(PlotPlayer player, String... args) {
   if (args.length == 1) {
     if ("next".equalsIgnoreCase(args[0])) {
       ArrayList<Plot> plots = new ArrayList<>(PS.get().getBasePlots());
       Collections.sort(
           plots,
           (p1, p2) -> {
             double v1 = 0;
             if (!p1.getRatings().isEmpty()) {
               for (Map.Entry<UUID, Rating> entry : p1.getRatings().entrySet()) {
                 v1 -= 11 - entry.getValue().getAverageRating();
               }
             }
             double v2 = 0;
             if (!p2.getRatings().isEmpty()) {
               for (Map.Entry<UUID, Rating> entry : p2.getRatings().entrySet()) {
                 v2 -= 11 - entry.getValue().getAverageRating();
               }
             }
             if (v1 == v2) {
               return -0;
             }
             return v2 > v1 ? 1 : -1;
           });
       UUID uuid = player.getUUID();
       for (Plot p : plots) {
         if ((!Settings.Done.REQUIRED_FOR_RATINGS || p.hasFlag(Flags.DONE))
             && p.isBasePlot()
             && (p.hasRatings() || !p.getRatings().containsKey(uuid))
             && !p.isAdded(uuid)) {
           p.teleportPlayer(player);
           MainUtil.sendMessage(player, C.RATE_THIS);
           return true;
         }
       }
       MainUtil.sendMessage(player, C.FOUND_NO_PLOTS);
       return false;
     }
   }
   Plot plot = player.getCurrentPlot();
   if (plot == null) {
     return !this.sendMessage(player, C.NOT_IN_PLOT);
   }
   if (!plot.hasOwner()) {
     this.sendMessage(player, C.RATING_NOT_OWNED);
     return false;
   }
   if (plot.isOwner(player.getUUID())) {
     this.sendMessage(player, C.RATING_NOT_YOUR_OWN);
     return false;
   }
   if (Settings.Done.REQUIRED_FOR_RATINGS && !plot.hasFlag(Flags.DONE)) {
     this.sendMessage(player, C.RATING_NOT_DONE);
     return false;
   }
   if (Settings.Ratings.CATEGORIES != null && !Settings.Ratings.CATEGORIES.isEmpty()) {
     Runnable run =
         new Runnable() {
           @Override
           public void run() {
             if (plot.getRatings().containsKey(player.getUUID())) {
               Rate.this.sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
               return;
             }
             MutableInt index = new MutableInt(0);
             MutableInt rating = new MutableInt(0);
             String title = Settings.Ratings.CATEGORIES.get(0);
             PlotInventory inventory =
                 new PlotInventory(player, 1, title) {
                   @Override
                   public boolean onClick(int i) {
                     rating.add((i + 1) * Math.pow(10, index.getValue()));
                     index.increment();
                     if (index.getValue() >= Settings.Ratings.CATEGORIES.size()) {
                       int rV = rating.getValue();
                       Rating result =
                           EventUtil.manager.callRating(this.player, plot, new Rating(rV));
                       plot.addRating(this.player.getUUID(), result);
                       Rate.this.sendMessage(
                           this.player, C.RATING_APPLIED, plot.getId().toString());
                       if (Permissions.hasPermission(this.player, "plots.comment")) {
                         Command command = MainCommand.getInstance().getCommand(Comment.class);
                         if (command != null) {
                           MainUtil.sendMessage(this.player, C.COMMENT_THIS, command.getUsage());
                         }
                       }
                       return false;
                     }
                     this.setTitle(Settings.Ratings.CATEGORIES.get(index.getValue()));
                     return true;
                   }
                 };
             inventory.setItem(0, new PlotItemStack(35, (short) 12, 0, "0/8"));
             inventory.setItem(1, new PlotItemStack(35, (short) 14, 1, "1/8"));
             inventory.setItem(2, new PlotItemStack(35, (short) 1, 2, "2/8"));
             inventory.setItem(3, new PlotItemStack(35, (short) 4, 3, "3/8"));
             inventory.setItem(4, new PlotItemStack(35, (short) 5, 4, "4/8"));
             inventory.setItem(5, new PlotItemStack(35, (short) 9, 5, "5/8"));
             inventory.setItem(6, new PlotItemStack(35, (short) 11, 6, "6/8"));
             inventory.setItem(7, new PlotItemStack(35, (short) 10, 7, "7/8"));
             inventory.setItem(8, new PlotItemStack(35, (short) 2, 8, "8/8"));
             inventory.openInventory();
           }
         };
     if (plot.getSettings().ratings == null) {
       if (!Settings.Enabled_Components.RATING_CACHE) {
         TaskManager.runTaskAsync(
             () -> {
               plot.getSettings().ratings = DBFunc.getRatings(plot);
               run.run();
             });
         return true;
       }
       plot.getSettings().ratings = new HashMap<>();
     }
     run.run();
     return true;
   }
   if (args.length < 1) {
     this.sendMessage(player, C.RATING_NOT_VALID);
     return true;
   }
   String arg = args[0];
   int rating;
   if (MathMan.isInteger(arg) && arg.length() < 3 && !arg.isEmpty()) {
     rating = Integer.parseInt(arg);
     if (rating > 10 || rating < 1) {
       this.sendMessage(player, C.RATING_NOT_VALID);
       return false;
     }
   } else {
     this.sendMessage(player, C.RATING_NOT_VALID);
     return false;
   }
   UUID uuid = player.getUUID();
   Runnable run =
       () -> {
         if (plot.getRatings().containsKey(uuid)) {
           this.sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
           return;
         }
         Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating));
         plot.addRating(uuid, result);
         this.sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
       };
   if (plot.getSettings().ratings == null) {
     if (!Settings.Enabled_Components.RATING_CACHE) {
       TaskManager.runTaskAsync(
           () -> {
             plot.getSettings().ratings = DBFunc.getRatings(plot);
             run.run();
           });
       return true;
     }
     plot.getSettings().ratings = new HashMap<>();
   }
   run.run();
   return true;
 }