mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-01-31 22:59:35 +01:00
Replaced playerName fields in PlayerProfile and Users by the Player object
This commit is contained in:
parent
b8be1d1866
commit
56aff1d191
@ -16,7 +16,7 @@ public class ExperienceAPI {
|
||||
*/
|
||||
private void checkXP(Player player, SkillType skillType) {
|
||||
if (skillType.equals(SkillType.ALL)) {
|
||||
Skills.xpCheckAll(player);
|
||||
Skills.xpCheckAll(player, Users.getProfile(player));
|
||||
}
|
||||
else {
|
||||
Skills.xpCheckSkill(skillType, player, Users.getProfile(player));
|
||||
@ -61,7 +61,7 @@ public class ExperienceAPI {
|
||||
* @param XP The amount of XP to add
|
||||
*/
|
||||
public void addXP(Player player, SkillType skillType, int XP) {
|
||||
Users.getProfile(player).addXP(player, skillType, XP);
|
||||
Users.getProfile(player).addXP(skillType, XP);
|
||||
checkXP(player, skillType);
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ public class AddxpCommand implements CommandExecutor {
|
||||
modifiedPlayer.sendMessage(ChatColor.GREEN + "You were awarded " + xp + " experience in " + skillName + "!"); //TODO: Needs more locale.
|
||||
|
||||
if (skill.equals(SkillType.ALL)) {
|
||||
Skills.xpCheckAll(modifiedPlayer);
|
||||
Skills.xpCheckAll(modifiedPlayer, profile);
|
||||
}
|
||||
else {
|
||||
Skills.xpCheckSkill(skill, modifiedPlayer, profile);
|
||||
@ -112,7 +112,7 @@ public class AddxpCommand implements CommandExecutor {
|
||||
modifiedPlayer.sendMessage(ChatColor.GREEN + "You were awarded " + xp + " experience in " + skillName + "!"); //TODO: Needs more locale.
|
||||
|
||||
if (skill.equals(SkillType.ALL)) {
|
||||
Skills.xpCheckAll(modifiedPlayer);
|
||||
Skills.xpCheckAll(modifiedPlayer, profile);
|
||||
}
|
||||
else {
|
||||
Skills.xpCheckSkill(skill, modifiedPlayer, profile);
|
||||
|
@ -98,7 +98,7 @@ public class McremoveCommand implements CommandExecutor {
|
||||
//Force PlayerProfile stuff to update
|
||||
Player player = plugin.getServer().getPlayer(playerName);
|
||||
|
||||
if (player != null && Users.players.containsKey(playerName.toLowerCase())) {
|
||||
if (player != null && Users.getProfiles().containsKey(player)) {
|
||||
Users.removeUser(player);
|
||||
Users.addUser(player);
|
||||
}
|
||||
|
@ -60,12 +60,12 @@ public class PlayerProfile {
|
||||
HashMap<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>();
|
||||
HashMap<ToolType, Integer> toolATS = new HashMap<ToolType, Integer>();
|
||||
|
||||
private String playerName;
|
||||
private Player player;
|
||||
private final static String location = mcMMO.usersFile;
|
||||
|
||||
public PlayerProfile(String name, boolean addNew) {
|
||||
public PlayerProfile(Player player, boolean addNew) {
|
||||
hud = SpoutConfig.getInstance().defaulthud;
|
||||
playerName = name;
|
||||
this.player = player;
|
||||
|
||||
for (AbilityType abilityType : AbilityType.values()) {
|
||||
skillsDATS.put(abilityType, 0);
|
||||
@ -91,17 +91,14 @@ public class PlayerProfile {
|
||||
lastlogin = ((Long) (System.currentTimeMillis() / 1000)).intValue();
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
return this.playerName;
|
||||
}
|
||||
|
||||
//This method is actually never used
|
||||
public Player getPlayer() {
|
||||
return mcMMO.p.getServer().getPlayer(playerName);
|
||||
return player;
|
||||
}
|
||||
|
||||
public boolean loadMySQL() {
|
||||
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 = '" + player.getName() + "'");
|
||||
|
||||
this.userid = id;
|
||||
|
||||
@ -183,8 +180,8 @@ public class PlayerProfile {
|
||||
|
||||
public void addMySQLPlayer() {
|
||||
int id = 0;
|
||||
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"users (user, lastlogin) VALUES ('" + playerName + "'," + System.currentTimeMillis() / 1000 +")");
|
||||
id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + playerName + "'");
|
||||
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"users (user, lastlogin) VALUES ('" + player.getName() + "'," + System.currentTimeMillis() / 1000 +")");
|
||||
id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + player.getName() + "'");
|
||||
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"cooldowns (user_id) VALUES ("+id+")");
|
||||
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"skills (user_id) VALUES ("+id+")");
|
||||
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"experience (user_id) VALUES ("+id+")");
|
||||
@ -203,7 +200,7 @@ public class PlayerProfile {
|
||||
//Find if the line contains the player we want.
|
||||
String[] character = line.split(":");
|
||||
|
||||
if(!character[0].equals(playerName)){continue;}
|
||||
if(!character[0].equals(player.getName())){continue;}
|
||||
|
||||
//Get Mining
|
||||
if(character.length > 1 && Misc.isInt(character[1]))
|
||||
@ -355,12 +352,12 @@ public class PlayerProfile {
|
||||
while ((line = in.readLine()) != null) {
|
||||
//Read the line in and copy it to the output it's not the player
|
||||
//we want to edit
|
||||
if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
|
||||
if (!line.split(":")[0].equalsIgnoreCase(player.getName())) {
|
||||
writer.append(line).append("\r\n");
|
||||
}
|
||||
else {
|
||||
//Otherwise write the new player information
|
||||
writer.append(playerName + ":");
|
||||
writer.append(player.getName() + ":");
|
||||
writer.append(skills.get(SkillType.MINING) + ":");
|
||||
writer.append("" + ":");
|
||||
writer.append(party+":");
|
||||
@ -442,7 +439,7 @@ public class PlayerProfile {
|
||||
BufferedWriter out = new BufferedWriter(file);
|
||||
|
||||
//Add the player to the end
|
||||
out.append(playerName + ":");
|
||||
out.append(player.getName() + ":");
|
||||
out.append(0 + ":"); //mining
|
||||
out.append(""+":");
|
||||
out.append(party+":");
|
||||
@ -971,8 +968,6 @@ public class PlayerProfile {
|
||||
* @param newValue The amount of XP to add
|
||||
*/
|
||||
public void addXPOverride(SkillType skillType, int newValue) {
|
||||
Player player = mcMMO.p.getServer().getPlayer(playerName);
|
||||
|
||||
if (skillType.equals(SkillType.ALL)) {
|
||||
for (SkillType x : SkillType.values()) {
|
||||
if (x.equals(SkillType.ALL)) {
|
||||
@ -1004,11 +999,10 @@ public class PlayerProfile {
|
||||
/**
|
||||
* Adds XP to the player, this is affected by skill modifiers and XP Rate and Permissions
|
||||
*
|
||||
* @param player The player to add XP to
|
||||
* @param skillType The skill to add XP to
|
||||
* @param newvalue The amount of XP to add
|
||||
*/
|
||||
public void addXP(Player player, SkillType skillType, int newValue) {
|
||||
public void addXP(SkillType skillType, int newValue) {
|
||||
if (System.currentTimeMillis() < ((xpGainATS * 1000) + 250) || player.getGameMode().equals(GameMode.CREATIVE)) {
|
||||
return;
|
||||
}
|
||||
@ -1142,7 +1136,6 @@ public class PlayerProfile {
|
||||
* @return the power level of the player
|
||||
*/
|
||||
public int getPowerLevel() {
|
||||
Player player = mcMMO.p.getServer().getPlayer(playerName);
|
||||
int powerLevel = 0;
|
||||
|
||||
for (SkillType type : SkillType.values()) {
|
||||
@ -1161,15 +1154,12 @@ public class PlayerProfile {
|
||||
* @return the party bonus multiplier
|
||||
*/
|
||||
private double partyModifier(SkillType skillType) {
|
||||
Player player = getPlayer();
|
||||
double bonusModifier = 0.0;
|
||||
|
||||
for (Player x : Party.getInstance().getOnlineMembers(player)) {
|
||||
String memberName = x.getName();
|
||||
|
||||
if (!memberName.equals(playerName) && Party.getInstance().isPartyLeader(memberName, getParty())) {
|
||||
if (Misc.isNear(player.getLocation(), x.getLocation(), 25.0)) {
|
||||
PlayerProfile PartyLeader = Users.getProfile(x);
|
||||
for (Player member : Party.getInstance().getOnlineMembers(player)) {
|
||||
if (!member.equals(player) && Party.getInstance().isPartyLeader(member.getName(), getParty())) {
|
||||
if (Misc.isNear(player.getLocation(), member.getLocation(), 25.0)) {
|
||||
PlayerProfile PartyLeader = Users.getProfile(member);
|
||||
int leaderSkill = PartyLeader.getSkillLevel(skillType);
|
||||
int playerSkill = getSkillLevel(skillType);
|
||||
|
||||
|
@ -147,7 +147,7 @@ public class PlayerListener implements Listener {
|
||||
BleedTimer.bleedOut(player);
|
||||
|
||||
//Schedule PlayerProfile removal 2 minutes after quitting
|
||||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveProfileFromMemoryTask(player.getName()), 2400);
|
||||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveProfileFromMemoryTask(player), 2400);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -148,12 +148,10 @@ public class Party {
|
||||
*/
|
||||
public ArrayList<Player> getAllMembers(Player player) {
|
||||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
HashMap<String, PlayerProfile> profiles = Users.getProfiles();
|
||||
HashMap<Player, PlayerProfile> profiles = Users.getProfiles();
|
||||
|
||||
if (player != null) {
|
||||
for (String name : profiles.keySet()) {
|
||||
Player otherPlayer = profiles.get(name).getPlayer();
|
||||
|
||||
for (Player otherPlayer : profiles.keySet()) {
|
||||
if (otherPlayer != null && inSameParty(otherPlayer, player)) {
|
||||
players.add(otherPlayer);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import org.bukkit.entity.Player;
|
||||
import com.gmail.nossr50.util.Users;
|
||||
|
||||
public class ProfileSaveTask implements Runnable {
|
||||
Player player = null;
|
||||
private Player player;
|
||||
|
||||
public ProfileSaveTask(Player player) {
|
||||
this.player = player;
|
||||
@ -14,7 +14,7 @@ public class ProfileSaveTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
if (player != null) {
|
||||
Users.getProfileByName(player.getName()).save();
|
||||
Users.getProfile(player).save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,22 @@
|
||||
package com.gmail.nossr50.runnables;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.util.Users;
|
||||
|
||||
public class RemoveProfileFromMemoryTask implements Runnable {
|
||||
private String playerName = null;
|
||||
private Player player;
|
||||
|
||||
public RemoveProfileFromMemoryTask(String playerName) {
|
||||
this.playerName = playerName;
|
||||
public RemoveProfileFromMemoryTask(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//Check if the profile still exists (stuff like MySQL reconnection removes profiles)
|
||||
if (Users.players.containsKey(playerName.toLowerCase())) {
|
||||
Users.getProfileByName(playerName.toLowerCase()).save(); //We save here so players don't quit/reconnect to cause lag
|
||||
Users.removeUserByName(playerName.toLowerCase());
|
||||
if (Users.getProfiles().containsKey(player)) {
|
||||
Users.getProfile(player).save(); //We save here so players don't quit/reconnect to cause lag
|
||||
Users.removeUser(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,11 @@ public class SQLReconnect implements Runnable {
|
||||
if (!Database.isConnected()) {
|
||||
Database.connect();
|
||||
if (Database.isConnected()) {
|
||||
for (PlayerProfile x : Users.players.values()) {
|
||||
for (PlayerProfile x : Users.getProfiles().values()) {
|
||||
x.save(); //Save all profiles
|
||||
}
|
||||
|
||||
Users.players.clear(); //Clear the 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
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ public class Fishing {
|
||||
FishingTreasure treasure = rewards.get(random.nextInt(rewards.size()));
|
||||
|
||||
if (random.nextDouble() * 100 <= treasure.getDropChance()) {
|
||||
Users.getProfile(player).addXP(player, SkillType.FISHING, treasure.getXp());
|
||||
Users.getProfile(player).addXP(SkillType.FISHING, treasure.getXp());
|
||||
theCatch.setItemStack(treasure.getDrop());
|
||||
}
|
||||
}
|
||||
|
@ -248,15 +248,16 @@ public class Skills {
|
||||
* Check XP of all skills.
|
||||
*
|
||||
* @param player The player to check XP for.
|
||||
* @param profile The profile of the player whose skill to check
|
||||
*/
|
||||
public static void xpCheckAll(Player player) {
|
||||
for (SkillType x : SkillType.values()) {
|
||||
public static void xpCheckAll(Player player, PlayerProfile profile) {
|
||||
for (SkillType skillType : SkillType.values()) {
|
||||
//Don't want to do anything with this one
|
||||
if (x == SkillType.ALL) {
|
||||
if (skillType == SkillType.ALL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
xpCheckSkill(x, player, Users.getProfile(player));
|
||||
xpCheckSkill(skillType, player, profile);
|
||||
}
|
||||
}
|
||||
|
||||
@ -459,7 +460,7 @@ public class Skills {
|
||||
* @param xp the amount of XP to gain
|
||||
*/
|
||||
public static void xpProcessing(Player player, PlayerProfile profile, SkillType type, int xp) {
|
||||
profile.addXP(player, type, xp);
|
||||
profile.addXP(type, xp);
|
||||
xpCheckSkill(type, player, profile);
|
||||
}
|
||||
}
|
||||
|
@ -12,18 +12,15 @@ import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.datatypes.PlayerProfile;
|
||||
|
||||
public class Users {
|
||||
private final static mcMMO plugin = mcMMO.p;
|
||||
public static HashMap<String, PlayerProfile> players = new HashMap<String, PlayerProfile>();
|
||||
|
||||
private static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
|
||||
|
||||
/**
|
||||
* Load users.
|
||||
*/
|
||||
public static void loadUsers() {
|
||||
|
||||
|
||||
new File(mcMMO.flatFileDirectory).mkdir();
|
||||
new File(mcMMO.leaderboardDirectory).mkdir();
|
||||
|
||||
File theDir = new File(mcMMO.usersFile);
|
||||
|
||||
if (!theDir.exists()) {
|
||||
@ -43,8 +40,8 @@ public class Users {
|
||||
* @param player The player to create a user record for
|
||||
*/
|
||||
public static void addUser(Player player) {
|
||||
if (!players.containsKey(player.getName().toLowerCase())) {
|
||||
players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName(), true));
|
||||
if (!players.containsKey(player)) {
|
||||
players.put(player, new PlayerProfile(player, true));
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +57,7 @@ public class Users {
|
||||
*
|
||||
* @return a HashMap containing the PlayerProfile of everyone in the database
|
||||
*/
|
||||
public static HashMap<String, PlayerProfile> getProfiles() {
|
||||
public static HashMap<Player, PlayerProfile> getProfiles() {
|
||||
return players;
|
||||
}
|
||||
|
||||
@ -70,11 +67,10 @@ 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.getName().toLowerCase())) {
|
||||
players.get(player.getName().toLowerCase()).save();
|
||||
players.remove(player.getName().toLowerCase());
|
||||
if (!player.isOnline() && players.containsKey(player)) {
|
||||
players.get(player).save();
|
||||
players.remove(player);
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +80,7 @@ public class Users {
|
||||
* @param playerName The name of the player to remove
|
||||
*/
|
||||
public static void removeUserByName(String playerName) {
|
||||
players.remove(playerName.toLowerCase());
|
||||
players.remove(mcMMO.p.getServer().getOfflinePlayer(playerName));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,7 +90,7 @@ public class Users {
|
||||
* @return the player's profile
|
||||
*/
|
||||
public static PlayerProfile getProfile(OfflinePlayer player) {
|
||||
return getProfileByName(player.getName());
|
||||
return players.get(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,17 +100,28 @@ public class Users {
|
||||
* @return the player's profile
|
||||
*/
|
||||
public static PlayerProfile getProfileByName(String playerName) {
|
||||
if (plugin.getServer().getOfflinePlayer(playerName).isOnline() || players.containsKey(playerName.toLowerCase())) {
|
||||
if (players.containsKey(playerName.toLowerCase())) {
|
||||
return players.get(playerName.toLowerCase());
|
||||
Player player = mcMMO.p.getServer().getPlayer(playerName);
|
||||
PlayerProfile profile = players.get(player);
|
||||
|
||||
if (profile == null) {
|
||||
if (player != null) {
|
||||
PlayerProfile newProfile = new PlayerProfile(player, true);
|
||||
|
||||
players.put(player, newProfile);
|
||||
return newProfile;
|
||||
}
|
||||
else {
|
||||
players.put(playerName.toLowerCase(), new PlayerProfile(playerName, true));
|
||||
return players.get(playerName.toLowerCase());
|
||||
mcMMO.p.getLogger().severe("getProfileByName(" + playerName + ") just returned null :(");
|
||||
|
||||
for (StackTraceElement ste : new Throwable().getStackTrace()) {
|
||||
System.out.println(ste);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return new PlayerProfile(playerName, false);
|
||||
return profile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user