Add expiry to indefinite bans

This commit is contained in:
Focusvity 2022-10-29 21:33:00 +11:00 committed by Paul Reilly
parent 88eceab480
commit bcf8c2f04a
3 changed files with 66 additions and 10 deletions

View File

@ -1,10 +1,14 @@
package me.totalfreedom.totalfreedommod.banning;
import com.google.common.collect.Lists;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.config.IConfig;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.configuration.ConfigurationSection;
public class IndefiniteBan implements IConfig
@ -13,6 +17,7 @@ public class IndefiniteBan implements IConfig
private String username = null;
private UUID uuid = null;
private String reason = null;
private Date expiry = null;
public IndefiniteBan()
{
@ -22,6 +27,7 @@ public class IndefiniteBan implements IConfig
public void loadFrom(ConfigurationSection cs)
{
this.username = cs.getName();
try
{
String strUUID = cs.getString("uuid", null);
@ -35,9 +41,20 @@ public class IndefiniteBan implements IConfig
{
FLog.warning("Failed to load indefinite banned UUID for " + this.username + ". Make sure the UUID is in the correct format with dashes.");
}
this.ips.clear();
this.ips.addAll(cs.getStringList("ips"));
this.reason = cs.getString("reason", null);
String date = cs.getString("expiry", null);
try
{
this.expiry = date != null ? new SimpleDateFormat("yyyy-MM-dd").parse(date) : null;
}
catch (ParseException ex)
{
FLog.warning("Failed to load indefinite banned expiry for " + this.username + ". Make sure the expiry is in the correct format (yyyy-MM-dd).");
}
}
@Override
@ -86,4 +103,24 @@ public class IndefiniteBan implements IConfig
{
this.reason = reason;
}
public Date getExpiry()
{
return expiry;
}
public void setExpiry(Date date)
{
this.expiry = date;
}
public boolean hasExpiry()
{
return this.expiry != null;
}
public boolean isExpired()
{
return hasExpiry() && expiry.before(new Date(FUtil.getUnixTime()));
}
}

View File

@ -2,6 +2,8 @@ package me.totalfreedom.totalfreedommod.banning;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService;
@ -19,30 +21,24 @@ public class IndefiniteBanList extends FreedomService
{
public static final String CONFIG_FILENAME = "indefinitebans.yml";
private YamlConfig config;
private final Set<IndefiniteBan> indefBans = Sets.newHashSet();
private int nameBanCount = 0;
private int uuidBanCount = 0;
private int ipBanCount = 0;
public static String getConfigFilename()
{
return CONFIG_FILENAME;
}
private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy, zzzz");
@Override
public void onStart()
{
indefBans.clear();
final YamlConfig config = new YamlConfig(plugin, CONFIG_FILENAME, true);
config = new YamlConfig(plugin, CONFIG_FILENAME, true);
config.load();
for (String name : config.getKeys(false))
@ -64,10 +60,18 @@ public class IndefiniteBanList extends FreedomService
continue;
}
if (indefBan.isExpired())
{
FLog.info("Removing " + name + " from indefinite ban list as the entry has expired!");
config.set(name, null);
continue;
}
indefBans.add(indefBan);
}
updateCount();
config.save();
FLog.info("Loaded " + nameBanCount + " indefinite name bans, " + uuidBanCount + " UUID bans, and " + ipBanCount + " ip bans");
}
@ -111,12 +115,26 @@ public class IndefiniteBanList extends FreedomService
if (ban != null)
{
if (ban.isExpired())
{
config.set(ban.getUsername(), null);
config.save();
return;
}
String kickMessage = ChatColor.RED + "Your " + bannedBy + " is indefinitely banned from this server.";
String reason = ban.getReason();
if (!Strings.isNullOrEmpty(reason))
{
kickMessage += "\nReason: " + ChatColor.GOLD + reason;
}
Date expiry = ban.getExpiry();
if (expiry != null)
{
kickMessage += ChatColor.RED + "\nExpiry: " + ChatColor.GOLD + dateFormat.format(expiry);
}
String appealURL = ConfigEntry.SERVER_INDEFBAN_URL.getString();
if (!Strings.isNullOrEmpty(appealURL))
{

View File

@ -1,10 +1,11 @@
package me.totalfreedom.totalfreedommod.config;
import java.text.ParseException;
import org.bukkit.configuration.ConfigurationSection;
public interface IConfig
{
void loadFrom(ConfigurationSection cs);
void loadFrom(ConfigurationSection cs) throws ParseException;
void saveTo(ConfigurationSection cs);