From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Ivan Pekov Date: Wed, 9 Sep 2020 16:14:00 +0300 Subject: [PATCH] ProxyForwardDataEvent diff --git a/src/main/java/net/yatopia/api/event/ProxyForwardDataEvent.java b/src/main/java/net/yatopia/api/event/ProxyForwardDataEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..d3780d965bdd491425a92bf5e554f7def6e0ecf0 --- /dev/null +++ b/src/main/java/net/yatopia/api/event/ProxyForwardDataEvent.java @@ -0,0 +1,125 @@ +package net.yatopia.api.event; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; +import com.mojang.authlib.properties.Property; +import java.util.List; +import java.util.UUID; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Called when a proxy such as BungeeCord or Velocity forwards player data to the server. + */ +public class ProxyForwardDataEvent extends Event { + + private final UUID uuid; + private final String name; + private List properties; + + public ProxyForwardDataEvent(boolean async, @NotNull UUID uuid, @NotNull String name, @NotNull List properties) { + super(async); + this.uuid = uuid; + this.name = name; + this.properties = properties; + } + + /** + * Returns the unique id of the profile forwarded. + * + * @return unique id + */ + @NotNull + public UUID getUuid() { + return uuid; + } + + /** + * Returns the name of the profile forwarded. + * + * @return name + */ + @NotNull + public String getName() { + return name; + } + + /** + * Returns an immutable copy of the forwarded properties. + *

+ * If there were no properties, it would return an empty list. + * + * @return properties + */ + @NotNull + public List getProperties() { + return ImmutableList.copyOf(properties); + } + + /** + * Adds a property to this game profile. If the property already exists, it overrides it. + * + * @param property property + */ + public void addProperty(@NotNull Property property) { + Preconditions.checkNotNull(property, "property"); + if (hasProperty(property.getName())) { + removeProperty(property.getName()); + } + properties.add(property); + } + + /** + * Returns the property with the specified name. + * + * @param property the property's name you want to get + * @return property if present + */ + @Nullable + public Property getProperty(@NotNull String property) { + Preconditions.checkNotNull(property, "property"); + for (Property prop : properties) { + if (prop.getName().equalsIgnoreCase(property)) { + return prop; + } + } + return null; + } + + /** + * Returns whether or not there's a property with the specified name. + * + * @param property the name of the property you want to check if exists + * @return boolean value + */ + public boolean hasProperty(@NotNull String property) { + return getProperty(property) != null; + } + + /** + * Removes the specified property if present + * + * @param property the property's name you want to remove + */ + public void removeProperty(@NotNull String property) { + properties.removeIf(prop -> prop.getName().equalsIgnoreCase(property)); + } + + /** + * Removes the specified property if present + * + * @param property the property you want to remove + */ + public void removeProperty(@NotNull Property property) { + Preconditions.checkNotNull(property, "property"); + properties.remove(property); + } + + // + private static final HandlerList handlers = new HandlerList(); + @NotNull @Override public HandlerList getHandlers() { return handlers; } + @NotNull public static HandlerList getHandlerList() { return handlers; } + // +}