Skip to content

Commit 51be213

Browse files
FearFreekhobbits
authored andcommitted
Update to new ban method.
1 parent 575a814 commit 51be213

10 files changed

+97
-83
lines changed

Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java

-38
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.earth2me.essentials.textreader.KeywordReplacer;
66
import com.earth2me.essentials.textreader.TextInput;
77
import com.earth2me.essentials.textreader.TextPager;
8-
import com.earth2me.essentials.utils.DateUtil;
98
import com.earth2me.essentials.utils.LocationUtil;
109
import java.io.IOException;
1110
import java.util.Iterator;
@@ -357,21 +356,6 @@ private void updateCompass(final User user)
357356
}
358357
}
359358

360-
@EventHandler(priority = EventPriority.LOWEST)
361-
public void onPlayerLogin2(final PlayerLoginEvent event)
362-
{
363-
switch (event.getResult())
364-
{
365-
case KICK_BANNED:
366-
break;
367-
default:
368-
return;
369-
}
370-
371-
final String banReason = tl("banFormat", tl("defaultBanReason"), "Console");
372-
event.disallow(Result.KICK_BANNED, banReason);
373-
}
374-
375359
@EventHandler(priority = EventPriority.HIGH)
376360
public void onPlayerLogin(final PlayerLoginEvent event)
377361
{
@@ -386,28 +370,6 @@ public void onPlayerLogin(final PlayerLoginEvent event)
386370
}
387371
event.disallow(Result.KICK_FULL, tl("serverFull"));
388372
break;
389-
390-
case KICK_BANNED:
391-
final User user = ess.getUser(event.getPlayer());
392-
final boolean banExpired = user.checkBanTimeout(System.currentTimeMillis());
393-
if (banExpired)
394-
{
395-
event.allow();
396-
return;
397-
}
398-
String banReason = user.getBanReason();
399-
if (banReason == null || banReason.isEmpty() || banReason.equalsIgnoreCase("ban"))
400-
{
401-
banReason = event.getKickMessage();
402-
}
403-
if (user.getBanTimeout() > 0)
404-
{
405-
//TODO: TL This
406-
banReason += "\n\n" + "Expires in " + DateUtil.formatDateDiff(user.getBanTimeout());
407-
}
408-
event.disallow(Result.KICK_BANNED, banReason);
409-
break;
410-
411373
default:
412374
break;
413375
}

Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java

+73
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.logging.Level;
1515
import java.util.logging.Logger;
1616
import net.ess3.api.IEssentials;
17+
import org.bukkit.BanList;
1718
import org.bukkit.Bukkit;
1819
import org.bukkit.Location;
1920
import org.bukkit.World;
@@ -24,6 +25,8 @@ public class EssentialsUpgrade
2425
private final static Logger LOGGER = Logger.getLogger("Essentials");
2526
private final transient IEssentials ess;
2627
private final transient EssentialsConf doneFile;
28+
private String banReason;
29+
private Long banTimeout;
2730

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

665+
public void banFormatChange()
666+
{
667+
if (doneFile.getBoolean("banFormatChange", false))
668+
{
669+
return;
670+
}
671+
672+
ess.getLogger().info("Starting Essentials ban format conversion");
673+
674+
final File userdir = new File(ess.getDataFolder(), "userdata");
675+
if (!userdir.exists())
676+
{
677+
return;
678+
}
679+
File[] playerFiles = userdir.listFiles();
680+
681+
for (File pFile : playerFiles)
682+
{
683+
EssentialsConf conf = new EssentialsConf(pFile);
684+
conf.load();
685+
try
686+
{
687+
banReason = conf.getConfigurationSection("ban").getString("reason");
688+
}
689+
catch (NullPointerException n)
690+
{
691+
//No ban in userfile
692+
banReason = "";
693+
}
694+
695+
String playerName = conf.getString("lastAccountName");
696+
if (!banReason.equals(""))
697+
{
698+
try
699+
{
700+
banTimeout = Long.parseLong(conf.getConfigurationSection("ban").getString("timeout"));
701+
}
702+
catch (NumberFormatException n)
703+
{
704+
//No ban timeout set, or malformed timeout.
705+
banTimeout = 0L;
706+
}
707+
org.bukkit.OfflinePlayer player = ess.getServer().getOfflinePlayer(playerName);
708+
if (player.isBanned())
709+
{
710+
updateBan(playerName, banReason, banTimeout);
711+
}
712+
}
713+
conf.removeProperty("ban");
714+
conf.save();
715+
}
716+
717+
doneFile.setProperty("banFormatChange", true);
718+
doneFile.save();
719+
ess.getLogger().info("Ban format update complete.");
720+
}
721+
722+
private void updateBan(String playerName, String banReason, Long banTimeout)
723+
{
724+
if (banTimeout == 0)
725+
{
726+
Bukkit.getBanList(BanList.Type.NAME).addBan(playerName, banReason, null, Console.NAME);
727+
}
728+
else
729+
{
730+
Bukkit.getBanList(BanList.Type.NAME).addBan(playerName, banReason, new Date(banTimeout), Console.NAME);
731+
}
732+
}
733+
662734
public void beforeSettings()
663735
{
664736
if (!ess.getDataFolder().exists())
@@ -678,6 +750,7 @@ public void afterSettings()
678750
updateSpawnsToNewSpawnsConfig();
679751
updateJailsToNewJailsConfig();
680752
uuidFileChange();
753+
banFormatChange();
681754
warnMetrics();
682755
}
683756
}

Essentials/src/com/earth2me/essentials/User.java

-12
Original file line numberDiff line numberDiff line change
@@ -594,18 +594,6 @@ public boolean checkMuteTimeout(final long currentTime)
594594
return false;
595595
}
596596

597-
//Returns true if status expired during this check
598-
public boolean checkBanTimeout(final long currentTime)
599-
{
600-
if (getBanTimeout() > 0 && getBanTimeout() < currentTime && this.getBase().isBanned())
601-
{
602-
setBanTimeout(0);
603-
this.getBase().setBanned(false);
604-
return true;
605-
}
606-
return false;
607-
}
608-
609597
public void updateActivity(final boolean broadcast)
610598
{
611599
if (isAfk() && ess.getSettings().cancelAfkOnInteract())

Essentials/src/com/earth2me/essentials/UserData.java

-21
Original file line numberDiff line numberDiff line change
@@ -667,27 +667,6 @@ public void setJailTimeout(long time)
667667
config.save();
668668
}
669669

670-
public String getBanReason()
671-
{
672-
return config.getString("ban.reason", "");
673-
}
674-
675-
public void setBanReason(String reason)
676-
{
677-
config.setProperty("ban.reason", StringUtil.sanitizeString(reason));
678-
config.save();
679-
}
680-
681-
public long getBanTimeout()
682-
{
683-
return config.getLong("ban.timeout", 0);
684-
}
685-
686-
public void setBanTimeout(long time)
687-
{
688-
config.setProperty("ban.timeout", time);
689-
config.save();
690-
}
691670
private long lastLogin;
692671

693672
private long _getLastLogin()

Essentials/src/com/earth2me/essentials/commands/Commandban.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.earth2me.essentials.User;
88
import com.earth2me.essentials.utils.FormatUtil;
99
import java.util.logging.Level;
10+
import org.bukkit.BanList;
11+
import org.bukkit.Bukkit;
1012
import org.bukkit.Server;
1113

1214

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

64-
user.setBanReason(tl("banFormat", banReason, senderName));
65-
user.getBase().setBanned(true);
66-
user.setBanTimeout(0);
66+
Bukkit.getBanList(BanList.Type.NAME).addBan(user.getName(), banReason, null, senderName);
6767
user.getBase().kickPlayer(tl("banFormat", banReason, senderName));
6868

6969
server.getLogger().log(Level.INFO, tl("playerBanned", senderName, user.getName(), banReason));

Essentials/src/com/earth2me/essentials/commands/Commandessentials.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void run()
289289
continue;
290290
}
291291

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

294294
long lastLog = user.getLastLogout();
295295
if (lastLog == 0)

Essentials/src/com/earth2me/essentials/commands/Commandseen.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.earth2me.essentials.utils.StringUtil;
1010
import java.util.ArrayList;
1111
import java.util.List;
12+
import org.bukkit.BanList;
13+
import org.bukkit.Bukkit;
1214
import java.util.UUID;
1315
import org.bukkit.Location;
1416
import org.bukkit.Server;
@@ -59,6 +61,10 @@ else if (FormatUtil.validIP(args[0]) && (server.getIPBans().contains(args[0])))
5961
sender.sendMessage(tl("isIpBanned", args[0]));
6062
return;
6163
}
64+
else if (Bukkit.getBannedPlayers().contains(Bukkit.getOfflinePlayer(args[0]))) {
65+
sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(Bukkit.getOfflinePlayer(args[0]).getName()).getReason() : tl("true")));
66+
return;
67+
}
6268
else
6369
{
6470
throw new PlayerNotFoundException();
@@ -137,7 +143,7 @@ private void seenOffline(final Server server, final CommandSource sender, User u
137143

138144
if (user.getBase().isBanned())
139145
{
140-
sender.sendMessage(tl("whoisBanned", showBan ? user.getBanReason() : tl("true")));
146+
sender.sendMessage(tl("whoisBanned", showBan ? Bukkit.getBanList(BanList.Type.NAME).getBanEntry(user.getName()).getReason() : tl("true")));
141147
}
142148
final String location = user.getGeoLocation();
143149
if (location != null && (!(sender.isPlayer()) || ess.getUser(sender.getPlayer()).isAuthorized("essentials.geoip.show")))

Essentials/src/com/earth2me/essentials/commands/Commandtempban.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import static com.earth2me.essentials.I18n.tl;
66
import com.earth2me.essentials.User;
77
import com.earth2me.essentials.utils.DateUtil;
8+
import java.util.Date;
89
import java.util.GregorianCalendar;
910
import java.util.logging.Level;
11+
import org.bukkit.BanList;
12+
import org.bukkit.Bukkit;
1013
import org.bukkit.Server;
1114

1215

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

6265
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
6366
final String banReason = tl("tempBanned", DateUtil.formatDateDiff(banTimestamp), senderName, stringDregs);
64-
user.setBanReason(banReason);
65-
user.setBanTimeout(banTimestamp);
66-
user.getBase().setBanned(true);
67+
68+
Bukkit.getBanList(BanList.Type.NAME).addBan(user.getName(), banReason, new Date(banTimestamp), senderName);
6769
user.getBase().kickPlayer(banReason);
6870

6971
final String message = tl("playerBanned", senderName, user.getName(), banReason, DateUtil.formatDateDiff(banTimestamp));

Essentials/src/com/earth2me/essentials/commands/Commandunban.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import static com.earth2me.essentials.I18n.tl;
66
import com.earth2me.essentials.User;
77
import java.util.logging.Level;
8+
import org.bukkit.BanList;
9+
import org.bukkit.Bukkit;
810
import org.bukkit.OfflinePlayer;
911
import org.bukkit.Server;
1012

@@ -28,8 +30,7 @@ public void run(final Server server, final CommandSource sender, final String co
2830
{
2931
final User user = getPlayer(server, args, 0, true, true);
3032
name = user.getName();
31-
user.getBase().setBanned(false);
32-
user.setBanTimeout(0);
33+
Bukkit.getBanList(BanList.Type.NAME).pardon(name);
3334
}
3435
catch (NoSuchFieldException e)
3536
{
@@ -39,7 +40,7 @@ public void run(final Server server, final CommandSource sender, final String co
3940
{
4041
throw new Exception(tl("playerNotFound"), e);
4142
}
42-
player.setBanned(false);
43+
Bukkit.getBanList(BanList.Type.NAME).pardon(name);
4344
}
4445

4546
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;

Essentials/src/com/earth2me/essentials/commands/Commandunbanip.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.earth2me.essentials.User;
77
import com.earth2me.essentials.utils.FormatUtil;
88
import java.util.logging.Level;
9+
import org.bukkit.BanList;
10+
import org.bukkit.Bukkit;
911
import org.bukkit.Server;
1012

1113

@@ -46,8 +48,9 @@ public void run(final Server server, final CommandSource sender, final String co
4648
{
4749
throw new PlayerNotFoundException();
4850
}
51+
4952

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

0 commit comments

Comments
 (0)