SPIGOT-6455, SPIGOT-7030, #1054: Improve ban API
By: Doc <nachito94@msn.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package org.bukkit.craftbukkit.ban;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import net.minecraft.server.players.IpBanEntry;
|
||||
import net.minecraft.server.players.IpBanList;
|
||||
import 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;
|
||||
private String source;
|
||||
private Date expiration;
|
||||
private String reason;
|
||||
|
||||
public CraftIpBanEntry(String target, IpBanEntry entry, IpBanList list) {
|
||||
this.list = list;
|
||||
this.target = target;
|
||||
this.created = entry.getCreated() != null ? new Date(entry.getCreated().getTime()) : null;
|
||||
this.source = entry.getSource();
|
||||
this.expiration = entry.getExpires() != null ? new Date(entry.getExpires().getTime()) : null;
|
||||
this.reason = entry.getReason();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTarget() {
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getExpiration() {
|
||||
return this.expiration == null ? null : (Date) this.expiration.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExpiration(Date expiration) {
|
||||
if (expiration != null && expiration.getTime() == minorDate.getTime()) {
|
||||
expiration = null; // Forces "forever"
|
||||
}
|
||||
|
||||
this.expiration = expiration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReason() {
|
||||
return this.reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReason(String reason) {
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
IpBanEntry entry = new IpBanEntry(this.target, this.created, this.source, this.expiration, this.reason);
|
||||
this.list.add(entry);
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.bukkit.craftbukkit.ban;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import net.minecraft.server.players.GameProfileBanEntry;
|
||||
import net.minecraft.server.players.GameProfileBanList;
|
||||
import org.bukkit.BanEntry;
|
||||
import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
|
||||
import org.bukkit.profile.PlayerProfile;
|
||||
|
||||
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;
|
||||
private String source;
|
||||
private Date expiration;
|
||||
private String reason;
|
||||
|
||||
public CraftProfileBanEntry(GameProfile profile, GameProfileBanEntry entry, GameProfileBanList list) {
|
||||
this.list = list;
|
||||
this.profile = profile;
|
||||
this.created = entry.getCreated() != null ? new Date(entry.getCreated().getTime()) : null;
|
||||
this.source = entry.getSource();
|
||||
this.expiration = entry.getExpires() != null ? new Date(entry.getExpires().getTime()) : null;
|
||||
this.reason = entry.getReason();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTarget() {
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSource() {
|
||||
return this.source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSource(String source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getExpiration() {
|
||||
return this.expiration == null ? null : (Date) this.expiration.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExpiration(Date expiration) {
|
||||
if (expiration != null && expiration.getTime() == minorDate.getTime()) {
|
||||
expiration = null; // Forces "forever"
|
||||
}
|
||||
|
||||
this.expiration = expiration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getReason() {
|
||||
return this.reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReason(String reason) {
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
GameProfileBanEntry entry = new GameProfileBanEntry(this.profile, this.created, this.source, this.expiration, this.reason);
|
||||
this.list.add(entry);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user