Skip to content

Commit

Permalink
accessing a web api in the main thread isn't safe (#1) :/ so made tha…
Browse files Browse the repository at this point in the history
…t async
  • Loading branch information
aBooDyy committed Oct 30, 2020
1 parent c19972f commit 6c0fc55
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/target/
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
Expand Down
55 changes: 33 additions & 22 deletions src/main/java/net/aboodyy/localtime/DateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

package net.aboodyy.localtime;

import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.BufferedReader;
import java.io.InputStreamReader;
Expand All @@ -36,7 +38,7 @@

public class DateManager implements Listener {

private Map<UUID, String> timezones;
private final Map<UUID, String> timezones;

DateManager() {
timezones = new HashMap<>();
Expand All @@ -51,44 +53,53 @@ public String getDate(String format, String timezone) {
return dateFormat.format(date);
}

public String getTimeZone(Player p) {
final String FAILED = "[LocalTime] Couldn't get " + p.getName() + "'s timezone. Will use default timezone.";
public String getTimeZone(Player player) {
final String FAILED = "[LocalTime] Couldn't get " + player.getName() + "'s timezone. Will use default timezone.";
String timezone = TimeZone.getDefault().getID();

if (timezones.containsKey(p.getUniqueId()))
return timezones.get(p.getUniqueId());
if (timezones.containsKey(player.getUniqueId()))
return timezones.get(player.getUniqueId());

InetSocketAddress address = player.getAddress();
timezones.put(player.getUniqueId(), timezone);

InetSocketAddress address = p.getAddress();
if (address == null) {
Bukkit.getLogger().info(FAILED);

timezones.put(p.getUniqueId(), timezone);
return timezone;
}

try {
URL api = new URL("https://ipapi.co/" + address.getAddress().getHostAddress() + "/timezone/");
URLConnection connection = api.openConnection();
new BukkitRunnable() {
@Override
public void run() {
String timezone;

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
timezone = bufferedReader.readLine();
} catch (Exception e) {
Bukkit.getLogger().info(FAILED);
}
try {
URL api = new URL("https://ipapi.co/" + address.getAddress().getHostAddress() + "/timezone/");
URLConnection connection = api.openConnection();

if (timezone.equalsIgnoreCase("undefined")) {
Bukkit.getLogger().info(FAILED);
timezone = TimeZone.getDefault().getID();
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
timezone = bufferedReader.readLine();
} catch (Exception e) {
timezone = "undefined";
}

if (timezone.equalsIgnoreCase("undefined")) {
Bukkit.getLogger().info(FAILED);
timezone = TimeZone.getDefault().getID();
}

timezones.put(player.getUniqueId(), timezone);
}
}.runTaskAsynchronously(PlaceholderAPIPlugin.getInstance());

timezones.put(p.getUniqueId(), timezone);
return timezone;
return timezones.get(player.getUniqueId());
}

public void clear() {
timezones.clear();
}

@SuppressWarnings("unused")
@EventHandler
public void onLeave(PlayerQuitEvent e) {
timezones.remove(e.getPlayer().getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused")
public class LocalTimeExpansion extends PlaceholderExpansion implements Cacheable, Configurable {

private DateManager dateManager;
Expand Down

0 comments on commit 6c0fc55

Please sign in to comment.