2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.banning;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import java.text.SimpleDateFormat;
|
2019-07-11 02:13:57 +00:00
|
|
|
import java.time.Instant;
|
2020-05-29 10:14:21 +00:00
|
|
|
import java.util.ArrayList;
|
2020-12-25 19:46:43 +00:00
|
|
|
import java.util.Collections;
|
2018-07-31 07:05:28 +00:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2020-08-10 05:25:04 +00:00
|
|
|
import java.util.UUID;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2020-08-10 00:47:53 +00:00
|
|
|
public class Ban
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
|
|
|
|
private final List<String> ips = Lists.newArrayList();
|
2015-10-19 17:43:46 +00:00
|
|
|
private String username = null;
|
2020-08-10 05:25:04 +00:00
|
|
|
private UUID uuid = null;
|
2015-10-19 17:43:46 +00:00
|
|
|
private String by = null;
|
2020-12-25 19:46:43 +00:00
|
|
|
|
|
|
|
|
2019-07-11 02:13:57 +00:00
|
|
|
private Date at = null;
|
2020-12-25 19:46:43 +00:00
|
|
|
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
private String reason = null; // Unformatted, &[0-9,a-f] instead of ChatColor
|
2020-12-25 19:46:43 +00:00
|
|
|
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
private long expiryUnix = -1;
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public Ban()
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:25:04 +00:00
|
|
|
public Ban(String username, UUID uuid, String ip, String by, Date at, Date expire, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
this(username,
|
2020-08-10 05:25:04 +00:00
|
|
|
uuid,
|
2020-12-25 19:46:43 +00:00
|
|
|
Collections.singletonList(ip),
|
2015-10-19 17:43:46 +00:00
|
|
|
by,
|
2019-07-11 02:13:57 +00:00
|
|
|
at,
|
2015-10-19 17:43:46 +00:00
|
|
|
expire,
|
|
|
|
reason);
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:25:04 +00:00
|
|
|
public Ban(String username, UUID uuid, List<String> ips, String by, Date at, Date expire, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
this.username = username;
|
2020-08-10 05:25:04 +00:00
|
|
|
this.uuid = uuid;
|
2015-10-19 17:43:46 +00:00
|
|
|
if (ips != null)
|
|
|
|
{
|
2020-05-29 10:14:21 +00:00
|
|
|
this.ips.addAll(ips);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
2016-03-06 15:56:15 +00:00
|
|
|
dedupeIps();
|
2015-10-19 17:43:46 +00:00
|
|
|
this.by = by;
|
2019-07-11 02:13:57 +00:00
|
|
|
this.at = at;
|
2020-09-30 06:54:17 +00:00
|
|
|
if (expire == null)
|
|
|
|
{
|
|
|
|
expire = FUtil.parseDateOffset("24h");
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
this.expiryUnix = FUtil.getUnixTime(expire);
|
2016-05-12 19:40:39 +00:00
|
|
|
this.reason = reason;
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For player IP
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerIp(Player player, CommandSender by)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return forPlayerIp(player, by, null, null);
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerIp(Player player, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2020-12-25 19:46:43 +00:00
|
|
|
return new Ban(null, null, Collections.singletonList(FUtil.getIp(player)), by.getName(), Date.from(Instant.now()), expiry, reason);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerIp(String ip, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2020-08-10 05:25:04 +00:00
|
|
|
return new Ban(null, null, ip, by.getName(), Date.from(Instant.now()), expiry, reason);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For player name
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerName(Player player, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return forPlayerName(player.getName(), by, expiry, reason);
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerName(String player, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
return new Ban(player,
|
2020-08-10 05:25:04 +00:00
|
|
|
null,
|
|
|
|
new ArrayList<>(),
|
2015-10-19 17:43:46 +00:00
|
|
|
by.getName(),
|
2019-07-11 02:13:57 +00:00
|
|
|
Date.from(Instant.now()),
|
2015-10-19 17:43:46 +00:00
|
|
|
expiry,
|
|
|
|
reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For player
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayer(Player player, CommandSender by)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return forPlayerName(player, by, null, null);
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayer(Player player, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
return new Ban(player.getName(),
|
2020-08-10 05:25:04 +00:00
|
|
|
player.getUniqueId(),
|
2020-07-01 01:51:06 +00:00
|
|
|
FUtil.getIp(player),
|
2015-10-19 17:43:46 +00:00
|
|
|
by.getName(),
|
2019-07-11 02:13:57 +00:00
|
|
|
Date.from(Instant.now()),
|
2015-10-19 17:43:46 +00:00
|
|
|
expiry,
|
|
|
|
reason);
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static Ban forPlayerFuzzy(Player player, CommandSender by, Date expiry, String reason)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
return new Ban(player.getName(),
|
2020-08-10 05:25:04 +00:00
|
|
|
player.getUniqueId(),
|
2020-07-01 01:51:06 +00:00
|
|
|
FUtil.getFuzzyIp(FUtil.getIp(player)),
|
2015-10-19 17:43:46 +00:00
|
|
|
by.getName(),
|
2019-07-11 02:13:57 +00:00
|
|
|
Date.from(Instant.now()),
|
2015-10-19 17:43:46 +00:00
|
|
|
expiry,
|
|
|
|
reason);
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
public static SimpleDateFormat getDateFormat()
|
|
|
|
{
|
|
|
|
return DATE_FORMAT;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public boolean hasUsername()
|
|
|
|
{
|
|
|
|
return username != null && !username.isEmpty();
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:25:04 +00:00
|
|
|
public boolean hasUUID()
|
|
|
|
{
|
|
|
|
return uuid != null;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public boolean addIp(String ip)
|
|
|
|
{
|
|
|
|
return ips.add(ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removeIp(String ip)
|
|
|
|
{
|
|
|
|
return ips.remove(ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasIps()
|
|
|
|
{
|
|
|
|
return !ips.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasExpiry()
|
|
|
|
{
|
|
|
|
return expiryUnix > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isExpired()
|
|
|
|
{
|
|
|
|
return hasExpiry() && expiryUnix < FUtil.getUnixTime();
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:14:21 +00:00
|
|
|
public String bakeKickMessage()
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
final StringBuilder message = new StringBuilder(ChatColor.GOLD + "You");
|
|
|
|
|
2020-08-01 04:10:44 +00:00
|
|
|
if (!hasUsername())
|
|
|
|
{
|
|
|
|
message.append("r IP address is");
|
|
|
|
}
|
|
|
|
else if (!hasIps())
|
|
|
|
{
|
|
|
|
message.append("r username is");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
message.append(" are");
|
|
|
|
}
|
|
|
|
|
|
|
|
message.append(" temporarily banned from this server.");
|
2015-10-19 17:43:46 +00:00
|
|
|
message.append("\nAppeal at ").append(ChatColor.BLUE)
|
|
|
|
.append(ConfigEntry.SERVER_BAN_URL.getString());
|
|
|
|
|
|
|
|
if (reason != null)
|
|
|
|
{
|
|
|
|
message.append("\n").append(ChatColor.RED).append("Reason: ").append(ChatColor.GOLD)
|
|
|
|
.append(ChatColor.translateAlternateColorCodes('&', reason));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (by != null)
|
|
|
|
{
|
|
|
|
message.append("\n").append(ChatColor.RED).append("Banned by: ").append(ChatColor.GOLD)
|
|
|
|
.append(by);
|
|
|
|
}
|
|
|
|
|
2019-07-11 02:13:57 +00:00
|
|
|
if (at != null)
|
|
|
|
{
|
|
|
|
message.append("\n").append(ChatColor.RED).append("Issued: ").append(ChatColor.GOLD)
|
|
|
|
.append(DATE_FORMAT.format(at));
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
if (getExpiryUnix() != 0)
|
|
|
|
{
|
|
|
|
message.append("\n").append(ChatColor.RED).append("Expires: ").append(ChatColor.GOLD)
|
|
|
|
.append(DATE_FORMAT.format(FUtil.getUnixDate(expiryUnix)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return message.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object object)
|
|
|
|
{
|
|
|
|
if (object == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
if (!(object instanceof Ban))
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
final Ban ban = (Ban)object;
|
2015-10-19 17:43:46 +00:00
|
|
|
if (hasIps() != ban.hasIps()
|
2018-07-31 06:47:26 +00:00
|
|
|
|| hasUsername() != ban.hasUsername())
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasIps() && !(getIps().equals(ban.getIps())))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
return !(hasUsername() && !(getUsername().equalsIgnoreCase(ban.getUsername())));
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
private void dedupeIps()
|
|
|
|
{
|
|
|
|
Set<String> uniqueIps = new HashSet<>();
|
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
//Fancy Collections.removeIf lets you do all that while loop work in one lambda.
|
|
|
|
ips.removeIf(s -> !uniqueIps.add(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getIps()
|
|
|
|
{
|
|
|
|
return ips;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername()
|
|
|
|
{
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUsername(String username)
|
|
|
|
{
|
|
|
|
this.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public UUID getUuid()
|
|
|
|
{
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUuid(UUID uuid)
|
|
|
|
{
|
|
|
|
this.uuid = uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBy()
|
|
|
|
{
|
|
|
|
return by;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setBy(String by)
|
|
|
|
{
|
|
|
|
this.by = by;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date getAt()
|
|
|
|
{
|
|
|
|
return at;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAt(Date at)
|
|
|
|
{
|
|
|
|
this.at = at;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getReason()
|
|
|
|
{
|
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReason(String reason)
|
|
|
|
{
|
|
|
|
this.reason = reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getExpiryUnix()
|
|
|
|
{
|
|
|
|
return expiryUnix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setExpiryUnix(long expiryUnix)
|
|
|
|
{
|
|
|
|
this.expiryUnix = expiryUnix;
|
2016-03-06 15:56:15 +00:00
|
|
|
}
|
2020-12-04 00:28:53 +00:00
|
|
|
}
|