Paper/CraftBukkit-Patches/0115-Convert-player-skulls-async.patch
Zach Brown 19972e09b8 Update SpigotMC's patches
5a0150f586ed3eb15fe6f1f596d1a5a7d806f0f9 Fix ITEM_BREAK
e6a3911057bd94d8bd7021cbb4923fb84fb106d1 Upstream merge
d1cdcf8d4c3639f956474f02ed662517cffbe23e Remove old patch
068df64aeee368377e1673667bffc7a6dcf90554 Rebuild all patches
2014-11-30 16:16:48 -06:00

224 lines
9.0 KiB
Diff

From a2bf85035fac8dc52b525d34579a57704bd9fa09 Mon Sep 17 00:00:00 2001
From: Thinkofdeath <thethinkofdeath@gmail.com>
Date: Sun, 20 Apr 2014 13:18:55 +0100
Subject: [PATCH] Convert player skulls async
diff --git a/src/main/java/net/minecraft/server/ItemSkull.java b/src/main/java/net/minecraft/server/ItemSkull.java
index 17e88b8..75763ab 100644
--- a/src/main/java/net/minecraft/server/ItemSkull.java
+++ b/src/main/java/net/minecraft/server/ItemSkull.java
@@ -109,17 +109,24 @@ public class ItemSkull extends Item {
return super.a(itemstack);
}
- public boolean a(NBTTagCompound nbttagcompound) {
+ public boolean a(final NBTTagCompound nbttagcompound) { // Spigot - make final
super.a(nbttagcompound);
if (nbttagcompound.hasKeyOfType("SkullOwner", 8) && nbttagcompound.getString("SkullOwner").length() > 0) {
GameProfile gameprofile = new GameProfile((UUID) null, nbttagcompound.getString("SkullOwner"));
- gameprofile = TileEntitySkull.b(gameprofile);
- nbttagcompound.set("SkullOwner", GameProfileSerializer.serialize(new NBTTagCompound(), gameprofile));
+ // Spigot start
+ TileEntitySkull.b(gameprofile, new com.google.common.base.Predicate<GameProfile>() {
+
+ @Override
+ public boolean apply(GameProfile gameprofile) {
+ nbttagcompound.set("SkullOwner", GameProfileSerializer.serialize(new NBTTagCompound(), gameprofile));
+ return false;
+ }
+ });
+ // Spigot end
return true;
} else {
return false;
}
}
-
}
diff --git a/src/main/java/net/minecraft/server/TileEntitySkull.java b/src/main/java/net/minecraft/server/TileEntitySkull.java
index 889ae50..6c33c2d 100644
--- a/src/main/java/net/minecraft/server/TileEntitySkull.java
+++ b/src/main/java/net/minecraft/server/TileEntitySkull.java
@@ -5,11 +5,66 @@ import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import java.util.UUID;
+// Spigot start
+import com.google.common.base.Predicate;
+import com.google.common.cache.LoadingCache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import com.mojang.authlib.Agent;
+// Spigot end
+
public class TileEntitySkull extends TileEntity {
private int a;
public int rotation;
private GameProfile g = null;
+ // Spigot start
+ public static final Executor executor = Executors.newFixedThreadPool(3,
+ new ThreadFactoryBuilder()
+ .setNameFormat("Head Conversion Thread - %1$d")
+ .build()
+ );
+ public static final LoadingCache<String, GameProfile> skinCache = CacheBuilder.newBuilder()
+ .maximumSize( 5000 )
+ .expireAfterAccess( 60, TimeUnit.MINUTES )
+ .build( new CacheLoader<String, GameProfile>()
+ {
+ @Override
+ public GameProfile load(String key) throws Exception
+ {
+ GameProfile[] profiles = new GameProfile[1];
+ GameProfileLookup gameProfileLookup = new GameProfileLookup(profiles);
+
+ MinecraftServer.getServer().getGameProfileRepository().findProfilesByNames(new String[] { key }, Agent.MINECRAFT, gameProfileLookup);
+
+ GameProfile profile = profiles[ 0 ];
+ if (profile == null) {
+ UUID uuid = EntityHuman.a(new GameProfile(null, key));
+ profile = new GameProfile(uuid, key);
+
+ gameProfileLookup.onProfileLookupSucceeded(profile);
+ } else
+ {
+
+ Property property = Iterables.getFirst( profile.getProperties().get( "textures" ), null );
+
+ if ( property == null )
+ {
+ profile = MinecraftServer.getServer().aB().fillProfileProperties( profile, true );
+ }
+ }
+
+
+ return profile;
+ }
+ } );
+
+ // Spigot end
public TileEntitySkull() {}
@@ -68,35 +123,58 @@ public class TileEntitySkull extends TileEntity {
}
private void e() {
- this.g = b(this.g);
- this.update();
+ // Spigot start
+ GameProfile profile = this.g;
+ setSkullType( 0 ); // Work around client bug
+ b(profile, new Predicate<GameProfile>() {
+
+ @Override
+ public boolean apply(GameProfile input) {
+ setSkullType( 3 ); // Work around client bug
+ g = input;
+ update();
+ world.notify(position);
+ return false;
+ }
+ });
+ // Spigot end
}
- public static GameProfile b(GameProfile gameprofile) {
+ // Spigot start - Support async lookups
+ public static void b(final GameProfile gameprofile, final Predicate<GameProfile> callback) {
if (gameprofile != null && !UtilColor.b(gameprofile.getName())) {
if (gameprofile.isComplete() && gameprofile.getProperties().containsKey("textures")) {
- return gameprofile;
+ callback.apply(gameprofile);
} else if (MinecraftServer.getServer() == null) {
- return gameprofile;
+ callback.apply(gameprofile);
} else {
- GameProfile gameprofile1 = MinecraftServer.getServer().getUserCache().getProfile(gameprofile.getName());
-
- if (gameprofile1 == null) {
- return gameprofile;
+ GameProfile profile = skinCache.getIfPresent(gameprofile.getName());
+ if (profile != null) {
+ callback.apply(profile);
} else {
- Property property = (Property) Iterables.getFirst(gameprofile1.getProperties().get("textures"), (Object) null);
-
- if (property == null) {
- gameprofile1 = MinecraftServer.getServer().aB().fillProfileProperties(gameprofile1, true);
- }
-
- return gameprofile1;
+ executor.execute(new Runnable() {
+ @Override
+ public void run() {
+ final GameProfile profile = skinCache.getUnchecked(gameprofile.getName().toLowerCase());
+ MinecraftServer.getServer().processQueue.add(new Runnable() {
+ @Override
+ public void run() {
+ if (profile == null) {
+ callback.apply(gameprofile);
+ } else {
+ callback.apply(profile);
+ }
+ }
+ });
+ }
+ });
}
}
} else {
- return gameprofile;
+ callback.apply(gameprofile);
}
}
+ // Spigot end
public int getSkullType() {
return this.a;
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
index 1d30e5e..e84f24c 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
@@ -45,13 +45,27 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
}
@Override
- void applyToItem(NBTTagCompound tag) {
+ void applyToItem(final NBTTagCompound tag) { // Spigot - make final
super.applyToItem(tag);
if (hasOwner()) {
NBTTagCompound owner = new NBTTagCompound();
GameProfileSerializer.serialize(owner, profile);
- tag.set(SKULL_OWNER.NBT, owner);
+ tag.set( SKULL_OWNER.NBT, owner );
+ // Spigot start - do an async lookup of the profile.
+ // Unfortunately there is not way to refresh the holding
+ // inventory, so that responsibility is left to the user.
+ net.minecraft.server.TileEntitySkull.b(profile, new com.google.common.base.Predicate<GameProfile>() {
+
+ @Override
+ public boolean apply(GameProfile input) {
+ NBTTagCompound owner = new NBTTagCompound();
+ GameProfileSerializer.serialize( owner, input );
+ tag.set( SKULL_OWNER.NBT, owner );
+ return false;
+ }
+ });
+ // Spigot end
}
}
--
2.1.0