Skip to content

Commit

Permalink
fix onlineplayer and timer stop issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Jan 19, 2023
1 parent 4844d13 commit 49ec7ff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
29 changes: 12 additions & 17 deletions src/main/java/net/emc/emce/modules/TaskScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public void start() {

// Pre-fill data.
if (config.general.enableMod) {
if (config.townless.enabled)
EarthMCAPI.getTownless().thenAccept(instance()::setTownlessResidents);

if (config.nearby.enabled)
EarthMCAPI.getNearby().thenAccept(instance()::setNearbyPlayers);
if (config.townless.enabled) EarthMCAPI.getTownless().thenAccept(instance()::setTownlessResidents);
if (config.nearby.enabled) EarthMCAPI.getNearby().thenAccept(instance()::setNearbyPlayers);
}

startTownless();
Expand All @@ -50,39 +47,37 @@ public void start() {
}

public void stop() {
service.shutdown();

townlessRunning = false;
nearbyRunning = false;
newsRunning = false;
cacheCheckRunning = false;

Messaging.sendDebugMessage("EMCE > Stopping scheduled tasks...");
}

public void initMap() {
service = Executors.newScheduledThreadPool(1);
service.schedule(() -> {
if (hasMap) {
Messaging.sendDebugMessage("EMCE > Map already initialized!");
return;
}
service = Executors.newScheduledThreadPool(2);
service.scheduleAtFixedRate(() -> {
if (hasMap) return;

if (playerOnline("aurora")) setHasMap("aurora");
else if (playerOnline("nova")) setHasMap("nova");
else setHasMap(null);
}, 10, TimeUnit.SECONDS); // Give enough time for dynmap to update players.
}, 8, 15, TimeUnit.SECONDS); // Give enough time for Dynmap & Vercel to update.
}

public void setHasMap(String map) {
if (map == null) {
if (!service.isShutdown()) stop();
Messaging.sendDebugMessage("EMCE > Player not found on any map.");
stop();

instance().mapName = "aurora";
hasMap = false;
}
else {
instance().mapName = map;
hasMap = true;
Messaging.sendDebugMessage("EMCE > Player found on: " + map);

hasMap = true;
start();
}
}
Expand Down
39 changes: 27 additions & 12 deletions src/main/java/net/emc/emce/utils/EarthMCAPI.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.emc.emce.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import net.emc.emce.config.ModConfig;
import net.emc.emce.objects.API.APIData;
import net.emc.emce.objects.API.APIRoute;
Expand All @@ -21,6 +18,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -72,8 +70,7 @@ public static CompletableFuture<JsonArray> getNearby(int xBlocks, int zBlocks) {
array.remove(i);
}
return array;
} else
return instance().getNearbyPlayers();
} else return instance().getNearbyPlayers();
} catch (APIException e) {
Messaging.sendDebugMessage(e.getMessage(), e);
return instance().getNearbyPlayers();
Expand All @@ -92,17 +89,35 @@ public static CompletableFuture<Resident> getResident(String residentName) {
});
}

public static CompletableFuture<JsonElement> getOnlinePlayer(String playerName) {
public static CompletableFuture<JsonArray> getOnlinePlayers() {
return CompletableFuture.supplyAsync(() -> {
try {
return JsonParser.parseString(getURL(getRoute(APIRoute.ONLINE_PLAYERS) + "/" + playerName));
return (JsonArray) JsonParser.parseString(getURL(getRoute(APIRoute.ONLINE_PLAYERS)));
} catch (APIException e) {
Messaging.sendDebugMessage(e.getMessage(), e);
return new JsonObject();
return new JsonArray();
}
});
}

public static JsonObject getOnlinePlayer(String name) {
JsonArray ops = getOnlinePlayers().join().getAsJsonArray();
JsonObject pl = new JsonObject();

if (!ops.isEmpty()) {
for (JsonElement op : ops) {
JsonObject cur = op.getAsJsonObject();

if (Objects.equals(cur.get("name").getAsString(), name)) {
pl = cur;
break;
}
}
}

return pl;
}

public static CompletableFuture<JsonArray> getTowns() {
return CompletableFuture.supplyAsync(() -> {
try {
Expand Down Expand Up @@ -199,10 +214,10 @@ public static String clientName() {
}

public static boolean playerOnline(String map) {
instance().mapName = map;
instance().mapName = map; // getOnlinePlayer uses mapName.

JsonObject player = (JsonObject) getOnlinePlayer(clientName()).join();
return player.has("name") && player.get("name").getAsString().equals(clientName());
JsonObject player = getOnlinePlayer(clientName());
return player.has("name");
}

private static String getURL(String urlString) throws APIException {
Expand Down

0 comments on commit 49ec7ff

Please sign in to comment.