From ede08ce5ac1acb4f5f7c8dc960509a69574e05ef Mon Sep 17 00:00:00 2001 From: bm01 Date: Sat, 7 Jul 2012 19:58:51 +0200 Subject: [PATCH] Moved hudType back to PlayerProfile Removed some convenient methods from PlayerProfile to avoid NPE because of spoutHud --- .../nossr50/commands/spout/MchudCommand.java | 23 ++++---- .../nossr50/commands/spout/XplockCommand.java | 23 +++++--- .../gmail/nossr50/datatypes/McMMOPlayer.java | 14 ++++- .../nossr50/datatypes/PlayerProfile.java | 53 ++++--------------- .../com/gmail/nossr50/datatypes/SpoutHud.java | 11 +--- .../java/com/gmail/nossr50/util/Skills.java | 16 +++--- 6 files changed, 57 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/spout/MchudCommand.java b/src/main/java/com/gmail/nossr50/commands/spout/MchudCommand.java index 1851b5aea..10dd6eb3b 100644 --- a/src/main/java/com/gmail/nossr50/commands/spout/MchudCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/spout/MchudCommand.java @@ -30,21 +30,22 @@ public class MchudCommand implements CommandExecutor { return true; } + Player player = (Player) sender; + PlayerProfile playerProfile = Users.getProfile(player); + SpoutHud spoutHud = playerProfile.getSpoutHud(); + + if (spoutHud == null) { + sender.sendMessage(LocaleLoader.getString("Commands.Disabled")); + return true; + } + switch (args.length) { case 1: - Player player = (Player) sender; - PlayerProfile playerProfile = Users.getProfile(player); - for (HudType hudType : HudType.values()) { if (hudType.toString().equalsIgnoreCase(args[0])) { playerProfile.setHudType(hudType); - - SpoutHud spoutHud = playerProfile.getSpoutHud(); - - if (spoutHud != null) { - spoutHud.initializeXpBar(); - spoutHud.updateXpBar(); - } + spoutHud.initializeXpBar(); + spoutHud.updateXpBar(); return true; } @@ -54,7 +55,7 @@ public class MchudCommand implements CommandExecutor { return true; default: - sender.sendMessage(usage); + player.sendMessage(usage); return true; } } diff --git a/src/main/java/com/gmail/nossr50/commands/spout/XplockCommand.java b/src/main/java/com/gmail/nossr50/commands/spout/XplockCommand.java index 38c3325b6..e322ae6b2 100644 --- a/src/main/java/com/gmail/nossr50/commands/spout/XplockCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/spout/XplockCommand.java @@ -12,6 +12,7 @@ import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.SpoutConfig; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; +import com.gmail.nossr50.datatypes.SpoutHud; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -34,20 +35,26 @@ public class XplockCommand implements CommandExecutor { Player player = (Player) sender; PlayerProfile playerProfile = Users.getProfile(player); + SpoutHud spoutHud = playerProfile.getSpoutHud(); + + if (spoutHud == null) { + sender.sendMessage(LocaleLoader.getString("Commands.Disabled")); + return true; + } switch (args.length) { case 0: - if (playerProfile.getXpBarLocked()) { - playerProfile.toggleXpBarLocked(); + if (spoutHud.getXpBarLocked()) { + spoutHud.toggleXpBarLocked(); player.sendMessage(LocaleLoader.getString("Commands.xplock.unlocked")); return true; } - SkillType lastGained = playerProfile.getLastGained(); + SkillType lastGained = spoutHud.getLastGained(); if (lastGained != null) { - playerProfile.toggleXpBarLocked(); - playerProfile.setSkillLock(lastGained); + spoutHud.toggleXpBarLocked(); + spoutHud.setSkillLock(lastGained); player.sendMessage(LocaleLoader.getString("Commands.xplock.locked", new Object[] { Misc.getCapitalized(lastGained.toString()) })); } else { @@ -59,9 +66,9 @@ public class XplockCommand implements CommandExecutor { case 1: if (Skills.isSkill(args[0])) { if (Permissions.getInstance().permission(player, "mcmmo.skills." + args[0].toLowerCase())) { - playerProfile.setXpBarLocked(true); - playerProfile.setSkillLock(Skills.getSkillType(args[0])); - playerProfile.updateXpBar(); + spoutHud.setXpBarLocked(true); + spoutHud.setSkillLock(Skills.getSkillType(args[0])); + spoutHud.updateXpBar(); player.sendMessage(LocaleLoader.getString("Commands.xplock.locked", new Object[] { Misc.getCapitalized(args[0]) })); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/McMMOPlayer.java index 21d242482..7cf3d5051 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/McMMOPlayer.java @@ -92,7 +92,12 @@ public class McMMOPlayer { else { mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp)); profile.setSkillXPLevel(skillType, profile.getSkillXpLevel(skillType) + xp); - profile.setLastGained(skillType); + + SpoutHud spoutHud = profile.getSpoutHud(); + + if (spoutHud != null) { + spoutHud.setLastGained(skillType); + } } } @@ -162,7 +167,12 @@ public class McMMOPlayer { mcMMO.p.getServer().getPluginManager().callEvent(new McMMOPlayerXpGainEvent(player, skillType, xp)); profile.setSkillXPLevel(skillType, profile.getSkillXpLevel(skillType) + xp); - profile.setLastGained(skillType); + + SpoutHud spoutHud = profile.getSpoutHud(); + + if (spoutHud != null) { + spoutHud.setLastGained(skillType); + } } // Players & Profiles diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index e50d03599..cbc6ae380 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -20,6 +20,7 @@ public class PlayerProfile { /* HUD */ private SpoutHud spoutHud; + private HudType hudType = SpoutConfig.getInstance().defaultHudType; /* Party Stuff */ private Party party; @@ -98,7 +99,7 @@ public class PlayerProfile { else { for (HudType type : HudType.values()) { if (type.toString().equals(huds.get(1).get(0))) { - spoutHud.setHudType(type); + hudType = type; } } } @@ -238,7 +239,7 @@ public class PlayerProfile { if (character.length > 33) { for (HudType type : HudType.values()) { if (type.toString().equalsIgnoreCase(character[33])) { - spoutHud.setHudType(type); + hudType = type; } } } @@ -267,7 +268,7 @@ public class PlayerProfile { // if we are using mysql save to database if (Config.getInstance().getUseMySQL()) { - mcMMO.database.write("UPDATE " + Config.getInstance().getMySQLTablePrefix() + "huds SET hudtype = '" + spoutHud.getHudType().toString() + "' WHERE user_id = " + userId); + mcMMO.database.write("UPDATE " + Config.getInstance().getMySQLTablePrefix() + "huds SET hudtype = '" + hudType.toString() + "' WHERE user_id = " + userId); mcMMO.database.write("UPDATE " + Config.getInstance().getMySQLTablePrefix() + "users SET lastlogin = " + timestamp.intValue() + " WHERE id = " + userId); mcMMO.database.write("UPDATE " + Config.getInstance().getMySQLTablePrefix() + "cooldowns SET " + " mining = " + skillsDATS.get(AbilityType.SUPER_BREAKER) @@ -361,7 +362,7 @@ public class PlayerProfile { writer.append(String.valueOf(skillsDATS.get(AbilityType.SERRATED_STRIKES)) + ":"); writer.append(String.valueOf(skillsDATS.get(AbilityType.SKULL_SPLIITER)) + ":"); writer.append(String.valueOf(skillsDATS.get(AbilityType.SUPER_BREAKER)) + ":"); - writer.append(spoutHud.getHudType().toString() + ":"); + writer.append(hudType.toString() + ":"); writer.append(skills.get(SkillType.FISHING) + ":"); writer.append(skillsXp.get(SkillType.FISHING) + ":"); writer.append(String.valueOf(skillsDATS.get(AbilityType.BLAST_MINING)) + ":"); @@ -475,6 +476,10 @@ public class PlayerProfile { * HUD Stuff */ + public HudType getHudType() { + return hudType; + } + public SpoutHud getSpoutHud() { return spoutHud; } @@ -483,44 +488,8 @@ public class PlayerProfile { this.spoutHud = spoutHud; } - public void setXpBarLocked(boolean locked) { - spoutHud.setXpBarLocked(locked); - } - - public boolean getXpBarLocked() { - return spoutHud.getXpBarLocked(); - } - - public void toggleXpBarLocked() { - spoutHud.toggleXpBarLocked(); - } - - public void setSkillLock(SkillType type) { - spoutHud.setSkillLock(type); - } - - public SkillType getSkillLock() { - return spoutHud.getSkillLock(); - } - - public HudType getHudType() { - return spoutHud.getHudType(); - } - - public void setHudType(HudType type) { - spoutHud.setHudType(type); - } - - public void setLastGained(SkillType type) { - spoutHud.setLastGained(type); - } - - public SkillType getLastGained() { - return spoutHud.getLastGained(); - } - - public void updateXpBar() { - spoutHud.updateXpBar(); + public void setHudType(HudType hudType) { + this.hudType = hudType; } /* diff --git a/src/main/java/com/gmail/nossr50/datatypes/SpoutHud.java b/src/main/java/com/gmail/nossr50/datatypes/SpoutHud.java index 70e6da0b8..3c3e6a45d 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/SpoutHud.java +++ b/src/main/java/com/gmail/nossr50/datatypes/SpoutHud.java @@ -12,7 +12,6 @@ public class SpoutHud { private Player player; private PlayerProfile profile; - private HudType hudType = SpoutConfig.getInstance().defaultHudType; private SkillType lastGained; private SkillType skillLock; private boolean xpBarLocked; @@ -36,7 +35,7 @@ public class SpoutHud { xpBar.removeWidgets(); } - xpBar = new XpBar(SpoutManager.getPlayer(player), hudType); + xpBar = new XpBar(SpoutManager.getPlayer(player), profile.getHudType()); } } @@ -73,14 +72,6 @@ public class SpoutHud { SpoutManager.getPlayer(player).getMainScreen().removeWidgets(mcMMO.p); } - public HudType getHudType() { - return hudType; - } - - public void setHudType(HudType type) { - this.hudType = type; - } - public SkillType getLastGained() { return lastGained; } diff --git a/src/main/java/com/gmail/nossr50/util/Skills.java b/src/main/java/com/gmail/nossr50/util/Skills.java index 37e79e312..6682fb12f 100644 --- a/src/main/java/com/gmail/nossr50/util/Skills.java +++ b/src/main/java/com/gmail/nossr50/util/Skills.java @@ -230,14 +230,10 @@ public class Skills { String capitalized = Misc.getCapitalized(skillType.toString()); /* Spout Stuff */ - if (mcMMO.spoutEnabled && player instanceof SpoutPlayer) { + if (mcMMO.spoutEnabled) { SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player); - if (spoutPlayer.isSpoutCraftEnabled()) { - if (SpoutConfig.getInstance().getXPBarEnabled()) { - profile.updateXpBar(); - } - + if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) { SpoutStuff.levelUpNotification(skillType, spoutPlayer); /* Update custom titles */ @@ -254,12 +250,12 @@ public class Skills { } } - /* Always update XP Bar (Check if no levels were gained first to remove redundancy) */ - if (skillups == 0 && mcMMO.spoutEnabled && player instanceof SpoutPlayer) { + if (mcMMO.spoutEnabled) { SpoutPlayer spoutPlayer = (SpoutPlayer) player; - if (spoutPlayer.isSpoutCraftEnabled()) { + + if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) { if (SpoutConfig.getInstance().getXPBarEnabled()) { - profile.updateXpBar(); + profile.getSpoutHud().updateXpBar(); } } }