-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2426 from BentoBoxWorld/develop
Release 2.4.1
- Loading branch information
Showing
12 changed files
with
383 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package world.bentobox.bentobox.database.json.adapters; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import world.bentobox.bentobox.util.Pair; | ||
|
||
public class PairTypeAdapter<X, Z> extends TypeAdapter<Pair<X, Z>> { | ||
private final Type xType; | ||
private final Type zType; | ||
|
||
public PairTypeAdapter(Type xType, Type zType) { | ||
this.xType = xType; | ||
this.zType = zType; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Pair<X, Z> pair) throws IOException { | ||
out.beginObject(); | ||
out.name("x"); | ||
Gson gson = new Gson(); | ||
gson.toJson(pair.getKey(), xType, out); | ||
out.name("z"); | ||
gson.toJson(pair.getValue(), zType, out); | ||
out.endObject(); | ||
} | ||
|
||
@Override | ||
public Pair<X, Z> read(JsonReader in) throws IOException { | ||
X x = null; | ||
Z z = null; | ||
|
||
in.beginObject(); | ||
while (in.hasNext()) { | ||
String name = in.nextName(); | ||
if (name.equals("x")) { | ||
x = new Gson().fromJson(in, xType); | ||
} else if (name.equals("z")) { | ||
z = new Gson().fromJson(in, zType); | ||
} | ||
} | ||
in.endObject(); | ||
return new Pair<>(x, z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/world/bentobox/bentobox/util/UUIDFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package world.bentobox.bentobox.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
/** | ||
* Fetches UUID for a player name from the Internet | ||
* @since 1.24.1 | ||
*/ | ||
public class UUIDFetcher { | ||
private static final String API_URL = "https://playerdb.co/api/player/minecraft/%s"; | ||
|
||
@Nullable | ||
public static UUID getUUID(@NotNull String name) { | ||
name = name.toLowerCase(); // Had some issues with upper-case letters in the username, so I added this to make sure that doesn't happen. | ||
|
||
try { | ||
HttpURLConnection connection = (HttpURLConnection) URI.create(String.format(API_URL, name)).toURL() | ||
.openConnection(); | ||
|
||
connection.setUseCaches(false); | ||
connection.setDefaultUseCaches(false); | ||
connection.addRequestProperty("User-Agent", "Mozilla/5.0"); | ||
connection.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate"); | ||
connection.addRequestProperty("Pragma", "no-cache"); | ||
connection.setReadTimeout(5000); | ||
|
||
// These connection parameters need to be set or the API won't accept the connection. | ||
|
||
try (BufferedReader bufferedReader = new BufferedReader( | ||
new InputStreamReader(connection.getInputStream()))) { | ||
StringBuilder response = new StringBuilder(); | ||
String line; | ||
|
||
while ((line = bufferedReader.readLine()) != null) | ||
response.append(line); | ||
|
||
final JsonElement parsed = JsonParser.parseString(response.toString()); | ||
|
||
if (parsed == null || !parsed.isJsonObject()) { | ||
return null; | ||
} | ||
|
||
JsonObject data = parsed.getAsJsonObject(); // Read the returned JSON data. | ||
|
||
return UUID.fromString(data.get("data").getAsJsonObject().get("player").getAsJsonObject().get("id") // Grab the UUID. | ||
.getAsString()); | ||
} | ||
} catch (Exception ignored) { | ||
// Ignoring exception since this is usually caused by non-existent usernames. | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.