Implement ability to select multiple Entities and Blocks in SelectBlockGUI and SelectEntityGUI. (#121)

Fix issue with unsplitted text in ManageBlocksGUI and ManageEntitiesGUI. (#121)
This commit is contained in:
BONNe 2019-05-18 00:01:03 +03:00
parent 413429a091
commit c3f93fd797
4 changed files with 100 additions and 25 deletions

View File

@ -111,6 +111,7 @@ public class ManageBlocksGUI extends CommonGUI
*/
private PanelItem createButton(Button button)
{
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();
PanelItemBuilder builder = new PanelItemBuilder();
switch (button)
@ -120,11 +121,13 @@ public class ManageBlocksGUI extends CommonGUI
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)
{
materials.forEach(material -> {
this.materialMap.put(material, 1);
this.materialList.add(material);
});
}
this.build();
@ -134,7 +137,7 @@ public class ManageBlocksGUI extends CommonGUI
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);

View File

@ -111,6 +111,7 @@ public class ManageEntitiesGUI extends CommonGUI
*/
private PanelItem createButton(Button button)
{
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();
PanelItemBuilder builder = new PanelItemBuilder();
switch (button)
@ -119,14 +120,13 @@ public class ManageEntitiesGUI extends CommonGUI
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();
@ -136,7 +136,7 @@ public class ManageEntitiesGUI extends CommonGUI
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);
@ -147,7 +147,7 @@ public class ManageEntitiesGUI extends CommonGUI
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;

View File

@ -4,10 +4,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;
import java.util.Set;
import java.util.*;
import java.util.function.BiConsumer;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -23,13 +20,13 @@ import world.bentobox.challenges.utils.GuiUtils;
*/
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;
@ -47,6 +44,7 @@ public class SelectBlocksGUI
excludedMaterial.add(Material.BARRIER);
this.elements = new ArrayList<>();
this.selectedMaterials = new HashSet<>();
for (Material material : Material.values())
{
@ -107,7 +105,7 @@ public class SelectBlocksGUI
index++;
}
panelBuilder.item(4,
panelBuilder.item(3,
new PanelItemBuilder().
icon(Material.RED_STAINED_GLASS_PANE).
name(this.user.getTranslation("challenges.gui.buttons.admin.cancel")).
@ -116,6 +114,24 @@ public class SelectBlocksGUI
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
@ -164,9 +180,22 @@ public class SelectBlocksGUI
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)).
@ -183,10 +212,15 @@ public class SelectBlocksGUI
*/
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.

View File

@ -20,19 +20,20 @@ import world.bentobox.challenges.utils.GuiUtils;
*/
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())
{
@ -81,7 +82,7 @@ public class SelectEntityGUI
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")).
@ -90,6 +91,23 @@ public class SelectEntityGUI
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
@ -156,10 +174,25 @@ public class SelectEntityGUI
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();
}
@ -171,7 +204,12 @@ public class SelectEntityGUI
/**
* 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.