This commit is contained in:
boy0001
2015-07-27 23:10:14 +10:00
parent dce25ba07a
commit a39ec22c8c
87 changed files with 785 additions and 752 deletions

View File

@ -92,7 +92,7 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
}
List<World> worlds = Bukkit.getWorlds();
if (worlds.size() > 0) {
UUIDHandler.startCaching();
UUIDHandler.startCaching(null);
for (World world : worlds) {
try {
SetGenCB.setGenerator(world);
@ -456,9 +456,9 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain {
FlagManager.removeFlag(FlagManager.getFlag("titles"));
} else {
AbstractTitle.TITLE_CLASS = new DefaultTitle();
if (UUIDHandler.getUUIDWrapper() instanceof DefaultUUIDWrapper) {
if (wrapper instanceof DefaultUUIDWrapper) {
Settings.TWIN_MODE_UUID = true;
} else if (UUIDHandler.getUUIDWrapper() instanceof OfflineUUIDWrapper && !Bukkit.getOnlineMode()) {
} else if (wrapper instanceof OfflineUUIDWrapper && !Bukkit.getOnlineMode()) {
Settings.TWIN_MODE_UUID = true;
}
}

View File

@ -15,6 +15,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.util.StringComparison;
import com.plotsquared.bukkit.util.bukkit.BukkitUtil;
@ -31,7 +32,7 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
if (commandSender instanceof Player) {
return MainCommand.onCommand(BukkitUtil.getPlayer((Player) commandSender), commandLabel, args);
}
return MainCommand.onCommand(null, commandLabel, args);
return MainCommand.onCommand(ConsolePlayer.getConsole(), commandLabel, args);
}
@Override

View File

@ -47,6 +47,6 @@ public class WorldEvents implements Listener {
@EventHandler
public void worldLoad(final WorldLoadEvent event) {
UUIDHandler.startCaching();
UUIDHandler.startCaching(null);
}
}

View File

@ -35,10 +35,14 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
}
@Override
public boolean startCaching() {
if (!super.startCaching()) {
public boolean startCaching(Runnable whenDone) {
if (!super.startCaching(whenDone)) {
return false;
}
return cache(whenDone);
}
public boolean cache(final Runnable whenDone) {
final File container = Bukkit.getWorldContainer();
List<World> worlds = Bukkit.getWorlds();
final String world;
@ -48,7 +52,6 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
else {
world = worlds.get(0).getName();
}
CACHED = true;
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
@ -88,6 +91,7 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
}
}
add(toAdd);
if (whenDone != null) whenDone.run();
return;
}
final HashSet<String> worlds = new HashSet<>();
@ -172,6 +176,7 @@ public class FileUUIDHandler extends UUIDHandlerImplementation {
}
}
add(toAdd);
if (whenDone != null) whenDone.run();
}
});
return true;

View File

@ -10,14 +10,15 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import com.google.common.collect.HashBiMap;
import com.intellectualcrafters.json.JSONObject;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
@ -43,13 +44,13 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
}
try {
PreparedStatement stmt = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS `usercache` (cache_key INTEGER PRIMARY KEY, uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL)");
PreparedStatement stmt = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid, username))");
stmt.execute();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
startCaching();
startCaching(null);
}
private class SQLUUIDHandlerException extends RuntimeException {
@ -71,97 +72,92 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
}
@Override
public boolean startCaching() {
if (!super.startCaching()) {
public boolean startCaching(final Runnable whenDone) {
if (!super.startCaching(whenDone)) {
return false;
}
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
try {
final HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
PreparedStatement statement = getConnection().prepareStatement("SELECT `uuid`, `username` FROM `usercache`");
ResultSet resultSet = statement.executeQuery();
StringWrapper username;
UUID uuid;
boolean found = false;
while (resultSet.next()) {
found = true;
username = new StringWrapper(resultSet.getString("username"));
uuid = UUID.fromString(resultSet.getString("uuid"));
add(new StringWrapper(username.value), uuid);
toAdd.put(new StringWrapper(username.value), uuid);
}
add(new StringWrapper("*"), DBFunc.everyone);
statement.close();
if (!found) {
PS.log(C.PREFIX.s() + "&cUsing player data files, couldn't find any cached UUIDs");
for (World world : Bukkit.getWorlds()) {
FileUUIDHandler fileHandler = new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper);
fileHandler.startCaching();
add(toAdd);
add(new StringWrapper("*"), DBFunc.everyone);
// This should be called as long as there are some unknown plots
final List<UUID> toFetch = new ArrayList<>();
for (UUID u : UUIDHandler.getAllUUIDS()) {
if (!uuidExists(u)) {
toFetch.add(u);
}
PS.log(C.PREFIX.s() + "&cWill fetch the uuids for all plots!");
List<UUID> toFetch = new ArrayList<>();
for (UUID u : UUIDHandler.getAllUUIDS()) {
if (!uuidExists(u)) {
toFetch.add(u);
}
}
PS.log(C.PREFIX.s() + "&cFetching &6" + toFetch.size() + "&c uuids!");
List<UUID> fetched = new ArrayList<>();
for (UUID u : toFetch) {
OfflinePlayer plr = Bukkit.getOfflinePlayer(u);
if (plr != null) {
if (plr.getName() != null) {
add(new StringWrapper(plr.getName()), u);
fetched.add(u);
}
}
}
PS.log(C.PREFIX.s() + "&cFetched &6" + fetched.size() + "&c from player files!");
toFetch.removeAll(fetched);
if (!Settings.OFFLINE_MODE) {
if (toFetch.isEmpty()) {
}
if (toFetch.isEmpty()) {
if (whenDone != null) whenDone.run();
return;
}
FileUUIDHandler fileHandler = new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper);
fileHandler.startCaching(new Runnable() {
@Override
public void run() {
// If the file based UUID handler didn't cache it, then we can't cache offline mode
// Also, trying to cache based on files again, is useless as that's what the file based uuid cacher does
if (Settings.OFFLINE_MODE) {
if (whenDone != null) whenDone.run();
return;
}
PS.log(C.PREFIX.s() + "&cWill fetch &6" + toFetch.size() + "&c from mojang!");
int i = 0;
Iterator<UUID> iterator = toFetch.iterator();
while (iterator.hasNext()) {
StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user=");
List<UUID> currentIteration = new ArrayList<>();
while (i++ <= 15 && iterator.hasNext()) {
UUID _uuid = iterator.next();
url.append(_uuid.toString());
if (iterator.hasNext()) {
url.append(",");
}
currentIteration.add(_uuid);
}
PS.log(C.PREFIX.s() + "&cWill attempt to fetch &6" + currentIteration.size() + "&c uuids from: &6" + url.toString());
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null) {
rawJSON.append(line);
}
reader.close();
JSONObject object = new JSONObject(rawJSON.toString());
for (UUID _u : currentIteration) {
Object o = object.getJSONObject(_u.toString().replace("-", "")).get("username");
if (o == null || !(o instanceof String)) {
continue;
if (!Settings.OFFLINE_MODE) {
PS.log(C.PREFIX.s() + "&cWill fetch &6" + toFetch.size() + "&c from mojang!");
int i = 0;
Iterator<UUID> iterator = toFetch.iterator();
while (iterator.hasNext()) {
StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user=");
List<UUID> currentIteration = new ArrayList<>();
while (i++ <= 15 && iterator.hasNext()) {
UUID _uuid = iterator.next();
url.append(_uuid.toString());
if (iterator.hasNext()) {
url.append(",");
}
add(new StringWrapper(o.toString()), _u);
currentIteration.add(_uuid);
}
} catch (final Exception e) {
e.printStackTrace();
PS.log(C.PREFIX.s() + "&cWill attempt to fetch &6" + currentIteration.size() + "&c uuids from: &6" + url.toString());
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null) {
rawJSON.append(line);
}
reader.close();
JSONObject object = new JSONObject(rawJSON.toString());
for (UUID _u : currentIteration) {
Object o = object.getJSONObject(_u.toString().replace("-", "")).get("username");
if (o == null || !(o instanceof String)) {
continue;
}
add(new StringWrapper(o.toString()), _u);
}
} catch (final Exception e) {
e.printStackTrace();
}
i = 0;
}
i = 0;
}
if (whenDone != null) whenDone.run();
}
}
});
} catch (SQLException e) {
throw new SQLUUIDHandlerException("Couldn't select :s", e);
}
@ -211,7 +207,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
@Override
public boolean add(final StringWrapper name, final UUID uuid) {
// Ignoring duplicates
if (!super.add(name, uuid)) {
if (super.add(name, uuid)) {
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
@ -226,9 +222,9 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
}
}
});
return false;
return true;
}
return true;
return false;
}
/**
@ -263,4 +259,23 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
});
return null;
}
@Override
public void rename(final UUID uuid, final StringWrapper name) {
super.rename(uuid, name);
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
try {
PreparedStatement statement = getConnection().prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?");
statement.setString(1, name.value);
statement.setString(2, uuid.toString());
statement.execute();
PS.log(C.PREFIX.s() + "Name change for '" + uuid + "' to '" + name.value + "'");
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
}

View File

@ -114,7 +114,7 @@ public class SpongeMain implements IPlotMain, PluginContainer {
Collection<World> worlds = server.getWorlds();
if (worlds.size() > 0) {
log("INJECTING WORLDS!!!!!!!");
UUIDHandler.startCaching();
UUIDHandler.startCaching(null);
for (World world : server.getWorlds()) {
log("INJECTING WORLD: " + world.getName());
world.setWorldGenerator(new SpongePlotGenerator(world.getName()));