mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-07-04 22:54:43 +02:00
Major code reformatting
This commit is contained in:
@ -15,7 +15,9 @@ import com.intellectualcrafters.plot.uuid.UUIDWrapper;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
@ -31,35 +33,34 @@ import java.util.UUID;
|
||||
|
||||
public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
|
||||
final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
final int MAX_REQUESTS = 500;
|
||||
final int INTERVAL = 12000;
|
||||
final JSONParser jsonParser = new JSONParser();
|
||||
private final SQLite _sqLite;
|
||||
private final String PROFILE_URL = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
private final int INTERVAL = 12000;
|
||||
private final JSONParser jsonParser = new JSONParser();
|
||||
private final SQLite sqlite;
|
||||
|
||||
public SQLUUIDHandler(final UUIDWrapper wrapper) {
|
||||
public SQLUUIDHandler(UUIDWrapper wrapper) {
|
||||
super(wrapper);
|
||||
_sqLite = new SQLite("./plugins/PlotSquared/usercache.db");
|
||||
this.sqlite = new SQLite("./plugins/PlotSquared/usercache.db");
|
||||
try {
|
||||
_sqLite.openConnection();
|
||||
} catch (final Exception e) {
|
||||
this.sqlite.openConnection();
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
final PreparedStatement stmt = getConnection().prepareStatement(
|
||||
"CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid, username))");
|
||||
try (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 (final SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
startCaching(null);
|
||||
}
|
||||
|
||||
private Connection getConnection() {
|
||||
synchronized (_sqLite) {
|
||||
return _sqLite.getConnection();
|
||||
synchronized (this.sqlite) {
|
||||
return this.sqlite.getConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,9 +73,9 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
||||
final PreparedStatement statement = getConnection().prepareStatement("SELECT `uuid`, `username` FROM `usercache`");
|
||||
final ResultSet resultSet = statement.executeQuery();
|
||||
HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
|
||||
PreparedStatement statement = getConnection().prepareStatement("SELECT `uuid`, `username` FROM `usercache`");
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
StringWrapper username = new StringWrapper(resultSet.getString("username"));
|
||||
UUID uuid = UUID.fromString(resultSet.getString("uuid"));
|
||||
@ -86,7 +87,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
|
||||
// This should be called as long as there are some unknown plots
|
||||
final ArrayDeque<UUID> toFetch = new ArrayDeque<>();
|
||||
for (final UUID u : UUIDHandler.getAllUUIDS()) {
|
||||
for (UUID u : UUIDHandler.getAllUUIDS()) {
|
||||
if (!uuidExists(u)) {
|
||||
toFetch.add(u);
|
||||
}
|
||||
@ -97,7 +98,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
}
|
||||
return;
|
||||
}
|
||||
final FileUUIDHandler fileHandler = new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper);
|
||||
FileUUIDHandler fileHandler = new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper);
|
||||
fileHandler.startCaching(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -122,70 +123,25 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
}
|
||||
for (int i = 0; i < Math.min(500, toFetch.size()); i++) {
|
||||
UUID uuid = toFetch.pop();
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(PROFILE_URL + uuid.toString().replace("-", "")).openConnection();
|
||||
HttpURLConnection connection =
|
||||
(HttpURLConnection) new URL(SQLUUIDHandler.this.PROFILE_URL + uuid.toString().replace("-", ""))
|
||||
.openConnection();
|
||||
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
|
||||
JSONObject response = (JSONObject) jsonParser.parse(reader);
|
||||
JSONObject response = (JSONObject) SQLUUIDHandler.this.jsonParser.parse(reader);
|
||||
String name = (String) response.get("name");
|
||||
if (name != null) {
|
||||
add(new StringWrapper(name), uuid);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
TaskManager.runTaskLaterAsync(this, INTERVAL);
|
||||
TaskManager.runTaskLaterAsync(this, SQLUUIDHandler.this.INTERVAL);
|
||||
}
|
||||
});
|
||||
/*
|
||||
* This API is no longer accessible.
|
||||
*/
|
||||
// if (!Settings.OFFLINE_MODE) {
|
||||
// PS.debug(C.PREFIX.s() + "&cWill fetch &6" + toFetch.size() + "&c from mojang!");
|
||||
//
|
||||
// int i = 0;
|
||||
// final Iterator<UUID> iterator = toFetch.iterator();
|
||||
// while (iterator.hasNext()) {
|
||||
// final StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user=");
|
||||
// final List<UUID> currentIteration = new ArrayList<>();
|
||||
// while ((i++ <= 15) && iterator.hasNext()) {
|
||||
// final UUID _uuid = iterator.next();
|
||||
// url.append(_uuid.toString());
|
||||
// if (iterator.hasNext()) {
|
||||
// url.append(",");
|
||||
// }
|
||||
// currentIteration.add(_uuid);
|
||||
// }
|
||||
// PS.debug(C.PREFIX.s() + "&cWill attempt to fetch &6" + currentIteration.size() + "&c uuids from: &6" + url.toString());
|
||||
// try {
|
||||
// final HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
|
||||
// connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
// final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
// String line;
|
||||
// final StringBuilder rawJSON = new StringBuilder();
|
||||
// while ((line = reader.readLine()) != null) {
|
||||
// rawJSON.append(line);
|
||||
// }
|
||||
// reader.close();
|
||||
// final JSONObject object = new JSONObject(rawJSON.toString());
|
||||
// for (final UUID _u : currentIteration) {
|
||||
// final 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;
|
||||
// }
|
||||
// }
|
||||
// if (whenDone != null) {
|
||||
// whenDone.run();
|
||||
// }
|
||||
}
|
||||
});
|
||||
} catch (final SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
throw new SQLUUIDHandlerException("Couldn't select :s", e);
|
||||
}
|
||||
}
|
||||
@ -203,7 +159,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL url = new URL(PROFILE_URL);
|
||||
URL url = new URL(SQLUUIDHandler.this.PROFILE_URL);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
@ -215,12 +171,14 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
stream.write(body.getBytes());
|
||||
stream.flush();
|
||||
stream.close();
|
||||
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
||||
JSONArray array = (JSONArray) SQLUUIDHandler.this.jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
||||
JSONObject jsonProfile = (JSONObject) array.get(0);
|
||||
String id = (String) jsonProfile.get("id");
|
||||
String name = (String) jsonProfile.get("name");
|
||||
ifFetch.value = UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
|
||||
} catch (Exception e) {
|
||||
ifFetch.value = UUID.fromString(
|
||||
id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id
|
||||
.substring(20, 32));
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
TaskManager.runTask(ifFetch);
|
||||
@ -233,7 +191,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
super.handleShutdown();
|
||||
try {
|
||||
getConnection().close();
|
||||
} catch (final SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
throw new SQLUUIDHandlerException("Couldn't close database connection", e);
|
||||
}
|
||||
}
|
||||
@ -245,13 +203,12 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
TaskManager.runTaskAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final PreparedStatement statement = getConnection().prepareStatement("REPLACE INTO usercache (`uuid`, `username`) VALUES(?, ?)");
|
||||
try (PreparedStatement statement = getConnection().prepareStatement("REPLACE INTO usercache (`uuid`, `username`) VALUES(?, ?)")) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, name.toString());
|
||||
statement.execute();
|
||||
PS.debug(C.PREFIX + "&cAdded '&6" + uuid + "&c' - '&6" + name + "&c'");
|
||||
} catch (final SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@ -270,13 +227,12 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
TaskManager.runTaskAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final PreparedStatement statement = getConnection().prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?");
|
||||
try (PreparedStatement statement = getConnection().prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?")) {
|
||||
statement.setString(1, name.value);
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.execute();
|
||||
PS.debug(C.PREFIX + "Name change for '" + uuid + "' to '" + name.value + "'");
|
||||
} catch (final SQLException e) {
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@ -285,7 +241,7 @@ public class SQLUUIDHandler extends UUIDHandlerImplementation {
|
||||
|
||||
private class SQLUUIDHandlerException extends RuntimeException {
|
||||
|
||||
SQLUUIDHandlerException(final String s, final Throwable c) {
|
||||
SQLUUIDHandlerException(String s, Throwable c) {
|
||||
super("SQLUUIDHandler caused an exception: " + s, c);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user