Skip to content

Commit

Permalink
Implemented UUID-based player handling in Asshat module
Browse files Browse the repository at this point in the history
----

Addresses #112
Non-UUID classes in the Asshat module have been marked deprecated and will be removed soon.
A system to convert the old DB table is still required but should be very easy to implement. However, names were stored lower-case while Mojang's API is case-sensitive.
Tab-completion for the UUID table is currently not supported.
Player-login and /ban, /unban, /banreason, /gag, /ungag commands can slow down the server's main event loop since a synchronous web request is sent to resolve a player's UUID.
  • Loading branch information
nristock committed Jun 17, 2014
1 parent 8933399 commit b1ac7b7
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
* Represents a list of banned players.
Expand All @@ -27,6 +28,7 @@ private Banlist()
*
* @return Returns true if the ban operation was successful. False indicates that the player is already banned.
*/
@Deprecated
public static boolean ban(final String playerName, final String banReason)
{
if (isPlayerBanned(playerName))
Expand All @@ -38,13 +40,25 @@ public static boolean ban(final String playerName, final String banReason)
return true;
}

public static boolean ban(final UUID playerUid, final String banReason)
{
if (isPlayerBanned(playerUid))
{
return false;
}

Persistence.getInstance().save(new BannedUUIDPlayer(playerUid, banReason));
return true;
}

/**
* Unbans the player named playerName.
*
* @param playerName The name of the player to unban.
*
* @return Returns true if the unban operation was successful. False indicates that the player is not banned.
*/
@Deprecated
public static boolean unban(final String playerName)
{
if (!isPlayerBanned(playerName))
Expand All @@ -56,11 +70,23 @@ public static boolean unban(final String playerName)
return true;
}

public static boolean unban(final UUID playerUid)
{
if (!isPlayerBanned(playerUid))
{
return false;
}

Persistence.getInstance().delete(getBannedPlayer(playerUid));
return true;
}

/**
* Loads all banned players from database and returns a String list of the player names.
*
* @return Returns a list of the names of all banned players.
*/
@Deprecated
public static List<String> getBannedNames()
{
final List<BannedPlayer> bannedPlayers = Persistence.getInstance().loadAll(BannedPlayer.class);
Expand All @@ -72,16 +98,28 @@ public static List<String> getBannedNames()
return bannedNames;
}

public static List<UUID> getBannedUUIDs()
{
final List<BannedUUIDPlayer> bannedPlayers = Persistence.getInstance().loadAll(BannedUUIDPlayer.class);
final List<UUID> bannedUUIDs = new ArrayList<>();
for (final BannedUUIDPlayer bannedPlayer : bannedPlayers)
{
bannedUUIDs.add(bannedPlayer.getPlayerUUID());
}
return bannedUUIDs;
}

/**
* Returns the number of banned players.
*
* @return Returns the number of banned players.
*/
public static int getBanCount()
{
return getBannedNames().size();
return getBannedNames().size() + getBannedUUIDs().size();
}

@Deprecated
private static BannedPlayer getBannedPlayer(final String playerName)
{
HashMap<String, Object> selectRestrictions = new HashMap<>();
Expand All @@ -100,28 +138,59 @@ private static BannedPlayer getBannedPlayer(final String playerName)
return null;
}

private static BannedUUIDPlayer getBannedPlayer(final UUID playerUUID)
{
HashMap<String, Object> selectRestrictions = new HashMap<>();
selectRestrictions.put("playerUUID", playerUUID);

final List<BannedUUIDPlayer> bannedPlayers = Persistence.getInstance().loadAll(BannedUUIDPlayer.class, selectRestrictions);

for (final BannedUUIDPlayer bannedPlayer : bannedPlayers)
{
if (bannedPlayer.getPlayerUUID().equals(playerUUID))
{
return bannedPlayer;
}
}

return null;
}

/**
* Checks if a player is banned.
*
* @param playerName The name of the player to check.
*
* @return Returns true if the player is banned, otherwise false.
*/
@Deprecated
public static boolean isPlayerBanned(final String playerName)
{
return getBannedPlayer(playerName) != null;
}

public static boolean isPlayerBanned(final UUID playerUUID)
{
return getBannedPlayer(playerUUID) != null;
}

/**
* Gets the reason why a player is banned.
*
* @param playerName The name of the player to check.
*
* @return Returns the reason the player is banned for.
*/
@Deprecated
public static String getPlayerBanreason(final String playerName)
{
Preconditions.checkState(isPlayerBanned(playerName), "Player %s must be banned in order to get the ban reason.", playerName);
return getBannedPlayer(playerName).getBanReason();
}

public static String getPlayerBanreason(final UUID playerUUID)
{
Preconditions.checkState(isPlayerBanned(playerUUID), "Player %s must be banned in order to get the ban reason.", playerUUID);
return getBannedPlayer(playerUUID).getBanReason();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Monofraps
*/
@DatabaseTable(tableName = "bans")
@Deprecated
public class BannedPlayer
{
@DatabaseField(generatedId = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thevoxelbox.voxelguest.modules.asshat.ban;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import java.util.UUID;

/**
* @author Monofraps
*/
@DatabaseTable(tableName = "bans_uuid")
public class BannedUUIDPlayer
{
@DatabaseField(generatedId = true)
private long id;
@DatabaseField
private UUID playerUUID;
@DatabaseField
private String banReason;

/**
* ORM constructor.
*/
public BannedUUIDPlayer()
{
}

/**
* @param playerName The name of the banned player.
* @param banReason The reason the player is banned for.
*/
public BannedUUIDPlayer(final UUID playerUUID, final String banReason)
{
this.playerUUID = playerUUID;
this.banReason = banReason;
}

public final UUID getPlayerUUID()
{
return playerUUID;
}

/**
* @return Returns the reason the player is banned for.
*/
public final String getBanReason()
{
return banReason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.thevoxelbox.voxelguest.modules.asshat.AsshatModuleConfiguration;
import com.thevoxelbox.voxelguest.modules.asshat.ban.Banlist;
import com.thevoxelbox.voxelguest.modules.asshat.command.argument.AsshatCommandArguments;
import com.thevoxelbox.voxelguest.utils.UUIDFetcher;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand All @@ -15,6 +16,7 @@

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* Executes /ban commands.
Expand Down Expand Up @@ -88,15 +90,28 @@ public final boolean onCommand(final CommandSender commandSender, final Command

private void safeBan(final String playerName, final String banReason, final CommandSender commandSender, final boolean silentFlag)
{
if (Banlist.isPlayerBanned(playerName))
// Resolve player name to UUID
final UUID playerUUID;
try
{
playerUUID = UUIDFetcher.getUUIDOf(playerName);
}
catch (Exception e)
{
e.printStackTrace();
commandSender.sendMessage("An error occurred resolving the player's UUID - check your input or file a bug report.");
return;
}

if (Banlist.isPlayerBanned(playerUUID))
{
commandSender.sendMessage(String.format("Player %s is already banned.", playerName));
return;
}

try
{
Banlist.ban(playerName, banReason);
Banlist.ban(playerUUID, banReason);
Bukkit.getLogger().info(String.format("%s banned by %s for %s", playerName, commandSender.getName(), banReason));
if (!silentFlag)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.thevoxelbox.voxelguest.modules.asshat.command;

import com.thevoxelbox.voxelguest.modules.asshat.ban.Banlist;
import com.thevoxelbox.voxelguest.utils.UUIDFetcher;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* Executes /banreason commands.
Expand All @@ -25,23 +27,36 @@ public final boolean onCommand(final CommandSender commandSender, final Command
return false;
}

final String playerName = args[0].toLowerCase();
final String playerName = args[0];
final UUID playerUUID;
try
{
playerUUID = UUIDFetcher.getUUIDOf(playerName);
}
catch (Exception e)
{
e.printStackTrace();
commandSender.sendMessage("An error occurred resolcing the players UUID - check your input or file a bug report.");
return true;
}

if (!Banlist.isPlayerBanned(playerName))
if (!Banlist.isPlayerBanned(playerUUID))
{
commandSender.sendMessage(String.format("Player %s is not banned.", playerName));
return true;
}

commandSender.sendMessage(String.format("%s is banned for %s", playerName, Banlist.getPlayerBanreason(playerName)));
commandSender.sendMessage(String.format("%s is banned for %s", playerName, Banlist.getPlayerBanreason(playerUUID)));
return true;
}

@Override
@Deprecated
public final List<String> onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] args)
{
if (commandSender.hasPermission("voxelguest.asshat.banreason"))
{
commandSender.sendMessage("You are using deprecated functionality. This feature will be removed with the next VG release.");
final List<String> bannedNamesList = Banlist.getBannedNames();
if (args.length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.thevoxelbox.voxelguest.modules.asshat.AsshatModuleConfiguration;
import com.thevoxelbox.voxelguest.modules.asshat.command.argument.AsshatCommandArguments;
import com.thevoxelbox.voxelguest.modules.asshat.mute.Mutelist;
import com.thevoxelbox.voxelguest.utils.UUIDFetcher;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand All @@ -15,6 +16,7 @@

import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* Executes /mute and /gag commands.
Expand Down Expand Up @@ -86,15 +88,27 @@ public final boolean onCommand(final CommandSender commandSender, final Command

private void safeMute(final String playerName, final String muteReason, final CommandSender commandSender, final boolean silentFlag, final boolean selfUngag)
{
if (Mutelist.isPlayerMuted(playerName))
final UUID playerUUID;
try
{
playerUUID = UUIDFetcher.getUUIDOf(playerName);
}
catch (Exception e)
{
e.printStackTrace();
commandSender.sendMessage("An error occurred while resolving the player's UUID - please check your input or file a bug report.");
return;
}

if (Mutelist.isPlayerMuted(playerUUID))
{
commandSender.sendMessage(String.format("Player %s is already gagged.", playerName));
return;
}

try
{
Mutelist.mute(playerName, muteReason, selfUngag);
Mutelist.mute(playerUUID, muteReason, selfUngag);
Bukkit.getLogger().info(String.format("%s gagged by %s for %s", playerName, commandSender.getName(), muteReason));
if (!silentFlag)
{
Expand Down
Loading

0 comments on commit b1ac7b7

Please sign in to comment.