2019-01-19 20:26:16 +01:00
|
|
|
package world.bentobox.challenges.panel.admin;
|
|
|
|
|
|
|
|
|
2019-10-10 07:35:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2019-01-19 20:26:16 +01:00
|
|
|
import org.apache.commons.lang.WordUtils;
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
|
|
|
|
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.ChallengesAddon;
|
|
|
|
import world.bentobox.challenges.panel.CommonGUI;
|
|
|
|
import world.bentobox.challenges.panel.util.NumberGUI;
|
|
|
|
import world.bentobox.challenges.panel.util.SelectEntityGUI;
|
2019-01-20 14:55:12 +01:00
|
|
|
import world.bentobox.challenges.utils.GuiUtils;
|
2019-01-19 20:26:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class allows to edit entities that are in required entities map.
|
|
|
|
*/
|
|
|
|
public class ManageEntitiesGUI extends CommonGUI
|
|
|
|
{
|
|
|
|
public ManageEntitiesGUI(ChallengesAddon addon,
|
|
|
|
World world,
|
|
|
|
User user,
|
|
|
|
Map<EntityType, Integer> requiredEntities,
|
|
|
|
String topLabel,
|
|
|
|
String permissionPrefix,
|
|
|
|
CommonGUI parentGUI)
|
|
|
|
{
|
|
|
|
super(addon, world, user, topLabel, permissionPrefix, parentGUI);
|
|
|
|
this.requiredEntities = requiredEntities;
|
|
|
|
|
|
|
|
this.entityList = new ArrayList<>(this.requiredEntities.keySet());
|
|
|
|
this.entityList.sort(Comparator.comparing(Enum::name));
|
|
|
|
|
|
|
|
this.selectedEntities = new HashSet<>(EntityType.values().length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Methods
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method builds all necessary elements in GUI panel.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void build()
|
|
|
|
{
|
|
|
|
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).
|
2019-02-12 00:38:16 +01:00
|
|
|
name(this.user.getTranslation("challenges.gui.title.admin.manage-entities"));
|
2019-01-19 20:26:16 +01:00
|
|
|
|
|
|
|
// create border
|
2019-01-20 14:55:12 +01:00
|
|
|
GuiUtils.fillBorder(panelBuilder);
|
2019-01-19 20:26:16 +01:00
|
|
|
|
|
|
|
panelBuilder.item(3, this.createButton(Button.ADD));
|
|
|
|
panelBuilder.item(5, this.createButton(Button.REMOVE));
|
2019-01-28 22:45:05 +01:00
|
|
|
panelBuilder.item(8, this.createButton(Button.SWITCH));
|
2019-01-19 20:26:16 +01:00
|
|
|
|
|
|
|
final int MAX_ELEMENTS = 21;
|
|
|
|
|
|
|
|
if (this.pageIndex < 0)
|
|
|
|
{
|
|
|
|
this.pageIndex = this.entityList.size() / MAX_ELEMENTS;
|
|
|
|
}
|
|
|
|
else if (this.pageIndex > (this.entityList.size() / MAX_ELEMENTS))
|
|
|
|
{
|
|
|
|
this.pageIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int entitiesIndex = MAX_ELEMENTS * this.pageIndex;
|
|
|
|
|
|
|
|
// I want first row to be only for navigation and return button.
|
|
|
|
int index = 10;
|
|
|
|
|
|
|
|
while (entitiesIndex < ((this.pageIndex + 1) * MAX_ELEMENTS) &&
|
2019-01-21 14:23:16 +01:00
|
|
|
entitiesIndex < this.entityList.size() &&
|
|
|
|
index < 26)
|
2019-01-19 20:26:16 +01:00
|
|
|
{
|
|
|
|
if (!panelBuilder.slotOccupied(index))
|
|
|
|
{
|
|
|
|
panelBuilder.item(index, this.createEntityButton(this.entityList.get(entitiesIndex++)));
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Navigation buttons only if necessary
|
|
|
|
if (this.entityList.size() > MAX_ELEMENTS)
|
|
|
|
{
|
|
|
|
panelBuilder.item(18, this.getButton(CommonButtons.PREVIOUS));
|
|
|
|
panelBuilder.item(26, this.getButton(CommonButtons.NEXT));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add return button.
|
|
|
|
panelBuilder.item(44, this.returnButton);
|
|
|
|
|
|
|
|
panelBuilder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method creates PanelItem button of requested type.
|
|
|
|
* @param button Button which must be created.
|
|
|
|
* @return new PanelItem with requested functionality.
|
|
|
|
*/
|
|
|
|
private PanelItem createButton(Button button)
|
|
|
|
{
|
2019-05-17 23:01:03 +02:00
|
|
|
int lineLength = this.addon.getChallengesSettings().getLoreLineLength();
|
2019-01-19 20:26:16 +01:00
|
|
|
PanelItemBuilder builder = new PanelItemBuilder();
|
|
|
|
|
|
|
|
switch (button)
|
|
|
|
{
|
|
|
|
case ADD:
|
2019-02-12 00:38:16 +01:00
|
|
|
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.add"));
|
2019-01-19 20:26:16 +01:00
|
|
|
builder.icon(Material.BUCKET);
|
|
|
|
builder.clickHandler((panel, user1, clickType, slot) -> {
|
2019-05-17 23:01:03 +02:00
|
|
|
new SelectEntityGUI(this.user, this.requiredEntities.keySet(), this.asEggs, (status, entities) -> {
|
2019-01-19 20:26:16 +01:00
|
|
|
if (status)
|
|
|
|
{
|
2019-05-17 23:01:03 +02:00
|
|
|
entities.forEach(entity -> {
|
2019-01-19 20:26:16 +01:00
|
|
|
this.requiredEntities.put(entity, 1);
|
|
|
|
this.entityList.add(entity);
|
2019-05-17 23:01:03 +02:00
|
|
|
});
|
2019-01-19 20:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.build();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case REMOVE:
|
2019-02-12 00:38:16 +01:00
|
|
|
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.remove-selected"));
|
2019-05-17 23:01:03 +02:00
|
|
|
builder.description(GuiUtils.stringSplit(this.user.getTranslation("challenges.gui.descriptions.admin.remove-selected"), lineLength));
|
2019-01-19 20:26:16 +01:00
|
|
|
builder.icon(Material.LAVA_BUCKET);
|
|
|
|
builder.clickHandler((panel, user1, clickType, slot) -> {
|
|
|
|
this.requiredEntities.keySet().removeAll(this.selectedEntities);
|
|
|
|
this.entityList.removeAll(this.selectedEntities);
|
2019-01-19 21:20:42 +01:00
|
|
|
this.build();
|
2019-01-19 20:26:16 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
break;
|
2019-01-28 22:45:05 +01:00
|
|
|
case SWITCH:
|
2019-02-12 00:38:16 +01:00
|
|
|
builder.name(this.user.getTranslation("challenges.gui.buttons.admin.show-eggs"));
|
2019-05-17 23:01:03 +02:00
|
|
|
builder.description(GuiUtils.stringSplit(this.user.getTranslation("challenges.gui.descriptions.admin.show-eggs"), lineLength));
|
2019-01-28 22:45:05 +01:00
|
|
|
builder.icon(this.asEggs ? Material.EGG : Material.PLAYER_HEAD);
|
|
|
|
builder.clickHandler((panel, user1, clickType, slot) -> {
|
|
|
|
this.asEggs = !this.asEggs;
|
|
|
|
this.build();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
break;
|
2019-01-19 20:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method creates button for given entity.
|
|
|
|
* @param entity Entity which button must be created.
|
|
|
|
* @return new Button for entity.
|
|
|
|
*/
|
|
|
|
private PanelItem createEntityButton(EntityType entity)
|
|
|
|
{
|
|
|
|
return new PanelItemBuilder().
|
|
|
|
name(WordUtils.capitalize(entity.name().toLowerCase().replace("_", " "))).
|
2019-02-12 00:38:16 +01:00
|
|
|
description(this.selectedEntities.contains(entity) ?
|
|
|
|
this.user.getTranslation("challenges.gui.descriptions.admin.selected") : "").
|
2019-01-20 14:55:12 +01:00
|
|
|
icon(this.asEggs ?
|
|
|
|
GuiUtils.getEntityEgg(entity, this.requiredEntities.get(entity)) :
|
|
|
|
GuiUtils.getEntityHead(entity, this.requiredEntities.get(entity))).
|
2019-01-19 20:26:16 +01:00
|
|
|
clickHandler((panel, user1, clickType, slot) -> {
|
|
|
|
// On right click change which entities are selected for deletion.
|
|
|
|
if (clickType.isRightClick())
|
|
|
|
{
|
|
|
|
if (!this.selectedEntities.add(entity))
|
|
|
|
{
|
|
|
|
// Remove entity if it is already selected
|
|
|
|
this.selectedEntities.remove(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.build();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-14 09:46:56 +01:00
|
|
|
new NumberGUI(this.user,
|
|
|
|
this.requiredEntities.get(entity),
|
|
|
|
1,
|
|
|
|
this.addon.getChallengesSettings().getLoreLineLength(),
|
|
|
|
(status, value) -> {
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
// Update value only when something changes.
|
|
|
|
this.requiredEntities.put(entity, value);
|
|
|
|
}
|
2019-01-19 20:26:16 +01:00
|
|
|
|
2019-02-14 09:46:56 +01:00
|
|
|
this.build();
|
|
|
|
});
|
2019-01-19 20:26:16 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}).
|
|
|
|
glow(this.selectedEntities.contains(entity)).
|
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Enums
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functional buttons in current GUI.
|
|
|
|
*/
|
|
|
|
private enum Button
|
|
|
|
{
|
|
|
|
ADD,
|
2019-01-28 22:45:05 +01:00
|
|
|
REMOVE,
|
|
|
|
SWITCH
|
2019-01-19 20:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Section: Variables
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List with entities to avoid list irregularities.
|
|
|
|
*/
|
|
|
|
private List<EntityType> entityList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set with entities that are selected.
|
|
|
|
*/
|
|
|
|
private Set<EntityType> selectedEntities;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map that contains all entities and their cound.
|
|
|
|
*/
|
|
|
|
private Map<EntityType, Integer> requiredEntities;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean indicate if entities should be displayed as eggs or mob heads.
|
|
|
|
*/
|
|
|
|
private boolean asEggs;
|
|
|
|
}
|