From 546827e94d2587faa6ad84f37acf2245a10839e7 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Sat, 1 Jul 2023 13:56:03 +1000 Subject: [PATCH] SPIGOT-6455, SPIGOT-7030, #1054: Improve ban API By: Doc --- .../bukkit/craftbukkit/CraftIpBanList.java | 76 --------- .../craftbukkit/CraftOfflinePlayer.java | 25 ++- .../craftbukkit/CraftProfileBanList.java | 109 ------------- .../org/bukkit/craftbukkit/CraftServer.java | 35 +++-- .../{ => ban}/CraftIpBanEntry.java | 30 ++-- .../craftbukkit/ban/CraftIpBanList.java | 96 ++++++++++++ .../{ => ban}/CraftProfileBanEntry.java | 31 ++-- .../craftbukkit/ban/CraftProfileBanList.java | 145 ++++++++++++++++++ .../craftbukkit/entity/CraftPlayer.java | 30 +++- 9 files changed, 343 insertions(+), 234 deletions(-) delete mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanList.java delete mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanList.java rename paper-server/src/main/java/org/bukkit/craftbukkit/{ => ban}/CraftIpBanEntry.java (71%) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanList.java rename paper-server/src/main/java/org/bukkit/craftbukkit/{ => ban}/CraftProfileBanEntry.java (70%) create mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanList.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanList.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanList.java deleted file mode 100644 index d66e3c0caf..0000000000 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanList.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.bukkit.craftbukkit; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.Date; -import java.util.Set; -import java.util.logging.Level; -import net.minecraft.server.players.IpBanEntry; -import net.minecraft.server.players.IpBanList; -import org.apache.commons.lang.StringUtils; -import org.bukkit.Bukkit; - -public class CraftIpBanList implements org.bukkit.BanList { - private final IpBanList list; - - public CraftIpBanList(IpBanList list) { - this.list = list; - } - - @Override - public org.bukkit.BanEntry getBanEntry(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - IpBanEntry entry = (IpBanEntry) list.get(target); - if (entry == null) { - return null; - } - - return new CraftIpBanEntry(target, entry, list); - } - - @Override - public org.bukkit.BanEntry addBan(String target, String reason, Date expires, String source) { - Preconditions.checkArgument(target != null, "Ban target cannot be null"); - - IpBanEntry entry = new IpBanEntry(target, new Date(), - StringUtils.isBlank(source) ? null : source, expires, - StringUtils.isBlank(reason) ? null : reason); - - list.add(entry); - - try { - list.save(); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Failed to save banned-ips.json, {0}", ex.getMessage()); - } - - return new CraftIpBanEntry(target, entry, list); - } - - @Override - public Set getBanEntries() { - ImmutableSet.Builder builder = ImmutableSet.builder(); - for (String target : list.getUserList()) { - builder.add(new CraftIpBanEntry(target, (IpBanEntry) list.get(target), list)); - } - - return builder.build(); - } - - @Override - public boolean isBanned(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - return list.isBanned(InetSocketAddress.createUnresolved(target, 0)); - } - - @Override - public void pardon(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - list.remove(target); - } -} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java index 3e462e63ff..05013e6f4d 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java @@ -2,6 +2,7 @@ package org.bukkit.craftbukkit; import com.mojang.authlib.GameProfile; import java.io.File; +import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -12,6 +13,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.server.players.WhiteListEntry; import net.minecraft.stats.ServerStatisticManager; import net.minecraft.world.level.storage.WorldNBTStorage; +import org.bukkit.BanEntry; import org.bukkit.BanList; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -19,6 +21,7 @@ import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.Statistic; +import org.bukkit.ban.ProfileBanList; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.craftbukkit.entity.memory.CraftMemoryMapper; @@ -104,22 +107,19 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa @Override public boolean isBanned() { - if (getName() == null) { - return false; - } + return ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).isBanned(getPlayerProfile()); + } - return server.getBanList(BanList.Type.NAME).isBanned(getName()); + @Override + public BanEntry ban(String reason, Date expires, String source) { + return ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).addBan(getPlayerProfile(), reason, expires, source); } public void setBanned(boolean value) { - if (getName() == null) { - return; - } - if (value) { - server.getBanList(BanList.Type.NAME).addBan(getName(), null, null, null); + ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).addBan(getPlayerProfile(), null, null, null); } else { - server.getBanList(BanList.Type.NAME).pardon(getName()); + ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).pardon(getPlayerProfile()); } } @@ -139,7 +139,7 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa @Override public Map serialize() { - Map result = new LinkedHashMap(); + Map result = new LinkedHashMap<>(); result.put("UUID", profile.getId().toString()); @@ -167,11 +167,10 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof OfflinePlayer)) { + if (!(obj instanceof OfflinePlayer other)) { return false; } - OfflinePlayer other = (OfflinePlayer) obj; if ((this.getUniqueId() == null) || (other.getUniqueId() == null)) { return false; } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanList.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanList.java deleted file mode 100644 index 7b68e25575..0000000000 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanList.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.bukkit.craftbukkit; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import com.mojang.authlib.GameProfile; -import java.io.IOException; -import java.util.Date; -import java.util.Set; -import java.util.UUID; -import java.util.logging.Level; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.players.GameProfileBanEntry; -import net.minecraft.server.players.GameProfileBanList; -import net.minecraft.server.players.JsonListEntry; -import org.apache.commons.lang.StringUtils; -import org.bukkit.Bukkit; - -public class CraftProfileBanList implements org.bukkit.BanList { - private final GameProfileBanList list; - - public CraftProfileBanList(GameProfileBanList list) { - this.list = list; - } - - @Override - public org.bukkit.BanEntry getBanEntry(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - GameProfile profile = getProfile(target); - if (profile == null) { - return null; - } - - GameProfileBanEntry entry = (GameProfileBanEntry) list.get(profile); - if (entry == null) { - return null; - } - - return new CraftProfileBanEntry(profile, entry, list); - } - - @Override - public org.bukkit.BanEntry addBan(String target, String reason, Date expires, String source) { - Preconditions.checkArgument(target != null, "Ban target cannot be null"); - - GameProfile profile = getProfile(target); - if (profile == null) { - return null; - } - - GameProfileBanEntry entry = new GameProfileBanEntry(profile, new Date(), - StringUtils.isBlank(source) ? null : source, expires, - StringUtils.isBlank(reason) ? null : reason); - - list.add(entry); - - try { - list.save(); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Failed to save banned-players.json, {0}", ex.getMessage()); - } - - return new CraftProfileBanEntry(profile, entry, list); - } - - @Override - public Set getBanEntries() { - ImmutableSet.Builder builder = ImmutableSet.builder(); - - for (JsonListEntry entry : list.getEntries()) { - GameProfile profile = (GameProfile) entry.getUser(); - builder.add(new CraftProfileBanEntry(profile, (GameProfileBanEntry) entry, list)); - } - - return builder.build(); - } - - @Override - public boolean isBanned(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - GameProfile profile = getProfile(target); - if (profile == null) { - return false; - } - - return list.isBanned(profile); - } - - @Override - public void pardon(String target) { - Preconditions.checkArgument(target != null, "Target cannot be null"); - - GameProfile profile = getProfile(target); - list.remove(profile); - } - - private GameProfile getProfile(String target) { - UUID uuid = null; - - try { - uuid = UUID.fromString(target); - } catch (IllegalArgumentException ex) { - // - } - - return ((uuid != null) ? MinecraftServer.getServer().getProfileCache().get(uuid) : MinecraftServer.getServer().getProfileCache().get(target)).orElse(null); - } -} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 904035b38f..24aaa290da 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -23,6 +23,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -33,7 +34,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -150,6 +150,8 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.conversations.Conversable; +import org.bukkit.craftbukkit.ban.CraftIpBanList; +import org.bukkit.craftbukkit.ban.CraftProfileBanList; import org.bukkit.craftbukkit.block.data.CraftBlockData; import org.bukkit.craftbukkit.boss.CraftBossBar; import org.bukkit.craftbukkit.boss.CraftKeyedBossbar; @@ -1706,18 +1708,32 @@ public final class CraftServer implements Server { @Override public void banIP(String address) { - Preconditions.checkArgument(address != null, "address cannot be null"); + Preconditions.checkArgument(address != null && !address.isBlank(), "Address cannot be null or blank."); this.getBanList(org.bukkit.BanList.Type.IP).addBan(address, null, null, null); } @Override public void unbanIP(String address) { - Preconditions.checkArgument(address != null, "address cannot be null"); + Preconditions.checkArgument(address != null && !address.isBlank(), "Address cannot be null or blank."); this.getBanList(org.bukkit.BanList.Type.IP).pardon(address); } + @Override + public void banIP(InetSocketAddress address) { + Preconditions.checkArgument(address != null, "Address cannot be null."); + + ((CraftIpBanList) this.getBanList(BanList.Type.IP)).addBan(address, null, null, null); + } + + @Override + public void unbanIP(InetSocketAddress address) { + Preconditions.checkArgument(address != null, "Address cannot be null."); + + ((CraftIpBanList) this.getBanList(BanList.Type.IP)).pardon(address); + } + @Override public Set getBannedPlayers() { Set result = new HashSet(); @@ -1730,16 +1746,13 @@ public final class CraftServer implements Server { } @Override - public BanList getBanList(BanList.Type type) { + public > T getBanList(BanList.Type type) { Preconditions.checkArgument(type != null, "BanList.Type cannot be null"); - switch (type) { - case IP: - return new CraftIpBanList(playerList.getIpBans()); - case NAME: - default: - return new CraftProfileBanList(playerList.getBans()); - } + return switch (type) { + case IP -> (T) new CraftIpBanList(this.playerList.getIpBans()); + case PROFILE, NAME -> (T) new CraftProfileBanList(this.playerList.getBans()); + }; } @Override diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanEntry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanEntry.java similarity index 71% rename from paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanEntry.java rename to paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanEntry.java index e2f3f8ec97..7fc94112ba 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftIpBanEntry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanEntry.java @@ -1,13 +1,14 @@ -package org.bukkit.craftbukkit; +package org.bukkit.craftbukkit.ban; -import java.io.IOException; +import java.net.InetSocketAddress; +import java.time.Instant; import java.util.Date; -import java.util.logging.Level; import net.minecraft.server.players.IpBanEntry; import net.minecraft.server.players.IpBanList; -import org.bukkit.Bukkit; +import org.bukkit.BanEntry; -public final class CraftIpBanEntry implements org.bukkit.BanEntry { +public final class CraftIpBanEntry implements BanEntry { + private static final Date minorDate = Date.from(Instant.parse("1899-12-31T04:00:00Z")); private final IpBanList list; private final String target; private Date created; @@ -29,6 +30,11 @@ public final class CraftIpBanEntry implements org.bukkit.BanEntry { return this.target; } + @Override + public InetSocketAddress getBanTarget() { + return new InetSocketAddress(this.target, 0); + } + @Override public Date getCreated() { return this.created == null ? null : (Date) this.created.clone(); @@ -56,7 +62,7 @@ public final class CraftIpBanEntry implements org.bukkit.BanEntry { @Override public void setExpiration(Date expiration) { - if (expiration != null && expiration.getTime() == new Date(0, 0, 0, 0, 0, 0).getTime()) { + if (expiration != null && expiration.getTime() == minorDate.getTime()) { expiration = null; // Forces "forever" } @@ -75,12 +81,12 @@ public final class CraftIpBanEntry implements org.bukkit.BanEntry { @Override public void save() { - IpBanEntry entry = new IpBanEntry(target, this.created, this.source, this.expiration, this.reason); + IpBanEntry entry = new IpBanEntry(this.target, this.created, this.source, this.expiration, this.reason); this.list.add(entry); - try { - this.list.save(); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Failed to save banned-ips.json, {0}", ex.getMessage()); - } + } + + @Override + public void remove() { + this.list.remove(target); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanList.java b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanList.java new file mode 100644 index 0000000000..dbc8d7a861 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftIpBanList.java @@ -0,0 +1,96 @@ +package org.bukkit.craftbukkit.ban; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.net.InetAddresses; +import java.net.InetSocketAddress; +import java.util.Date; +import java.util.Set; +import net.minecraft.server.players.IpBanEntry; +import net.minecraft.server.players.IpBanList; +import org.bukkit.BanEntry; + +public class CraftIpBanList implements org.bukkit.ban.IpBanList { + private final IpBanList list; + + public CraftIpBanList(IpBanList list) { + this.list = list; + } + + @Override + public BanEntry getBanEntry(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + IpBanEntry entry = this.list.get(target); + if (entry == null) { + return null; + } + + return new CraftIpBanEntry(target, entry, list); + } + + @Override + public BanEntry getBanEntry(InetSocketAddress target) { + return this.getBanEntry(this.getIpFromAddress(target)); + } + + @Override + public BanEntry addBan(String target, String reason, Date expires, String source) { + Preconditions.checkArgument(target != null, "Ban target cannot be null"); + + IpBanEntry entry = new IpBanEntry(target, new Date(), + (source == null || source.isBlank()) ? null : source, expires, + (reason == null || reason.isBlank()) ? null : reason); + + this.list.add(entry); + + return new CraftIpBanEntry(target, entry, list); + } + + @Override + public BanEntry addBan(InetSocketAddress target, String reason, Date expires, String source) { + return this.addBan(this.getIpFromAddress(target), reason, expires, source); + } + + @Override + public Set> getBanEntries() { + ImmutableSet.Builder> builder = ImmutableSet.builder(); + for (String target : list.getUserList()) { + IpBanEntry ipBanEntry = list.get(target); + if (ipBanEntry != null) { + builder.add(new CraftIpBanEntry(target, ipBanEntry, list)); + } + } + return builder.build(); + } + + @Override + public boolean isBanned(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + return this.list.isBanned(target); + } + + @Override + public boolean isBanned(InetSocketAddress target) { + return this.isBanned(getIpFromAddress(target)); + } + + @Override + public void pardon(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + this.list.remove(target); + } + + @Override + public void pardon(InetSocketAddress target) { + this.pardon(getIpFromAddress(target)); + } + + private String getIpFromAddress(InetSocketAddress address) { + if (address == null) { + return null; + } + Preconditions.checkArgument(!address.isUnresolved(), "%s its not a valid address", address); + return InetAddresses.toAddrString(address.getAddress()); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanEntry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanEntry.java similarity index 70% rename from paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanEntry.java rename to paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanEntry.java index 37b487bd9e..6cc8990ee0 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftProfileBanEntry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanEntry.java @@ -1,14 +1,16 @@ -package org.bukkit.craftbukkit; +package org.bukkit.craftbukkit.ban; import com.mojang.authlib.GameProfile; -import java.io.IOException; +import java.time.Instant; import java.util.Date; -import java.util.logging.Level; import net.minecraft.server.players.GameProfileBanEntry; import net.minecraft.server.players.GameProfileBanList; -import org.bukkit.Bukkit; +import org.bukkit.BanEntry; +import org.bukkit.craftbukkit.profile.CraftPlayerProfile; +import org.bukkit.profile.PlayerProfile; -public final class CraftProfileBanEntry implements org.bukkit.BanEntry { +public final class CraftProfileBanEntry implements BanEntry { + private static final Date minorDate = Date.from(Instant.parse("1899-12-31T04:00:00Z")); private final GameProfileBanList list; private final GameProfile profile; private Date created; @@ -30,6 +32,11 @@ public final class CraftProfileBanEntry implements org.bukkit.BanEntry { return this.profile.getName(); } + @Override + public PlayerProfile getBanTarget() { + return new CraftPlayerProfile(this.profile); + } + @Override public Date getCreated() { return this.created == null ? null : (Date) this.created.clone(); @@ -57,7 +64,7 @@ public final class CraftProfileBanEntry implements org.bukkit.BanEntry { @Override public void setExpiration(Date expiration) { - if (expiration != null && expiration.getTime() == new Date(0, 0, 0, 0, 0, 0).getTime()) { + if (expiration != null && expiration.getTime() == minorDate.getTime()) { expiration = null; // Forces "forever" } @@ -76,12 +83,12 @@ public final class CraftProfileBanEntry implements org.bukkit.BanEntry { @Override public void save() { - GameProfileBanEntry entry = new GameProfileBanEntry(profile, this.created, this.source, this.expiration, this.reason); + GameProfileBanEntry entry = new GameProfileBanEntry(this.profile, this.created, this.source, this.expiration, this.reason); this.list.add(entry); - try { - this.list.save(); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Failed to save banned-players.json, {0}", ex.getMessage()); - } + } + + @Override + public void remove() { + this.list.remove(this.profile); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanList.java b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanList.java new file mode 100644 index 0000000000..d8cac25b50 --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/ban/CraftProfileBanList.java @@ -0,0 +1,145 @@ +package org.bukkit.craftbukkit.ban; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.mojang.authlib.GameProfile; +import java.util.Date; +import java.util.Set; +import java.util.UUID; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.players.GameProfileBanEntry; +import net.minecraft.server.players.GameProfileBanList; +import org.bukkit.BanEntry; +import org.bukkit.ban.ProfileBanList; +import org.bukkit.craftbukkit.profile.CraftPlayerProfile; +import org.bukkit.profile.PlayerProfile; + +public class CraftProfileBanList implements ProfileBanList { + private final GameProfileBanList list; + + public CraftProfileBanList(GameProfileBanList list) { + this.list = list; + } + + @Override + public BanEntry getBanEntry(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + return this.getBanEntry(getProfile(target)); + } + + @Override + public BanEntry getBanEntry(PlayerProfile target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + return this.getBanEntry(((CraftPlayerProfile) target).buildGameProfile()); + } + + @Override + public BanEntry addBan(String target, String reason, Date expires, String source) { + Preconditions.checkArgument(target != null, "Ban target cannot be null"); + + return this.addBan(getProfileByName(target), reason, expires, source); + } + + @Override + public BanEntry addBan(PlayerProfile target, String reason, Date expires, String source) { + Preconditions.checkArgument(target != null, "PlayerProfile cannot be null"); + Preconditions.checkArgument(target.getUniqueId() != null, "The PlayerProfile UUID cannot be null"); + + return this.addBan(((CraftPlayerProfile) target).buildGameProfile(), reason, expires, source); + } + + @Override + public Set> getBanEntries() { + ImmutableSet.Builder> builder = ImmutableSet.builder(); + for (GameProfileBanEntry entry : list.getEntries()) { + GameProfile profile = entry.getUser(); + builder.add(new CraftProfileBanEntry(profile, entry, list)); + } + + return builder.build(); + } + + @Override + public boolean isBanned(PlayerProfile target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + return this.isBanned(((CraftPlayerProfile) target).buildGameProfile()); + } + + @Override + public boolean isBanned(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + return this.isBanned(getProfile(target)); + } + + @Override + public void pardon(PlayerProfile target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + this.pardon(((CraftPlayerProfile) target).buildGameProfile()); + } + + @Override + public void pardon(String target) { + Preconditions.checkArgument(target != null, "Target cannot be null"); + + this.pardon(getProfile(target)); + } + + public BanEntry getBanEntry(GameProfile profile) { + if (profile == null) { + return null; + } + + GameProfileBanEntry entry = list.get(profile); + if (entry == null) { + return null; + } + + return new CraftProfileBanEntry(profile, entry, list); + } + + public BanEntry addBan(GameProfile profile, String reason, Date expires, String source) { + if (profile == null) { + return null; + } + + GameProfileBanEntry entry = new GameProfileBanEntry(profile, new Date(), + (source == null || source.isBlank()) ? null : source, expires, + (reason == null || reason.isBlank()) ? null : reason); + + list.add(entry); + + return new CraftProfileBanEntry(profile, entry, list); + } + + private void pardon(GameProfile profile) { + list.remove(profile); + } + + private boolean isBanned(GameProfile profile) { + return profile != null && list.isBanned(profile); + } + + static GameProfile getProfile(String target) { + UUID uuid = null; + + try { + uuid = UUID.fromString(target); + } catch (IllegalArgumentException ignored) { + } + + return (uuid != null) ? getProfileByUUID(uuid) : getProfileByName(target); + } + + static GameProfile getProfileByUUID(UUID uuid) { + return (MinecraftServer.getServer() != null) ? MinecraftServer.getServer().getProfileCache().get(uuid).orElse(null) : null; + } + + static GameProfile getProfileByName(String name) { + return (MinecraftServer.getServer() != null) ? MinecraftServer.getServer().getProfileCache().get(name).orElse(null) : null; + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 3dfcd56766..2576596f5c 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -15,6 +15,7 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -96,6 +97,7 @@ import net.minecraft.world.level.border.IWorldBorderListener; import net.minecraft.world.level.saveddata.maps.MapIcon; import net.minecraft.world.level.saveddata.maps.WorldMap; import net.minecraft.world.phys.Vec3D; +import org.bukkit.BanEntry; import org.bukkit.BanList; import org.bukkit.Bukkit; import org.bukkit.DyeColor; @@ -112,6 +114,8 @@ import org.bukkit.Sound; import org.bukkit.Statistic; import org.bukkit.WeatherType; import org.bukkit.WorldBorder; +import org.bukkit.ban.IpBanList; +import org.bukkit.ban.ProfileBanList; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; @@ -1186,7 +1190,31 @@ public class CraftPlayer extends CraftHumanEntity implements Player { @Override public boolean isBanned() { - return server.getBanList(BanList.Type.NAME).isBanned(getName()); + return ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).isBanned(getPlayerProfile()); + } + + @Override + public BanEntry ban(String reason, Date expires, String source) { + return this.ban(reason, expires, source, true); + } + + @Override + public BanEntry ban(String reason, Date expires, String source, boolean kickPlayer) { + BanEntry banEntry = ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).addBan(getPlayerProfile(), reason, expires, source); + if (kickPlayer) { + this.kickPlayer(reason); + } + return banEntry; + } + + @Override + public BanEntry banIp(String reason, Date expires, String source, boolean kickPlayer) { + Preconditions.checkArgument(getAddress() != null, "The Address of this Player is null"); + BanEntry banEntry = ((IpBanList) server.getBanList(BanList.Type.IP)).addBan(getAddress(), reason, expires, source); + if (kickPlayer) { + this.kickPlayer(reason); + } + return banEntry; } @Override