Minor refactoring

This commit is contained in:
games647 committed 2018-03-02 18:29:38 +01:00
1 parent 04b00f4f22
commit 86694982c7
14 files changed
+52 -56

No files matched your search

@@ -1,5 +1,6 @@
package com.github.games647.fastlogin.core;
import com.github.games647.fastlogin.core.mojang.UUIDTypeAdapter;
import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@@ -35,6 +36,8 @@ public class AuthStorage {
this.core = core;
HikariConfig config = new HikariConfig();
config.setPoolName(core.getPlugin().getName());
config.setUsername(user);
config.setPassword(pass);
config.setDriverClassName(driver);
@@ -97,7 +100,7 @@ public class AuthStorage {
if (resultSet.next()) {
long userId = resultSet.getInt(1);
UUID uuid = CommonUtil.parseId(resultSet.getString(2));
UUID uuid = UUIDTypeAdapter.parseId(resultSet.getString(2));
boolean premium = resultSet.getBoolean(4);
String lastIp = resultSet.getString(5);
@@ -117,7 +120,7 @@ public class AuthStorage {
public PlayerProfile loadProfile(UUID uuid) {
try (Connection con = dataSource.getConnection();
PreparedStatement loadStmt = con.prepareStatement(LOAD_BY_UUID)) {
loadStmt.setString(1, CommonUtil.toMojangId(uuid));
loadStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
try (ResultSet resultSet = loadStmt.executeQuery()) {
if (resultSet.next()) {
@@ -146,7 +149,7 @@ public class AuthStorage {
if (uuid == null) {
saveStmt.setString(1, null);
} else {
saveStmt.setString(1, CommonUtil.toMojangId(uuid));
saveStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
}
saveStmt.setString(2, playerProfile.getPlayerName());
@@ -166,7 +169,7 @@ public class AuthStorage {
if (uuid == null) {
saveStmt.setString(1, null);
} else {
saveStmt.setString(1, CommonUtil.toMojangId(uuid));
saveStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
}
saveStmt.setString(2, playerProfile.getPlayerName());
@@ -3,12 +3,9 @@ package com.github.games647.fastlogin.core;
import com.google.common.cache.CacheLoader;
import java.lang.reflect.Constructor;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -16,8 +13,6 @@ import org.slf4j.impl.JDK14LoggerAdapter;
public class CommonUtil {
private static final Pattern UUID_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
private static final char COLOR_CHAR = '&';
private static final char TRANSLATED_CHAR = '§';
@@ -37,18 +32,6 @@ public class CommonUtil {
}));
}
public static UUID parseId(String withoutDashes) {
return UUID.fromString(UUID_PATTERN.matcher(withoutDashes).replaceAll("$1-$2-$3-$4-$5"));
}
public static String toMojangId(UUID uuid) {
return uuid.toString().replace("-", "");
}
public static UUID getOfflineUUID(String playerName) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(StandardCharsets.UTF_8));
}
public static String translateColorCodes(String rawMessage) {
char[] chars = rawMessage.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
@@ -3,7 +3,7 @@ package com.github.games647.fastlogin.core.hooks;
/**
* Represents a supporting authentication plugin in BungeeCord and Bukkit/Spigot/... servers
*
* @param <P> either org.bukkit.entity.GameProfile for Bukkit or net.md_5.bungee.api.connection.ProxiedPlayer
* @param <P> either {@link org.bukkit.entity.Player} for Bukkit or {@link net.md_5.bungee.api.connection.ProxiedPlayer}
* for BungeeCord
*/
public interface AuthPlugin<P> {
@@ -7,6 +7,15 @@ public class GameProfile {
private UUID id;
private String name;
public GameProfile(UUID id, String name) {
this.id = id;
this.name = name;
}
public GameProfile() {
//gson
}
public UUID getId() {
return id;
}
@@ -30,6 +30,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
@@ -98,8 +99,8 @@ public class MojangApiConnector {
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line = reader.readLine();
return Optional.of(getUUIDFromJson(line));
String content = reader.lines().collect(Collectors.joining());
return Optional.of(getUUIDFromJson(content));
}
} else if (connection.getResponseCode() == RATE_LIMIT_CODE) {
logger.info("Mojang's rate-limit reached. The public IPv4 address of this server issued more than 600" +
@@ -1,21 +1,36 @@
package com.github.games647.fastlogin.core.mojang;
import com.github.games647.fastlogin.core.CommonUtil;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.TypeAdapters;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.regex.Pattern;
public class UUIDTypeAdapter extends TypeAdapter<UUID> {
private static final Pattern UUID_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
public static UUID parseId(String withoutDashes) {
return UUID.fromString(UUID_PATTERN.matcher(withoutDashes).replaceAll("$1-$2-$3-$4-$5"));
}
public static String toMojangId(UUID uuid) {
return uuid.toString().replace("-", "");
}
public static UUID getOfflineUUID(String playerName) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(StandardCharsets.UTF_8));
}
public void write(JsonWriter out, UUID value) throws IOException {
TypeAdapters.STRING.write(out, CommonUtil.toMojangId(value));
TypeAdapters.STRING.write(out, toMojangId(value));
}
public UUID read(JsonReader in) throws IOException {
return CommonUtil.parseId(TypeAdapters.STRING.read(in));
return parseId(TypeAdapters.STRING.read(in));
}
}
@@ -19,7 +19,9 @@ public interface PlatformPlugin<C> {
void sendMessage(C receiver, String message);
ThreadFactory getThreadFactory();
default ThreadFactory getThreadFactory() {
return null;
}
MojangApiConnector makeApiConnector(List<String> addresses, int requests, List<HostAndPort> proxies);
}