From 23983422e0981bf1fd67513660d6b9d5357ffbc9 Mon Sep 17 00:00:00 2001 From: GJ Date: Fri, 23 Aug 2013 14:07:59 -0400 Subject: [PATCH] Cleanup & remove code duplication. --- .../commands/hardcore/HardcoreCommand.java | 49 +++++++++---------- .../commands/hardcore/VampirismCommand.java | 49 +++++++++---------- .../gmail/nossr50/util/HardcoreManager.java | 36 -------------- .../java/com/gmail/nossr50/util/Motd.java | 29 ++++++----- 4 files changed, 65 insertions(+), 98 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/hardcore/HardcoreCommand.java b/src/main/java/com/gmail/nossr50/commands/hardcore/HardcoreCommand.java index 383e3a2c8..4ea3de6a2 100644 --- a/src/main/java/com/gmail/nossr50/commands/hardcore/HardcoreCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/hardcore/HardcoreCommand.java @@ -4,7 +4,6 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.util.HardcoreManager; import com.gmail.nossr50.util.Permissions; public class HardcoreCommand extends HardcoreModeCommand { @@ -21,39 +20,26 @@ public class HardcoreCommand extends HardcoreModeCommand { @Override protected boolean checkEnabled(String skill) { if (skill.equalsIgnoreCase("ALL")) { - return !HardcoreManager.getHardcoreStatLossDisabled(); - } - else { - return SkillType.getSkill(skill).getHardcoreStatLossEnabled(); + for (SkillType skillType : SkillType.values()) { + if (!skillType.getHardcoreStatLossEnabled()) { + return false; + } + } + + return true; } + + return SkillType.getSkill(skill).getHardcoreStatLossEnabled(); } @Override protected void enable(String skill) { - if (skill.equalsIgnoreCase("ALL")) { - for (SkillType skillType : SkillType.nonChildSkills()) { - Config.getInstance().setHardcoreStatLossEnabled(skillType, true); - } - } - else { - Config.getInstance().setHardcoreStatLossEnabled(SkillType.getSkill(skill), true); - } - - mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode.Enabled", LocaleLoader.getString("Hardcore.DeathStatLoss.Name"), skill)); + toggle(true); } @Override protected void disable(String skill) { - if (skill.equalsIgnoreCase("ALL")) { - for (SkillType skillType : SkillType.nonChildSkills()) { - Config.getInstance().setHardcoreStatLossEnabled(skillType, false); - } - } - else { - Config.getInstance().setHardcoreStatLossEnabled(SkillType.getSkill(skill), false); - } - - mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode.Disabled", LocaleLoader.getString("Hardcore.DeathStatLoss.Name"), skill)); + toggle(false); } @Override @@ -61,4 +47,17 @@ public class HardcoreCommand extends HardcoreModeCommand { Config.getInstance().setHardcoreDeathStatPenaltyPercentage(newPercent); sender.sendMessage(LocaleLoader.getString("Hardcore.DeathStatLoss.PercentageChanged", percent.format(newPercent / 100D))); } + + private void toggle(boolean enable) { + if (skill.equalsIgnoreCase("ALL")) { + for (SkillType skillType : SkillType.nonChildSkills()) { + Config.getInstance().setHardcoreStatLossEnabled(skillType, enable); + } + } + else { + Config.getInstance().setHardcoreStatLossEnabled(SkillType.getSkill(skill), enable); + } + + mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode." + (enable ? "Enabled" : "Disabled"), LocaleLoader.getString("Hardcore.DeathStatLoss.Name"), skill)); + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/commands/hardcore/VampirismCommand.java b/src/main/java/com/gmail/nossr50/commands/hardcore/VampirismCommand.java index ec51212ae..0866d8173 100644 --- a/src/main/java/com/gmail/nossr50/commands/hardcore/VampirismCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/hardcore/VampirismCommand.java @@ -4,7 +4,6 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.util.HardcoreManager; import com.gmail.nossr50.util.Permissions; public class VampirismCommand extends HardcoreModeCommand { @@ -21,39 +20,26 @@ public class VampirismCommand extends HardcoreModeCommand { @Override protected boolean checkEnabled(String skill) { if (skill.equalsIgnoreCase("ALL")) { - return !HardcoreManager.getHardcoreVampirismDisabled(); - } - else { - return SkillType.getSkill(skill).getHardcoreVampirismEnabled(); + for (SkillType skillType : SkillType.values()) { + if (!skillType.getHardcoreVampirismEnabled()) { + return false; + } + } + + return true; } + + return SkillType.getSkill(skill).getHardcoreVampirismEnabled(); } @Override protected void enable(String skill) { - if (skill.equalsIgnoreCase("ALL")) { - for (SkillType skillType : SkillType.nonChildSkills()) { - Config.getInstance().setHardcoreVampirismEnabled(skillType, true); - } - } - else { - Config.getInstance().setHardcoreVampirismEnabled(SkillType.getSkill(skill), true); - } - - mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode.Enabled", LocaleLoader.getString("Hardcore.Vampirism.Name"), skill)); + toggle(true); } @Override protected void disable(String skill) { - if (skill.equalsIgnoreCase("ALL")) { - for (SkillType skillType : SkillType.nonChildSkills()) { - Config.getInstance().setHardcoreVampirismEnabled(skillType, false); - } - } - else { - Config.getInstance().setHardcoreVampirismEnabled(SkillType.getSkill(skill), false); - } - - mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode.Disabled", LocaleLoader.getString("Hardcore.Vampirism.Name"), skill)); + toggle(false); } @Override @@ -61,4 +47,17 @@ public class VampirismCommand extends HardcoreModeCommand { Config.getInstance().setHardcoreVampirismStatLeechPercentage(newPercent); sender.sendMessage(LocaleLoader.getString("Hardcore.Vampirism.PercentageChanged", percent.format(newPercent / 100D))); } + + private void toggle(boolean enabled) { + if (skill.equalsIgnoreCase("ALL")) { + for (SkillType skillType : SkillType.nonChildSkills()) { + Config.getInstance().setHardcoreVampirismEnabled(skillType, enabled); + } + } + else { + Config.getInstance().setHardcoreVampirismEnabled(SkillType.getSkill(skill), enabled); + } + + mcMMO.p.getServer().broadcastMessage(LocaleLoader.getString("Hardcore.Mode." + (enabled ? "Enabled" : "Disabled"), LocaleLoader.getString("Hardcore.Vampirism.Name"), skill)); + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index 8a71c545c..258df3c49 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -107,24 +107,6 @@ public final class HardcoreManager { return enabled; } - /** - * Check if Hardcore Stat Loss is disabled for one or more skill types - * - * @return true if Stat Loss is disabled for one or more skill types - */ - public static boolean getHardcoreStatLossDisabled() { - boolean disabled = false; - - for (SkillType skillType : SkillType.nonChildSkills()) { - if (!skillType.getHardcoreStatLossEnabled()) { - disabled = true; - break; - } - } - - return disabled; - } - /** * Check if Hardcore Vampirism is enabled for one or more skill types * @@ -142,22 +124,4 @@ public final class HardcoreManager { return enabled; } - - /** - * Check if Hardcore Vampirism is disabled for one or more skill types - * - * @return true if Vampirism is disabled for one or more skill types - */ - public static boolean getHardcoreVampirismDisabled() { - boolean disabled = false; - - for (SkillType skillType : SkillType.nonChildSkills()) { - if (!skillType.getHardcoreVampirismEnabled()) { - disabled = true; - break; - } - } - - return disabled; - } } diff --git a/src/main/java/com/gmail/nossr50/util/Motd.java b/src/main/java/com/gmail/nossr50/util/Motd.java index bef33ebeb..3c3ea727d 100644 --- a/src/main/java/com/gmail/nossr50/util/Motd.java +++ b/src/main/java/com/gmail/nossr50/util/Motd.java @@ -43,25 +43,30 @@ public final class Motd { * @param player Target player */ public static void displayHardcoreSettings(Player player) { - if (!HardcoreManager.getHardcoreStatLossEnabled() && !HardcoreManager.getHardcoreVampirismEnabled()) { + boolean deathStatLossEnabled = HardcoreManager.getHardcoreStatLossEnabled(); + boolean vampirismEnabled = HardcoreManager.getHardcoreVampirismEnabled(); + + if (!deathStatLossEnabled && !vampirismEnabled) { return; } - String enabledModes; + String statLossInfo = ""; + String vampirismInfo = ""; + String seperator = ""; + + if (deathStatLossEnabled) { + statLossInfo = LocaleLoader.getString("Hardcore.DeathStatLoss.Name"); + } + + if (vampirismEnabled) { + vampirismInfo = LocaleLoader.getString("Hardcore.Vampirism.Name"); + } - boolean deathStatLossEnabled = HardcoreManager.getHardcoreStatLossEnabled(); - boolean vampirismEnabled = HardcoreManager.getHardcoreVampirismEnabled(); if (deathStatLossEnabled && vampirismEnabled) { - enabledModes = LocaleLoader.getString("Hardcore.DeathStatLoss.Name") + " & " + LocaleLoader.getString("Hardcore.Vampirism.Name"); - } - else if (deathStatLossEnabled) { - enabledModes = LocaleLoader.getString("Hardcore.DeathStatLoss.Name"); - } - else { - enabledModes = LocaleLoader.getString("Hardcore.Vampirism.Name"); + seperator = " & "; } - player.sendMessage(LocaleLoader.getString("MOTD.Hardcore.Enabled", enabledModes)); + player.sendMessage(LocaleLoader.getString("MOTD.Hardcore.Enabled", statLossInfo + seperator + vampirismInfo)); if (deathStatLossEnabled) { player.sendMessage(LocaleLoader.getString("MOTD.Hardcore.DeathStatLoss.Stats", Config.getInstance().getHardcoreDeathStatPenaltyPercentage()));