forked from PaperMC/Paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0061-Profile-Lookup-Events.patch
172 lines (170 loc) · 5.87 KB
/
0061-Profile-Lookup-Events.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <[email protected]>
Date: Sat, 17 Jun 2017 16:30:44 -0400
Subject: [PATCH] Profile Lookup Events
Adds a Pre Lookup Event and a Post Lookup Event so that plugins may prefill in profile data, and cache the responses from
profiles that had to be looked up.
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 0000000000000000000000000000000000000000..9e8ae0ab13cac9a260c9959eb6bf5b93a3c15018
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/LookupProfileEvent.java
@@ -0,0 +1,45 @@
+package com.destroystokyo.paper.event.profile;
+
+import com.destroystokyo.paper.profile.PlayerProfile;
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+
+/**
+ * Allows a plugin to be notified anytime AFTER a Profile has been looked up from the Mojang API
+ * This is an opportunity to view the response and potentially cache things.
+ * <p>
+ * No guarantees are made about thread execution context for this event. If you need to know, check
+ * {@link Event#isAsynchronous()}
+ */
+@NullMarked
+public class LookupProfileEvent extends Event {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final PlayerProfile profile;
+
+ @ApiStatus.Internal
+ public LookupProfileEvent(final PlayerProfile profile) {
+ super(!Bukkit.isPrimaryThread());
+ this.profile = profile;
+ }
+
+ /**
+ * @return The profile that was recently looked up. This profile can be mutated
+ */
+ public PlayerProfile getPlayerProfile() {
+ return this.profile;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+}
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 0000000000000000000000000000000000000000..07416cc9e2b8156be2cc92d6d974b881b427fd99
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/PreLookupProfileEvent.java
@@ -0,0 +1,107 @@
+package com.destroystokyo.paper.event.profile;
+
+import com.destroystokyo.paper.profile.ProfileProperty;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
+
+/**
+ * Allows a plugin to intercept a Profile Lookup for a Profile by name
+ * <p>
+ * At the point of event fire, the UUID and properties are unset.
+ * <p>
+ * If a plugin sets the UUID, and optionally the properties, the API call to look up the profile may be skipped.
+ * <p>
+ * No guarantees are made about thread execution context for this event. If you need to know, check
+ * {@link Event#isAsynchronous()}
+ */
+@NullMarked
+public class PreLookupProfileEvent extends Event {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final String name;
+
+ private @Nullable UUID uuid;
+ private Set<ProfileProperty> properties = new HashSet<>();
+
+ @ApiStatus.Internal
+ public PreLookupProfileEvent(final String name) {
+ super(!Bukkit.isPrimaryThread());
+ this.name = name;
+ }
+
+ /**
+ * @return Name of the profile
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * If this value is left {@code null} by the completion of the event call, then the server will
+ * trigger a call to the Mojang API to look up the UUID (Network Request), and subsequently, fire a
+ * {@link LookupProfileEvent}
+ *
+ * @return The UUID of the profile if it has already been provided by a plugin
+ */
+ public @Nullable UUID getUUID() {
+ return this.uuid;
+ }
+
+ /**
+ * Sets the UUID for this player name. This will skip the initial API call to find the players UUID.
+ * <p>
+ * 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 for the profile or {@code null} to reset
+ */
+ public void setUUID(final @Nullable UUID uuid) {
+ this.uuid = uuid;
+ }
+
+ /**
+ * @return The currently pending pre-populated properties.
+ * Any property in this Set will be automatically prefilled on this Profile
+ */
+ public Set<ProfileProperty> getProfileProperties() {
+ return this.properties;
+ }
+
+ /**
+ * Clears any existing pre-populated 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(final Set<ProfileProperty> properties) {
+ this.properties = new HashSet<>();
+ this.properties.addAll(properties);
+ }
+
+ /**
+ * Adds any properties currently missing to the pre-populated 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(final Set<ProfileProperty> properties) {
+ this.properties.addAll(properties);
+ }
+
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+
+}