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
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.addUser(player);
}

View File

@ -93,11 +93,18 @@ public class PlayerProfile {
lastlogin = ((Long) (System.currentTimeMillis() / 1000)).intValue();
}
//This method is actually never used
public Player getPlayer() {
return player;
}
public String getPlayerName() {
return playerName;
}
public void setPlayer(Player player) {
this.player = player;
}
public boolean loadMySQL() {
int id = 0;
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;
}
/**
* 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.
* </br>
@ -256,19 +244,19 @@ public class mcMMO extends JavaPlugin {
* @param playerName Name of player whose profile to get
* @return the PlayerProfile object
*/
public PlayerProfile getPlayerProfileByName(String playerName) {
return Users.getProfileByName(playerName);
public PlayerProfile getPlayerProfile(String playerName) {
return Users.getProfile(playerName);
}
/**
* Get profile of the offline player.
* Get profile of the player.
* </br>
* 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
*/
public PlayerProfile getOfflinePlayerProfile(OfflinePlayer player) {
public PlayerProfile getPlayerProfile(OfflinePlayer player) {
return Users.getProfile(player);
}
@ -278,8 +266,8 @@ public class mcMMO extends JavaPlugin {
@Override
public void onDisable() {
//Make sure to save player information if the server shuts down
for (PlayerProfile x : Users.getProfiles().values()) {
x.save();
for (PlayerProfile playerProfile : Users.getProfiles()) {
playerProfile.save();
}
getServer().getScheduler().cancelTasks(this); //This removes our tasks

View File

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

View File

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

View File

@ -19,13 +19,13 @@ public class SQLReconnect implements Runnable {
if (!Database.isConnected()) {
Database.connect();
if (Database.isConnected()) {
for (PlayerProfile x : Users.getProfiles().values()) {
x.save(); //Save all profiles
for (PlayerProfile playerProfile : Users.getProfiles()) {
playerProfile.save(); //Save all profiles
}
Users.getProfiles().clear(); //Clear the profiles
for (Player x : plugin.getServer().getOnlinePlayers()) {
Users.addUser(x); //Add in new profiles, forcing them to 'load' again from MySQL
Users.clearUsers(); //Clear the profiles
for (Player player : plugin.getServer().getOnlinePlayers()) {
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.FileWriter;
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.entity.Player;
@ -12,7 +14,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile;
public class Users {
private static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
private static List<PlayerProfile> profiles = new ArrayList<PlayerProfile>();
/**
* Load users.
@ -40,16 +42,27 @@ public class Users {
* @param player The player to create a user record for
*/
public static void addUser(Player player) {
if (!players.containsKey(player)) {
players.put(player, new PlayerProfile(player, true));
String playerName = player.getName();
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.
*/
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
*/
public static HashMap<Player, PlayerProfile> getProfiles() {
return players;
public static List<PlayerProfile> getProfiles() {
return profiles;
}
/**
@ -66,11 +79,8 @@ public class Users {
*
* @param player The player to remove
*/
public static void removeUser(Player player) {
//Only remove PlayerProfile if user is offline and we have it in memory
if (!player.isOnline() && players.containsKey(player)) {
players.remove(player);
}
public static void removeUser(OfflinePlayer player) {
removeUser(player.getName());
}
/**
@ -78,8 +88,22 @@ public class Users {
*
* @param playerName The name of the player to remove
*/
public static void removeUserByName(String playerName) {
players.remove(mcMMO.p.getServer().getOfflinePlayer(playerName));
public static void removeUser(String 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
*/
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
* @return the player's profile
*/
public static PlayerProfile getProfileByName(String playerName) {
Player player = mcMMO.p.getServer().getPlayer(playerName);
PlayerProfile profile = players.get(player);
public static PlayerProfile getProfile(String playerName) {
for (Iterator<PlayerProfile> it = profiles.iterator() ; it.hasNext() ; ) {
PlayerProfile playerProfile = it.next();
if (profile == null) {
if (player != null) {
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;
if (playerProfile.getPlayerName().equals(playerName)) {
return playerProfile;
}
}
else {
return profile;
}
return null;
}
}