Reworked a little Users.java

This commit is contained in:
bm01 2012-06-08 19:13:17 +02:00
parent d644b4c331
commit 90c8e57ed7
7 changed files with 80 additions and 67 deletions

View File

@ -98,7 +98,8 @@ public class McremoveCommand implements CommandExecutor {
//Force PlayerProfile stuff to update //Force PlayerProfile stuff to update
Player player = plugin.getServer().getPlayer(playerName); Player player = plugin.getServer().getPlayer(playerName);
if (player != null && Users.getProfiles().containsKey(player)) { //TODO fix this
if (player != null /*&& Users.getProfiles().containsKey(player)*/) {
Users.removeUser(player); Users.removeUser(player);
Users.addUser(player); Users.addUser(player);
} }

View File

@ -93,11 +93,18 @@ public class PlayerProfile {
lastlogin = ((Long) (System.currentTimeMillis() / 1000)).intValue(); lastlogin = ((Long) (System.currentTimeMillis() / 1000)).intValue();
} }
//This method is actually never used
public Player getPlayer() { public Player getPlayer() {
return player; return player;
} }
public String getPlayerName() {
return playerName;
}
public void setPlayer(Player player) {
this.player = player;
}
public boolean loadMySQL() { public boolean loadMySQL() {
int id = 0; int id = 0;
id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + playerName + "'"); id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + playerName + "'");

View File

@ -236,18 +236,6 @@ public class mcMMO extends JavaPlugin {
modDirectory = mainDirectory + "ModConfigs" + File.separator; modDirectory = mainDirectory + "ModConfigs" + File.separator;
} }
/**
* Get profile of the player.
* </br>
* This function is designed for API usage.
*
* @param player Player whose profile to get
* @return the PlayerProfile object
*/
public PlayerProfile getPlayerProfile(Player player) {
return Users.getProfile(player);
}
/** /**
* Get profile of the player by name. * Get profile of the player by name.
* </br> * </br>
@ -256,19 +244,19 @@ public class mcMMO extends JavaPlugin {
* @param playerName Name of player whose profile to get * @param playerName Name of player whose profile to get
* @return the PlayerProfile object * @return the PlayerProfile object
*/ */
public PlayerProfile getPlayerProfileByName(String playerName) { public PlayerProfile getPlayerProfile(String playerName) {
return Users.getProfileByName(playerName); return Users.getProfile(playerName);
} }
/** /**
* Get profile of the offline player. * Get profile of the player.
* </br> * </br>
* This function is designed for API usage. * This function is designed for API usage.
* *
* @param player Offline player whose profile to get * @param player player whose profile to get
* @return the PlayerProfile object * @return the PlayerProfile object
*/ */
public PlayerProfile getOfflinePlayerProfile(OfflinePlayer player) { public PlayerProfile getPlayerProfile(OfflinePlayer player) {
return Users.getProfile(player); return Users.getProfile(player);
} }
@ -278,8 +266,8 @@ public class mcMMO extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
//Make sure to save player information if the server shuts down //Make sure to save player information if the server shuts down
for (PlayerProfile x : Users.getProfiles().values()) { for (PlayerProfile playerProfile : Users.getProfiles()) {
x.save(); playerProfile.save();
} }
getServer().getScheduler().cancelTasks(this); //This removes our tasks getServer().getScheduler().cancelTasks(this); //This removes our tasks

View File

@ -148,10 +148,11 @@ public class Party {
*/ */
public ArrayList<Player> getAllMembers(Player player) { public ArrayList<Player> getAllMembers(Player player) {
ArrayList<Player> players = new ArrayList<Player>(); ArrayList<Player> players = new ArrayList<Player>();
HashMap<Player, PlayerProfile> profiles = Users.getProfiles();
if (player != null) { if (player != null) {
for (Player otherPlayer : profiles.keySet()) { for (PlayerProfile playerProfile : Users.getProfiles()) {
Player otherPlayer = playerProfile.getPlayer();
if (otherPlayer != null && inSameParty(otherPlayer, player)) { if (otherPlayer != null && inSameParty(otherPlayer, player)) {
players.add(otherPlayer); players.add(otherPlayer);
} }

View File

@ -2,6 +2,7 @@ package com.gmail.nossr50.runnables;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.util.Users; import com.gmail.nossr50.util.Users;
public class RemoveProfileFromMemoryTask implements Runnable { public class RemoveProfileFromMemoryTask implements Runnable {
@ -13,10 +14,15 @@ public class RemoveProfileFromMemoryTask implements Runnable {
@Override @Override
public void run() { public void run() {
PlayerProfile playerProfile = Users.getProfile(player);
//Check if the profile still exists (stuff like MySQL reconnection removes profiles) //Check if the profile still exists (stuff like MySQL reconnection removes profiles)
if (Users.getProfiles().containsKey(player)) { if (playerProfile != null) {
Users.getProfile(player).save(); //We save here so players don't quit/reconnect to cause lag playerProfile.save(); //We save here so players don't quit/reconnect to cause lag
Users.removeUser(player);
if (!player.isOnline()) {
Users.removeUser(playerProfile);
}
} }
} }
} }

View File

@ -19,13 +19,13 @@ public class SQLReconnect implements Runnable {
if (!Database.isConnected()) { if (!Database.isConnected()) {
Database.connect(); Database.connect();
if (Database.isConnected()) { if (Database.isConnected()) {
for (PlayerProfile x : Users.getProfiles().values()) { for (PlayerProfile playerProfile : Users.getProfiles()) {
x.save(); //Save all profiles playerProfile.save(); //Save all profiles
} }
Users.getProfiles().clear(); //Clear the profiles Users.clearUsers(); //Clear the profiles
for (Player x : plugin.getServer().getOnlinePlayers()) { for (Player player : plugin.getServer().getOnlinePlayers()) {
Users.addUser(x); //Add in new profiles, forcing them to 'load' again from MySQL Users.addUser(player); //Add in new profiles, forcing them to 'load' again from MySQL
} }
} }
} }

View File

@ -3,7 +3,9 @@ package com.gmail.nossr50.util;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,7 +14,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
public class Users { public class Users {
private static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>(); private static List<PlayerProfile> profiles = new ArrayList<PlayerProfile>();
/** /**
* Load users. * Load users.
@ -40,16 +42,27 @@ public class Users {
* @param player The player to create a user record for * @param player The player to create a user record for
*/ */
public static void addUser(Player player) { public static void addUser(Player player) {
if (!players.containsKey(player)) { String playerName = player.getName();
players.put(player, new PlayerProfile(player, true));
for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
PlayerProfile playerProfile = it.next();
if (playerProfile.getPlayerName().equals(playerName)) {
//The player object is different on each reconnection and must be updated
playerProfile.setPlayer(player);
return;
}
} }
//New player, or already removed from the list
profiles.add(new PlayerProfile(player, true));
} }
/** /**
* Clear all users. * Clear all users.
*/ */
public static void clearUsers() { public static void clearUsers() {
players.clear(); profiles.clear();
} }
/** /**
@ -57,8 +70,8 @@ public class Users {
* *
* @return a HashMap containing the PlayerProfile of everyone in the database * @return a HashMap containing the PlayerProfile of everyone in the database
*/ */
public static HashMap<Player, PlayerProfile> getProfiles() { public static List<PlayerProfile> getProfiles() {
return players; return profiles;
} }
/** /**
@ -66,11 +79,8 @@ public class Users {
* *
* @param player The player to remove * @param player The player to remove
*/ */
public static void removeUser(Player player) { public static void removeUser(OfflinePlayer player) {
//Only remove PlayerProfile if user is offline and we have it in memory removeUser(player.getName());
if (!player.isOnline() && players.containsKey(player)) {
players.remove(player);
}
} }
/** /**
@ -78,8 +88,22 @@ public class Users {
* *
* @param playerName The name of the player to remove * @param playerName The name of the player to remove
*/ */
public static void removeUserByName(String playerName) { public static void removeUser(String playerName) {
players.remove(mcMMO.p.getServer().getOfflinePlayer(playerName)); for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
if (it.next().getPlayer().getName().equals(playerName)) {
it.remove();
return;
}
}
}
/**
* Remove a user from the DB by its profile.
*
* @param playerProfile the profile of the player to remove
*/
public static void removeUser(PlayerProfile playerProfile) {
profiles.remove(playerProfile);
} }
/** /**
@ -89,7 +113,7 @@ public class Users {
* @return the player's profile * @return the player's profile
*/ */
public static PlayerProfile getProfile(OfflinePlayer player) { public static PlayerProfile getProfile(OfflinePlayer player) {
return players.get(player); return getProfile(player.getName());
} }
/** /**
@ -98,29 +122,15 @@ public class Users {
* @param player The name of the player whose profile to retrieve * @param player The name of the player whose profile to retrieve
* @return the player's profile * @return the player's profile
*/ */
public static PlayerProfile getProfileByName(String playerName) { public static PlayerProfile getProfile(String playerName) {
Player player = mcMMO.p.getServer().getPlayer(playerName); for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
PlayerProfile profile = players.get(player); PlayerProfile playerProfile = it.next();
if (profile == null) { if (playerProfile.getPlayerName().equals(playerName)) {
if (player != null) { return playerProfile;
PlayerProfile newProfile = new PlayerProfile(player, true);
players.put(player, newProfile);
return newProfile;
}
else {
mcMMO.p.getLogger().severe("getProfileByName(" + playerName + ") just returned null :(");
for (StackTraceElement ste : new Throwable().getStackTrace()) {
System.out.println(ste);
}
return null;
} }
} }
else {
return profile; return null;
}
} }
} }