Optimizations and cleaning.

Fixes #943 (even though it should be impossible)
This commit is contained in:
Matt 2016-03-08 21:18:54 -05:00
parent 286e5b8b97
commit 57fa265b48
10 changed files with 48 additions and 91 deletions

View File

@ -1098,7 +1098,7 @@ public class PS {
}
}
}
ArrayList<PlotArea> areas = new ArrayList<>(Arrays.asList(plotareas));
List<PlotArea> areas = Arrays.asList(plotareas);
Collections.sort(areas, new Comparator<PlotArea>() {
@Override
public int compare(final PlotArea a, final PlotArea b) {

View File

@ -20,13 +20,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
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.UUID;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
@ -44,6 +37,13 @@ import com.intellectualcrafters.plot.util.TaskManager;
import com.plotsquared.general.commands.Command;
import com.plotsquared.general.commands.CommandDeclaration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.UUID;
@CommandDeclaration(
command = "rate",
permission = "plots.rate",
@ -129,9 +129,7 @@ public class Rate extends SubCommand {
close();
final int rV = rating.getValue();
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rV));
plot.getSettings().ratings.put(player.getUUID(), result.getAggregate());
DBFunc.setRating(plot, player.getUUID(), rV);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
plot.addRating(player.getUUID(), result);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
return false;
}
@ -193,14 +191,12 @@ public class Rate extends SubCommand {
final Runnable run = new Runnable() {
@Override
public void run() {
if (plot.getSettings().ratings.containsKey(uuid)) {
if (plot.getRatings().containsKey(uuid)) {
sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
return;
}
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating));
final int resultVal = result.getAggregate();
plot.getSettings().ratings.put(uuid, resultVal);
DBFunc.setRating(plot, uuid, resultVal);
plot.addRating(uuid,result);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
}
};

View File

@ -140,7 +140,7 @@ public class list extends SubCommand {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.shared");
return false;
}
plots = new ArrayList<Plot>();
plots = new ArrayList<>();
for (final Plot plot : PS.get().getPlots()) {
if (plot.getTrusted().contains(plr.getUUID()) || plot.getMembers().contains(plr.getUUID())) {
plots.add(plot);
@ -177,7 +177,7 @@ public class list extends SubCommand {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.world." + world);
return false;
}
plots = area == null ? new ArrayList<Plot>() : new ArrayList<Plot>(area.getPlots());
plots = area == null ? new ArrayList<Plot>() : new ArrayList<>(area.getPlots());
break;
}
case "all": {
@ -348,7 +348,7 @@ public class list extends SubCommand {
}
if (plots == null) {
sendMessage(plr, C.DID_YOU_MEAN, new StringComparison<String>(args[0], new String[] { "mine", "shared", "world", "all" }).getBestMatch());
sendMessage(plr, C.DID_YOU_MEAN, new StringComparison<>(args[0], new String[]{"mine", "shared", "world", "all"}).getBestMatch());
return false;
}
@ -371,7 +371,7 @@ public class list extends SubCommand {
if (sort) {
plots = PS.get().sortPlots(plots, SortType.CREATION_DATE, area);
}
this.<Plot> paginate(player, plots, pageSize, page, new RunnableVal3<Integer, Plot, PlotMessage>() {
this.paginate(player, plots, pageSize, page, new RunnableVal3<Integer, Plot, PlotMessage>() {
@Override
public void run(Integer i, Plot plot, PlotMessage message) {
String color;

View File

@ -1631,17 +1631,8 @@ public class SQLManager implements AbstractDB {
user = UUID.fromString(o);
uuids.put(o, user);
}
Timestamp timestamp = null;
try {
timestamp = r.getTimestamp("timestamp");
} catch (SQLException ignored) {
}
long time;
if (timestamp == null) {
time = plot_id.hashCode();
} else {
time = timestamp.getTime();
}
Timestamp timestamp = r.getTimestamp("timestamp");
long time = timestamp.getTime();
Plot p = new Plot(plot_id, user, new HashSet<UUID>(), new HashSet<UUID>(), new HashSet<UUID>(), "", null, null, null,
new boolean[]{false, false, false, false}, time, id);
HashMap<PlotId, Plot> map = newplots.get(areaid);

View File

@ -1182,7 +1182,7 @@ public class Plot {
public void clearRatings() {
final Plot base = this.getBasePlot(false);
final PlotSettings baseSettings = base.getSettings();
if ((baseSettings.ratings != null) && !baseSettings.ratings.isEmpty()) {
if ((baseSettings.ratings != null) && !baseSettings.getRatings().isEmpty()) {
DBFunc.deleteRatings(base);
baseSettings.ratings = null;
}
@ -1196,10 +1196,10 @@ public class Plot {
public HashMap<UUID, Rating> getRatings() {
final Plot base = this.getBasePlot(false);
final HashMap<UUID, Rating> map = new HashMap<>();
if (base.getSettings().ratings == null) {
if (!base.hasRatings()) {
return map;
}
for (final Entry<UUID, Integer> entry : base.getSettings().ratings.entrySet()) {
for (final Entry<UUID, Integer> entry : base.getSettings().getRatings().entrySet()) {
map.put(entry.getKey(), new Rating(entry.getValue()));
}
return map;
@ -1207,7 +1207,11 @@ public class Plot {
public boolean hasRatings() {
Plot base = this.getBasePlot(false);
return base.settings != null && base.settings.ratings != null;
if (base.settings != null && base.settings.ratings != null) {
return true;
} else {
return false;
}
}
/**
@ -1946,6 +1950,8 @@ public class Plot {
final int index = caption.indexOf("%plr%");
if (index == -1) {
continue;
} else if (index < -1) {
PS.debug("This should NEVER happen. Seriously, it's impossible.");
}
final String name = lines[i - 1].substring(index);
if (name.isEmpty()) {

View File

@ -84,7 +84,7 @@ public class PlotAnalysis {
PS.debug(" - $1Reducing " + plots.size() + " plots to those with sufficient data");
while (iter.hasNext()) {
final Plot plot = iter.next();
if ((plot.getSettings().ratings == null) || (plot.getSettings().ratings.isEmpty())) {
if ((plot.getSettings().ratings == null) || (plot.getSettings().getRatings().isEmpty())) {
iter.remove();
} else {
plot.addRunning();
@ -125,7 +125,7 @@ public class PlotAnalysis {
for (; mi.intValue() < plots.size(); mi.incrementAndGet()) {
final int i = mi.intValue();
final Plot plot = plots.get(i);
ratings[i] = (int) ((plot.getAverageRating() + plot.getSettings().ratings.size()) * 100);
ratings[i] = (int) ((plot.getAverageRating() + plot.getSettings().getRatings().size()) * 100);
PS.debug(" | " + plot + " (rating) " + (ratings[i]));
}
}

View File

@ -1,12 +1,12 @@
package com.intellectualcrafters.plot.object;
import com.intellectualcrafters.plot.config.Settings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import com.intellectualcrafters.plot.config.Settings;
public class Rating {
/**
@ -22,15 +22,15 @@ public class Rating {
ratingMap = new HashMap<>();
if ((Settings.RATING_CATEGORIES != null) && (Settings.RATING_CATEGORIES.size() > 1)) {
if (value < 10) {
for (int i = 0; i < Settings.RATING_CATEGORIES.size(); i++) {
ratingMap.put(Settings.RATING_CATEGORIES.get(i), value);
for (String ratingCategory : Settings.RATING_CATEGORIES) {
ratingMap.put(ratingCategory, value);
}
changed = true;
return;
}
for (int i = 0; i < Settings.RATING_CATEGORIES.size(); i++) {
ratingMap.put(Settings.RATING_CATEGORIES.get(i), (value % 10) - 1);
value /= 10;
for (String ratingCategory : Settings.RATING_CATEGORIES) {
ratingMap.put(ratingCategory, (value % 10) - 1);
value = value / 10;
}
} else {
ratingMap.put(null, value);

View File

@ -614,42 +614,6 @@ public class MainUtil {
return true;
}
/**
* Get the average rating for a plot
* @see Plot#getAverageRating()
* @param plot
* @return
*/
public static double getAverageRating(final Plot plot) {
HashMap<UUID, Integer> rating;
if (plot.getSettings().ratings != null) {
rating = plot.getSettings().ratings;
} else if (Settings.CACHE_RATINGS) {
rating = new HashMap<>();
} else {
rating = DBFunc.getRatings(plot);
}
if (rating == null || rating.isEmpty()) {
return 0;
}
double val = 0;
int size = 0;
for (final Entry<UUID, Integer> entry : rating.entrySet()) {
int current = entry.getValue();
if (Settings.RATING_CATEGORIES == null || Settings.RATING_CATEGORIES.isEmpty()) {
val += current;
size++;
} else {
for (int i = 0; i < Settings.RATING_CATEGORIES.size(); i++) {
val += current % 10 - 1;
current /= 10;
size++;
}
}
}
return val / size;
}
/**
* If rating categories are enabled, get the average rating by category.<br>
* - The index corresponds to the index of the category in the config
@ -755,7 +719,7 @@ public class MainUtil {
}
info = newInfo.replaceAll("%rating%", rating);
} else {
info = newInfo.replaceAll("%rating%", String.format("%.1f", MainUtil.getAverageRating(plot)) + "/" + max);
info = newInfo.replaceAll("%rating%", String.format("%.1f", plot.getAverageRating()) + "/" + max);
}
whenDone.run(info);
}

View File

@ -16,7 +16,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
dependencies {
compile project(':Core')
compile 'org.spongepowered:spongeapi:3.1.0-SNAPSHOT'
compile 'org.spongepowered:spongeapi:4.0.3'
}
sourceCompatibility = 1.8

View File

@ -94,7 +94,7 @@ public class MainListener {
@Listener
public void onChat(final MessageEvent event) {
// TODO
Player player = SpongeUtil.<Player> getCause(event.getCause(), Player.class);
Player player = SpongeUtil.getCause(event.getCause(), Player.class);
if (player == null) {
return;
}
@ -112,7 +112,7 @@ public class MainListener {
if (plot == null) {
return;
}
final Text message = event.getMessage().orElse(Text.EMPTY);
final Text message = event.getMessage();
// TODO use display name rather than username
// - Getting displayname currently causes NPE, so wait until sponge fixes that
@ -120,7 +120,7 @@ public class MainListener {
final String sender = player.getName();
final PlotId id = plot.getId();
final String newMessage = StringMan.replaceAll(C.PLOT_CHAT_FORMAT.s(), "%plot_id%", id.x + ";" + id.y, "%sender%", sender);
final Text forcedMessage = event.getMessage().orElse(Text.EMPTY);
final Text forcedMessage = event.getMessage();
// String forcedMessage = StringMan.replaceAll(C.PLOT_CHAT_FORCED.s(), "%plot_id%", id.x + ";" + id.y, "%sender%", sender);
for (Entry<String, PlotPlayer> entry : UUIDHandler.getPlayers().entrySet()) {
PlotPlayer user = entry.getValue();
@ -146,7 +146,7 @@ public class MainListener {
}
((SpongePlayer) user).player.sendMessage(Text.join(components));
}
event.setMessage(null);
event.clearMessage();
}
@Listener
@ -321,7 +321,7 @@ public class MainListener {
@Listener
public void onInteract(InteractEvent event) throws Exception {
final Player player = SpongeUtil.<Player> getCause(event.getCause(), Player.class);
final Player player = SpongeUtil.getCause(event.getCause(), Player.class);
if (player == null) {
event.setCancelled(true);
return;
@ -455,7 +455,7 @@ public class MainListener {
@Listener
public void onBlockBreak(final ChangeBlockEvent.Break event) {
Player player = SpongeUtil.<Player> getCause(event.getCause(), Player.class);
Player player = SpongeUtil.getCause(event.getCause(), Player.class);
if (player == null) {
// SpongeUtil.printCause("break", event.getCause());
return;
@ -539,7 +539,7 @@ public class MainListener {
@Listener
public void onBlockPlace(final ChangeBlockEvent.Place event) {
Player player = SpongeUtil.<Player> getCause(event.getCause(), Player.class);
Player player = SpongeUtil.getCause(event.getCause(), Player.class);
if (player == null) {
// SpongeUtil.printCause("place", event.getCause());
return;