From 24646678bf0924831acaef7192a3d392e92643aa Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 20 Jan 2019 17:54:51 +0200 Subject: [PATCH] Remove code duplication. Material icon and Entities icon creation now are in GuiUtils class. --- .../panel/util/SelectBlocksGUI.java | 120 ++---------------- .../panel/util/SelectEntityGUI.java | 114 +---------------- 2 files changed, 16 insertions(+), 218 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java b/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java index e1c8315..e5b5e8d 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java @@ -3,6 +3,7 @@ package world.bentobox.challenges.panel.util; import org.apache.commons.lang.WordUtils; import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -13,6 +14,7 @@ import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; +import world.bentobox.challenges.utils.GuiUtils; /** @@ -144,115 +146,17 @@ public class SelectBlocksGUI */ private PanelItem createMaterialButton(Material material) { - PanelItemBuilder builder = new PanelItemBuilder(). - name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " "))); + ItemStack itemStack = GuiUtils.getMaterialItem(material); - // Process items that cannot be item-stacks. - if (material.name().contains("_WALL")) - { - // Materials that is attached to wall cannot be showed in GUI. But they should be in list. - builder.icon(Material.getMaterial(material.name().replace("WALL_", ""))); - builder.glow(true); - } - else if (material.name().startsWith("POTTED_")) - { - // Materials Potted elements cannot be in inventory. - builder.icon(Material.getMaterial(material.name().replace("POTTED_", ""))); - builder.glow(true); - } - else if (material.name().startsWith("POTTED_")) - { - // Materials Potted elements cannot be in inventory. - builder.icon(Material.getMaterial(material.name().replace("POTTED_", ""))); - builder.glow(true); - } - else if (material.equals(Material.MELON_STEM) || material.equals(Material.ATTACHED_MELON_STEM)) - { - builder.icon(Material.MELON_SEEDS); - builder.glow(true); - } - else if (material.equals(Material.PUMPKIN_STEM) || material.equals(Material.ATTACHED_PUMPKIN_STEM)) - { - builder.icon(Material.PUMPKIN_SEEDS); - builder.glow(true); - } - else if (material.equals(Material.TALL_SEAGRASS)) - { - builder.icon(Material.SEAGRASS); - builder.glow(true); - } - else if (material.equals(Material.CARROTS)) - { - builder.icon(Material.CARROT); - builder.glow(true); - } - else if (material.equals(Material.BEETROOTS)) - { - builder.icon(Material.BEETROOT); - builder.glow(true); - } - else if (material.equals(Material.POTATOES)) - { - builder.icon(Material.POTATO); - builder.glow(true); - } - else if (material.equals(Material.COCOA)) - { - builder.icon(Material.COCOA_BEANS); - builder.glow(true); - } - else if (material.equals(Material.KELP_PLANT)) - { - builder.icon(Material.KELP); - builder.glow(true); - } - else if (material.equals(Material.REDSTONE_WIRE)) - { - builder.icon(Material.REDSTONE); - builder.glow(true); - } - else if (material.equals(Material.TRIPWIRE)) - { - builder.icon(Material.STRING); - builder.glow(true); - } - else if (material.equals(Material.FROSTED_ICE)) - { - builder.icon(Material.ICE); - builder.glow(true); - } - else if (material.equals(Material.END_PORTAL) || material.equals(Material.END_GATEWAY) || material.equals(Material.NETHER_PORTAL)) - { - builder.icon(Material.PAPER); - builder.glow(true); - } - else if (material.equals(Material.BUBBLE_COLUMN) || material.equals(Material.WATER)) - { - builder.icon(Material.WATER_BUCKET); - builder.glow(true); - } - else if (material.equals(Material.LAVA)) - { - builder.icon(Material.LAVA_BUCKET); - builder.glow(true); - } - else if (material.equals(Material.FIRE)) - { - builder.icon(Material.FIRE_CHARGE); - builder.glow(true); - } - else - { - builder.icon(material); - builder.glow(false); - } - - builder.clickHandler((panel, user1, clickType, slot) -> { - this.consumer.accept(true, material); - return true; - }); - - return builder.build(); + return new PanelItemBuilder(). + name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " "))). + icon(itemStack). + clickHandler((panel, user1, clickType, slot) -> { + this.consumer.accept(true, material); + return true; + }). + glow(!itemStack.getType().equals(material)). + build(); } diff --git a/src/main/java/world/bentobox/challenges/panel/util/SelectEntityGUI.java b/src/main/java/world/bentobox/challenges/panel/util/SelectEntityGUI.java index 22f6c70..c16c5a2 100644 --- a/src/main/java/world/bentobox/challenges/panel/util/SelectEntityGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/util/SelectEntityGUI.java @@ -12,7 +12,7 @@ import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; -import world.bentobox.challenges.utils.HeadLib; +import world.bentobox.challenges.utils.GuiUtils; /** @@ -120,8 +120,6 @@ public class SelectEntityGUI } panelBuilder.build(); - - panelBuilder.build(); } @@ -132,9 +130,11 @@ public class SelectEntityGUI */ private PanelItem createEntityButton(EntityType entity) { + ItemStack itemStack = this.asEggs ? GuiUtils.getEntityEgg(entity) : GuiUtils.getEntityHead(entity); + return new PanelItemBuilder(). name(WordUtils.capitalize(entity.name().toLowerCase().replace("_", " "))). - icon(this.asEggs ? this.getEntityEgg(entity) : this.getEntityHead(entity)). + icon(itemStack). clickHandler((panel, user1, clickType, slot) -> { this.consumer.accept(true, entity); return true; @@ -142,112 +142,6 @@ public class SelectEntityGUI } - /** - * This method transforms entity into egg or block that corresponds given entity. If entity egg is not - * found, then it is replaced by block that represents entity or barrier block. - * @param entity Entity which egg must be returned. - * @return ItemStack that may be egg for given entity. - */ - private ItemStack getEntityEgg(EntityType entity) - { - ItemStack itemStack; - - switch (entity) - { - case PIG_ZOMBIE: - itemStack = new ItemStack(Material.ZOMBIE_PIGMAN_SPAWN_EGG); - break; - case ENDER_DRAGON: - itemStack = new ItemStack(Material.DRAGON_EGG); - break; - case WITHER: - itemStack = new ItemStack(Material.SOUL_SAND); - break; - case PLAYER: - itemStack = new ItemStack(Material.PLAYER_HEAD); - break; - case MUSHROOM_COW: - itemStack = new ItemStack(Material.MOOSHROOM_SPAWN_EGG); - break; - case SNOWMAN: - itemStack = new ItemStack(Material.CARVED_PUMPKIN); - break; - case IRON_GOLEM: - itemStack = new ItemStack(Material.IRON_BLOCK); - break; - case ARMOR_STAND: - itemStack = new ItemStack(Material.ARMOR_STAND); - break; - default: - Material material = Material.getMaterial(entity.name() + "_SPAWN_EGG"); - - if (material == null) - { - itemStack = new ItemStack(Material.BARRIER); - } - else - { - itemStack = new ItemStack(material); - } - - break; - } - - return itemStack; - } - - - /** - * This method transforms entity into player head with skin that corresponds given entity. If entity head - * is not found, then it is replaced by barrier block. - * @param entity Entity which head must be returned. - * @return ItemStack that may be head for given entity. - */ - private ItemStack getEntityHead(EntityType entity) - { - ItemStack itemStack; - - switch (entity) - { - case PLAYER: - itemStack = new ItemStack(Material.PLAYER_HEAD); - break; - case WITHER_SKELETON: - itemStack = new ItemStack(Material.WITHER_SKELETON_SKULL); - break; - case ARMOR_STAND: - itemStack = new ItemStack(Material.ARMOR_STAND); - break; - case SKELETON: - itemStack = new ItemStack(Material.SKELETON_SKULL); - break; - case GIANT: - case ZOMBIE: - itemStack = new ItemStack(Material.ZOMBIE_HEAD); - break; - case CREEPER: - itemStack = new ItemStack(Material.CREEPER_HEAD); - break; - case ENDER_DRAGON: - itemStack = new ItemStack(Material.DRAGON_HEAD); - break; - default: - HeadLib head = HeadLib.getHead(entity.name()); - - if (head == null) - { - itemStack = new ItemStack(Material.BARRIER); - } - else - { - itemStack = head.toItemStack(); - } - break; - } - - return itemStack; - } - // --------------------------------------------------------------------- // Section: Variables