Add SkullMeta.setPlayerProfile API

This allows you to create already filled textures on Skulls to avoid texture lookups
which commonly cause rate limit issues with Mojang API
This commit is contained in:
Aikar 2018-01-19 00:38:49 -05:00
parent d065e0b00e
commit e06cc30ff7
4 changed files with 127 additions and 6 deletions

View file

@ -0,0 +1,46 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 19 Jan 2018 00:29:28 -0500
Subject: [PATCH] Add SkullMeta.setPlayerProfile API
This allows you to create already filled textures on Skulls to avoid texture lookups
which commonly cause rate limit issues with Mojang API
diff --git a/src/main/java/org/bukkit/inventory/meta/SkullMeta.java b/src/main/java/org/bukkit/inventory/meta/SkullMeta.java
index c60860e1..3eea5909 100644
--- a/src/main/java/org/bukkit/inventory/meta/SkullMeta.java
+++ b/src/main/java/org/bukkit/inventory/meta/SkullMeta.java
@@ -0,0 +0,0 @@
package org.bukkit.inventory.meta;
+import com.destroystokyo.paper.profile.PlayerProfile;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
+import javax.annotation.Nullable;
+
/**
* Represents a skull ({@link Material#SKULL_ITEM}) that can have an owner.
*/
@@ -0,0 +0,0 @@ public interface SkullMeta extends ItemMeta {
@Deprecated
boolean setOwner(String owner);
+ // Paper start
+ /**
+ * Sets this skull to use the supplied Player Profile, which can include textures already prefilled.
+ * @param profile The profile to set this Skull to use, or null to clear owner
+ */
+ void setPlayerProfile(@Nullable PlayerProfile profile);
+
+ /**
+ * If the skull has an owner, per {@link #hasOwner()}, return the owners {@link PlayerProfile}
+ * @return The profile of the owner, if set
+ */
+ @Nullable PlayerProfile getPlayerProfile();
+ // Paper end
+
/**
* Gets the owner of the skull.
*
--

View file

@ -0,0 +1,51 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 19 Jan 2018 00:36:25 -0500
Subject: [PATCH] Add SkullMeta.setPlayerProfile API
This allows you to create already filled textures on Skulls to avoid texture lookups
which commonly cause rate limit issues with Mojang API
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
index e2ea49cd9..4855307b9 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
@@ -0,0 +0,0 @@ package org.bukkit.craftbukkit.inventory;
import java.util.Map;
+import com.destroystokyo.paper.profile.CraftPlayerProfile;
+import com.destroystokyo.paper.profile.PlayerProfile;
import net.minecraft.server.GameProfileSerializer;
import net.minecraft.server.NBTBase;
import net.minecraft.server.NBTTagCompound;
@@ -0,0 +0,0 @@ import org.bukkit.inventory.meta.SkullMeta;
import com.google.common.collect.ImmutableMap.Builder;
import com.mojang.authlib.GameProfile;
+import javax.annotation.Nullable;
+
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
@@ -0,0 +0,0 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
return hasOwner() ? profile.getName() : null;
}
+ // Paper start
+ @Override
+ public void setPlayerProfile(@Nullable PlayerProfile profile) {
+ this.profile = (profile == null) ? null : CraftPlayerProfile.asAuthlibCopy(profile);
+ }
+
+ @Nullable
+ @Override
+ public PlayerProfile getPlayerProfile() {
+ return profile != null ? CraftPlayerProfile.asBukkitMirror(profile) : null;
+ }
+ // Paper end
+
@Override
public OfflinePlayer getOwningPlayer() {
if (hasOwner()) {
--

View file

@ -6,7 +6,7 @@ Subject: [PATCH] Basic PlayerProfile API
diff --git a/src/main/java/com/destroystokyo/paper/profile/CraftPlayerProfile.java b/src/main/java/com/destroystokyo/paper/profile/CraftPlayerProfile.java
new file mode 100644
index 000000000..6e9a6a2e6
index 000000000..6868ee9a4
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/profile/CraftPlayerProfile.java
@@ -0,0 +0,0 @@
@ -40,6 +40,10 @@ index 000000000..6e9a6a2e6
+ this.profile = new GameProfile(id, name);
+ }
+
+ public CraftPlayerProfile(GameProfile profile) {
+ this.profile = profile;
+ }
+
+ public GameProfile getGameProfile() {
+ return profile;
+ }
@ -78,6 +82,13 @@ index 000000000..6e9a6a2e6
+ }
+
+ @Override
+ public CraftPlayerProfile clone() {
+ CraftPlayerProfile clone = new CraftPlayerProfile(this.getId(), this.getName());
+ clone.setProperties(getProperties());
+ return clone;
+ }
+
+ @Override
+ public void setProperty(ProfileProperty property) {
+ String name = property.getName();
+ PropertyMap properties = profile.getProperties();
@ -109,18 +120,31 @@ index 000000000..6e9a6a2e6
+ return new ProfileProperty(property.getName(), property.getValue(), property.getSignature());
+ }
+
+ public static PlayerProfile from(GameProfile gameProfile) {
+ public static PlayerProfile asBukkitCopy(GameProfile gameProfile) {
+ PlayerProfile profile = new CraftPlayerProfile(gameProfile.getId(), gameProfile.getName());
+ gameProfile.getProperties().values().forEach(property -> profile.setProperty(toBukkit(property)));
+ return profile;
+ }
+
+ public static PlayerProfile asBukkitMirror(GameProfile profile) {
+ return new CraftPlayerProfile(profile);
+ }
+
+ public static Property asAuthlib(ProfileProperty property) {
+ return new Property(property.getName(), property.getValue(), property.getSignature());
+ }
+ public static GameProfile asAuthlibCopy(PlayerProfile profile) {
+ CraftPlayerProfile craft = ((CraftPlayerProfile) profile);
+ return asAuthlib(craft.clone());
+ }
+
+ public static GameProfile asAuthlib(PlayerProfile profile) {
+ CraftPlayerProfile craft = ((CraftPlayerProfile) profile);
+ return craft.getGameProfile();
+ }
+}
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
index 02940d697..f38afb72e 100644
index 02940d697..4539b5601 100644
--- a/src/main/java/net/minecraft/server/MCUtil.java
+++ b/src/main/java/net/minecraft/server/MCUtil.java
@@ -0,0 +0,0 @@
@ -138,7 +162,7 @@ index 02940d697..f38afb72e 100644
}
+ public static PlayerProfile toBukkit(GameProfile profile) {
+ return CraftPlayerProfile.from(profile);
+ return CraftPlayerProfile.asBukkitMirror(profile);
+ }
+
/**

View file

@ -8,7 +8,7 @@ profiles that had to be looked up.
diff --git a/src/main/java/com/destroystokyo/paper/profile/WrappedGameProfileRepository.java b/src/main/java/com/destroystokyo/paper/profile/WrappedGameProfileRepository.java
new file mode 100644
index 000000000..950fa930d
index 000000000..bffba6a65
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/profile/WrappedGameProfileRepository.java
@@ -0,0 +0,0 @@
@ -71,7 +71,7 @@ index 000000000..950fa930d
+
+ @Override
+ public void onProfileLookupSucceeded(GameProfile gameProfile) {
+ PlayerProfile from = CraftPlayerProfile.from(gameProfile);
+ PlayerProfile from = CraftPlayerProfile.asBukkitMirror(gameProfile);
+ new LookupProfileEvent(from).callEvent();
+ callback.onProfileLookupSucceeded(gameProfile);
+ }