Possible fix for NPE on save

This commit is contained in:
nossr50 2012-04-27 02:21:43 -07:00
parent e3803702c5
commit 8dec24693c
3 changed files with 27 additions and 31 deletions

View File

@ -30,7 +30,6 @@ public class PlayerProfile {
/* Party Stuff */
private String party;
private String invite;
private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
/* Toggles */
private boolean loaded = false;
@ -101,15 +100,15 @@ public class PlayerProfile {
public boolean loadMySQL() {
int id = 0;
id = mcMMO.database.getInt("SELECT id FROM "+tablePrefix+"users WHERE user = '" + playerName + "'");
if(id == 0)
return false;
id = mcMMO.database.getInt("SELECT id FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE user = '" + playerName + "'");
this.userid = id;
if (id > 0) {
HashMap<Integer, ArrayList<String>> huds = mcMMO.database.read("SELECT hudtype FROM "+tablePrefix+"huds WHERE user_id = " + id);
HashMap<Integer, ArrayList<String>> huds = mcMMO.database.read("SELECT hudtype FROM "+Config.getInstance().getMySQLTablePrefix()+"huds WHERE user_id = " + id);
if(huds.get(1) == null)
{
mcMMO.database.write("INSERT INTO "+tablePrefix+"huds (user_id) VALUES ("+id+")");
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"huds (user_id) VALUES ("+id+")");
} else {
if(huds.get(1).get(0) != null)
{
@ -124,17 +123,17 @@ public class PlayerProfile {
hud = Config.getInstance().defaulthud;
}
}
HashMap<Integer, ArrayList<String>> users = mcMMO.database.read("SELECT lastlogin, party FROM "+tablePrefix+"users WHERE id = " + id);
HashMap<Integer, ArrayList<String>> users = mcMMO.database.read("SELECT lastlogin, party FROM "+Config.getInstance().getMySQLTablePrefix()+"users WHERE id = " + id);
//lastlogin = Integer.parseInt(users.get(1).get(0));
party = users.get(1).get(1);
HashMap<Integer, ArrayList<String>> cooldowns = mcMMO.database.read("SELECT mining, woodcutting, unarmed, herbalism, excavation, swords, axes, blast_mining FROM "+tablePrefix+"cooldowns WHERE user_id = " + id);
HashMap<Integer, ArrayList<String>> cooldowns = mcMMO.database.read("SELECT mining, woodcutting, unarmed, herbalism, excavation, swords, axes, blast_mining FROM "+Config.getInstance().getMySQLTablePrefix()+"cooldowns WHERE user_id = " + id);
/*
* I'm still learning MySQL, this is a fix for adding a new table
* its not pretty but it works
*/
if(cooldowns.get(1) == null)
{
mcMMO.database.write("INSERT INTO "+tablePrefix+"cooldowns (user_id) VALUES ("+id+")");
mcMMO.database.write("INSERT INTO "+Config.getInstance().getMySQLTablePrefix()+"cooldowns (user_id) VALUES ("+id+")");
}
else
{
@ -147,7 +146,7 @@ public class PlayerProfile {
skillsDATS.put(AbilityType.SKULL_SPLIITER, Integer.valueOf(cooldowns.get(1).get(6)));
skillsDATS.put(AbilityType.BLAST_MINING, Integer.valueOf(cooldowns.get(1).get(7)));
}
HashMap<Integer, ArrayList<String>> stats = mcMMO.database.read("SELECT taming, mining, repair, woodcutting, unarmed, herbalism, excavation, archery, swords, axes, acrobatics, fishing FROM "+tablePrefix+"skills WHERE user_id = " + id);
HashMap<Integer, ArrayList<String>> stats = mcMMO.database.read("SELECT taming, mining, repair, woodcutting, unarmed, herbalism, excavation, archery, swords, axes, acrobatics, fishing FROM "+Config.getInstance().getMySQLTablePrefix()+"skills WHERE user_id = " + id);
skills.put(SkillType.TAMING, Integer.valueOf(stats.get(1).get(0)));
skills.put(SkillType.MINING, Integer.valueOf(stats.get(1).get(1)));
skills.put(SkillType.REPAIR, Integer.valueOf(stats.get(1).get(2)));
@ -160,7 +159,7 @@ public class PlayerProfile {
skills.put(SkillType.AXES, Integer.valueOf(stats.get(1).get(9)));
skills.put(SkillType.ACROBATICS, Integer.valueOf(stats.get(1).get(10)));
skills.put(SkillType.FISHING, Integer.valueOf(stats.get(1).get(11)));
HashMap<Integer, ArrayList<String>> experience = mcMMO.database.read("SELECT taming, mining, repair, woodcutting, unarmed, herbalism, excavation, archery, swords, axes, acrobatics, fishing FROM "+tablePrefix+"experience WHERE user_id = " + id);
HashMap<Integer, ArrayList<String>> experience = mcMMO.database.read("SELECT taming, mining, repair, woodcutting, unarmed, herbalism, excavation, archery, swords, axes, acrobatics, fishing FROM "+Config.getInstance().getMySQLTablePrefix()+"experience WHERE user_id = " + id);
skillsXp.put(SkillType.TAMING, Integer.valueOf(experience.get(1).get(0)));
skillsXp.put(SkillType.MINING, Integer.valueOf(experience.get(1).get(1)));
skillsXp.put(SkillType.REPAIR, Integer.valueOf(experience.get(1).get(2)));
@ -183,11 +182,11 @@ public class PlayerProfile {
public void addMySQLPlayer() {
int id = 0;
mcMMO.database.write("INSERT INTO "+tablePrefix+"users (user, lastlogin) VALUES ('" + playerName + "'," + System.currentTimeMillis() / 1000 +")");
id = mcMMO.database.getInt("SELECT id FROM "+tablePrefix+"users WHERE user = '" + playerName + "'");
mcMMO.database.write("INSERT INTO "+tablePrefix+"cooldowns (user_id) VALUES ("+id+")");
mcMMO.database.write("INSERT INTO "+tablePrefix+"skills (user_id) VALUES ("+id+")");
mcMMO.database.write("INSERT INTO "+tablePrefix+"experience (user_id) VALUES ("+id+")");
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()+"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+")");
this.userid = id;
}
@ -301,11 +300,11 @@ public class PlayerProfile {
// if we are using mysql save to database
if (Config.getInstance().getUseMySQL())
{
mcMMO.database.write("UPDATE "+tablePrefix+"huds SET "
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"huds SET "
+" hudtype = '"+hud.toString()+"' WHERE user_id = "+this.userid);
mcMMO.database.write("UPDATE "+tablePrefix+"users SET lastlogin = " + timestamp.intValue() + " WHERE id = " + this.userid);
mcMMO.database.write("UPDATE "+tablePrefix+"users SET party = '"+this.party+"' WHERE id = " +this.userid);
mcMMO.database.write("UPDATE "+tablePrefix+"cooldowns SET "
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"users SET lastlogin = " + timestamp.intValue() + " WHERE id = " + this.userid);
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"users SET party = '"+this.party+"' WHERE id = " +this.userid);
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"cooldowns SET "
+" mining = " + skillsDATS.get(AbilityType.SUPER_BREAKER)
+", woodcutting = " + skillsDATS.get(AbilityType.TREE_FELLER)
+", unarmed = " + skillsDATS.get(AbilityType.BERSERK)
@ -315,7 +314,7 @@ public class PlayerProfile {
+", axes = " + skillsDATS.get(AbilityType.SKULL_SPLIITER)
+", blast_mining = " + skillsDATS.get(AbilityType.BLAST_MINING)
+" WHERE user_id = "+this.userid);
mcMMO.database.write("UPDATE "+tablePrefix+"skills SET "
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"skills SET "
+" taming = "+skills.get(SkillType.TAMING)
+", mining = "+skills.get(SkillType.MINING)
+", repair = "+skills.get(SkillType.REPAIR)
@ -329,7 +328,7 @@ public class PlayerProfile {
+", acrobatics = "+skills.get(SkillType.ACROBATICS)
+", fishing = "+skills.get(SkillType.FISHING)
+" WHERE user_id = "+this.userid);
mcMMO.database.write("UPDATE "+tablePrefix+"experience SET "
mcMMO.database.write("UPDATE "+Config.getInstance().getMySQLTablePrefix()+"experience SET "
+" taming = "+skillsXp.get(SkillType.TAMING)
+", mining = "+skillsXp.get(SkillType.MINING)
+", repair = "+skillsXp.get(SkillType.REPAIR)

View File

@ -155,7 +155,7 @@ public class PlayerListener implements Listener {
}
//Schedule PlayerProfile removal 2 minutes after quitting
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveProfileFromMemoryTask(player), 2400);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new RemoveProfileFromMemoryTask(player.getName()), 2400);
}
/**

View File

@ -1,23 +1,20 @@
package com.gmail.nossr50.runnables;
import org.bukkit.entity.Player;
import com.gmail.nossr50.Users;
public class RemoveProfileFromMemoryTask implements Runnable {
private Player player;
private String playerName = null;
public RemoveProfileFromMemoryTask(Player player) {
this.player = player;
public RemoveProfileFromMemoryTask(String playerName) {
this.playerName = playerName;
}
@Override
public void run() {
String playerName = player.getName();
//Check if the profile still exists (stuff like MySQL reconnection removes profiles)
if (Users.players.containsKey(playerName.toLowerCase())) {
Users.getProfileByName(playerName).save(); //We save here so players don't quit/reconnect to cause lag
Users.removeUserByName(playerName);
Users.getProfileByName(playerName.toLowerCase()).save(); //We save here so players don't quit/reconnect to cause lag
Users.removeUserByName(playerName.toLowerCase());
}
}
}