Skip to content

Commit

Permalink
enhance: Cache player skin
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 13, 2023
1 parent 6889c3b commit 4dfa320
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import xiamomc.morph.backends.server.renderer.network.listeners.SpawnPacketHandler;
import xiamomc.morph.backends.server.renderer.network.ProtocolHandler;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers;
import xiamomc.morph.backends.server.renderer.skins.SkinStore;
import xiamomc.pluginbase.Managers.DependencyManager;

public class ServerRenderer extends MorphPluginObject
Expand All @@ -17,9 +18,12 @@ public class ServerRenderer extends MorphPluginObject

private final RenderRegistry registry = new RenderRegistry();

private final SkinStore skinStore = new SkinStore();

public ServerRenderer()
{
dependencies.cache(registry);
dependencies.cache(skinStore);
dependencies.cache(protocolHandler = new ProtocolHandler());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@
import com.comphenix.protocol.wrappers.WrappedDataValue;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.mojang.authlib.GameProfile;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.Util;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.server.packs.repository.Pack;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.GameType;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.PlayerSkinProvider;
import xiamomc.morph.backends.server.renderer.skins.PlayerSkinProvider;
import xiamomc.morph.backends.server.renderer.network.DisplayParameters;
import xiamomc.morph.backends.server.renderer.network.ProtocolEquipment;
import xiamomc.morph.backends.server.renderer.network.RenderRegistry;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher;
import xiamomc.morph.backends.server.renderer.utilties.ProtocolRegistryUtils;
import xiamomc.morph.misc.MorphGameProfile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package xiamomc.morph.backends.server.renderer;
package xiamomc.morph.backends.server.renderer.skins;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.yggdrasil.ProfileResult;
import net.minecraft.Util;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.players.GameProfileCache;
import org.jetbrains.annotations.Nullable;
import org.bukkit.Bukkit;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.misc.NmsRecord;
import xiamomc.pluginbase.Annotations.Initializer;
import xiamomc.pluginbase.Annotations.Resolved;

import java.io.File;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

Expand All @@ -26,6 +27,9 @@ public static PlayerSkinProvider getInstance()

private GameProfileCache userCache;

@Resolved
private SkinStore skinStore;

@Initializer
private void load()
{
Expand All @@ -38,7 +42,10 @@ public CompletableFuture<Optional<GameProfile>> fetchSkinFromProfile(GameProfile
{
if (profile.getProperties().containsKey("textures"))
{
return CompletableFuture.completedFuture(Optional.of(profile));
var optional = Optional.of(profile);
profileCache.put(profile.getName(), profile);

return CompletableFuture.completedFuture(optional);
}
else
{
Expand All @@ -51,8 +58,21 @@ public CompletableFuture<Optional<GameProfile>> fetchSkinFromProfile(GameProfile
}
}

private final Map<String, GameProfile> profileCache = new Object2ObjectOpenHashMap<>();

public CompletableFuture<Optional<GameProfile>> fetchSkin(String profileName)
{
var player = Bukkit.getPlayerExact(profileName);
if (player != null)
{
profileCache.remove(profileName);
return CompletableFuture.completedFuture(Optional.of(NmsRecord.ofPlayer(player).gameProfile));
}

var cachedSkin = profileCache.getOrDefault(profileName, null);
if (cachedSkin != null)
return CompletableFuture.completedFuture(Optional.of(cachedSkin));

var server = MinecraftServer.getServer();
var userCache = this.userCache == null
? server.getProfileCache()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package xiamomc.morph.backends.server.renderer.skins;

import com.mojang.authlib.GameProfile;
import net.minecraft.Util;
import xiamomc.morph.misc.MorphGameProfile;

import java.util.UUID;

public class SingleSkin
{
public String name = "unknown";

public UUID uuid = Util.NIL_UUID;

public String texture = "";

public String signature = "";

public int expiresAt = 0;

/*
public static SingleSkin fromProfile(GameProfile profile)
{
var wrapped = new MorphGameProfile(profile);
var instance = new SingleSkin();
instance.name = profile.getName();
instance.uuid = profile.getId();
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package xiamomc.morph.backends.server.renderer.skins;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.misc.MorphGameProfile;
import xiamomc.morph.storage.MorphJsonBasedStorage;

public class SkinStore extends MorphJsonBasedStorage<SkinStoreRoot>
{
@Override
protected @NotNull String getFileName()
{
return "stored_skins.json";
}

@Override
protected @NotNull SkinStoreRoot createDefault()
{
return new SkinStoreRoot();
}

@Override
protected @NotNull String getDisplayName()
{
return "Server renderer skin store";
}
/*
@Nullable
public GameProfile getProfile(String skinName)
{
var matchedSkin = storingObject.storedSkins.stream()
.filter(skin -> skin.name.equalsIgnoreCase(skinName))
.findFirst().orElse(null);
if (matchedSkin == null) return null;
var profile = new GameProfile(matchedSkin.uuid, matchedSkin.name);
profile.getProperties().put("textures", new Property())
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package xiamomc.morph.backends.server.renderer.skins;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;

import java.util.List;

public class SkinStoreRoot
{
public List<SingleSkin> storedSkins = new ObjectArrayList<>();
}

0 comments on commit 4dfa320

Please sign in to comment.