TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/banning/Ban.java

323 lines
7.5 KiB
Java
Raw Normal View History

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;
import java.util.Collections;
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;
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
{
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
private final List<String> ips = Lists.newArrayList();
private String username = null;
2020-08-10 05:25:04 +00:00
private UUID uuid = null;
private String by = null;
2019-07-11 02:13:57 +00:00
private Date at = null;
private String reason = null; // Unformatted, &[0-9,a-f] instead of ChatColor
private long expiryUnix = -1;
public Ban()
{
}
2020-08-10 05:25:04 +00:00
public Ban(String username, UUID uuid, String ip, String by, Date at, Date expire, String reason)
{
this(username,
2020-08-10 05:25:04 +00:00
uuid,
Collections.singletonList(ip),
by,
2019-07-11 02:13:57 +00:00
at,
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)
{
this.username = username;
2020-08-10 05:25:04 +00:00
this.uuid = uuid;
if (ips != null)
{
2020-05-29 10:14:21 +00:00
this.ips.addAll(ips);
}
dedupeIps();
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");
}
this.expiryUnix = FUtil.getUnixTime(expire);
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
this.reason = reason;
}
//
// For player IP
public static Ban forPlayerIp(Player player, CommandSender by)
{
return forPlayerIp(player, by, null, null);
}
public static Ban forPlayerIp(Player player, CommandSender by, Date expiry, String reason)
{
return new Ban(null, null, Collections.singletonList(FUtil.getIp(player)), by.getName(), Date.from(Instant.now()), expiry, reason);
}
public static Ban forPlayerIp(String ip, CommandSender by, Date expiry, String reason)
{
2020-08-10 05:25:04 +00:00
return new Ban(null, null, ip, by.getName(), Date.from(Instant.now()), expiry, reason);
}
//
// For player name
public static Ban forPlayerName(Player player, CommandSender by, Date expiry, String reason)
{
return forPlayerName(player.getName(), by, expiry, reason);
}
public static Ban forPlayerName(String player, CommandSender by, Date expiry, String reason)
{
return new Ban(player,
2020-08-10 05:25:04 +00:00
null,
new ArrayList<>(),
by.getName(),
2019-07-11 02:13:57 +00:00
Date.from(Instant.now()),
expiry,
reason);
}
//
// For player
public static Ban forPlayer(Player player, CommandSender by)
{
return forPlayerName(player, by, null, null);
}
public static Ban forPlayer(Player player, CommandSender by, Date expiry, String reason)
{
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),
by.getName(),
2019-07-11 02:13:57 +00:00
Date.from(Instant.now()),
expiry,
reason);
}
public static Ban forPlayerFuzzy(Player player, CommandSender by, Date expiry, String reason)
{
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)),
by.getName(),
2019-07-11 02:13:57 +00:00
Date.from(Instant.now()),
expiry,
reason);
}
public static SimpleDateFormat getDateFormat()
{
return DATE_FORMAT;
}
public boolean hasUsername()
{
return username != null && !username.isEmpty();
}
2020-08-10 05:25:04 +00:00
public boolean hasUUID()
{
return uuid != null;
}
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()
{
2021-03-21 01:21:15 +00:00
return hasExpiry() && FUtil.getUnixDate(expiryUnix).before(new Date(FUtil.getUnixTime()));
}
2020-05-29 10:14:21 +00:00
public String bakeKickMessage()
{
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.");
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));
}
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;
}
if (!(object instanceof Ban))
{
return false;
}
final Ban ban = (Ban)object;
if (hasIps() != ban.hasIps()
2018-07-31 06:47:26 +00:00
|| hasUsername() != ban.hasUsername())
{
return false;
}
if (hasIps() && !(getIps().equals(ban.getIps())))
{
return false;
}
return !(hasUsername() && !(getUsername().equalsIgnoreCase(ban.getUsername())));
}
private void dedupeIps()
{
Set<String> uniqueIps = new HashSet<>();
//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;
}
}