Skip to content

Commit

Permalink
v2.0.24
Browse files Browse the repository at this point in the history
* implemented lock/unlock force feature
* implemented lock/unlock all command
* added new permission node xauth.admin.count
* added new command xauth count <all|active|locked>
* if lock is executed in-game the authenticated user is excluded from the lock list
  • Loading branch information
lycano committed Oct 14, 2012
1 parent 4ad2821 commit c2db71e
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.luricos.bukkit</groupId>
<version>2.0.23</version>
<version>2.0.24</version>
<artifactId>xAuth</artifactId>
<packaging>jar</packaging>
<name>xAuth</name>
Expand Down
96 changes: 93 additions & 3 deletions src/main/java/com/cypherx/xauth/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.cypherx.xauth.database.Table;
import com.cypherx.xauth.exceptions.xAuthPlayerUnprotectException;
import com.cypherx.xauth.utils.Utils;
import com.cypherx.xauth.utils.xAuthLog;
import com.cypherx.xauth.xAuthPlayer.Status;
import org.bukkit.Bukkit;
Expand All @@ -30,7 +31,9 @@
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlayerManager {
Expand Down Expand Up @@ -83,6 +86,24 @@ public xAuthPlayer getPlayerById(int id) {
return null;
}

public List<xAuthPlayer> getPlayers(List<String> playerNames) {
List<xAuthPlayer> xPlayers = new ArrayList<xAuthPlayer>();
for (String playerName: playerNames) {
xPlayers.add(getPlayer(playerName));
}

return xPlayers;
}

public List<xAuthPlayer> getPlayersByIds(List<Integer> accountIds) {
List<xAuthPlayer> xPlayers = new ArrayList<xAuthPlayer>();
for (int accountId: accountIds) {
xPlayers.add(getPlayerById(accountId));
}

return xPlayers;
}

public boolean hasAccountId(int id) {
return playerIds.containsKey(id);
}
Expand Down Expand Up @@ -314,14 +335,14 @@ public boolean isActive(int id) {
}

public boolean activateAcc(int id) {
return setActive(id, true);
return setActiveState(id, true);
}

public boolean lockAcc(int id) {
return setActive(id, false);
return setActiveState(id, false);
}

private boolean setActive(int id, boolean active) {
private boolean setActiveState(int id, boolean active) {
Connection conn = plugin.getDatabaseController().getConnection();
PreparedStatement ps = null;

Expand All @@ -343,6 +364,75 @@ private boolean setActive(int id, boolean active) {
}
}

public boolean activateAll() {
return setAllActiveStates(true, null);
}

public boolean lockAll() {
return setAllActiveStates(false, null);
}

public boolean setAllActiveStates(boolean state, Integer[] excludeIds) {
Connection conn = plugin.getDatabaseController().getConnection();
PreparedStatement ps = null;

try {
String query = "UPDATE `%s` SET `active` = %d";
if ((excludeIds != null) && (excludeIds.length > 0))
query = "UPDATE `%s` SET `active` = %d WHERE `id` NOT IN (" + Utils.join(excludeIds) + ")";

String sql = String.format(query, plugin.getDatabaseController().getTable(Table.ACCOUNT), ((state) ? 1 : 0));
ps = conn.prepareStatement(sql);
ps.executeUpdate();

// clear cache
reload();

return true;
} catch (SQLException e) {
xAuthLog.severe("Failed to " + ((state) ? "activate" : "lock") + " accounts", e);
return false;
} finally {
plugin.getDatabaseController().close(conn, ps);
}
}

public Integer countAll() {
return getActiveStatesCount(false, true);
}

public Integer countActive() {
return getActiveStatesCount(true, false);
}

public Integer countLocked() {
return getActiveStatesCount(false, false);
}

private Integer getActiveStatesCount(boolean state, boolean bypassState) {
Connection conn = plugin.getDatabaseController().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;

try {
String query = "SELECT COUNT(*) AS `state` FROM `%s` WHERE `active` = %d";
if (bypassState)
query = "SELECT COUNT(*) AS `state` FROM `%s`";

String sql = String.format(query, plugin.getDatabaseController().getTable(Table.ACCOUNT), ((state) ? 1 : 0));
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
rs.next();

return rs.getInt("state");
} catch (SQLException e) {
xAuthLog.severe("Failed to check " + ((state) ? "active" : "lock") + " state", e);
return null;
} finally {
plugin.getDatabaseController().close(conn, ps);
}
}

public boolean doLogin(xAuthPlayer xp) {
int accountId = xp.getAccountId();
String ipAddress = xp.getIPAddress();
Expand Down
82 changes: 77 additions & 5 deletions src/main/java/com/cypherx/xauth/commands/xAuthCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.bukkit.entity.Player;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;

public class xAuthCommand implements CommandExecutor {
Expand All @@ -57,10 +59,12 @@ else if (subCommand.equals("location") || subCommand.equals("loc"))
return locationCommand(sender, args);
else if (subCommand.equals("reload"))
return reloadCommand(sender, args);
else if (subCommand.equals("activate"))
else if ((subCommand.equals("activate")) || (subCommand.equals("unlock")))
return activateCommand(sender, args);
else if (subCommand.equals("lock"))
return lockCommand(sender, args);
else if (subCommand.equals("count"))
return countCommand(sender, args);
else if (subCommand.equals("config") || subCommand.equals("conf"))
return configCommand(sender, args);
else if (subCommand.equals("profile") || subCommand.equals("info"))
Expand Down Expand Up @@ -268,6 +272,10 @@ private boolean reloadCommand(CommandSender sender, String[] args) {
}

private boolean activateCommand(CommandSender sender, String[] args) {
return activateCommand(sender, args, false);
}

private boolean activateCommand(CommandSender sender, String[] args, boolean force) {
if (!xAuth.getPermissionManager().has(sender, "xauth.admin.activate")) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.permission", sender);
return true;
Expand All @@ -277,13 +285,21 @@ private boolean activateCommand(CommandSender sender, String[] args) {
}

String targetName = args[1];
force = ((args.length > 2) && (args[2].equals("force")));
xAuthPlayer xp = xAuth.getPlugin().getPlayerManager().getPlayer(targetName);
if (targetName.equals("*")) {
Integer countState = xAuth.getPlugin().getPlayerManager().countLocked();
boolean success = xAuth.getPlugin().getPlayerManager().setAllActiveStates(true, null);

xAuth.getPlugin().getMessageHandler().sendMessage(success ? "admin.activate.successM" : "admin.activate.error.generalM", sender, countState.toString());
return true;
}

if (!xp.isRegistered()) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.activate.error.registered", sender);
xAuth.getPlugin().getMessageHandler().sendMessage("admin.activate.error.registered", sender, targetName);
return true;
} else if (xAuth.getPlugin().getPlayerManager().isActive(xp.getAccountId())) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.activate.error.active", sender);
} else if ((!force) && (xAuth.getPlugin().getPlayerManager().isActive(xp.getAccountId()))) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.activate.error.active", sender, targetName);
return true;
}

Expand All @@ -294,6 +310,10 @@ private boolean activateCommand(CommandSender sender, String[] args) {
}

private boolean lockCommand(CommandSender sender, String[] args) {
return lockCommand(sender, args, false);
}

private boolean lockCommand(CommandSender sender, String[] args, boolean force) {
if (!xAuth.getPermissionManager().has(sender, "xauth.admin.lock")) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.permission", sender);
return true;
Expand All @@ -303,12 +323,26 @@ private boolean lockCommand(CommandSender sender, String[] args) {
}

String targetName = args[1];
force = ((args.length > 2) && (args[2].equals("force")));
xAuthPlayer xp = xAuth.getPlugin().getPlayerManager().getPlayer(targetName);
if (targetName.equals("*")) {
Integer countState = xAuth.getPlugin().getPlayerManager().countActive();
if (sender instanceof Player) {
xp = xAuth.getPlugin().getPlayerManager().getPlayer(sender.getName());
if (countState > 0)
countState--;
}

boolean success = xAuth.getPlugin().getPlayerManager().setAllActiveStates(false, new Integer[]{xp.getAccountId()});

xAuth.getPlugin().getMessageHandler().sendMessage(success ? "admin.lock.successM" : "admin.lock.error.generalM", sender, countState.toString());
return true;
}

if (!xp.isRegistered()) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.lock.error.registered", sender);
return true;
} else if (!xAuth.getPlugin().getPlayerManager().isActive(xp.getAccountId())) {
} else if ((!force) && (!xAuth.getPlugin().getPlayerManager().isActive(xp.getAccountId()))) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.lock.error.locked", sender);
return true;
}
Expand All @@ -319,6 +353,44 @@ private boolean lockCommand(CommandSender sender, String[] args) {
return true;
}

private boolean countCommand(CommandSender sender, String[] args) {
if (!xAuth.getPermissionManager().has(sender, "xauth.admin.count")) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.permission", sender);
return true;
} else if (args.length < 2) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.count.usage", sender);
return true;
}

Integer count = 0;
Integer mode = -1;
String modeName = args[1];
List<Integer> modesIds = Arrays.asList(0, 1, 2);
List<String> modeNames = Arrays.asList("all", "active", "locked");
if (modeNames.contains(modeName))
mode = modesIds.get(modeNames.indexOf(modeName));

switch(mode) {
case 0:
count = xAuth.getPlugin().getPlayerManager().countAll();
xAuth.getPlugin().getMessageHandler().sendMessage("admin.count.successAll", sender, count.toString());
break;
case 1:
count = xAuth.getPlugin().getPlayerManager().countActive();
xAuth.getPlugin().getMessageHandler().sendMessage("admin.count.successA", sender, count.toString());
break;
case 2:
count = xAuth.getPlugin().getPlayerManager().countLocked();
xAuth.getPlugin().getMessageHandler().sendMessage("admin.count.successL", sender, count.toString());
break;
default:
xAuth.getPlugin().getMessageHandler().sendMessage("admin.count.usage", sender);
break;
}

return true;
}

private boolean configCommand(CommandSender sender, String[] args) {
if (!xAuth.getPermissionManager().has(sender, "xauth.admin.config")) {
xAuth.getPlugin().getMessageHandler().sendMessage("admin.permission", sender);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/cypherx/xauth/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,19 @@ public static String replaceColors(String s) {

return s;
}

public static String join(Object[] collection) {
return join(collection, ",");
}

public static String join(Object[] collection, String with) {
StringBuilder sb = new StringBuilder("");
String delim = "";
for (Object el: collection) {
sb.append(delim).append(el.toString());
delim = with;
}

return sb.toString();
}
}
31 changes: 23 additions & 8 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ admin:
activate:
usage: '{RED}Correct Usage: /xauth activate <player>'
error:
registered: '{RED}This player is not registered!'
active: '{RED}This player is already active!'
general: '{RED}Something went wrong while activating this player!'
success: '{BRIGHTGREEN}{TARGET} has been activated!'
registered: '{RED}The player {TARGET} is not registered!'
registeredM: '{RED}{TARGET} aren''t registered!'
active: '{RED}The account for {TARGET} is already active!'
activeM: '{RED}The accounts for {TARGET} are already active!'
general: '{RED}Something went wrong while activating the account {TARGET}!'
generalM: '{RED}Something went wrong while activating the accounts!'
success: '{BRIGHTGREEN}The account for {TARGET} has been activated!'
successM: '{BRIGHTGREEN}{TARGET} accounts have been activated!'
profile:
usage: '{RED}Correct Usage: /xauth profile [player]'
error:
Expand All @@ -127,10 +131,21 @@ admin:
lock:
usage: '{RED}Correct Usage: /xauth lock <player>'
error:
registered: '{RED}This player is not registered!'
locked: '{RED}This player is already locked!'
general: '{RED}Something went wrong while locking this player!'
success: '{BRIGHTGREEN}{TARGET} has been locked!'
registered: '{RED}The player {TARGET} is not registered!'
registeredM: '{RED}The players {TARGET} aren''t registered!'
locked: '{RED}The account for {TARGET} is already locked!'
lockedM: '{RED}The accounts for {TARGET} are already locked!'
general: '{RED}Something went wrong while locking the account {TARGET}!'
generalM: '{RED}Something went wrong while locking the accounts!'
success: '{BRIGHTGREEN}The account for {TARGET} has been locked!'
successM: '{BRIGHTGREEN}{TARGET} accounts have been locked!'
count:
usage: '{RED}Correct Usage: /xauth count <all|active|locked>'
error:
general: '{RED}Something went wrong while counting the accounts!'
successAll: '{BRIGHTGREEN}{TARGET} accounts are currently registered'
successA: '{BRIGHTGREEN}{TARGET} accounts are currently active'
successL: '{BRIGHTGREEN}{TARGET} accounts are currently locked'
config:
usage: '{RED}Correct Usage: /xauth config <node> <value>'
error:
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: xAuth
version: 2.0.23
version: 2.0.24
description: Offline-mode Authentication
author: [CypherX, lycano]
website: https://github.com/lycano/xAuth
Expand Down Expand Up @@ -37,6 +37,7 @@ commands:
/<command> reload - reload xauth
/<command> activate <player> - activate a player's account
/<command> lock <player> - lock a player's account
/<command> count <all|active|locked> - Display the number of accounts for each state
/<command> profile <player> - show the info page for a player
/<command> info <player> - see profile <player>
/<command> config <node> <value> - set/get configuration nodes
Expand All @@ -58,6 +59,7 @@ permissions:
xauth.admin.location: true
xauth.admin.reload: true
xauth.admin.activate: true
xauth.admin.count: true
xauth.admin.lock: true
xauth.admin.config: true
xauth.admin.profile: true
Expand Down Expand Up @@ -85,6 +87,9 @@ permissions:
xauth.admin.activate:
description: Allows you to activate a player's account.
default: op
xauth.admin.count:
description: Allows you to count the account states.
default: op
xauth.admin.lock:
description: Allows you to lock a player's account.
default: op
Expand Down

0 comments on commit c2db71e

Please sign in to comment.