3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-15 04:20:04 +01:00
Paper/patches/api/0061-Profile-Lookup-Events.patch

173 Zeilen
5.9 KiB
Diff

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 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
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
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/LookupProfileEvent.java
@@ -0,0 +1,45 @@
2021-06-11 14:02:28 +02:00
+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;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * 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.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * No guarantees are made about thread execution context for this event. If you need to know, check
2024-02-01 10:15:57 +01:00
+ * {@link Event#isAsynchronous()}
2021-06-11 14:02:28 +02:00
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class LookupProfileEvent extends Event {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
2021-06-11 14:02:28 +02:00
+
+ private final PlayerProfile profile;
2021-06-11 14:02:28 +02:00
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public LookupProfileEvent(final PlayerProfile profile) {
2021-06-11 14:02:28 +02:00
+ super(!Bukkit.isPrimaryThread());
+ this.profile = profile;
+ }
+
+ /**
+ * @return The profile that was recently looked up. This profile can be mutated
+ */
+ public PlayerProfile getPlayerProfile() {
2024-02-01 10:15:57 +01:00
+ return this.profile;
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}
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
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/profile/PreLookupProfileEvent.java
@@ -0,0 +1,107 @@
2021-06-11 14:02:28 +02:00
+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;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Allows a plugin to intercept a Profile Lookup for a Profile by name
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * At the point of event fire, the UUID and properties are unset.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * If a plugin sets the UUID, and optionally the properties, the API call to look up the profile may be skipped.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * No guarantees are made about thread execution context for this event. If you need to know, check
2024-02-01 10:15:57 +01:00
+ * {@link Event#isAsynchronous()}
2021-06-11 14:02:28 +02:00
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PreLookupProfileEvent extends Event {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final String name;
2024-02-01 10:15:57 +01:00
+
+ private @Nullable UUID uuid;
+ private Set<ProfileProperty> properties = new HashSet<>();
2021-06-11 14:02:28 +02:00
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public PreLookupProfileEvent(final String name) {
2021-06-11 14:02:28 +02:00
+ super(!Bukkit.isPrimaryThread());
+ this.name = name;
+ }
+
+ /**
+ * @return Name of the profile
+ */
+ public String getName() {
2024-02-01 10:15:57 +01:00
+ return this.name;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * If this value is left {@code null} by the completion of the event call, then the server will
2021-06-11 14:02:28 +02:00
+ * 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() {
2024-02-01 10:15:57 +01:00
+ return this.uuid;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the UUID for this player name. This will skip the initial API call to find the players UUID.
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * However, if Profile Properties are needed by the server, you must also set them or else an API call might still be made.
+ *
2024-02-01 10:15:57 +01:00
+ * @param uuid the UUID to set for the profile or {@code null} to reset
2021-06-11 14:02:28 +02:00
+ */
+ public void setUUID(final @Nullable UUID uuid) {
2021-06-11 14:02:28 +02:00
+ this.uuid = uuid;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * @return The currently pending pre-populated properties.
2021-06-11 14:02:28 +02:00
+ * Any property in this Set will be automatically prefilled on this Profile
+ */
+ public Set<ProfileProperty> getProfileProperties() {
+ return this.properties;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Clears any existing pre-populated properties and uses the supplied properties
2021-06-11 14:02:28 +02:00
+ * Any property in this Set will be automatically prefilled on this Profile
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @param properties The properties to add
+ */
+ public void setProfileProperties(final Set<ProfileProperty> properties) {
2021-06-11 14:02:28 +02:00
+ this.properties = new HashSet<>();
+ this.properties.addAll(properties);
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Adds any properties currently missing to the pre-populated properties set, replacing any that already were set.
2021-06-11 14:02:28 +02:00
+ * Any property in this Set will be automatically prefilled on this Profile
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @param properties The properties to add
+ */
+ public void addProfileProperties(final Set<ProfileProperty> properties) {
2021-06-11 14:02:28 +02:00
+ this.properties.addAll(properties);
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+}