Merge pull request #284 from AtlasMediaGroup/FS-454

FS-454 - Add expiry to indefinite bans
This commit is contained in:
Paldiu 2022-11-14 17:12:17 -06:00 committed by GitHub
commit fa85b8e160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 10 deletions

View File

@ -1,10 +1,14 @@
package me.totalfreedom.totalfreedommod.banning; package me.totalfreedom.totalfreedommod.banning;
import com.google.common.collect.Lists; 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.List;
import java.util.UUID; import java.util.UUID;
import me.totalfreedom.totalfreedommod.config.IConfig; import me.totalfreedom.totalfreedommod.config.IConfig;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
public class IndefiniteBan implements IConfig public class IndefiniteBan implements IConfig
@ -13,6 +17,7 @@ public class IndefiniteBan implements IConfig
private String username = null; private String username = null;
private UUID uuid = null; private UUID uuid = null;
private String reason = null; private String reason = null;
private Date expiry = null;
public IndefiniteBan() public IndefiniteBan()
{ {
@ -22,6 +27,7 @@ public class IndefiniteBan implements IConfig
public void loadFrom(ConfigurationSection cs) public void loadFrom(ConfigurationSection cs)
{ {
this.username = cs.getName(); this.username = cs.getName();
try try
{ {
String strUUID = cs.getString("uuid", null); 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."); 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.clear();
this.ips.addAll(cs.getStringList("ips")); this.ips.addAll(cs.getStringList("ips"));
this.reason = cs.getString("reason", null); 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 @Override
@ -86,4 +103,24 @@ public class IndefiniteBan implements IConfig
{ {
this.reason = reason; 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.base.Strings;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
@ -19,30 +21,24 @@ public class IndefiniteBanList extends FreedomService
{ {
public static final String CONFIG_FILENAME = "indefinitebans.yml"; public static final String CONFIG_FILENAME = "indefinitebans.yml";
private YamlConfig config;
private final Set<IndefiniteBan> indefBans = Sets.newHashSet(); private final Set<IndefiniteBan> indefBans = Sets.newHashSet();
private int nameBanCount = 0; private int nameBanCount = 0;
private int uuidBanCount = 0; private int uuidBanCount = 0;
private int ipBanCount = 0; private int ipBanCount = 0;
public static String getConfigFilename() private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy, zzzz");
{
return CONFIG_FILENAME;
}
@Override @Override
public void onStart() public void onStart()
{ {
indefBans.clear(); indefBans.clear();
final YamlConfig config = new YamlConfig(plugin, CONFIG_FILENAME, true); config = new YamlConfig(plugin, CONFIG_FILENAME, true);
config.load(); config.load();
for (String name : config.getKeys(false)) for (String name : config.getKeys(false))
@ -64,10 +60,18 @@ public class IndefiniteBanList extends FreedomService
continue; 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); indefBans.add(indefBan);
} }
updateCount(); updateCount();
config.save();
FLog.info("Loaded " + nameBanCount + " indefinite name bans, " + uuidBanCount + " UUID bans, and " + ipBanCount + " ip bans"); FLog.info("Loaded " + nameBanCount + " indefinite name bans, " + uuidBanCount + " UUID bans, and " + ipBanCount + " ip bans");
} }
@ -111,12 +115,28 @@ public class IndefiniteBanList extends FreedomService
if (ban != null) if (ban != null)
{ {
String kickMessage = ChatColor.RED + "Your " + bannedBy + " is indefinitely banned from this server."; if (ban.isExpired())
{
config.set(ban.getUsername(), null);
config.save();
return;
}
String kickMessage = ChatColor.RED + "Your " + bannedBy + " is "
+ (ban.hasExpiry() ? "" : "indefinitely ")
+ "banned from this server.";
String reason = ban.getReason(); String reason = ban.getReason();
if (!Strings.isNullOrEmpty(reason)) if (!Strings.isNullOrEmpty(reason))
{ {
kickMessage += "\nReason: " + ChatColor.GOLD + 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(); String appealURL = ConfigEntry.SERVER_INDEFBAN_URL.getString();
if (!Strings.isNullOrEmpty(appealURL)) if (!Strings.isNullOrEmpty(appealURL))
{ {