Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.function.Consumer;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.banning.Ban;
import me.totalfreedom.totalfreedommod.command.FreedomCommand;
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.fun.Jumppads;
import me.totalfreedom.totalfreedommod.player.FPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import me.totalfreedom.totalfreedommod.blocking.command.CommandBlocker;
import me.totalfreedom.totalfreedommod.bridge.*;
import me.totalfreedom.totalfreedommod.caging.Cager;
import me.totalfreedom.totalfreedommod.command.CommandLoader;
import me.totalfreedom.totalfreedommod.command.handling.CommandLoader;
import me.totalfreedom.totalfreedommod.config.MainConfig;
import me.totalfreedom.totalfreedommod.freeze.Freezer;
import me.totalfreedom.totalfreedommod.fun.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.rank.DisplayableGroup;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class Admin
Expand All @@ -32,23 +30,22 @@ public Admin(Player player)
this.ips.add(FUtil.getIp(player));
}

public Admin(ResultSet resultSet)
public Admin(ResultSetProvider resultSet)
{
try
if (resultSet.getString("uuid") == null)
{
this.uuid = UUID.fromString(resultSet.getString("uuid"));
this.active = resultSet.getBoolean("active");
this.rank = GroupProvider.fromArgument(resultSet.getString("rank")).getGroup();
this.ips.clear();
this.ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
this.lastLogin = new Date(resultSet.getLong("last_login"));
this.commandSpy = resultSet.getBoolean("command_spy");
this.potionSpy = resultSet.getBoolean("potion_spy");
this.acFormat = resultSet.getString("ac_format");
} catch (SQLException e)
{
FLog.severe("Failed to load admin: " + e.getMessage());
throw new IllegalArgumentException("Failed to load admin, as the UUID is null.");
}

this.uuid = UUID.fromString(resultSet.getString("uuid"));
this.active = resultSet.getBoolean("active");
this.rank = GroupProvider.fromString(resultSet.getString("rank")).getGroup();
this.ips.clear();
this.ips.addAll(FUtil.stringToList(resultSet.getString("ips")));
this.lastLogin = new Date(resultSet.getLong("last_login"));
this.commandSpy = resultSet.getBoolean("command_spy");
this.potionSpy = resultSet.getBoolean("potion_spy");
this.acFormat = resultSet.getString("ac_format");
}

@Override
Expand All @@ -70,15 +67,15 @@ public String toString()
public Map<String, Object> toSQLStorable()
{
HashMap<String, Object> map = new HashMap<>();
map.put("uuid", uuid.toString());
map.put("active", active);
map.put("rank", rank.toString());
map.put("ips", FUtil.listToString(ips));
map.put("last_login", lastLogin.getTime());
map.put("command_spy", commandSpy);
map.put("potion_spy", potionSpy);
map.put("ac_format", acFormat);
return map;
map.put("uuid", uuid.toString());
map.put("active", active);
map.put("rank", rank.toString());
map.put("ips", FUtil.listToString(ips));
map.put("last_login", lastLogin.getTime());
map.put("command_spy", commandSpy);
map.put("potion_spy", potionSpy);
map.put("ac_format", acFormat);
return map;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.sql.ResultSetProvider;
import me.totalfreedom.totalfreedommod.rank.Hierarchy;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AdminList extends FreedomService
{
public static final List<UUID> vanished = new ArrayList<>();
public static final String SQL_SAVE_FAILED = "Failed to save admin: ";
private final Set<Admin> allAdmins = Sets.newHashSet(); // Includes disabled admins
// Only active admins below
private final Set<Admin> activeAdmins = Sets.newHashSet();
Expand All @@ -43,33 +46,37 @@ public void onStop()
public void load()
{
allAdmins.clear();
try
plugin.sql.getAdminList().thenAcceptAsync(provider ->
{
ResultSet adminSet = plugin.sql.getAdminList();
while (adminSet.next())
AtomicInteger row = new AtomicInteger(1);
provider.getAllRowsResultSet().forEach(adminSet ->
{
tryAddAdmin(adminSet);
}
} catch (SQLException e)
try
{
tryAddAdmin(ResultSetProvider.fromRow(adminSet));
row.set(row.get() + 1);
} catch (Throwable e)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allman's style

{
FLog.warning("An error occurred whilst reading the admin entry at row #" + row.get());
throw new CompletionException(e); // handle downstream.
}
});
}).whenComplete((result, throwable) ->
{
FLog.severe("Failed to load admin list: " + e.getMessage());
}

updateTables();
FLog.info("Loaded " + allAdmins.size() + " admins (" + uuidTable.size() + " active, " + ipTable.size() + " IPs)");
if (throwable != null)
{
FLog.severe(throwable.getMessage());
return;
}
updateTables();
FLog.info("Loaded " + allAdmins.size() + " admins (" + uuidTable.size() + " active, " + ipTable.size() + " IPs)");
});
}

private void tryAddAdmin(ResultSet adminSet) throws SQLException
private void tryAddAdmin(ResultSetProvider adminSet)
{
try
{
Admin admin = new Admin(adminSet);
allAdmins.add(admin);
} catch (Throwable ex)
{
FLog.warning("An error occurred whilst reading the admin entry at row #" + adminSet.getRow());
FLog.warning(ex);
}
Admin admin = new Admin(adminSet);
allAdmins.add(admin);
}

public void messageAllAdmins(Component message)
Expand Down Expand Up @@ -106,9 +113,10 @@ public boolean isAdmin(CommandSender sender)
return admin != null && admin.isActive();
}

// Cast to OfflinePlayer
public boolean isAdmin(Player player)
{
if (Hierarchy.getHierarchy().isUserOnAdminTrack(player)) return true;

return isAdmin((OfflinePlayer) player);
}

Expand All @@ -124,19 +132,6 @@ public boolean isAdmin(OfflinePlayer player)
return admin != null && admin.isActive();
}

public boolean isSeniorAdmin(CommandSender sender)
{
Admin admin = getAdmin(sender);
if (admin == null)
{
return false;
}

return admin.getRank().getLuckPermsGroup().getWeight().orElse(0)
>= GroupProvider.SENIOR_ADMIN.getGroup().getLuckPermsGroup().getWeight().orElse(1);
// We don't want this to return true if there's no group weight available.
}

public Admin getAdmin(CommandSender sender)
{
if (sender instanceof Player player)
Expand Down Expand Up @@ -198,7 +193,13 @@ public void addAdmin(Admin admin)
updateTables();

// Save admin
plugin.sql.addAdmin(admin);
plugin.sql.addAdmin(admin).whenComplete((result, exception) ->
{
if (exception != null)
{
FLog.warning(SQL_SAVE_FAILED + admin.getName() + " to the database.\n" + exception.getMessage());
}
});
}

public boolean removeAdmin(Admin admin)
Expand All @@ -216,7 +217,13 @@ public boolean removeAdmin(Admin admin)
updateTables();

// Unsave admin
plugin.sql.removeAdmin(admin);
plugin.sql.removeAdmin(admin).whenComplete((result, exception) ->
{
if (exception != null)
{
FLog.warning("Failed to remove admin: " + admin.getName() + " from the database.\n" + exception.getMessage());
}
});

return true;
}
Expand Down Expand Up @@ -261,21 +268,30 @@ public Set<String> getAdminIps()

public void save(Admin admin)
{
try
plugin.sql.getAdminByUuid(admin.getUuid()).thenApplyAsync(result ->
{
ResultSet currentSave = plugin.sql.getAdminByUuid(admin.getUuid());
for (Map.Entry<String, Object> entry : admin.toSQLStorable().entrySet())
{
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());
Object storedValue = plugin.sql.getValue(result, entry.getKey(), entry.getValue());
if (storedValue != null && !storedValue.equals(entry.getValue()) || storedValue == null && entry.getValue() != null || entry.getValue() == null)
{
plugin.sql.setAdminValue(admin, entry.getKey(), entry.getValue());
plugin.sql.setAdminValue(admin, entry.getKey(), entry.getValue()).whenComplete((result2, exception) ->
{
if (exception != null)
{
throw new CompletionException(exception); // We want to handle downstream in #whenComplete()
}
});
}
}
} catch (SQLException e)
return null;
}).whenComplete((result, exception) ->
{
FLog.severe("Failed to save admin: " + e.getMessage());
}
if (exception != null)
{
FLog.severe(SQL_SAVE_FAILED + exception.getMessage());
}
});
}

public void deactivateOldEntries(boolean verbose)
Expand Down
Loading