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

265 lines
12 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-30 16:25:16 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
2015-07-30 16:25:16 +02:00
import java.util.Map.Entry;
import java.util.UUID;
import org.apache.commons.lang.mutable.MutableInt;
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-30 16:25:16 +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-08-08 09:36:02 +02:00
import com.intellectualcrafters.plot.util.EventUtil;
2015-02-22 07:06:59 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.MathMan;
2015-08-13 19:22:32 +02:00
import com.intellectualcrafters.plot.util.Permissions;
2015-06-05 12:45:13 +02:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-08-13 19:22:32 +02:00
import com.plotsquared.general.commands.Command;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2014-12-30 14:09:11 +01:00
@CommandDeclaration(
2015-09-11 12:09:22 +02:00
command = "rate",
permission = "plots.rate",
description = "Rate the plot",
usage = "/plot rate [#|next]",
aliases = { "rt" },
category = CommandCategory.ACTIONS,
requiredType = RequiredType.NONE)
public class Rate extends SubCommand
{
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
@Override
2015-09-11 12:09:22 +02:00
public boolean onCommand(final PlotPlayer player, final String[] args)
{
if (args.length == 1)
{
if (args[0].equalsIgnoreCase("next"))
{
final ArrayList<Plot> plots = new ArrayList<>(PS.get().getPlots());
Collections.sort(plots, new Comparator<Plot>()
{
@Override
2015-09-11 12:09:22 +02:00
public int compare(final Plot p1, final Plot p2)
{
double v1 = 0;
double v2 = 0;
2015-09-11 12:09:22 +02:00
if (p1.getSettings().ratings != null)
{
for (final Entry<UUID, Rating> entry : p1.getRatings().entrySet())
{
v1 -= 11 - entry.getValue().getAverageRating();
}
}
2015-09-11 12:09:22 +02:00
if (p2.getSettings().ratings != null)
{
for (final Entry<UUID, Rating> entry : p2.getRatings().entrySet())
{
v2 -= 11 - entry.getValue().getAverageRating();
}
}
return v2 > v1 ? 1 : -1;
}
});
2015-09-11 12:09:22 +02:00
final UUID uuid = player.getUUID();
for (final Plot p : plots)
{
if ((!Settings.REQUIRE_DONE || p.getSettings().flags.containsKey("done"))
&& p.isBasePlot()
&& ((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-09-11 12:09:22 +02:00
if (plot == null) { return !sendMessage(player, C.NOT_IN_PLOT); }
if (!plot.hasOwner())
{
sendMessage(player, C.RATING_NOT_OWNED);
2015-08-13 19:22:32 +02:00
return false;
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
if (plot.isOwner(player.getUUID()))
{
sendMessage(player, C.RATING_NOT_YOUR_OWN);
2015-08-13 19:22:32 +02:00
return false;
}
2015-09-11 12:09:22 +02:00
if (Settings.REQUIRE_DONE && !plot.getSettings().flags.containsKey("done"))
{
2015-08-13 19:22:32 +02:00
sendMessage(player, C.RATING_NOT_DONE);
return false;
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
if ((Settings.RATING_CATEGORIES != null) && (Settings.RATING_CATEGORIES.size() != 0))
{
final Runnable run = new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
if (plot.getSettings().ratings.containsKey(player.getUUID()))
{
2015-08-13 19:22:32 +02:00
sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
return;
}
final MutableInt index = new MutableInt(0);
final MutableInt rating = new MutableInt(0);
2015-09-11 12:09:22 +02:00
final String title = Settings.RATING_CATEGORIES.get(0);
final PlotInventory inventory = new PlotInventory(player, 1, title)
{
@Override
public boolean onClick(final int i)
{
2015-08-13 19:22:32 +02:00
rating.add((i + 1) * Math.pow(10, index.intValue()));
index.increment();
2015-09-11 12:09:22 +02:00
if (index.intValue() >= Settings.RATING_CATEGORIES.size())
{
2015-08-13 19:22:32 +02:00
close();
2015-09-11 12:09:22 +02:00
final int rV = rating.intValue();
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rV));
2015-08-13 19:22:32 +02:00
plot.getSettings().ratings.put(player.getUUID(), result.getAggregate());
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()));
2015-09-11 12:09:22 +02:00
if (Permissions.hasPermission(player, "plots.comment"))
{
final Command<PlotPlayer> command = MainCommand.getInstance().getCommand("comment");
if (command != null)
{
2015-08-13 19:22:32 +02:00
MainUtil.sendMessage(player, C.COMMENT_THIS, command.getUsage().replaceAll("{label}", "plot"));
}
}
return false;
}
2015-08-13 19:22:32 +02:00
};
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();
}
};
2015-09-11 12:09:22 +02:00
if (plot.getSettings().ratings == null)
{
if (!Settings.CACHE_RATINGS)
{
TaskManager.runTaskAsync(new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
plot.getSettings().ratings = new HashMap<UUID, Integer>();
}
run.run();
return true;
}
2015-09-11 12:09:22 +02:00
if (args.length < 1)
{
sendMessage(player, C.RATING_NOT_VALID);
return true;
}
final String arg = args[0];
2015-06-05 12:45:13 +02:00
final int rating;
2015-09-11 12:09:22 +02:00
if (MathMan.isInteger(arg) && (arg.length() < 3) && (arg.length() > 0))
{
2014-11-05 04:42:08 +01:00
rating = Integer.parseInt(arg);
2015-09-11 12:09:22 +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-09-11 12:09:22 +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();
2015-09-11 12:09:22 +02:00
final Runnable run = new Runnable()
{
2015-06-05 12:45:13 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
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-09-11 12:09:22 +02:00
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating));
final int resultVal = result.getAggregate();
2015-08-08 09:36:02 +02:00
plot.getSettings().ratings.put(uuid, resultVal);
DBFunc.setRating(plot, uuid, resultVal);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
2015-06-05 12:45:13 +02:00
}
};
2015-09-11 12:09:22 +02:00
if (plot.getSettings().ratings == null)
{
if (!Settings.CACHE_RATINGS)
{
TaskManager.runTaskAsync(new Runnable()
{
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
plot.getSettings().ratings = DBFunc.getRatings(plot);
run.run();
}
});
return true;
}
plot.getSettings().ratings = new HashMap<UUID, Integer>();
}
run.run();
2014-11-05 04:42:08 +01:00
return true;
}
2014-10-02 17:38:31 +02:00
}