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

306 lines
7.1 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.banning;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.pravian.aero.config.YamlConfig;
import net.pravian.aero.util.Ips;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
public class BanManager extends FreedomService
{
private final Set<Ban> bans = Sets.newHashSet();
private final Map<String, Ban> ipBans = Maps.newHashMap();
private final Map<String, Ban> nameBans = Maps.newHashMap();
private final List<String> unbannableUsernames = Lists.newArrayList();
public static final String CONFIG_FILENAME = "bans.yml";
//
private final YamlConfig config;
public BanManager(TotalFreedomMod plugin)
{
super(plugin);
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.config = new YamlConfig(plugin, "bans.yml");
}
@Override
protected void onStart()
{
config.load();
bans.clear();
for (String id : config.getKeys(false))
{
if (!config.isConfigurationSection(id))
{
FLog.warning("Could not load username ban: " + id + ". Invalid format!");
continue;
}
Ban ban = new Ban();
ban.loadFrom(config.getConfigurationSection(id));
if (!ban.isValid())
{
FLog.warning("Not adding username ban: " + id + ". Missing information.");
continue;
}
bans.add(ban);
}
// Remove expired bans, repopulate ipBans and nameBans,
updateViews();
FLog.info("Loaded " + ipBans.size() + " IP bans and " + nameBans.size() + " username bans.");
// Load unbannable usernames
unbannableUsernames.clear();
unbannableUsernames.addAll((Collection<? extends String>) ConfigEntry.FAMOUS_PLAYERS.getList());
FLog.info("Loaded " + unbannableUsernames.size() + " unbannable usernames.");
}
@Override
protected void onStop()
{
saveAll();
logger.info("Saved " + bans.size() + " player bans");
}
public Set<Ban> getAllBans()
{
return Collections.unmodifiableSet(bans);
}
public Collection<Ban> getIpBans()
{
return Collections.unmodifiableCollection(ipBans.values());
}
public Collection<Ban> getUsernameBans()
{
return Collections.unmodifiableCollection(nameBans.values());
}
public void saveAll()
{
// Remove expired
updateViews();
config.clear();
for (Ban ban : bans)
{
ban.saveTo(config.createSection(String.valueOf(ban.hashCode())));
}
// Save config
config.save();
}
public Ban getByIp(String ip)
{
final Ban directBan = ipBans.get(ip);
if (directBan != null && !directBan.isExpired())
{
return directBan;
}
// Match fuzzy IP
for (Ban loopBan : ipBans.values())
{
if (loopBan.isExpired())
{
continue;
}
for (String loopIp : loopBan.getIps())
{
if (!loopIp.contains("*"))
{
continue;
}
if (Ips.fuzzyIpMatch(ip, loopIp, 4))
{
return loopBan;
}
}
}
return null;
}
public Ban getByUsername(String username)
{
username = username.toLowerCase();
final Ban directBan = nameBans.get(username);
if (directBan != null && !directBan.isExpired())
{
return directBan;
}
return null;
}
public Ban unbanIp(String ip)
{
final Ban ban = getByIp(ip);
if (ban != null)
{
bans.remove(ban);
saveAll();
}
return ban;
}
public Ban unbanUsername(String username)
{
final Ban ban = getByUsername(username);
if (ban != null)
{
bans.remove(ban);
saveAll();
}
return ban;
}
public boolean isIpBanned(String ip)
{
return getByIp(ip) != null;
}
public boolean isUsernameBanned(String username)
{
return getByUsername(username) != null;
}
public boolean addBan(Ban ban)
{
if (bans.add(ban))
{
saveAll();
return true;
}
return false;
}
public boolean removeBan(Ban ban)
{
if (bans.remove(ban))
{
saveAll();
return true;
}
return false;
}
public int purge()
{
config.clear();
config.save();
int size = bans.size();
bans.clear();
updateViews();
return size;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event)
{
final String username = event.getPlayer().getName();
final String ip = Ips.getIp(event);
// Regular ban
Ban ban = getByUsername(username);
if (ban == null)
{
ban = getByIp(ip);
}
if (ban != null && !ban.isExpired())
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, ban.bakeKickMessage());
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerJoin(PlayerJoinEvent event)
{
final Player player = event.getPlayer();
final PlayerData data = plugin.pl.getData(player);
if (!plugin.al.isAdmin(player))
{
return;
}
// Unban admins
for (String storedIp : data.getIps())
{
unbanIp(storedIp);
unbanIp(FUtil.getFuzzyIp(storedIp));
}
unbanUsername(player.getName());
player.setOp(true);
}
private void updateViews()
{
// Remove expired bans
for (Iterator<Ban> it = bans.iterator(); it.hasNext();)
{
if (it.next().isExpired())
{
it.remove();
}
}
nameBans.clear();
ipBans.clear();
for (Ban ban : bans)
{
if (ban.hasUsername())
{
nameBans.put(ban.getUsername().toLowerCase(), ban);
}
if (ban.hasIps())
{
for (String ip : ban.getIps())
{
ipBans.put(ip, ban);
}
}
}
}
}