mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-06 08:17:44 +01:00
SPIGOT-6455, SPIGOT-7030, #1054: Improve ban API
By: Doc <nachito94@msn.com>
This commit is contained in:
parent
5c8c4bbe5b
commit
546827e94d
@ -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<org.bukkit.BanEntry> getBanEntries() {
|
||||
ImmutableSet.Builder<org.bukkit.BanEntry> 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);
|
||||
}
|
||||
}
|
@ -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<PlayerProfile> 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<String, Object> serialize() {
|
||||
Map<String, Object> result = new LinkedHashMap<String, Object>();
|
||||
Map<String, Object> 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;
|
||||
}
|
||||
|
@ -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<org.bukkit.BanEntry> getBanEntries() {
|
||||
ImmutableSet.Builder<org.bukkit.BanEntry> 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);
|
||||
}
|
||||
}
|
@ -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<OfflinePlayer> getBannedPlayers() {
|
||||
Set<OfflinePlayer> result = new HashSet<OfflinePlayer>();
|
||||
@ -1730,16 +1746,13 @@ public final class CraftServer implements Server {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanList getBanList(BanList.Type type) {
|
||||
public <T extends BanList<?>> 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
|
||||
|
@ -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<InetSocketAddress> {
|
||||
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);
|
||||
}
|
||||
}
|
@ -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<InetSocketAddress> 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<InetSocketAddress> getBanEntry(InetSocketAddress target) {
|
||||
return this.getBanEntry(this.getIpFromAddress(target));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanEntry<InetSocketAddress> 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<InetSocketAddress> addBan(InetSocketAddress target, String reason, Date expires, String source) {
|
||||
return this.addBan(this.getIpFromAddress(target), reason, expires, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<BanEntry<InetSocketAddress>> getBanEntries() {
|
||||
ImmutableSet.Builder<BanEntry<InetSocketAddress>> 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());
|
||||
}
|
||||
}
|
@ -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<PlayerProfile> {
|
||||
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);
|
||||
}
|
||||
}
|
@ -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<PlayerProfile> getBanEntry(String target) {
|
||||
Preconditions.checkArgument(target != null, "Target cannot be null");
|
||||
|
||||
return this.getBanEntry(getProfile(target));
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanEntry<PlayerProfile> getBanEntry(PlayerProfile target) {
|
||||
Preconditions.checkArgument(target != null, "Target cannot be null");
|
||||
|
||||
return this.getBanEntry(((CraftPlayerProfile) target).buildGameProfile());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanEntry<PlayerProfile> 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<PlayerProfile> 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<BanEntry<PlayerProfile>> getBanEntries() {
|
||||
ImmutableSet.Builder<BanEntry<PlayerProfile>> 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<PlayerProfile> 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<PlayerProfile> 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;
|
||||
}
|
||||
}
|
@ -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<PlayerProfile> ban(String reason, Date expires, String source) {
|
||||
return this.ban(reason, expires, source, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanEntry<PlayerProfile> ban(String reason, Date expires, String source, boolean kickPlayer) {
|
||||
BanEntry<PlayerProfile> banEntry = ((ProfileBanList) server.getBanList(BanList.Type.PROFILE)).addBan(getPlayerProfile(), reason, expires, source);
|
||||
if (kickPlayer) {
|
||||
this.kickPlayer(reason);
|
||||
}
|
||||
return banEntry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BanEntry<InetSocketAddress> banIp(String reason, Date expires, String source, boolean kickPlayer) {
|
||||
Preconditions.checkArgument(getAddress() != null, "The Address of this Player is null");
|
||||
BanEntry<InetSocketAddress> banEntry = ((IpBanList) server.getBanList(BanList.Type.IP)).addBan(getAddress(), reason, expires, source);
|
||||
if (kickPlayer) {
|
||||
this.kickPlayer(reason);
|
||||
}
|
||||
return banEntry;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user