Update Profile Lookup Events to use new PlayerProfile API

This commit is contained in:
Aikar 2018-01-19 00:03:09 -05:00
parent 71c18fd5c9
commit 18e3bf685c
No known key found for this signature in database
GPG key ID: 401ADFC9891FAAFE
4 changed files with 176 additions and 101 deletions

View file

@ -1,4 +1,4 @@
From da39e33d6a82ceead9d973492435f4d5954ffc30 Mon Sep 17 00:00:00 2001
From 094a75a6f82a16f8a5f6c27c24ba503b74443a8f Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 17 Jun 2017 16:30:44 -0400
Subject: [PATCH] Profile Lookup Events
@ -26,12 +26,13 @@ index c8b37997..13994dc2 100644
<artifactId>fastutil-lite</artifactId>
diff --git a/src/main/java/com/destroystokyo/paper/event/profile/LookupProfileEvent.java b/src/main/java/com/destroystokyo/paper/event/profile/LookupProfileEvent.java
new file mode 100644
index 00000000..37e957f4
index 00000000..e4b36255
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/LookupProfileEvent.java
@@ -0,0 +1,53 @@
@@ -0,0 +1,55 @@
+package com.destroystokyo.paper.event.profile;
+
+import com.destroystokyo.paper.profile.PlayerProfile;
+import com.mojang.authlib.GameProfile;
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
@ -49,17 +50,10 @@ index 00000000..37e957f4
+public class LookupProfileEvent extends Event {
+
+ private static final HandlerList handlers = new HandlerList();
+ /**
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ private final GameProfile profile;
+
+ /**
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ public LookupProfileEvent(@Nonnull GameProfile profile) {
+ private final PlayerProfile profile;
+
+ public LookupProfileEvent(@Nonnull PlayerProfile profile) {
+ super(!Bukkit.isPrimaryThread());
+ this.profile = profile;
+ }
@ -71,6 +65,14 @@ index 00000000..37e957f4
+ @Deprecated
+ @Nonnull
+ public GameProfile getProfile() {
+ return profile.getGameProfile();
+ }
+
+ /**
+ * @return The profile that was recently looked up. This profile can be mutated
+ */
+ @Nonnull
+ public PlayerProfile getPlayerProfile() {
+ return profile;
+ }
+
@ -85,25 +87,24 @@ index 00000000..37e957f4
+}
diff --git a/src/main/java/com/destroystokyo/paper/event/profile/PreLookupProfileEvent.java b/src/main/java/com/destroystokyo/paper/event/profile/PreLookupProfileEvent.java
new file mode 100644
index 00000000..455ffaa1
index 00000000..0a657904
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/PreLookupProfileEvent.java
@@ -0,0 +1,170 @@
@@ -0,0 +1,149 @@
+package com.destroystokyo.paper.event.profile;
+
+import com.destroystokyo.paper.profile.PlayerProfile;
+import com.destroystokyo.paper.profile.ProfileProperty;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+import com.mojang.authlib.GameProfile;
+import com.mojang.authlib.GameProfileRepository;
+import com.mojang.authlib.ProfileLookupCallback;
+import com.mojang.authlib.properties.Property;
+import com.mojang.authlib.properties.PropertyMap;
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
@ -122,11 +123,7 @@ index 00000000..455ffaa1
+ private static final HandlerList handlers = new HandlerList();
+ private final String name;
+ private UUID uuid;
+ /**
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ private PropertyMap properties = new PropertyMap();
+ private Set<ProfileProperty> properties = new HashSet<>();
+
+ public PreLookupProfileEvent(@Nonnull String name) {
+ super(!Bukkit.isPrimaryThread());
@ -156,9 +153,9 @@ index 00000000..455ffaa1
+ /**
+ * Sets the UUID for this player name. This will skip the initial API call to find the players UUID.
+ *
+ * However, if Profile Properties are needed by the server, you must also set them or else an API call will still be made.
+ * However, if Profile Properties are needed by the server, you must also set them or else an API call might still be made.
+ *
+ * @param uuid the UUID to set on the {@link GameProfile} or null to reset
+ * @param uuid the UUID to set for the profile or null to reset
+ */
+ public void setUUID(@Nullable UUID uuid) {
+ this.uuid = uuid;
@ -167,13 +164,18 @@ index 00000000..455ffaa1
+ /**
+ * Get the properties for this profile
+ *
+ * @return the property map to attach to the new {@link GameProfile}
+ * @return the property map to attach to the new {@link PlayerProfile}
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ @Nonnull
+ public Multimap<String, Property> getProperties() {
+ return properties;
+ Multimap<String, Property> props = ArrayListMultimap.create();
+
+ for (ProfileProperty property : properties) {
+ props.put(property.getName(), new Property(property.getName(), property.getValue(), property.getSignature()));
+ }
+ return props;
+ }
+
+ /**
@ -183,8 +185,10 @@ index 00000000..455ffaa1
+ */
+ @Deprecated
+ public void setProperties(Multimap<String, Property> properties) {
+ this.properties = new PropertyMap();
+ this.properties.putAll(properties);
+ this.properties = new HashSet<>();
+ properties.values().forEach(property -> {
+ this.properties.add(new ProfileProperty(property.getName(), property.getValue(), property.getSignature()));
+ });
+ }
+
+ /**
@ -194,7 +198,36 @@ index 00000000..455ffaa1
+ */
+ @Deprecated
+ public void addProperties(Multimap<String, Property> properties) {
+ this.properties.putAll(properties);
+ properties.values().forEach(property -> {
+ this.properties.add(new ProfileProperty(property.getName(), property.getValue(), property.getSignature()));
+ });
+ }
+
+ /**
+ * @return The currently pending prepopulated properties.
+ * Any property in this Set will be automatically prefilled on this Profile
+ */
+ public Set<ProfileProperty> getProfileProperties() {
+ return this.properties;
+ }
+
+ /**
+ * Clears any existing prepopulated properties and uses the supplied properties
+ * Any property in this Set will be automatically prefilled on this Profile
+ * @param properties The properties to add
+ */
+ public void setProfileProperties(Set<ProfileProperty> properties) {
+ this.properties = new HashSet<>();
+ this.properties.addAll(properties);
+ }
+
+ /**
+ * Adds any properties currently missing to the prepopulated properties set, replacing any that already were set.
+ * Any property in this Set will be automatically prefilled on this Profile
+ * @param properties The properties to add
+ */
+ public void addProfileProperties(Set<ProfileProperty> properties) {
+ this.properties.addAll(properties);
+ }
+
+ @Override
@ -206,58 +239,6 @@ index 00000000..455ffaa1
+ return handlers;
+ }
+
+ /**
+ * Wraps the Profile Repository so we can intercept all lookups
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ public static GameProfileRepository wrapProfileRepository(final GameProfileRepository orig) {
+ return (names, agent, callback) -> {
+ Set<String> unfoundNames = Sets.newHashSet();
+ for (String name : names) {
+ PreLookupProfileEvent event = new PreLookupProfileEvent(name);
+ event.callEvent();
+ if (event.getUUID() != null) {
+ // Plugin provided UUI, we can skip network call.
+ GameProfile gameprofile = new GameProfile(event.getUUID(), name);
+ // We might even have properties!
+ gameprofile.getProperties().putAll(event.getProperties());
+ callback.onProfileLookupSucceeded(gameprofile);
+ } else {
+ unfoundNames.add(name);
+ }
+ }
+
+ // Some things were not found.... Proceed to look up.
+ if (!unfoundNames.isEmpty() && orig != null) {
+ String[] namesArr = unfoundNames.toArray(new String[unfoundNames.size()]);
+ orig.findProfilesByNames(namesArr, agent, new PreProfileLookupCallback(callback));
+ }
+ };
+ }
+
+ /**
+ * @deprecated will be removed with 1.13
+ */
+ @Deprecated
+ private static class PreProfileLookupCallback implements ProfileLookupCallback {
+ private final ProfileLookupCallback callback;
+
+ PreProfileLookupCallback(ProfileLookupCallback callback) {
+ this.callback = callback;
+ }
+
+ @Override
+ public void onProfileLookupSucceeded(GameProfile gameProfile) {
+ new LookupProfileEvent(gameProfile).callEvent();
+ callback.onProfileLookupSucceeded(gameProfile);
+ }
+
+ @Override
+ public void onProfileLookupFailed(GameProfile gameProfile, Exception e) {
+ callback.onProfileLookupFailed(gameProfile, e);
+ }
+ }
+}
--
2.15.1