Skip to content

Commit

Permalink
Remove code duplication.
Browse files Browse the repository at this point in the history
Material icon and Entities icon creation now are in GuiUtils class.
  • Loading branch information
BONNe committed Jan 20, 2019
1 parent 7b0df2d commit 2464667
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 219 deletions.
122 changes: 13 additions & 109 deletions src/main/java/world/bentobox/challenges/panel/util/SelectBlocksGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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;
Expand All @@ -13,6 +14,7 @@
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;


/**
Expand Down Expand Up @@ -144,115 +146,17 @@ else if (pageIndex > (this.elements.size() / MAX_ELEMENTS))
*/
private PanelItem createMaterialButton(Material material)
{
PanelItemBuilder builder = new PanelItemBuilder().
name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " ")));

// 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();
ItemStack itemStack = GuiUtils.getMaterialItem(material);

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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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;


/**
Expand Down Expand Up @@ -120,8 +120,6 @@ else if (pageIndex > (this.entities.size() / MAX_ELEMENTS))
}

panelBuilder.build();

panelBuilder.build();
}


Expand All @@ -132,122 +130,18 @@ else if (pageIndex > (this.entities.size() / MAX_ELEMENTS))
*/
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;
}).build();
}


/**
* 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
Expand Down

0 comments on commit 2464667

Please sign in to comment.