2021-09-18 11:51:00 +02:00
|
|
|
//
|
|
|
|
// Created by BONNe
|
|
|
|
// Copyright - 2021
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
package world.bentobox.challenges.panel.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2021-09-20 13:39:21 +02:00
|
|
|
import java.util.function.Consumer;
|
2021-09-18 11:51:00 +02:00
|
|
|
|
|
|
|
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;
|
2021-09-20 13:39:21 +02:00
|
|
|
import world.bentobox.challenges.panel.ConversationUtils;
|
2021-09-18 11:51:00 +02:00
|
|
|
import world.bentobox.challenges.utils.Constants;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This single abstract class will manage paged selectors similar to CommonPagedPanel.
|
|
|
|
*/
|
|
|
|
public abstract class PagedSelector<T>
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Instantiates a new Paged selector.
|
|
|
|
*
|
|
|
|
* @param user the user
|
|
|
|
*/
|
|
|
|
protected PagedSelector(User user)
|
|
|
|
{
|
|
|
|
this.user = user;
|
2021-09-20 13:39:21 +02:00
|
|
|
this.searchString = "";
|
2021-09-18 11:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build.
|
|
|
|
*/
|
|
|
|
protected abstract void build();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create element button panel item.
|
|
|
|
*
|
|
|
|
* @param object the object
|
|
|
|
* @return the panel item
|
|
|
|
*/
|
|
|
|
protected abstract PanelItem createElementButton(T object);
|
|
|
|
|
|
|
|
|
2021-09-20 13:39:21 +02:00
|
|
|
/**
|
|
|
|
* This method is called when filter value is updated.
|
|
|
|
*/
|
|
|
|
protected abstract void updateFilters();
|
|
|
|
|
|
|
|
|
2021-09-18 11:51:00 +02:00
|
|
|
/**
|
|
|
|
* Populate elements.
|
|
|
|
*
|
|
|
|
* @param panelBuilder the panel builder
|
|
|
|
* @param objectList the object list
|
|
|
|
*/
|
|
|
|
protected void populateElements(PanelBuilder panelBuilder, List<T> objectList)
|
|
|
|
{
|
|
|
|
final int MAX_ELEMENTS = 21;
|
|
|
|
final int size = objectList.size();
|
|
|
|
|
|
|
|
if (this.pageIndex < 0)
|
|
|
|
{
|
|
|
|
this.pageIndex = size / MAX_ELEMENTS;
|
|
|
|
}
|
|
|
|
else if (this.pageIndex > (size / MAX_ELEMENTS))
|
|
|
|
{
|
|
|
|
this.pageIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int objectIndex = MAX_ELEMENTS * this.pageIndex;
|
|
|
|
|
|
|
|
// I want first row to be only for navigation and return button.
|
|
|
|
int index = 10;
|
|
|
|
|
|
|
|
while (objectIndex < ((this.pageIndex + 1) * MAX_ELEMENTS) &&
|
|
|
|
objectIndex < size &&
|
|
|
|
index < 36)
|
|
|
|
{
|
|
|
|
if (!panelBuilder.slotOccupied(index))
|
|
|
|
{
|
|
|
|
panelBuilder.item(index, this.createElementButton(objectList.get(objectIndex++)));
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2021-09-20 13:39:21 +02:00
|
|
|
// Add next page button if there are more than MAX_ELEMENTS objects and pageIndex + 1 is
|
|
|
|
// larger or equal to the max page count.
|
2021-09-18 11:51:00 +02:00
|
|
|
if (size > MAX_ELEMENTS && !(1.0 * size / MAX_ELEMENTS <= this.pageIndex + 1))
|
|
|
|
{
|
|
|
|
panelBuilder.item(26, this.getButton(CommonButtons.NEXT));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-20 13:39:21 +02:00
|
|
|
// Add previous page button if pageIndex is not 0.
|
2021-09-18 11:51:00 +02:00
|
|
|
if (this.pageIndex > 0)
|
|
|
|
{
|
|
|
|
panelBuilder.item(18, this.getButton(CommonButtons.PREVIOUS));
|
|
|
|
}
|
2021-09-20 13:39:21 +02:00
|
|
|
|
|
|
|
// Add search button only if there is more than MAX_ELEMENTS objects or searchString
|
|
|
|
// is not blank.
|
|
|
|
if (!this.searchString.isBlank() || objectList.size() > MAX_ELEMENTS)
|
|
|
|
{
|
|
|
|
panelBuilder.item(40, this.getButton(CommonButtons.SEARCH));
|
|
|
|
}
|
2021-09-18 11:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method returns PanelItem that represents given Button.
|
|
|
|
* @param button Button that must be returned.
|
|
|
|
* @return PanelItem with requested functionality.
|
|
|
|
*/
|
|
|
|
protected PanelItem getButton(CommonButtons button)
|
|
|
|
{
|
|
|
|
final String reference = Constants.BUTTON + button.name().toLowerCase() + ".";
|
|
|
|
|
|
|
|
final String name = this.user.getTranslation(reference + "name");
|
|
|
|
final List<String> description = new ArrayList<>(3);
|
|
|
|
|
|
|
|
ItemStack icon;
|
|
|
|
PanelItem.ClickHandler clickHandler;
|
|
|
|
|
|
|
|
if (button == CommonButtons.NEXT)
|
|
|
|
{
|
|
|
|
description.add(this.user.getTranslation(reference + "description",
|
2021-09-19 20:04:09 +02:00
|
|
|
Constants.PARAMETER_NUMBER, String.valueOf(this.pageIndex + 2)));
|
2021-09-18 11:51:00 +02:00
|
|
|
|
2021-09-20 13:39:21 +02:00
|
|
|
description.add("");
|
|
|
|
description.add(this.user.getTranslation(Constants.TIPS + "click-to-next"));
|
|
|
|
|
2021-09-18 11:51:00 +02:00
|
|
|
icon = new ItemStack(Material.OAK_SIGN, this.pageIndex + 2);
|
|
|
|
clickHandler = (panel, user, clickType, slot) ->
|
|
|
|
{
|
|
|
|
this.pageIndex++;
|
|
|
|
this.build();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (button == CommonButtons.PREVIOUS)
|
|
|
|
{
|
|
|
|
description.add(this.user.getTranslation(reference + "description",
|
2021-09-19 20:04:09 +02:00
|
|
|
Constants.PARAMETER_NUMBER, String.valueOf(this.pageIndex)));
|
2021-09-18 11:51:00 +02:00
|
|
|
|
2021-09-20 13:39:21 +02:00
|
|
|
description.add("");
|
|
|
|
description.add(this.user.getTranslation(Constants.TIPS + "click-to-previous"));
|
|
|
|
|
2021-09-18 11:51:00 +02:00
|
|
|
icon = new ItemStack(Material.OAK_SIGN, Math.max(1, this.pageIndex));
|
|
|
|
clickHandler = (panel, user, clickType, slot) ->
|
|
|
|
{
|
|
|
|
this.pageIndex--;
|
|
|
|
this.build();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2021-09-20 13:39:21 +02:00
|
|
|
else if (button == CommonButtons.SEARCH)
|
|
|
|
{
|
|
|
|
description.add(this.user.getTranslation(reference + "description"));
|
|
|
|
|
|
|
|
if (this.searchString != null && !this.searchString.isEmpty())
|
|
|
|
{
|
|
|
|
description.add(this.user.getTranslation(reference + "search",
|
|
|
|
Constants.PARAMETER_VALUE, this.searchString));
|
|
|
|
}
|
|
|
|
|
|
|
|
description.add("");
|
|
|
|
description.add(this.user.getTranslation(Constants.TIPS + "left-click-to-edit"));
|
|
|
|
|
|
|
|
if (!this.searchString.isEmpty())
|
|
|
|
{
|
|
|
|
description.add(this.user.getTranslation(Constants.TIPS + "right-click-to-clear"));
|
|
|
|
}
|
|
|
|
|
|
|
|
icon = new ItemStack(Material.ANVIL);
|
|
|
|
|
|
|
|
clickHandler = (panel, user, clickType, slot) -> {
|
|
|
|
if (clickType.isRightClick())
|
|
|
|
{
|
|
|
|
// Clear string.
|
|
|
|
this.searchString = "";
|
|
|
|
this.updateFilters();
|
|
|
|
// Rebuild gui.
|
|
|
|
this.build();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create consumer that process description change
|
|
|
|
Consumer<String> consumer = value ->
|
|
|
|
{
|
|
|
|
if (value != null)
|
|
|
|
{
|
|
|
|
this.searchString = value;
|
|
|
|
this.updateFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.build();
|
|
|
|
};
|
|
|
|
|
|
|
|
// start conversation
|
|
|
|
ConversationUtils.createStringInput(consumer,
|
|
|
|
user,
|
|
|
|
user.getTranslation(Constants.CONVERSATIONS + "write-search"),
|
|
|
|
user.getTranslation(Constants.CONVERSATIONS + "search-updated"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2021-09-18 11:51:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
icon = new ItemStack(Material.PAPER);
|
|
|
|
clickHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PanelItemBuilder().
|
|
|
|
icon(icon).
|
|
|
|
name(name).
|
|
|
|
description(description).
|
|
|
|
clickHandler(clickHandler).
|
|
|
|
build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Next and Previous Buttons.
|
|
|
|
*/
|
|
|
|
private enum CommonButtons
|
|
|
|
{
|
|
|
|
NEXT,
|
2021-09-20 13:39:21 +02:00
|
|
|
PREVIOUS,
|
|
|
|
SEARCH
|
2021-09-18 11:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current page index.
|
|
|
|
*/
|
|
|
|
private int pageIndex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User who opens gui.
|
|
|
|
*/
|
|
|
|
protected final User user;
|
2021-09-20 13:39:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text that contains filter string.
|
|
|
|
*/
|
|
|
|
protected String searchString;
|
2021-09-18 11:51:00 +02:00
|
|
|
}
|