Skip to content

Commit

Permalink
Update to new ban method.
Browse files Browse the repository at this point in the history
  • Loading branch information
FearFree authored and khobbits committed Jul 12, 2014
1 parent 575a814 commit 51be213
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.TextInput;
import com.earth2me.essentials.textreader.TextPager;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.LocationUtil;
import java.io.IOException;
import java.util.Iterator;
Expand Down Expand Up @@ -357,21 +356,6 @@ private void updateCompass(final User user)
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerLogin2(final PlayerLoginEvent event)
{
switch (event.getResult())
{
case KICK_BANNED:
break;
default:
return;
}

final String banReason = tl("banFormat", tl("defaultBanReason"), "Console");
event.disallow(Result.KICK_BANNED, banReason);
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerLogin(final PlayerLoginEvent event)
{
Expand All @@ -386,28 +370,6 @@ public void onPlayerLogin(final PlayerLoginEvent event)
}
event.disallow(Result.KICK_FULL, tl("serverFull"));
break;

case KICK_BANNED:
final User user = ess.getUser(event.getPlayer());
final boolean banExpired = user.checkBanTimeout(System.currentTimeMillis());
if (banExpired)
{
event.allow();
return;
}
String banReason = user.getBanReason();
if (banReason == null || banReason.isEmpty() || banReason.equalsIgnoreCase("ban"))
{
banReason = event.getKickMessage();
}
if (user.getBanTimeout() > 0)
{
//TODO: TL This
banReason += "\n\n" + "Expires in " + DateUtil.formatDateDiff(user.getBanTimeout());
}
event.disallow(Result.KICK_BANNED, banReason);
break;

default:
break;
}
Expand Down
73 changes: 73 additions & 0 deletions Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ess3.api.IEssentials;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand All @@ -24,6 +25,8 @@ public class EssentialsUpgrade
private final static Logger LOGGER = Logger.getLogger("Essentials");
private final transient IEssentials ess;
private final transient EssentialsConf doneFile;
private String banReason;
private Long banTimeout;

EssentialsUpgrade(final IEssentials essentials)
{
Expand Down Expand Up @@ -659,6 +662,75 @@ public static void uuidFileConvert(IEssentials ess, Boolean ignoreUFCache)
ess.getLogger().info("To rerun the conversion type /essentials uuidconvert");
}

public void banFormatChange()
{
if (doneFile.getBoolean("banFormatChange", false))
{
return;
}

ess.getLogger().info("Starting Essentials ban format conversion");

final File userdir = new File(ess.getDataFolder(), "userdata");
if (!userdir.exists())
{
return;
}
File[] playerFiles = userdir.listFiles();

for (File pFile : playerFiles)
{
EssentialsConf conf = new EssentialsConf(pFile);
conf.load();
try
{
banReason = conf.getConfigurationSection("ban").getString("reason");
}
catch (NullPointerException n)
{
//No ban in userfile
banReason = "";
}

String playerName = conf.getString("lastAccountName");
if (!banReason.equals(""))
{
try
{
banTimeout = Long.parseLong(conf.getConfigurationSection("ban").getString("timeout"));
}
catch (NumberFormatException n)
{
//No ban timeout set, or malformed timeout.
banTimeout = 0L;
}
org.bukkit.OfflinePlayer player = ess.getServer().getOfflinePlayer(playerName);
if (player.isBanned())
{
updateBan(playerName, banReason, banTimeout);
}
}
conf.removeProperty("ban");
conf.save();
}

doneFile.setProperty("banFormatChange", true);
doneFile.save();
ess.getLogger().info("Ban format update complete.");
}

private void updateBan(String playerName, String banReason, Long banTimeout)
{
if (banTimeout == 0)
{
Bukkit.getBanList(BanList.Type.NAME).addBan(playerName, banReason, null, Console.NAME);
}
else
{
Bukkit.getBanList(BanList.Type.NAME).addBan(playerName, banReason, new Date(banTimeout), Console.NAME);
}
}

public void beforeSettings()
{
if (!ess.getDataFolder().exists())
Expand All @@ -678,6 +750,7 @@ public void afterSettings()
updateSpawnsToNewSpawnsConfig();
updateJailsToNewJailsConfig();
uuidFileChange();
banFormatChange();
warnMetrics();
}
}
12 changes: 0 additions & 12 deletions Essentials/src/com/earth2me/essentials/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -594,18 +594,6 @@ public boolean checkMuteTimeout(final long currentTime)
return false;
}

//Returns true if status expired during this check
public boolean checkBanTimeout(final long currentTime)
{
if (getBanTimeout() > 0 && getBanTimeout() < currentTime && this.getBase().isBanned())
{
setBanTimeout(0);
this.getBase().setBanned(false);
return true;
}
return false;
}

public void updateActivity(final boolean broadcast)
{
if (isAfk() && ess.getSettings().cancelAfkOnInteract())
Expand Down
21 changes: 0 additions & 21 deletions Essentials/src/com/earth2me/essentials/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,27 +667,6 @@ public void setJailTimeout(long time)
config.save();
}

public String getBanReason()
{
return config.getString("ban.reason", "");
}

public void setBanReason(String reason)
{
config.setProperty("ban.reason", StringUtil.sanitizeString(reason));
config.save();
}

public long getBanTimeout()
{
return config.getLong("ban.timeout", 0);
}

public void setBanTimeout(long time)
{
config.setProperty("ban.timeout", time);
config.save();
}
private long lastLogin;

private long _getLastLogin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.FormatUtil;
import java.util.logging.Level;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Server;


Expand Down Expand Up @@ -61,9 +63,7 @@ public void run(final Server server, final CommandSource sender, final String co
banReason = tl("defaultBanReason");
}

user.setBanReason(tl("banFormat", banReason, senderName));
user.getBase().setBanned(true);
user.setBanTimeout(0);
Bukkit.getBanList(BanList.Type.NAME).addBan(user.getName(), banReason, null, senderName);
user.getBase().kickPlayer(tl("banFormat", banReason, senderName));

server.getLogger().log(Level.INFO, tl("playerBanned", senderName, user.getName(), banReason));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void run()
continue;
}

int ban = user.getBanReason().isEmpty() ? 0 : 1;
int ban = user.getBase().isBanned() ? 0 : 1;

long lastLog = user.getLastLogout();
if (lastLog == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.earth2me.essentials.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.Server;
Expand Down Expand Up @@ -59,6 +61,10 @@ else if (FormatUtil.validIP(args[0]) && (server.getIPBans().contains(args[0])))
sender.sendMessage(tl("isIpBanned", args[0]));
return;
}
else if (Bukkit.getBannedPlayers().contains(Bukkit.getOfflinePlayer(args[0]))) {
sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(Bukkit.getOfflinePlayer(args[0]).getName()).getReason() : tl("true")));
return;
}
else
{
throw new PlayerNotFoundException();
Expand Down Expand Up @@ -137,7 +143,7 @@ private void seenOffline(final Server server, final CommandSource sender, User u

if (user.getBase().isBanned())
{
sender.sendMessage(tl("whoisBanned", showBan ? user.getBanReason() : tl("true")));
sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(user.getName()).getReason() : tl("true")));
}
final String location = user.getGeoLocation();
if (location != null && (!(sender.isPlayer()) || ess.getUser(sender.getPlayer()).isAuthorized("essentials.geoip.show")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Server;


Expand Down Expand Up @@ -61,9 +64,8 @@ public void run(final Server server, final CommandSource sender, final String co

final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
final String banReason = tl("tempBanned", DateUtil.formatDateDiff(banTimestamp), senderName, stringDregs);
user.setBanReason(banReason);
user.setBanTimeout(banTimestamp);
user.getBase().setBanned(true);

Bukkit.getBanList(BanList.Type.NAME).addBan(user.getName(), banReason, new Date(banTimestamp), senderName);
user.getBase().kickPlayer(banReason);

final String message = tl("playerBanned", senderName, user.getName(), banReason, DateUtil.formatDateDiff(banTimestamp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import java.util.logging.Level;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;

Expand All @@ -28,8 +30,7 @@ public void run(final Server server, final CommandSource sender, final String co
{
final User user = getPlayer(server, args, 0, true, true);
name = user.getName();
user.getBase().setBanned(false);
user.setBanTimeout(0);
Bukkit.getBanList(BanList.Type.NAME).pardon(name);
}
catch (NoSuchFieldException e)
{
Expand All @@ -39,7 +40,7 @@ public void run(final Server server, final CommandSource sender, final String co
{
throw new Exception(tl("playerNotFound"), e);
}
player.setBanned(false);
Bukkit.getBanList(BanList.Type.NAME).pardon(name);
}

final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.FormatUtil;
import java.util.logging.Level;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.Server;


Expand Down Expand Up @@ -46,8 +48,9 @@ public void run(final Server server, final CommandSource sender, final String co
{
throw new PlayerNotFoundException();
}


ess.getServer().unbanIP(ipAddress);
Bukkit.getBanList(BanList.Type.IP).pardon(ipAddress);
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
server.getLogger().log(Level.INFO, tl("playerUnbanIpAddress", senderName, ipAddress));

Expand Down

0 comments on commit 51be213

Please sign in to comment.