From 7a7ca4238323c4184aa3d1746585c201750dea7a Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 23 Mar 2019 18:59:30 -0700 Subject: [PATCH] Refactored toPanelItem to make it easier to understand --- .../bentobox/bentobox/api/flags/Flag.java | 80 ++++++++++--------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java index 2835a8eed..ce0310b80 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java @@ -299,46 +299,54 @@ public class Flag implements Comparable { pib.description(user.getTranslation("protection.panel.flag-item.menu-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()))); return pib.build(); } - // Check if this is a setting or world setting - if (getType().equals(Type.WORLD_SETTING)) { - String worldDetting = this.isSetForWorld(user.getWorld()) ? user.getTranslation("protection.panel.flag-item.setting-active") - : user.getTranslation("protection.panel.flag-item.setting-disabled"); - pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()) - , "[setting]", worldDetting)); + Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId())); + switch(getType()) { + case PROTECTION: + return createProtectionFlag(plugin, user, island, pib).build(); + case SETTING: + return createSettingFlag(plugin, user, island, pib).build(); + case WORLD_SETTING: + return createWorldSettingFlag(plugin, user, pib).build(); + default: return pib.build(); } - - // Get the island this user is on or their own - Island island = plugin.getIslands().getIslandAt(user.getLocation()).orElse(plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId())); - if (island != null) { - if (getType().equals(Type.SETTING)) { - String islandSetting = island.isAllowed(this) ? user.getTranslation("protection.panel.flag-item.setting-active") - : user.getTranslation("protection.panel.flag-item.setting-disabled"); - pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()) - , "[setting]", islandSetting)); - return pib.build(); - } - // TODO: Get the world settings - the player has no island and is not in an island location - // Dynamic rank list - if (getType().equals(Type.PROTECTION)) { - // Protection flag - String d = user.getTranslation(getDescriptionReference()); - d = user.getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, d); - pib.description(d); - plugin.getRanksManager().getRanks().forEach((reference, score) -> { - if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) { - pib.description(user.getTranslation("protection.panel.flag-item.blocked-rank") + user.getTranslation(reference)); - } else if (score <= RanksManager.OWNER_RANK && score > island.getFlag(this)) { - pib.description(user.getTranslation("protection.panel.flag-item.allowed-rank") + user.getTranslation(reference)); - } else if (score == island.getFlag(this)) { - pib.description(user.getTranslation("protection.panel.flag-item.minimal-rank") + user.getTranslation(reference)); - } - }); - } - } - return pib.build(); } + private PanelItemBuilder createWorldSettingFlag(BentoBox plugin, User user, PanelItemBuilder pib) { + String worldSetting = this.isSetForWorld(user.getWorld()) ? user.getTranslation("protection.panel.flag-item.setting-active") + : user.getTranslation("protection.panel.flag-item.setting-disabled"); + pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()) + , "[setting]", worldSetting)); + return pib; + } + + private PanelItemBuilder createSettingFlag(BentoBox plugin, User user, Island island, PanelItemBuilder pib) { + if (island != null) { + String islandSetting = island.isAllowed(this) ? user.getTranslation("protection.panel.flag-item.setting-active") + : user.getTranslation("protection.panel.flag-item.setting-disabled"); + pib.description(user.getTranslation("protection.panel.flag-item.setting-layout", TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()) + , "[setting]", islandSetting)); + } + return pib; + } + + private PanelItemBuilder createProtectionFlag(BentoBox plugin, User user, Island island, PanelItemBuilder pib) { + if (island != null) { + // Protection flag + pib.description(user.getTranslation("protection.panel.flag-item.description-layout", + TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()))); + plugin.getRanksManager().getRanks().forEach((reference, score) -> { + if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) { + pib.description(user.getTranslation("protection.panel.flag-item.blocked-rank") + user.getTranslation(reference)); + } else if (score <= RanksManager.OWNER_RANK && score > island.getFlag(this)) { + pib.description(user.getTranslation("protection.panel.flag-item.allowed-rank") + user.getTranslation(reference)); + } else if (score == island.getFlag(this)) { + pib.description(user.getTranslation("protection.panel.flag-item.minimal-rank") + user.getTranslation(reference)); + } + }); + } + return pib; + } /* (non-Javadoc) * @see java.lang.Object#toString()