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

219 lines
10 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;
2015-07-05 17:44:10 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.UUID;
import com.intellectualsites.commands.CommandDeclaration;
import com.intellectualsites.commands.callers.CommandCaller;
2015-07-05 17:44:10 +02:00
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.mutable.MutableInt;
2015-07-18 14:21:36 +02:00
import org.bukkit.Bukkit;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.PS;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
2014-11-15 12:05:48 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2015-07-26 16:51:12 +02:00
import com.plotsquared.bukkit.events.PlotRateEvent;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotInventory;
import com.intellectualcrafters.plot.object.PlotItemStack;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.Rating;
2015-02-22 07:06:59 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-06-05 12:45:13 +02:00
import com.intellectualcrafters.plot.util.TaskManager;
2014-12-30 14:09:11 +01:00
@CommandDeclaration(
command = "rate",
permission = "plots.rate",
description = "Rate the plot",
usage = "/plot rate [#|next]",
aliases = {"rt"},
category = CommandCategory.ACTIONS,
requiredType = PlotPlayer.class
)
2015-02-20 07:34:19 +01:00
public class Rate extends SubCommand {
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
@Override
public boolean onCommand(final CommandCaller caller, final String[] args) {
final PlotPlayer player = (PlotPlayer) caller.getSuperCaller();
if (args.length == 1) {
if (args[0].equalsIgnoreCase("next")) {
ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots());
Collections.sort(plots, new Comparator<Plot>() {
@Override
public int compare(Plot p1, Plot p2) {
double v1 = 0;
double v2 = 0;
2015-07-21 20:31:12 +02:00
if (p1.getSettings().ratings != null) {
for (Entry<UUID, Rating> entry : p1.getRatings().entrySet()) {
v1 -= 11 - entry.getValue().getAverageRating();
}
}
2015-07-21 20:31:12 +02:00
if (p2.getSettings().ratings != null) {
for (Entry<UUID, Rating> entry : p2.getRatings().entrySet()) {
v2 -= 11 - entry.getValue().getAverageRating();
}
}
return v2 > v1 ? 1 : -1;
}
});
UUID uuid = player.getUUID();
for (Plot p : plots) {
2015-07-21 20:31:12 +02:00
if ((p.getSettings().ratings == null || !p.getSettings().ratings.containsKey(uuid)) && !p.isAdded(uuid)) {
MainUtil.teleportPlayer(player, player.getLocation(), p);
MainUtil.sendMessage(player, C.RATE_THIS);
return true;
}
}
MainUtil.sendMessage(player, C.FOUND_NO_PLOTS);
return false;
}
2014-11-05 04:42:08 +01:00
}
final Location loc = player.getLocation();
2015-02-21 13:01:15 +01:00
final Plot plot = MainUtil.getPlot(loc);
2015-02-22 08:03:25 +01:00
if (plot == null) {
return !sendMessage(player, C.NOT_IN_PLOT);
2015-02-22 08:03:25 +01:00
}
2014-11-05 04:42:08 +01:00
if (!plot.hasOwner()) {
sendMessage(player, C.RATING_NOT_OWNED);
2014-11-05 04:42:08 +01:00
return true;
}
if (plot.isOwner(player.getUUID())) {
sendMessage(player, C.RATING_NOT_YOUR_OWN);
2014-11-05 04:42:08 +01:00
return true;
}
if (Settings.RATING_CATEGORIES != null && Settings.RATING_CATEGORIES.size() != 0) {
final Runnable run = new Runnable() {
@Override
public void run() {
2015-07-21 20:31:12 +02:00
if (plot.getSettings().ratings.containsKey(player.getUUID())) {
sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
return;
}
final MutableInt index = new MutableInt(0);
final MutableInt rating = new MutableInt(0);
String title = Settings.RATING_CATEGORIES.get(0);
PlotInventory inventory = new PlotInventory(player, 1, title) {
public boolean onClick(int i) {
rating.add((i + 1) * Math.pow(10, index.intValue()));
index.increment();
if (index.intValue() >= Settings.RATING_CATEGORIES.size()) {
close();
2015-07-13 19:48:22 +02:00
// handle ratings
int rV = rating.intValue();
// CALL THE EVENT
PlotRateEvent rateEvent = new PlotRateEvent(player, rV, plot);
Bukkit.getPluginManager().callEvent(rateEvent);
// DONE CALLING THE EVENT
// get new rating
rV = rateEvent.getRating();
// set rating
2015-07-21 20:31:12 +02:00
plot.getSettings().ratings.put(player.getUUID(), rV);
2015-07-13 19:48:22 +02:00
DBFunc.setRating(plot, player.getUUID(), rV);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
return false;
}
setTitle(Settings.RATING_CATEGORIES.get(index.intValue()));
return false;
}
};
inventory.setItem(0, new PlotItemStack(35, (short) 12, 0, "0/8", null));
inventory.setItem(1, new PlotItemStack(35, (short) 14, 1, "1/8", null));
inventory.setItem(2, new PlotItemStack(35, (short) 1, 2, "2/8", null));
inventory.setItem(3, new PlotItemStack(35, (short) 4, 3, "3/8", null));
inventory.setItem(4, new PlotItemStack(35, (short) 5, 4, "4/8", null));
inventory.setItem(5, new PlotItemStack(35, (short) 9, 5, "5/8", null));
inventory.setItem(6, new PlotItemStack(35, (short) 11, 6, "6/8", null));
inventory.setItem(7, new PlotItemStack(35, (short) 10, 7, "7/8", null));
inventory.setItem(8, new PlotItemStack(35, (short) 2, 8, "8/8", null));
inventory.openInventory();
}
};
2015-07-21 20:31:12 +02:00
if (plot.getSettings().ratings == null) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
2015-07-21 20:31:12 +02:00
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
run.run();
return true;
}
if (args.length < 1) {
sendMessage(player, C.RATING_NOT_VALID);
return true;
}
final String arg = args[0];
if (arg.equalsIgnoreCase("next")) {
}
2015-06-05 12:45:13 +02:00
final int rating;
if (StringUtils.isNumeric(arg) && arg.length() < 3 && arg.length() > 0) {
2014-11-05 04:42:08 +01:00
rating = Integer.parseInt(arg);
2015-06-05 12:45:13 +02:00
if (rating > 10) {
sendMessage(player, C.RATING_NOT_VALID);
2015-06-05 12:45:13 +02:00
return false;
}
2014-11-05 04:42:08 +01:00
}
2015-06-05 12:45:13 +02:00
else {
sendMessage(player, C.RATING_NOT_VALID);
2015-05-15 11:32:38 +02:00
return false;
2014-11-05 04:42:08 +01:00
}
final UUID uuid = player.getUUID();
final Runnable run = new Runnable() {
2015-06-05 12:45:13 +02:00
@Override
public void run() {
2015-07-21 20:31:12 +02:00
if (plot.getSettings().ratings.containsKey(uuid)) {
sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
2015-06-05 12:45:13 +02:00
return;
}
2015-07-21 20:31:12 +02:00
plot.getSettings().ratings.put(uuid, rating);
2015-06-05 12:45:13 +02:00
DBFunc.setRating(plot, uuid, rating);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
2015-06-05 12:45:13 +02:00
}
};
2015-07-21 20:31:12 +02:00
if (plot.getSettings().ratings == null) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
2015-07-21 20:31:12 +02:00
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
run.run();
2014-11-05 04:42:08 +01:00
return true;
}
2014-10-02 17:38:31 +02:00
}