Skip to content

Commit

Permalink
Implement ability to select multiple Entities and Blocks in SelectBlo…
Browse files Browse the repository at this point in the history
…ckGUI and SelectEntityGUI. (#121)

Fix issue with unsplitted text in ManageBlocksGUI and ManageEntitiesGUI. (#121)
  • Loading branch information
BONNe committed May 17, 2019
1 parent 413429a commit c3f93fd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ else if (this.pageIndex > (this.materialList.size() / MAX_ELEMENTS))
*/
private PanelItem createButton(Button button)
{
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();
PanelItemBuilder builder = new PanelItemBuilder();

switch (button)
Expand All @@ -120,11 +121,13 @@ private PanelItem createButton(Button button)
builder.icon(Material.BUCKET);
builder.clickHandler((panel, user1, clickType, slot) -> {

new SelectBlocksGUI(this.user, new HashSet<>(this.materialList), (status, material) -> {
new SelectBlocksGUI(this.user, new HashSet<>(this.materialList), (status, materials) -> {
if (status)
{
this.materialMap.put(material, 1);
this.materialList.add(material);
materials.forEach(material -> {
this.materialMap.put(material, 1);
this.materialList.add(material);
});
}

this.build();
Expand All @@ -134,7 +137,7 @@ private PanelItem createButton(Button button)
break;
case REMOVE:
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.remove-selected"));
builder.description(this.user.getTranslation("challenges.gui.descriptions.admin.remove-selected"));
builder.description(GuiUtils.stringSplit(this.user.getTranslation("challenges.gui.descriptions.admin.remove-selected"), lineLength));
builder.icon(Material.LAVA_BUCKET);
builder.clickHandler((panel, user1, clickType, slot) -> {
this.materialMap.keySet().removeAll(this.selectedMaterials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ else if (this.pageIndex > (this.entityList.size() / MAX_ELEMENTS))
*/
private PanelItem createButton(Button button)
{
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();
PanelItemBuilder builder = new PanelItemBuilder();

switch (button)
Expand All @@ -119,14 +120,13 @@ private PanelItem createButton(Button button)
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.add"));
builder.icon(Material.BUCKET);
builder.clickHandler((panel, user1, clickType, slot) -> {
new SelectEntityGUI(this.user, Collections.emptySet(), this.asEggs, (status, entity) -> {
new SelectEntityGUI(this.user, this.requiredEntities.keySet(), this.asEggs, (status, entities) -> {
if (status)
{
if (!this.requiredEntities.containsKey(entity))
{
entities.forEach(entity -> {
this.requiredEntities.put(entity, 1);
this.entityList.add(entity);
}
});
}

this.build();
Expand All @@ -136,7 +136,7 @@ private PanelItem createButton(Button button)
break;
case REMOVE:
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.remove-selected"));
builder.description(this.user.getTranslation("challenges.gui.descriptions.admin.remove-selected"));
builder.description(GuiUtils.stringSplit(this.user.getTranslation("challenges.gui.descriptions.admin.remove-selected"), lineLength));
builder.icon(Material.LAVA_BUCKET);
builder.clickHandler((panel, user1, clickType, slot) -> {
this.requiredEntities.keySet().removeAll(this.selectedEntities);
Expand All @@ -147,7 +147,7 @@ private PanelItem createButton(Button button)
break;
case SWITCH:
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.show-eggs"));
builder.description(this.user.getTranslation("challenges.gui.descriptions.admin.show-eggs"));
builder.description(GuiUtils.stringSplit(this.user.getTranslation("challenges.gui.descriptions.admin.show-eggs"), lineLength));
builder.icon(this.asEggs ? Material.EGG : Material.PLAYER_HEAD);
builder.clickHandler((panel, user1, clickType, slot) -> {
this.asEggs = !this.asEggs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,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;
import java.util.Set;
import java.util.*;
import java.util.function.BiConsumer;

import world.bentobox.bentobox.api.panels.PanelItem;
Expand All @@ -23,13 +20,13 @@
*/
public class SelectBlocksGUI
{
public SelectBlocksGUI(User user, BiConsumer<Boolean, Material> consumer)
public SelectBlocksGUI(User user, BiConsumer<Boolean, Set<Material>> consumer)
{
this(user, Collections.emptySet(), consumer);
}


public SelectBlocksGUI(User user, Set<Material> excludedMaterial, BiConsumer<Boolean, Material> consumer)
public SelectBlocksGUI(User user, Set<Material> excludedMaterial, BiConsumer<Boolean, Set<Material>> consumer)
{
this.consumer = consumer;
this.user = user;
Expand All @@ -47,6 +44,7 @@ public SelectBlocksGUI(User user, Set<Material> excludedMaterial, BiConsumer<Boo
excludedMaterial.add(Material.BARRIER);

this.elements = new ArrayList<>();
this.selectedMaterials = new HashSet<>();

for (Material material : Material.values())
{
Expand Down Expand Up @@ -107,7 +105,7 @@ else if (pageIndex > (this.elements.size() / MAX_ELEMENTS))
index++;
}

panelBuilder.item(4,
panelBuilder.item(3,
new PanelItemBuilder().
icon(Material.RED_STAINED_GLASS_PANE).
name(this.user.getTranslation("challenges.gui.buttons.admin.cancel")).
Expand All @@ -116,6 +114,24 @@ else if (pageIndex > (this.elements.size() / MAX_ELEMENTS))
return true;
}).build());


List<String> description = new ArrayList<>();
if (!this.selectedMaterials.isEmpty())
{
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.selected") + ":");
this.selectedMaterials.forEach(material -> description.add(" - " + material.name()));
}

panelBuilder.item(5,
new PanelItemBuilder().
icon(Material.GREEN_STAINED_GLASS_PANE).
name(this.user.getTranslation("challenges.gui.buttons.admin.accept")).
description(description).
clickHandler( (panel, user1, clickType, slot) -> {
this.consumer.accept(true, this.selectedMaterials);
return true;
}).build());

if (this.elements.size() > MAX_ELEMENTS)
{
// Navigation buttons if necessary
Expand Down Expand Up @@ -164,9 +180,22 @@ private PanelItem createMaterialButton(Material material)

return new PanelItemBuilder().
name(WordUtils.capitalize(material.name().toLowerCase().replace("_", " "))).
description(this.selectedMaterials.contains(material) ?
this.user.getTranslation("challenges.gui.descriptions.admin.selected") : "").
icon(itemStack).
clickHandler((panel, user1, clickType, slot) -> {
this.consumer.accept(true, material);
if (clickType.isRightClick())
{
if (!this.selectedMaterials.add(material))
{
this.selectedMaterials.remove(material);
}
}
else
{
this.consumer.accept(true, this.selectedMaterials);
}

return true;
}).
glow(!itemStack.getType().equals(material)).
Expand All @@ -183,10 +212,15 @@ private PanelItem createMaterialButton(Material material)
*/
private List<Material> elements;

/**
* Set that contains selected materials.
*/
private Set<Material> selectedMaterials;

/**
* This variable stores consumer.
*/
private BiConsumer<Boolean, Material> consumer;
private BiConsumer<Boolean, Set<Material>> consumer;

/**
* User who runs GUI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
*/
public class SelectEntityGUI
{
public SelectEntityGUI(User user, BiConsumer<Boolean, EntityType> consumer)
public SelectEntityGUI(User user, BiConsumer<Boolean, Set<EntityType>> consumer)
{
this(user, Collections.emptySet(), true, consumer);
}


public SelectEntityGUI(User user, Set<EntityType> excludedEntities, boolean asEggs, BiConsumer<Boolean, EntityType> consumer)
public SelectEntityGUI(User user, Set<EntityType> excludedEntities, boolean asEggs, BiConsumer<Boolean, Set<EntityType>> consumer)
{
this.consumer = consumer;
this.user = user;
this.asEggs = asEggs;

this.entities = new ArrayList<>(EntityType.values().length);
this.selectedEntities = new HashSet<>(EntityType.values().length);

for (EntityType entityType : EntityType.values())
{
Expand Down Expand Up @@ -81,7 +82,7 @@ else if (pageIndex > (this.entities.size() / MAX_ELEMENTS))
correctPage = pageIndex;
}

panelBuilder.item(4,
panelBuilder.item(3,
new PanelItemBuilder().
icon(Material.RED_STAINED_GLASS_PANE).
name(this.user.getTranslation("challenges.gui.buttons.admin.cancel")).
Expand All @@ -90,6 +91,23 @@ else if (pageIndex > (this.entities.size() / MAX_ELEMENTS))
return true;
}).build());

List<String> description = new ArrayList<>();
if (!this.selectedEntities.isEmpty())
{
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.selected") + ":");
this.selectedEntities.forEach(entity -> description.add(" - " + entity.name()));
}

panelBuilder.item(5,
new PanelItemBuilder().
icon(Material.GREEN_STAINED_GLASS_PANE).
name(this.user.getTranslation("challenges.gui.buttons.admin.accept")).
description(description).
clickHandler( (panel, user1, clickType, slot) -> {
this.consumer.accept(true, this.selectedEntities);
return true;
}).build());

if (this.entities.size() > MAX_ELEMENTS)
{
// Navigation buttons if necessary
Expand Down Expand Up @@ -156,10 +174,25 @@ private PanelItem createEntityButton(EntityType entity)
return new PanelItemBuilder().
name(WordUtils.capitalize(entity.name().toLowerCase().replace("_", " "))).
icon(itemStack).
description(this.selectedEntities.contains(entity) ?
this.user.getTranslation("challenges.gui.descriptions.admin.selected") : "").
clickHandler((panel, user1, clickType, slot) -> {
this.consumer.accept(true, entity);
if (clickType.isRightClick())
{
if (!this.selectedEntities.add(entity))
{
this.selectedEntities.remove(entity);
}
}
else
{
this.consumer.accept(true, this.selectedEntities);
}

return true;
}).build();
}).
glow(this.selectedEntities.contains(entity)).
build();
}


Expand All @@ -171,7 +204,12 @@ private PanelItem createEntityButton(EntityType entity)
/**
* This variable stores consumer.
*/
private BiConsumer<Boolean, EntityType> consumer;
private BiConsumer<Boolean, Set<EntityType>> consumer;

/**
* Set that contains selected entities.
*/
private Set<EntityType> selectedEntities;

/**
* User who runs GUI.
Expand Down

0 comments on commit c3f93fd

Please sign in to comment.