TF-PlotSquared/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Rate.java

143 lines
6.3 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
2015-06-05 20:45:13 +10:00
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.PlotSquared;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
2014-11-15 12:05:48 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2015-02-22 18:03:25 +11:00
import com.intellectualcrafters.plot.object.Location;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.Plot;
2015-02-21 22:38:44 +11:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-02-22 17:06:59 +11:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-06-05 20:45:13 +10:00
import com.intellectualcrafters.plot.util.TaskManager;
2014-12-31 00:09:11 +11:00
2015-02-20 17:34:19 +11:00
public class Rate extends SubCommand {
2014-11-05 14:42:08 +11:00
/*
* String cmd, String permission, String description, String usage, String
* alias, CommandCategory category
*/
public Rate() {
super("rate", "plots.rate", "Rate the plot", "rate [#|next]", "rt", CommandCategory.ACTIONS, true);
2014-11-05 14:42:08 +11:00
}
2015-02-23 12:32:27 +11:00
2014-11-05 14:42:08 +11:00
@Override
2015-02-21 18:30:55 +11:00
public boolean execute(final PlotPlayer plr, final String... args) {
2014-11-05 14:42:08 +11:00
if (args.length < 1) {
sendMessage(plr, C.RATING_NOT_VALID);
return true;
}
2015-02-23 12:32:27 +11:00
final Location loc = plr.getLocation();
2015-02-21 23:01:15 +11:00
final Plot plot = MainUtil.getPlot(loc);
2015-02-22 18:03:25 +11:00
if (plot == null) {
return !sendMessage(plr, C.NOT_IN_PLOT);
}
2014-11-05 14:42:08 +11:00
if (!plot.hasOwner()) {
sendMessage(plr, C.RATING_NOT_OWNED);
return true;
}
2015-03-20 14:00:02 +11:00
if (plot.isOwner(plr.getUUID())) {
2014-11-05 14:42:08 +11:00
sendMessage(plr, C.RATING_NOT_YOUR_OWN);
return true;
}
final String arg = args[0];
if (arg.equalsIgnoreCase("next")) {
ArrayList<Plot> plots = new ArrayList<>(PlotSquared.getPlots());
Collections.sort(plots, new Comparator<Plot>() {
@Override
public int compare(Plot p1, Plot p2) {
int v1 = 0;
int v2 = 0;
if (p1.settings.ratings != null) {
for (Entry<UUID, Integer> entry : p1.settings.ratings.entrySet()) {
v1 -= 11 - entry.getValue();
}
}
if (p2.settings.ratings != null) {
for (Entry<UUID, Integer> entry : p2.settings.ratings.entrySet()) {
v2 -= 11 - entry.getValue();
}
}
return v2 - v1;
}
});
UUID uuid = plr.getUUID();
for (Plot p : plots) {
if (plot.settings.ratings == null || !plot.settings.ratings.containsKey(uuid)) {
MainUtil.teleportPlayer(plr, plr.getLocation(), p);
MainUtil.sendMessage(plr, C.RATE_THIS);
return true;
}
}
MainUtil.sendMessage(plr, C.FOUND_NO_PLOTS);
return false;
}
2015-06-05 20:45:13 +10:00
final int rating;
if (StringUtils.isNumeric(arg) && arg.length() < 3 && arg.length() > 0) {
2014-11-05 14:42:08 +11:00
rating = Integer.parseInt(arg);
2015-06-05 20:45:13 +10:00
if (rating > 10) {
sendMessage(plr, C.RATING_NOT_VALID);
return false;
}
2014-11-05 14:42:08 +11:00
}
2015-06-05 20:45:13 +10:00
else {
2014-11-05 14:42:08 +11:00
sendMessage(plr, C.RATING_NOT_VALID);
2015-05-15 19:32:38 +10:00
return false;
2014-11-05 14:42:08 +11:00
}
2015-06-05 20:45:13 +10:00
final UUID uuid = plr.getUUID();
final Runnable run = new Runnable() {
2015-06-05 20:45:13 +10:00
@Override
public void run() {
if (plot.settings.ratings.containsKey(uuid)) {
2015-06-05 20:45:13 +10:00
sendMessage(plr, C.RATING_ALREADY_EXISTS, plot.getId().toString());
return;
}
plot.settings.ratings.put(uuid, rating);
2015-06-05 20:45:13 +10:00
DBFunc.setRating(plot, uuid, rating);
sendMessage(plr, C.RATING_APPLIED, plot.getId().toString());
}
};
if (plot.settings.ratings == null) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
plot.settings.ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
run.run();
2014-11-05 14:42:08 +11:00
return true;
}
2014-10-02 17:38:31 +02:00
}