improve BanList types (#10239)

This commit is contained in:
Yannick Lamprecht 2024-03-03 21:36:17 +01:00 committed by GitHub
parent 8870d22bc7
commit bbc03d8116
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 163 additions and 0 deletions

View file

@ -0,0 +1,131 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Yannick Lamprecht <yannicklamprecht@live.de>
Date: Sat, 10 Feb 2024 20:49:47 +0100
Subject: [PATCH] improve BanList types
diff --git a/src/main/java/io/papermc/paper/ban/BanListType.java b/src/main/java/io/papermc/paper/ban/BanListType.java
new file mode 100644
index 0000000000000000000000000000000000000000..2980abf2f41cb14f0ee5c829c365f8e304130618
--- /dev/null
+++ b/src/main/java/io/papermc/paper/ban/BanListType.java
@@ -0,0 +1,29 @@
+package io.papermc.paper.ban;
+
+import org.bukkit.BanList;
+import org.bukkit.ban.IpBanList;
+import org.bukkit.ban.ProfileBanList;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Represents a ban-type that a {@link BanList} may track.
+ * It enforces the correct return value at compile time.
+ */
+public interface BanListType<T> {
+
+ /**
+ * Banned IP addresses
+ */
+ BanListType<IpBanList> IP = new BanListTypeImpl<>(IpBanList.class);
+ /**
+ * Banned player profiles
+ */
+ BanListType<ProfileBanList> PROFILE = new BanListTypeImpl<>(ProfileBanList.class);
+
+ /**
+ * Returns the type class of the ban list used generically
+ *
+ * @return the type class
+ */
+ @NotNull Class<T> typeClass();
+}
diff --git a/src/main/java/io/papermc/paper/ban/BanListTypeImpl.java b/src/main/java/io/papermc/paper/ban/BanListTypeImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..1159e7cd29fbf11f3fa1448fcf9d0768e1bcb0a3
--- /dev/null
+++ b/src/main/java/io/papermc/paper/ban/BanListTypeImpl.java
@@ -0,0 +1,8 @@
+package io.papermc.paper.ban;
+
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+@ApiStatus.Internal
+record BanListTypeImpl<T>(@NotNull Class<T> typeClass) implements BanListType<T> {
+}
diff --git a/src/main/java/org/bukkit/BanList.java b/src/main/java/org/bukkit/BanList.java
index a77c0411a68a9bad33ddfb335b7a996a843e478c..0e0baadde9e34d28db56dc68e66aaf66c60d12df 100644
--- a/src/main/java/org/bukkit/BanList.java
+++ b/src/main/java/org/bukkit/BanList.java
@@ -16,7 +16,9 @@ public interface BanList<T> {
/**
* Represents a ban-type that a {@link BanList} may track.
+ * @deprecated use {@link io.papermc.paper.ban.BanListType} to enforce the correct return value at compile time.
*/
+ @Deprecated // Paper - BanList Type Improvements
public enum Type {
/**
* Banned player names
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index ca27559cf4aa1c2e44fdca2022e213b1b1c80f4e..db46e3233edf18bd91a6da8612ccc9f0080d5c17 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -1633,11 +1633,27 @@ public final class Bukkit {
* @param <T> The ban target
*
* @return a ban list of the specified type
+ * @deprecated use {@link #getBanList(io.papermc.paper.ban.BanListType)} to enforce the correct return value at compile time.
*/
@NotNull
+ @Deprecated // Paper - add BanListType (which has a generic)
public static <T extends BanList<?>> T getBanList(@NotNull BanList.Type type) {
return server.getBanList(type);
}
+ // Paper start - add BanListType (which has a generic)
+ /**
+ * Gets a ban list for the supplied type.
+ *
+ * @param type the type of list to fetch, cannot be null
+ * @param <B> The ban target
+ *
+ * @return a ban list of the specified type
+ */
+ @NotNull
+ public static <B extends BanList<E>, E> B getBanList(final io.papermc.paper.ban.@NotNull BanListType<B> type) {
+ return server.getBanList(type);
+ }
+ // Paper end - add BanListType (which has a generic)
/**
* Gets a set containing all player operators.
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index b4f8281d3797ec825a7671f38077cd65d5a1d76e..0eb61b090d6f8fa6d99735ff3680dac774c52c1f 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -1394,10 +1394,25 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
* @param <T> The ban target
*
* @return a ban list of the specified type
+ * @deprecated use {@link #getBanList(io.papermc.paper.ban.BanListType)} to enforce the correct return value at compile time.
*/
+ @Deprecated // Paper - add BanListType (which has a generic)
@NotNull
public <T extends BanList<?>> T getBanList(@NotNull BanList.Type type);
+ // Paper start - add BanListType (which has a generic)
+ /**
+ * Gets a ban list for the supplied type.
+ *
+ * @param type the type of list to fetch, cannot be null
+ * @param <B> The ban target
+ *
+ * @return a ban list of the specified type
+ */
+ @NotNull
+ <B extends BanList<E>, E> B getBanList(@NotNull io.papermc.paper.ban.BanListType<B> type);
+ // Paper end - add BanListType (which has a generic)
+
/**
* Gets a set containing all player operators.
*

View file

@ -0,0 +1,32 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Yannick Lamprecht <yannicklamprecht@live.de>
Date: Sat, 10 Feb 2024 20:50:01 +0100
Subject: [PATCH] improve BanList types
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 9f7ed337463cc9bb370a5541d9de5cd8f9c1a78a..c490a29bcf7410bc54959ee71375605964379ed5 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -2215,6 +2215,21 @@ public final class CraftServer implements Server {
};
}
+ // Paper start - add BanListType (which has a generic)
+ @SuppressWarnings("unchecked")
+ @Override
+ public <B extends BanList<E>, E> B getBanList(final io.papermc.paper.ban.BanListType<B> type) {
+ Preconditions.checkArgument(type != null, "BanList.BanType cannot be null");
+ if (type == io.papermc.paper.ban.BanListType.IP) {
+ return (B) new CraftIpBanList(this.playerList.getIpBans());
+ } else if (type == io.papermc.paper.ban.BanListType.PROFILE) {
+ return (B) new CraftProfileBanList(this.playerList.getBans());
+ } else {
+ throw new IllegalArgumentException("Unknown BanListType: " + type);
+ }
+ }
+ // Paper end - add BanListType (which has a generic)
+
@Override
public void setWhitelist(boolean value) {
this.playerList.setUsingWhiteList(value);