new apimanager, updated joinleavemanager to use it

This commit is contained in:
noil 2019-11-19 23:24:45 -05:00
parent cd3f0dd3ae
commit 4acfdcb46c
3 changed files with 64 additions and 3 deletions

View File

@ -31,6 +31,8 @@ public final class Seppuku {
private EventManager eventManager;
private APIManager apiManager;
private ModuleManager moduleManager;
private CommandManager commandManager;
@ -74,6 +76,7 @@ public final class Seppuku {
public void init() {
this.initLogger();
this.eventManager = new AnnotatedEventManager();
this.apiManager = new APIManager();
this.moduleManager = new ModuleManager();
this.commandManager = new CommandManager();
this.friendManager = new FriendManager();
@ -124,6 +127,7 @@ public final class Seppuku {
public void unload() {
this.moduleManager.unload();
this.apiManager.unload();
this.commandManager.unload();
this.friendManager.unload();
this.waypointManager.unload();
@ -194,6 +198,13 @@ public final class Seppuku {
return this.eventManager;
}
public APIManager getApiManager() {
if (this.apiManager == null) {
this.apiManager = new APIManager();
}
return this.apiManager;
}
public ModuleManager getModuleManager() {
if (this.moduleManager == null) {
this.moduleManager = new ModuleManager();

View File

@ -0,0 +1,51 @@
package me.rigamortis.seppuku.impl.management;
import com.google.common.collect.Maps;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
public final class APIManager {
private final Map<String, String> uuidNameCache = Maps.newConcurrentMap();
public APIManager() {
}
public void unload() {
this.uuidNameCache.clear();
}
public String resolveName(String uuid) {
uuid = uuid.replace("-", "");
if (uuidNameCache.containsKey(uuid)) {
return uuidNameCache.get(uuid);
}
final String url = "https://api.mojang.com/user/profiles/" + uuid + "/names";
try {
final String nameJson = IOUtils.toString(new URL(url));
if (nameJson != null && nameJson.length() > 0) {
final JSONArray jsonArray = (JSONArray) JSONValue.parseWithException(nameJson);
if (jsonArray != null) {
final JSONObject latestName = (JSONObject) jsonArray.get(jsonArray.size() - 1);
if (latestName != null) {
return latestName.get("name").toString();
}
}
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -5,7 +5,6 @@ import me.rigamortis.seppuku.api.event.EventStageable;
import me.rigamortis.seppuku.api.event.network.EventReceivePacket;
import me.rigamortis.seppuku.api.event.player.EventPlayerJoin;
import me.rigamortis.seppuku.api.event.player.EventPlayerLeave;
import me.rigamortis.seppuku.api.util.NetworkUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.server.SPacketPlayerListItem;
import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
@ -31,7 +30,7 @@ public final class JoinLeaveManager {
for (SPacketPlayerListItem.AddPlayerData playerData : packet.getEntries()) {
if (playerData.getProfile().getId() != mc.session.getProfile().getId()) {
new Thread(() -> {
final String name = NetworkUtil.resolveUsername(playerData.getProfile().getId());
final String name = Seppuku.INSTANCE.getApiManager().resolveName(playerData.getProfile().getId().toString());
if (name != null) {
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventPlayerJoin(name, playerData.getProfile().getId().toString()));
}
@ -43,7 +42,7 @@ public final class JoinLeaveManager {
for (SPacketPlayerListItem.AddPlayerData playerData : packet.getEntries()) {
if (playerData.getProfile().getId() != mc.session.getProfile().getId()) {
new Thread(() -> {
final String name = NetworkUtil.resolveUsername(playerData.getProfile().getId());
final String name = Seppuku.INSTANCE.getApiManager().resolveName(playerData.getProfile().getId().toString());
if (name != null) {
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventPlayerLeave(name, playerData.getProfile().getId().toString()));
}