Improved and implemented GameProfile caching.

This commit is contained in:
Jonathan Williams 2015-12-11 01:24:59 -05:00
parent b5c157c123
commit 222173ab17
5 changed files with 173 additions and 82 deletions

View File

@ -0,0 +1,29 @@
package mineplex.core.profileCache;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.ProfileLookupCallback;
public class ProfileCacheLookupCallback implements ProfileLookupCallback
{
private ProfileCacheManager _profileCacheManager;
private ProfileLookupCallback _profileLookupCallback;
public ProfileCacheLookupCallback(ProfileCacheManager profileCacheManager, ProfileLookupCallback profileLookupCallback)
{
_profileCacheManager = profileCacheManager;
_profileLookupCallback = profileLookupCallback;
}
@Override
public void onProfileLookupFailed(GameProfile gameProfile, Exception exception)
{
_profileLookupCallback.onProfileLookupFailed(gameProfile, exception);
}
@Override
public void onProfileLookupSucceeded(GameProfile gameProfile)
{
_profileCacheManager.cacheProfile(gameProfile);
_profileLookupCallback.onProfileLookupSucceeded(gameProfile);
}
}

View File

@ -1,81 +1,134 @@
//package mineplex.core.profileCache;
//
//import java.util.UUID;
//
//import org.bukkit.plugin.java.JavaPlugin;
//import net.minecraft.util.com.google.gson.Gson;
//import net.minecraft.util.com.google.gson.GsonBuilder;
//import net.minecraft.util.com.mojang.authlib.GameProfile;
//import net.minecraft.util.com.mojang.authlib.properties.PropertyMap;
//import net.minecraft.util.com.mojang.authlib.yggdrasil.ProfileCache;
//import net.minecraft.util.com.mojang.authlib.yggdrasil.YggdrasilGameProfileRepository;
//import net.minecraft.util.com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService;
//import net.minecraft.util.com.mojang.authlib.yggdrasil.response.ProfileSearchResultsResponse;
//import net.minecraft.util.com.mojang.util.UUIDTypeAdapter;
//
//import mineplex.core.MiniPlugin;
//import mineplex.serverdata.Region;
//import mineplex.serverdata.redis.RedisDataRepository;
//import mineplex.serverdata.servers.ServerManager;
//
//public class ProfileCacheManager extends MiniPlugin implements ProfileCache
//{
// private RedisDataRepository<ProfileData> _profileRepository;
// private Gson _gson;
//
// public ProfileCacheManager(JavaPlugin plugin)
// {
// super("Profile Cache", plugin);
//
// _profileRepository = new RedisDataRepository<ProfileData>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
// Region.ALL, ProfileData.class, "profileCacheRepo");
//
// GsonBuilder builder = new GsonBuilder();
// builder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
// builder.registerTypeAdapter(UUID.class, new UUIDTypeAdapter());
// builder.registerTypeAdapter(ProfileSearchResultsResponse.class, new net.minecraft.util.com.mojang.authlib.yggdrasil.response.ProfileSearchResultsResponse.Serializer());
// _gson = builder.create();
//
// try
// {
// YggdrasilMinecraftSessionService.setProfileCache(this);
// YggdrasilGameProfileRepository.setProfileCache(this);
// }
// catch (Exception e)
// {
// System.out.println("================================================");
// System.out.println("Failed to load Profile Cache (Skins)");
// System.out.println("Are you using the correct modified Craftbukkit?");
// System.out.println("================================================");
// }
// }
//
//
// @Override
// public GameProfile attemptToLoadProfile(String playerName)
// {
// ProfileData profile = _profileRepository.getElement(playerName.toLowerCase());
//
// if (profile != null)
// {
// PropertyMap propertyMap = _gson.fromJson(profile.getPropertyMap(), PropertyMap.class);
// GameProfile gameProfile = new GameProfile(profile.getUuid(), profile.getPlayerName());
// gameProfile.getProperties().putAll(propertyMap);
//
//// System.out.println("Loaded profile " + playerName + " from repository!");
// return gameProfile;
// }
//
//// System.out.println("Profile Null");
//
// return null;
// }
//
// @Override
// public void cacheProfile(GameProfile profile)
// {
//// System.out.println("Cached profile: " + profile.getName());
// ProfileData data = new ProfileData(profile.getId(), profile.getName(), _gson.toJson(profile.getProperties()));
// _profileRepository.addElement(data, 60 * 60 * 24); // 1 day
// }
//}
package mineplex.core.profileCache;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.CraftServer;
import org.bukkit.event.EventHandler;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.authlib.Agent;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.ProfileLookupCallback;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.authlib.yggdrasil.YggdrasilGameProfileRepository;
import com.mojang.authlib.yggdrasil.response.ProfileSearchResultsResponse;
import com.mojang.util.UUIDTypeAdapter;
import mineplex.core.MiniPlugin;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.serverdata.Region;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
public class ProfileCacheManager extends MiniPlugin implements GameProfileRepository
{
private YggdrasilGameProfileRepository _mojangProfileRepository;
private RedisDataRepository<ProfileData> _profileRepository;
private Gson _gson;
public ProfileCacheManager(JavaPlugin plugin)
{
super("Profile Cache", plugin);
_profileRepository = new RedisDataRepository<ProfileData>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
Region.ALL, ProfileData.class, "profileCacheRepo");
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
builder.registerTypeAdapter(UUID.class, new UUIDTypeAdapter());
builder.registerTypeAdapter(ProfileSearchResultsResponse.class, new com.mojang.authlib.yggdrasil.response.ProfileSearchResultsResponse.Serializer());
_gson = builder.create();
try
{
Field mojangProfileRepo = net.minecraft.server.v1_8_R3.MinecraftServer.class.getDeclaredField("Y");
mojangProfileRepo.setAccessible(true);
_mojangProfileRepository = (YggdrasilGameProfileRepository) mojangProfileRepo.get(((CraftServer)Bukkit.getServer()).getServer());
mojangProfileRepo.set(((CraftServer)Bukkit.getServer()).getServer(), this);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("================================================");
System.out.println("Failed to load Profile Cache (Skins)");
System.out.println("Are you using the correct modified Craftbukkit?");
System.out.println("================================================");
}
}
public GameProfile attemptToLoadProfile(String playerName)
{
ProfileData profile = _profileRepository.getElement(playerName.toLowerCase());
if (profile != null)
{
PropertyMap propertyMap = _gson.fromJson(profile.getPropertyMap(), PropertyMap.class);
GameProfile gameProfile = new GameProfile(profile.getUuid(), profile.getPlayerName());
gameProfile.getProperties().putAll(propertyMap);
return gameProfile;
}
return null;
}
public void cacheProfile(final GameProfile profile)
{
if (Bukkit.isPrimaryThread())
{
runAsync(new Runnable()
{
public void run()
{
cacheProfileSafely(profile);
}
});
}
else
cacheProfileSafely(profile);
}
@Override
public void findProfilesByNames(String[] profileNames, Agent agent, ProfileLookupCallback profileLookupCallback)
{
List<String> uncachedProfileNames = new ArrayList<>();
for (String profileName : profileNames)
{
GameProfile profile = attemptToLoadProfile(profileName);
if (profile == null)
uncachedProfileNames.add(profileName);
else
profileLookupCallback.onProfileLookupSucceeded(profile);
}
_mojangProfileRepository.findProfilesByNames(uncachedProfileNames.toArray(new String[uncachedProfileNames.size()]), agent, new ProfileCacheLookupCallback(this, profileLookupCallback));
}
@EventHandler
public void clearRepository(UpdateEvent event)
{
if (event.getType() != UpdateType.MIN_10)
return;
_profileRepository.clean();
}
private void cacheProfileSafely(GameProfile profile)
{
ProfileData data = new ProfileData(profile.getId(), profile.getName(), _gson.toJson(profile.getProperties()));
_profileRepository.addElement(data, 60 * 60 * 24); // 1 day
}
}

View File

@ -38,6 +38,7 @@ import mineplex.core.pet.PetManager;
import mineplex.core.poll.PollManager;
import mineplex.core.portal.Portal;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.profileCache.ProfileCacheManager;
import mineplex.core.projectile.ProjectileManager;
import mineplex.core.punish.Punish;
import mineplex.core.recharge.Recharge;
@ -182,6 +183,8 @@ public class Hub extends JavaPlugin implements IRelation
new ClassCombatShop(shopManager, clientManager, donationManager, false, "Knight", classManager.GetClass("Knight"), true);
new ClassCombatShop(shopManager, clientManager, donationManager, false, "Assassin", classManager.GetClass("Assassin"), true);
new ProfileCacheManager(this);
//Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1);
}

View File

@ -15,6 +15,7 @@ import mineplex.core.monitor.LagMeter;
import mineplex.core.npc.NpcManager;
import mineplex.core.portal.Portal;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.profileCache.ProfileCacheManager;
import mineplex.core.punish.Punish;
import mineplex.core.recharge.Recharge;
import mineplex.core.stats.StatsManager;
@ -88,6 +89,8 @@ public class StaffServer extends JavaPlugin
((CraftServer)getServer()).getHandle().addOp(new GameProfile(UUID.fromString("377bdea3-badc-448d-81c1-65db43b17ea4"), "Strutt20"));
((CraftServer)getServer()).getHandle().addOp(new GameProfile(UUID.fromString("efaf9a17-2304-4f42-8433-421523c308dc"), "B2_mp"));
((CraftServer)getServer()).getHandle().addOp(new GameProfile(UUID.fromString("cf85f470-5248-4978-8208-435736fa136e"), "RustyRoo"));
((CraftServer)getServer()).getHandle().addOp(new GameProfile(UUID.fromString("cf85f470-5248-4978-8208-435736fa136e"), "RustyRoo"));
new ProfileCacheManager(this);
}
}

View File

@ -45,6 +45,7 @@ import mineplex.core.pet.PetManager;
import mineplex.core.poll.PollManager;
import mineplex.core.portal.Portal;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.profileCache.ProfileCacheManager;
import mineplex.core.projectile.ProjectileManager;
import mineplex.core.punish.Punish;
import mineplex.core.recharge.Recharge;
@ -160,6 +161,8 @@ public class Arcade extends JavaPlugin
new PacketsInteractionFix(this, packetHandler);
new FoodDupeFix(this);
new ProfileCacheManager(this);
//Updates
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Updater(this), 1, 1);