Ejemplo 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;
 }
Ejemplo n.º 2
0
 @Override
 public boolean clearInbox(Plot plot) {
   if (plot == null || plot.owner == null) {
     return false;
   }
   DBFunc.clearInbox(plot, toString());
   return false;
 }
Ejemplo n.º 3
0
 @Override
 public boolean removeComment(Plot plot, PlotComment comment) {
   if (plot == null || plot.owner == null) {
     return false;
   }
   DBFunc.removeComment(plot, comment);
   return false;
 }
Ejemplo n.º 4
0
 @Override
 public boolean addComment(Plot plot, PlotComment comment) {
   if (plot == null || plot.owner == null) {
     return false;
   }
   DBFunc.setComment(plot, comment);
   return true;
 }
Ejemplo n.º 5
0
 /**
  * Add a flag to a plot
  *
  * @param origin
  * @param flag
  */
 public static boolean addPlotFlag(final Plot origin, final Flag flag) {
   final boolean result = EventUtil.manager.callFlagAdd(flag, origin);
   if (!result) {
     return false;
   }
   for (Plot plot : origin.getConnectedPlots()) {
     plot.getFlags().put(flag.getKey(), flag);
     plot.reEnter();
     DBFunc.setFlags(plot, plot.getFlags().values());
   }
   return true;
 }
Ejemplo n.º 6
0
 public static void setClusterFlags(final PlotCluster cluster, final Set<Flag> flags) {
   if (flags != null && !flags.isEmpty()) {
     cluster.settings.flags.clear();
     for (final Flag flag : flags) {
       cluster.settings.flags.put(flag.getKey(), flag);
     }
   } else if (cluster.settings.flags.isEmpty()) {
     return;
   } else {
     cluster.settings.flags.clear();
   }
   DBFunc.setFlags(cluster, cluster.settings.flags.values());
 }
Ejemplo n.º 7
0
 public static boolean removeClusterFlag(final PlotCluster cluster, final String id) {
   final Flag flag = cluster.settings.flags.remove(id);
   if (flag == null) {
     return false;
   }
   final boolean result = EventUtil.manager.callFlagRemove(flag, cluster);
   if (!result) {
     cluster.settings.flags.put(id, flag);
     return false;
   }
   DBFunc.setFlags(cluster, cluster.settings.flags.values());
   return true;
 }
Ejemplo n.º 8
0
 public static boolean removePlotFlag(final Plot plot, final String id) {
   final Flag flag = plot.getFlags().remove(id);
   if (flag == null) {
     return false;
   }
   final boolean result = EventUtil.manager.callFlagRemove(flag, plot);
   if (!result) {
     plot.getFlags().put(id, flag);
     return false;
   }
   plot.reEnter();
   DBFunc.setFlags(plot, plot.getFlags().values());
   return true;
 }
Ejemplo n.º 9
0
 @Override
 public boolean getComments(final Plot plot, final RunnableVal whenDone) {
   DBFunc.getComments(
       null,
       toString(),
       new RunnableVal() {
         @Override
         public void run() {
           whenDone.value = value;
           TaskManager.runTask(whenDone);
         }
       });
   return true;
 }
Ejemplo n.º 10
0
 public static void getPersistentMeta(UUID uuid, String key, RunnableVal<byte[]> result) {
   PlotPlayer player = UUIDHandler.getPlayer(uuid);
   if (player != null) {
     result.run(player.getPersistentMeta(key));
   } else {
     DBFunc.getPersistentMeta(
         uuid,
         new RunnableVal<Map<String, byte[]>>() {
           @Override
           public void run(Map<String, byte[]> value) {
             result.run(value.get(key));
           }
         });
   }
 }
Ejemplo n.º 11
0
 public static void setPlotFlags(final Plot origin, final Set<Flag> flags) {
   for (Plot plot : origin.getConnectedPlots()) {
     if (flags != null && !flags.isEmpty()) {
       plot.getFlags().clear();
       for (final Flag flag : flags) {
         plot.getFlags().put(flag.getKey(), flag);
       }
     } else if (plot.getFlags().isEmpty()) {
       return;
     } else {
       plot.getFlags().clear();
     }
     plot.reEnter();
     DBFunc.setFlags(plot, plot.getFlags().values());
   }
 }
Ejemplo n.º 12
0
 public static boolean addClusterFlag(final PlotCluster cluster, final Flag flag) {
   getSettingFlag(cluster.area, cluster.settings, flag.getKey());
   cluster.settings.flags.put(flag.getKey(), flag);
   DBFunc.setFlags(cluster, cluster.settings.flags.values());
   return true;
 }
Ejemplo n.º 13
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;
 }
  public boolean add(final StringWrapper name, final UUID uuid) {
    if ((uuid == null)) {
      return false;
    }
    if (name == null) {
      try {
        unknown.add(uuid);
      } catch (Exception e) {
        PS.log("&c(minor) Invalid UUID mapping: " + uuid);
        e.printStackTrace();
      }
      return false;
    }

    /*
     * lazy UUID conversion:
     *  - Useful if the person misconfigured the database, or settings before PlotMe conversion
     */
    if (!Settings.OFFLINE_MODE) {
      UUID offline =
          UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
      if (!unknown.contains(offline) && !name.value.equals(name.value.toLowerCase())) {
        offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
        if (!unknown.contains(offline)) {
          offline = null;
        }
      }
      if (offline != null) {
        unknown.remove(offline);
        Set<Plot> plots = PS.get().getPlots(offline);
        if (plots.size() > 0) {
          for (Plot plot : PS.get().getPlots(offline)) {
            plot.owner = uuid;
          }
          DBFunc.replaceUUID(offline, uuid);
          PS.debug("&cDetected invalid UUID stored for: " + name.value);
          PS.debug(
              "&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
          PS.debug(
              "&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
        }
      }
    }
    try {
      UUID offline = uuidMap.put(name, uuid);
      if (offline != null && !offline.equals(uuid)) {
        Set<Plot> plots = PS.get().getPlots(offline);
        if (plots.size() > 0) {
          for (Plot plot : PS.get().getPlots(offline)) {
            plot.owner = uuid;
          }
          DBFunc.replaceUUID(offline, uuid);
          PS.debug("&cDetected invalid UUID stored for (1): " + name.value);
          PS.debug(
              "&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
          PS.debug(
              "&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
        }
      }
    } catch (Exception e) {
      BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
      if (inverse.containsKey(uuid)) {
        if (uuidMap.containsKey(name)) {
          return false;
        }
        rename(uuid, name);
        return false;
      }
      uuidMap.put(name, uuid);
    }
    return true;
  }
 @Override
 public boolean execute(final PlotPlayer plr, final String... args) {
   if (plr == null) {
     if (args.length < 3) {
       return !MainUtil.sendMessage(
           null,
           "如果你删除了你的数据库, 这个指令将可以识别地皮牌子来恢复数据库. \n\n&c缺失地皮世界参数 /plot debugclaimtest {世界名称} {最小地皮ID} {最大地皮ID}");
     }
     final String world = args[0];
     if (!BlockManager.manager.isWorld(world) || !PS.get().isPlotWorld(world)) {
       return !MainUtil.sendMessage(null, "&c无效的地皮世界!");
     }
     PlotId min, max;
     try {
       final String[] split1 = args[1].split(";");
       final String[] split2 = args[2].split(";");
       min = new PlotId(Integer.parseInt(split1[0]), Integer.parseInt(split1[1]));
       max = new PlotId(Integer.parseInt(split2[0]), Integer.parseInt(split2[1]));
     } catch (final Exception e) {
       return !MainUtil.sendMessage(
           null, "&c无效的最大最小地皮ID参数. &7地皮ID参数格式 &cX;Y &7这个 X,Y 是地皮的坐标\n加入这个参数后, 数据恢复将在所选区域进行.");
     }
     MainUtil.sendMessage(null, "&3木牌恢复&8->&3PlotSquared&8: &7正在开始地皮数据恢复. 该过程需要一些时间...");
     MainUtil.sendMessage(null, "&3木牌恢复&8->&3PlotSquared&8: 发现超过 25000 区块. 请限制半径... (~3.8 分钟)");
     final PlotManager manager = PS.get().getPlotManager(world);
     final PlotWorld plotworld = PS.get().getPlotWorld(world);
     final ArrayList<Plot> plots = new ArrayList<>();
     for (final PlotId id : MainUtil.getPlotSelectionIds(min, max)) {
       final Plot plot = MainUtil.getPlot(world, id);
       final boolean contains = PS.get().getPlots(world).containsKey(plot.id);
       if (contains) {
         MainUtil.sendMessage(null, " - &c数据库已存在: " + plot.id);
         continue;
       }
       final Location loc = manager.getSignLoc(plotworld, plot);
       final ChunkLoc chunk = new ChunkLoc(loc.getX() >> 4, loc.getZ() >> 4);
       final boolean result = ChunkManager.manager.loadChunk(world, chunk);
       if (!result) {
         continue;
       }
       final String[] lines = BlockManager.manager.getSign(loc);
       if (lines != null) {
         String line = lines[2];
         if ((line != null) && (line.length() > 2)) {
           line = line.substring(2);
           final BiMap<StringWrapper, UUID> map = UUIDHandler.getUuidMap();
           UUID uuid = (map.get(new StringWrapper(line)));
           if (uuid == null) {
             for (final StringWrapper string : map.keySet()) {
               if (string.value.toLowerCase().startsWith(line.toLowerCase())) {
                 uuid = map.get(string);
                 break;
               }
             }
           }
           if (uuid == null) {
             uuid = UUIDHandler.getUUID(line);
           }
           if (uuid != null) {
             MainUtil.sendMessage(null, " - &a发现了地皮: " + plot.id + " : " + line);
             plot.owner = uuid;
             plot.hasChanged = true;
             plots.add(plot);
           } else {
             MainUtil.sendMessage(null, " - &c无效的玩家名称: " + plot.id + " : " + line);
           }
         }
       }
     }
     if (plots.size() > 0) {
       MainUtil.sendMessage(null, "&3木牌恢复&8->&3PlotSquared&8: &7更新了 '" + plots.size() + "' 块地皮!");
       DBFunc.createPlotsAndData(
           plots,
           new Runnable() {
             @Override
             public void run() {
               MainUtil.sendMessage(null, "&6数据库更新成功!");
             }
           });
       for (final Plot plot : plots) {
         PS.get().updatePlot(plot);
       }
       MainUtil.sendMessage(null, "&3木牌恢复&8->&3PlotSquared&8: &7已完成!");
     } else {
       MainUtil.sendMessage(null, "给出的搜索条件已经没有未恢复地图了.");
     }
   } else {
     MainUtil.sendMessage(plr, "&6这个指令只能通过控制台使用.");
   }
   return true;
 }