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>() { Collections.sort(areas, new Comparator<PlotArea>() {
@Override @Override
public int compare(final PlotArea a, final PlotArea b) { public int compare(final PlotArea a, final PlotArea b) {

View File

@ -20,13 +20,6 @@
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands; 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.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings; 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.Command;
import com.plotsquared.general.commands.CommandDeclaration; 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( @CommandDeclaration(
command = "rate", command = "rate",
permission = "plots.rate", permission = "plots.rate",
@ -129,9 +129,7 @@ public class Rate extends SubCommand {
close(); close();
final int rV = rating.getValue(); final int rV = rating.getValue();
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rV)); final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rV));
plot.getSettings().ratings.put(player.getUUID(), result.getAggregate()); plot.addRating(player.getUUID(), result);
DBFunc.setRating(plot, player.getUUID(), rV);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
sendMessage(player, C.RATING_APPLIED, plot.getId().toString()); sendMessage(player, C.RATING_APPLIED, plot.getId().toString());
return false; return false;
} }
@ -193,14 +191,12 @@ public class Rate extends SubCommand {
final Runnable run = new Runnable() { final Runnable run = new Runnable() {
@Override @Override
public void run() { public void run() {
if (plot.getSettings().ratings.containsKey(uuid)) { if (plot.getRatings().containsKey(uuid)) {
sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString()); sendMessage(player, C.RATING_ALREADY_EXISTS, plot.getId().toString());
return; return;
} }
final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating)); final Rating result = EventUtil.manager.callRating(player, plot, new Rating(rating));
final int resultVal = result.getAggregate(); plot.addRating(uuid,result);
plot.getSettings().ratings.put(uuid, resultVal);
DBFunc.setRating(plot, uuid, resultVal);
sendMessage(player, C.RATING_APPLIED, plot.getId().toString()); 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"); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.shared");
return false; return false;
} }
plots = new ArrayList<Plot>(); plots = new ArrayList<>();
for (final Plot plot : PS.get().getPlots()) { for (final Plot plot : PS.get().getPlots()) {
if (plot.getTrusted().contains(plr.getUUID()) || plot.getMembers().contains(plr.getUUID())) { if (plot.getTrusted().contains(plr.getUUID()) || plot.getMembers().contains(plr.getUUID())) {
plots.add(plot); plots.add(plot);
@ -177,7 +177,7 @@ public class list extends SubCommand {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.world." + world); MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.list.world." + world);
return false; return false;
} }
plots = area == null ? new ArrayList<Plot>() : new ArrayList<Plot>(area.getPlots()); plots = area == null ? new ArrayList<Plot>() : new ArrayList<>(area.getPlots());
break; break;
} }
case "all": { case "all": {
@ -348,7 +348,7 @@ public class list extends SubCommand {
} }
if (plots == null) { 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; return false;
} }
@ -371,7 +371,7 @@ public class list extends SubCommand {
if (sort) { if (sort) {
plots = PS.get().sortPlots(plots, SortType.CREATION_DATE, area); 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 @Override
public void run(Integer i, Plot plot, PlotMessage message) { public void run(Integer i, Plot plot, PlotMessage message) {
String color; String color;

View File

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

View File

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

View File

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

View File

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

View File

@ -614,42 +614,6 @@ public class MainUtil {
return true; 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> * If rating categories are enabled, get the average rating by category.<br>
* - The index corresponds to the index of the category in the config * - The index corresponds to the index of the category in the config
@ -755,7 +719,7 @@ public class MainUtil {
} }
info = newInfo.replaceAll("%rating%", rating); info = newInfo.replaceAll("%rating%", rating);
} else { } 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); whenDone.run(info);
} }

View File

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

View File

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