Optimized plot saving + plot music

Fixes #244
- Faster PlotMe conversion
- faster uuid mode conversion
This commit is contained in:
boy0001 2015-05-31 05:47:31 +10:00
parent f46386bdb4
commit d68f57efb8
7 changed files with 156 additions and 54 deletions

View File

@ -732,7 +732,7 @@ public class PlotSquared {
final List<String> booleanFlags = Arrays.asList("notify-enter", "notify-leave", "item-drop", "invincible", "instabreak", "drop-protection", "forcefield", "titles", "pve", "pvp", "no-worldedit", "redstone", "keep"); final List<String> booleanFlags = Arrays.asList("notify-enter", "notify-leave", "item-drop", "invincible", "instabreak", "drop-protection", "forcefield", "titles", "pve", "pvp", "no-worldedit", "redstone", "keep");
final List<String> intervalFlags = Arrays.asList("feed", "heal"); final List<String> intervalFlags = Arrays.asList("feed", "heal");
final List<String> stringFlags = Arrays.asList("greeting", "farewell"); final List<String> stringFlags = Arrays.asList("greeting", "farewell");
final List<String> intFlags = Arrays.asList("entity-cap", "mob-cap", "animal-cap", "hostile-cap", "vehicle-cap"); final List<String> intFlags = Arrays.asList("entity-cap", "mob-cap", "animal-cap", "hostile-cap", "vehicle-cap", "music");
for (final String flag : stringFlags) { for (final String flag : stringFlags) {
FlagManager.addFlag(new AbstractFlag(flag)); FlagManager.addFlag(new AbstractFlag(flag));
} }
@ -798,6 +798,9 @@ public class PlotSquared {
case "storm": case "storm":
case "on": case "on":
return "rain"; return "rain";
case "lightning":
case "thunder":
return "thunder";
case "clear": case "clear":
case "off": case "off":
case "sun": case "sun":

View File

@ -329,64 +329,124 @@ public class SQLManager implements AbstractDB {
} }
int packet; int packet;
if (Settings.DB.USE_MYSQL) { if (Settings.DB.USE_MYSQL) {
packet = Math.min(size, 50000); packet = Math.min(size, 5000);
} else { } else {
packet = Math.min(size, 50); packet = Math.min(size, 50);
} }
final int amount = size / packet; final int amount = size / packet;
try {
int count = 0;
PreparedStatement preparedStmt = null;
String statement = null;
int last = -1;
for (int j = 0; j <= amount; j++) { for (int j = 0; j <= amount; j++) {
final List<T> subList = objList.subList(j * packet, Math.min(size, (j + 1) * packet)); final List<T> subList = objList.subList(j * packet, Math.min(size, (j + 1) * packet));
if (subList.size() == 0) { if (subList.size() == 0) {
return; return;
} }
String statement = mod.getCreateMySQL(subList.size()); if (last == -1) {
PreparedStatement stmt = null; last = subList.size();
try { statement = mod.getCreateMySQL(subList.size());
stmt = this.connection.prepareStatement(statement.toString()); preparedStmt = this.connection.prepareStatement(statement.toString());
}
if (subList.size() != last || (count % 5000 == 0 && count > 0)) {
preparedStmt.executeBatch();
preparedStmt.close();
statement = mod.getCreateMySQL(subList.size());
preparedStmt = this.connection.prepareStatement(statement.toString());
}
for (int i = 0; i < subList.size(); i++) { for (int i = 0; i < subList.size(); i++) {
count++;
final T obj = subList.get(i); final T obj = subList.get(i);
mod.setMySQL(stmt,i , obj); mod.setMySQL(preparedStmt, i , obj);
} }
stmt.executeUpdate(); last = subList.size();
stmt.close(); preparedStmt.addBatch();
} catch (final Exception e) { }
PlotSquared.log("&aSuccess 1: " + count + " | " + objList.get(0).getClass().getCanonicalName());
preparedStmt.executeBatch();
preparedStmt.clearParameters();
preparedStmt.close();
return;
}
catch (Exception e) {
if (Settings.DB.USE_MYSQL) {
e.printStackTrace();
PlotSquared.log("&cERROR 1: " + " | " + objList.get(0).getClass().getCanonicalName());
}
}
try { try {
String unionstmt = mod.getCreateSQLite(subList.size()); int count = 0;
stmt = this.connection.prepareStatement(unionstmt.toString()); PreparedStatement preparedStmt = null;
String statement = null;
int last = -1;
for (int j = 0; j <= amount; j++) {
final List<T> subList = objList.subList(j * packet, Math.min(size, (j + 1) * packet));
if (subList.size() == 0) {
return;
}
if (last == -1) {
last = subList.size();
statement = mod.getCreateSQLite(subList.size());
preparedStmt = this.connection.prepareStatement(statement.toString());
}
if (subList.size() != last || (count % 5000 == 0 && count > 0)) {
preparedStmt.executeBatch();
preparedStmt.close();
statement = mod.getCreateSQLite(subList.size());
preparedStmt = this.connection.prepareStatement(statement.toString());
}
for (int i = 0; i < subList.size(); i++) { for (int i = 0; i < subList.size(); i++) {
mod.setSQLite(stmt, i, subList.get(i)); count++;
final T obj = subList.get(i);
mod.setSQLite(preparedStmt, i , obj);
} }
stmt.executeUpdate(); last = subList.size();
stmt.close(); preparedStmt.addBatch();
} }
catch (Exception e2) { PlotSquared.log("&aSuccess 2: " + count + " | " + objList.get(0).getClass().getCanonicalName());
e2.printStackTrace(); preparedStmt.executeBatch();
preparedStmt.clearParameters();
preparedStmt.close();
return;
}
catch (Exception e) {
e.printStackTrace();
PlotSquared.log("&cERROR 2: " + " | " + objList.get(0).getClass().getCanonicalName());
}
PlotSquared.log("&6[WARN] " + "Could not bulk save!"); PlotSquared.log("&6[WARN] " + "Could not bulk save!");
try { try {
for (final T obj : subList) { PreparedStatement preparedStmt = null;
String nonBulk = mod.getCreateSQL();
preparedStmt = this.connection.prepareStatement(nonBulk.toString());
for (final T obj : objList) {
try { try {
stmt = connection.prepareStatement(mod.getCreateSQL()); mod.setSQL(preparedStmt, obj);
mod.setSQL(stmt, obj); preparedStmt.addBatch();
stmt.executeUpdate();
stmt.close();
} catch (final Exception e3) { } catch (final Exception e3) {
PlotSquared.log("&c[ERROR] " + "Failed to save " + obj + "!"); PlotSquared.log("&c[ERROR] " + "Failed to save " + obj + "!");
} }
} }
} catch (final Exception e4) { PlotSquared.log("&aSuccess 3");
e4.printStackTrace(); preparedStmt.executeBatch();
preparedStmt.close();
}
catch (Exception e3) {
e3.printStackTrace();
PlotSquared.log("&c[ERROR] " + "Failed to save all!"); PlotSquared.log("&c[ERROR] " + "Failed to save all!");
} }
} }
}
}
}
public void createSettings(final ArrayList<SettingsPair> myList) { public void createSettings(final ArrayList<SettingsPair> myList) {
final StmtMod<SettingsPair> mod = new StmtMod<SettingsPair>() { final StmtMod<SettingsPair> mod = new StmtMod<SettingsPair>() {
@Override @Override
public String getCreateMySQL(int size) { public String getCreateMySQL(int size) {
return getCreateMySQL(size, CREATE_SETTINGS, 10); return getCreateMySQL(size, "INSERT INTO `" + prefix + "plot_settings`(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`flags`,`merged`,`position`) VALUES ", 10);
} }
@Override @Override
@ -397,7 +457,6 @@ public class SQLManager implements AbstractDB {
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)"; return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`) VALUES(?)";
// return "INSERT INTO `" + SQLManager.this.prefix + "plot_settings`(`plot_plot_id`,`biome`,`rain`,`custom_time`,`time`,`deny_entry`,`alias`,`flags`,`merged`,`position`) VALUES(?,?,?,?,?,?,?,?,?,?)";
} }
@Override @Override

View File

@ -11,9 +11,9 @@ public abstract class StmtMod<T> {
public String getCreateMySQL(int size, String query, int params) { public String getCreateMySQL(int size, String query, int params) {
final StringBuilder statement = new StringBuilder(query); final StringBuilder statement = new StringBuilder(query);
for (int i = 0; i < size - 1; i++) { for (int i = 0; i < size - 1; i++) {
statement.append(StringUtils.repeat("(?),", params)); statement.append("(" + StringUtils.repeat(",(?)", params).substring(1) + "),");
} }
statement.append(StringUtils.repeat(",(?)", params).substring(1)); statement.append("(" + StringUtils.repeat(",(?)", params).substring(1) + ")");
return statement.toString(); return statement.toString();
} }

View File

@ -180,6 +180,19 @@ public class FlagManager {
return true; return true;
} }
public static boolean addPlotFlagAbs(final Plot plot, final Flag flag) {
final boolean result = EventUtil.manager.callFlagAdd(flag, plot);
if (!result) {
return false;
}
final Flag hasFlag = getPlotFlag(plot, flag.getKey());
if (hasFlag != null) {
plot.settings.flags.remove(hasFlag);
}
plot.settings.flags.add(flag);
return true;
}
public static boolean addClusterFlag(final PlotCluster cluster, final Flag flag) { public static boolean addClusterFlag(final PlotCluster cluster, final Flag flag) {
//TODO plot cluster flag event //TODO plot cluster flag event
final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag.getKey()); final Flag hasFlag = getSettingFlag(cluster.world, cluster.settings, flag.getKey());

View File

@ -25,7 +25,9 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Effect;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.WeatherType; import org.bukkit.WeatherType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -65,12 +67,16 @@ public class PlotListener extends APlotListener {
return name; return name;
} }
private WeatherType getWeatherType(String str) { private void setWeather(Player player, String str) {
str = str.toLowerCase(); switch (str.toLowerCase()) {
if (str.equals("rain")) { case "clear": {
return WeatherType.DOWNFALL; player.setPlayerWeather(WeatherType.CLEAR);
} else { return;
return WeatherType.CLEAR; }
case "rain": {
player.setPlayerWeather(WeatherType.DOWNFALL);
return;
}
} }
} }
@ -109,7 +115,7 @@ public class PlotListener extends APlotListener {
} }
final Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather"); final Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather");
if (weatherFlag != null) { if (weatherFlag != null) {
player.setPlayerWeather(getWeatherType(weatherFlag.getValueString())); setWeather(player, weatherFlag.getValueString());
} }
if ((FlagManager.isBooleanFlag(plot, "titles", Settings.TITLES)) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) { if ((FlagManager.isBooleanFlag(plot, "titles", Settings.TITLES)) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) {
Flag greetingFlag = FlagManager.getPlotFlag(plot, "greeting"); Flag greetingFlag = FlagManager.getPlotFlag(plot, "greeting");
@ -130,6 +136,14 @@ public class PlotListener extends APlotListener {
final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot); final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot);
Bukkit.getPluginManager().callEvent(callEvent); Bukkit.getPluginManager().callEvent(callEvent);
} }
Flag musicFlag = FlagManager.getPlotFlag(plot, "music");
if (musicFlag != null) {
player.playEffect(player.getLocation(), Effect.RECORD_PLAY, 0);
Integer id = (Integer) musicFlag.getValue();
if (id != 0) {
player.playEffect(player.getLocation(), Effect.RECORD_PLAY, Material.getMaterial(id));
}
}
CommentManager.sendTitle(pp, plot); CommentManager.sendTitle(pp, plot);
} }
} }
@ -150,6 +164,9 @@ public class PlotListener extends APlotListener {
if (FlagManager.getPlotFlag(plot, "weather") != null) { if (FlagManager.getPlotFlag(plot, "weather") != null) {
player.resetPlayerWeather(); player.resetPlayerWeather();
} }
if (FlagManager.getPlotFlag(plot, "music") != null) {
player.playEffect(player.getLocation(), Effect.RECORD_PLAY, 0);
}
} }
public boolean getFlagValue(final String value) { public boolean getFlagValue(final String value) {

View File

@ -51,6 +51,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent; import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent;
import com.intellectualcrafters.plot.events.PlayerLeavePlotEvent; import com.intellectualcrafters.plot.events.PlayerLeavePlotEvent;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotHandler; import com.intellectualcrafters.plot.object.PlotHandler;
@ -143,9 +144,13 @@ public class PlotPlusListener extends PlotListener implements Listener {
return; return;
} }
if (meta != null) { if (meta != null) {
int id = meta.getMaterial().getId();
FlagManager.addPlotFlag(plot, new Flag(FlagManager.getFlag("music"), id));
player.playEffect(player.getLocation(), Effect.RECORD_PLAY, 0);
for (final Player p : plotPlayers) { for (final Player p : plotPlayers) {
p.playEffect(p.getLocation(), Effect.RECORD_PLAY, meta.getMaterial()); player.playEffect(player.getLocation(), Effect.RECORD_PLAY, 0);
MainUtil.sendMessage(pp, C.RECORD_PLAY.s().replaceAll("%player", player.getName()).replaceAll("%name", meta.toString())); player.playEffect(player.getLocation(), Effect.RECORD_PLAY, id);
APlotListener.manager.plotEntry(BukkitUtil.getPlayer(p), plot);
} }
} }
} }

View File

@ -144,4 +144,9 @@ public class BukkitPlayer implements PlotPlayer {
this.meta.remove(key); this.meta.remove(key);
} }
} }
@Override
public String toString() {
return getName();
}
} }