Remove code duplication.

Material icon and Entities icon creation now are in GuiUtils class.
This commit is contained in:
BONNe 2019-01-20 17:54:51 +02:00
parent 7b0df2d5a7
commit 24646678bf
2 changed files with 16 additions and 218 deletions

View File

@ -3,6 +3,7 @@ package world.bentobox.challenges.panel.util;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User; 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) private PanelItem createMaterialButton(Material material)
{ {
PanelItemBuilder builder = new PanelItemBuilder(). ItemStack itemStack = GuiUtils.getMaterialItem(material);
name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " ")));
// Process items that cannot be item-stacks. return new PanelItemBuilder().
if (material.name().contains("_WALL")) name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " "))).
{ icon(itemStack).
// Materials that is attached to wall cannot be showed in GUI. But they should be in list. clickHandler((panel, user1, clickType, slot) -> {
builder.icon(Material.getMaterial(material.name().replace("WALL_", ""))); this.consumer.accept(true, material);
builder.glow(true); return true;
} }).
else if (material.name().startsWith("POTTED_")) glow(!itemStack.getType().equals(material)).
{ build();
// 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();
} }

View File

@ -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.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User; 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();
panelBuilder.build();
} }
@ -132,9 +130,11 @@ public class SelectEntityGUI
*/ */
private PanelItem createEntityButton(EntityType entity) private PanelItem createEntityButton(EntityType entity)
{ {
ItemStack itemStack = this.asEggs ? GuiUtils.getEntityEgg(entity) : GuiUtils.getEntityHead(entity);
return new PanelItemBuilder(). return new PanelItemBuilder().
name(WordUtils.capitalize(entity.name().toLowerCase().replace("_", " "))). name(WordUtils.capitalize(entity.name().toLowerCase().replace("_", " "))).
icon(this.asEggs ? this.getEntityEgg(entity) : this.getEntityHead(entity)). icon(itemStack).
clickHandler((panel, user1, clickType, slot) -> { clickHandler((panel, user1, clickType, slot) -> {
this.consumer.accept(true, entity); this.consumer.accept(true, entity);
return true; 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 // Section: Variables