Fix unban

This commit is contained in:
Telesphoreo 2022-01-29 19:53:22 -06:00
parent fd780463f8
commit bc41415034
9 changed files with 71 additions and 42 deletions

View File

@ -84,6 +84,7 @@ public class Plex extends JavaPlugin
{ {
config.load(); config.load();
messages.load(); messages.load();
ranksOrPermissions = config.getString("commands.permissions");
try try
{ {
@ -108,7 +109,6 @@ public class Plex extends JavaPlugin
new ListenerHandler(); // this doesn't need a variable. new ListenerHandler(); // this doesn't need a variable.
new CommandHandler(); new CommandHandler();
ranksOrPermissions = config.getString("commands.permissions");
rankManager = new RankManager(); rankManager = new RankManager();
rankManager.generateDefaultRanks(); rankManager.generateDefaultRanks();

View File

@ -24,10 +24,11 @@ public class BanManager
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
{ {
Plex.get().getMongoConnection().getDatastore().save(ban); Plex.get().getMongoConnection().getDatastore().save(ban);
} else { }
else
{
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement(INSERT); PreparedStatement statement = con.prepareStatement(INSERT);
statement.setString(1, ban.getId()); statement.setString(1, ban.getId());
statement.setString(2, ban.getUuid().toString()); statement.setString(2, ban.getUuid().toString());
@ -38,7 +39,9 @@ public class BanManager
statement.setBoolean(7, ban.isActive()); statement.setBoolean(7, ban.isActive());
statement.execute(); statement.execute();
} catch (SQLException throwables) { }
catch (SQLException throwables)
{
throwables.printStackTrace(); throwables.printStackTrace();
} }
} }
@ -50,18 +53,28 @@ public class BanManager
{ {
return Plex.get().getMongoConnection().getDatastore().find(Ban.class) return Plex.get().getMongoConnection().getDatastore().find(Ban.class)
.filter(Filters.eq("uuid", uuid.toString())).filter(Filters.eq("active", true)).first() != null; .filter(Filters.eq("uuid", uuid.toString())).filter(Filters.eq("active", true)).first() != null;
} else { }
else
{
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement(SELECT); PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery(); ResultSet set = statement.executeQuery();
if (!set.next()) return false; if (!set.next())
{
return false;
}
while (set.next()) while (set.next())
{ {
if (set.getBoolean("active")) return true; if (set.getBoolean("active"))
{
return true;
} }
} catch (SQLException throwables) { }
}
catch (SQLException throwables)
{
throwables.printStackTrace(); throwables.printStackTrace();
} }
} }
@ -77,14 +90,18 @@ public class BanManager
{ {
query.update(UpdateOperators.set("active", false)).execute(); query.update(UpdateOperators.set("active", false)).execute();
} }
} else { }
else
{
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE uuid=?"); PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE uuid=?");
statement.setBoolean(1, false); statement.setBoolean(1, false);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.executeUpdate(); statement.executeUpdate();
} catch (SQLException throwables) { }
catch (SQLException throwables)
{
throwables.printStackTrace(); throwables.printStackTrace();
} }
} }
@ -99,14 +116,18 @@ public class BanManager
{ {
query.update(UpdateOperators.set("active", false)).execute(); query.update(UpdateOperators.set("active", false)).execute();
} }
} else { }
else
{
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE banID=?"); PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE banID=?");
statement.setBoolean(1, false); statement.setBoolean(1, false);
statement.setString(2, id); statement.setString(2, id);
statement.executeUpdate(); statement.executeUpdate();
} catch (SQLException throwables) { }
catch (SQLException throwables)
{
throwables.printStackTrace(); throwables.printStackTrace();
} }
} }
@ -121,7 +142,9 @@ public class BanManager
{ {
bans.add(ban); bans.add(ban);
} }
} else { }
else
{
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement("SELECT * FROM `bans`"); PreparedStatement statement = con.prepareStatement("SELECT * FROM `bans`");
@ -140,12 +163,12 @@ public class BanManager
bans.add(ban); bans.add(ban);
} }
} }
} catch (SQLException throwables) { }
catch (SQLException throwables)
{
throwables.printStackTrace(); throwables.printStackTrace();
} }
} }
return bans; return bans;
} }
} }

View File

@ -9,6 +9,7 @@ import dev.plex.command.exception.CommandArgumentException;
import dev.plex.command.exception.CommandFailException; import dev.plex.command.exception.CommandFailException;
import dev.plex.command.exception.ConsoleMustDefinePlayerException; import dev.plex.command.exception.ConsoleMustDefinePlayerException;
import dev.plex.command.exception.ConsoleOnlyException; import dev.plex.command.exception.ConsoleOnlyException;
import dev.plex.command.exception.PlayerNotBannedException;
import dev.plex.command.exception.PlayerNotFoundException; import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
@ -123,7 +124,8 @@ public abstract class PlexCommand extends Command
send(sender, getUsage().replace("<command>", getLabel())); send(sender, getUsage().replace("<command>", getLabel()));
} }
catch (PlayerNotFoundException | CommandFailException catch (PlayerNotFoundException | CommandFailException
| ConsoleOnlyException | ConsoleMustDefinePlayerException ex) | ConsoleOnlyException | ConsoleMustDefinePlayerException
| PlayerNotBannedException ex)
{ {
send(sender, ex.getMessage()); send(sender, ex.getMessage());
} }

View File

@ -4,7 +4,6 @@ import dev.plex.command.source.RequiredCommandSource;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import org.bukkit.permissions.Permission;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface CommandPermissions public @interface CommandPermissions

View File

@ -0,0 +1,11 @@
package dev.plex.command.exception;
import static dev.plex.util.PlexUtils.tl;
public class PlayerNotBannedException extends RuntimeException
{
public PlayerNotBannedException()
{
super(tl("playerNotBanned"));
}
}

View File

@ -2,24 +2,17 @@ package dev.plex.command.impl;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import dev.plex.cache.DataUtils; import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
import dev.plex.command.PlexCommand; import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.PlayerNotBannedException;
import dev.plex.command.exception.PlayerNotFoundException; import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.time.Instant;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -40,15 +33,20 @@ public class UnbanCMD extends PlexCommand
if (args.length == 1) if (args.length == 1)
{ {
UUID targetUUID = PlexUtils.getFromName(args[0]); UUID targetUUID = PlexUtils.getFromName(args[0]);
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); Player player = getNonNullPlayer(args[0]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{ {
throw new PlayerNotFoundException(); throw new PlayerNotFoundException();
} }
if (!plugin.getBanManager().isBanned(targetUUID))
{
throw new PlayerNotBannedException();
}
plugin.getBanManager().unban(targetUUID); plugin.getBanManager().unban(targetUUID);
PlexUtils.broadcast(tl("unbanningPlayer", sender.getName(), plexPlayer.getName())); PlexUtils.broadcast(tl("unbanningPlayer", sender.getName(), player.getName()));
} }
return null; return null;
} }

View File

@ -1,7 +1,6 @@
package dev.plex.services.impl; package dev.plex.services.impl;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.banning.Ban; import dev.plex.banning.Ban;
import dev.plex.services.AbstractService; import dev.plex.services.AbstractService;
import java.util.Date; import java.util.Date;

View File

@ -162,12 +162,7 @@ public class PlexUtils extends PlexBase
{ {
return; return;
} }
int sy = Math.min(c1.getBlockY(), c2.getBlockY()), int sy = Math.min(c1.getBlockY(), c2.getBlockY()), ey = Math.max(c1.getBlockY(), c2.getBlockY()), sx = Math.min(c1.getBlockX(), c2.getBlockX()), ex = Math.max(c1.getBlockX(), c2.getBlockX()), sz = Math.min(c1.getBlockZ(), c2.getBlockZ()), ez = Math.max(c1.getBlockZ(), c2.getBlockZ());
ey = Math.max(c1.getBlockY(), c2.getBlockY()),
sx = Math.min(c1.getBlockX(), c2.getBlockX()),
ex = Math.max(c1.getBlockX(), c2.getBlockX()),
sz = Math.min(c1.getBlockZ(), c2.getBlockZ()),
ez = Math.max(c1.getBlockZ(), c2.getBlockZ());
World world = c1.getWorld(); World world = c1.getWorld();
for (int y = sy; y <= ey; y++) for (int y = sy; y <= ey; y++)
{ {
@ -238,6 +233,7 @@ public class PlexUtils extends PlexBase
{ {
return ThreadLocalRandom.current().nextInt(limit); return ThreadLocalRandom.current().nextInt(limit);
} }
public static int randomNum(int start, int limit) public static int randomNum(int start, int limit)
{ {
return ThreadLocalRandom.current().nextInt(start, limit); return ThreadLocalRandom.current().nextInt(start, limit);

View File

@ -70,3 +70,4 @@ consoleOnly: "<e>This command can only be executed by the console."
yourRank: "<b>Your rank is: <v>" yourRank: "<b>Your rank is: <v>"
banningPlayer: "<e><v> - Banning <v>" banningPlayer: "<e><v> - Banning <v>"
unbanningPlayer: "<b><v> - Unbanning <v>" unbanningPlayer: "<b><v> - Unbanning <v>"
playerNotBanned: "<e>That player is not banned!"