Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.landonjw.togapi.implementation.inventory.linked;
- import com.landonjw.togapi.api.inventory.Button;
- import com.landonjw.togapi.api.inventory.linked.LinkedButtonType;
- import com.landonjw.togapi.api.inventory.linked.LinkedPage;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.inventory.ClickType;
- import net.minecraft.item.ItemStack;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.util.Objects;
- import java.util.Optional;
- /**
- * Creates a button that links to or characterizes a {@link LinkedPage}.
- *
- * @author landonjw
- * @since 2.1.0
- */
- public class LinkedButton implements Button {
- /** The placeholder to substitute current page button is on. */
- public static final String CURRENT_PAGE_PLACEHOLDER = "{current}";
- /** The placeholder to substitute total page number. */
- public static final String TOTAL_PAGES_PLACEHOLDER = "{total}";
- /** The button that links to a page. */
- private Button button;
- /** The page that is being linked to. */
- private LinkedPage page;
- /** The {@link LinkedButtonType} of the button. */
- private LinkedButtonType buttonType;
- /**
- * Constructor for a linked button.
- *
- * @param button the button that links to a page.
- * @param buttonType the page that is being linked to.
- */
- public LinkedButton(@Nonnull Button button, @Nonnull LinkedButtonType buttonType, @Nullable LinkedPage page){
- this.button = Objects.requireNonNull(button, "button must not be null");
- this.buttonType = Objects.requireNonNull(buttonType, "button type must not be null");
- this.page = page;
- }
- /**
- * Constructor for a linked button.
- *
- * @param button the button that links to a page.
- * @param buttonType the page that is being linked to.
- */
- public LinkedButton(@Nonnull Button button, @Nonnull LinkedButtonType buttonType){
- this(button, buttonType, null);
- }
- /**
- * Sets the page the button links to.
- *
- * @param page the page the button links to
- */
- public void setLinkedPage(LinkedPage page){
- this.page = page;
- }
- /**
- * Gets the page the button is linked to.
- *
- * @return the page the button is linked to, if present
- */
- public Optional<LinkedPage> getLinkedPage(){
- return Optional.ofNullable(page);
- }
- /**
- * Gets the {@link LinkedButtonType} of the button.
- *
- * @return the button type
- */
- public LinkedButtonType getButtonType() {
- return buttonType;
- }
- /**
- * {@inheritDoc}
- *
- * <p>This will modify the display of the button if it is set as a 'current' linked button,
- * to replace current and total page numbers if the '{current}' or '{total}' placeholders are given.</p>
- */
- @Override
- public ItemStack getDisplay() {
- if(page != null){
- if(buttonType == LinkedButtonType.Current){
- ItemStack itemDisplay = button.getDisplay().copy();
- String displayName = itemDisplay.getDisplayName();
- displayName = displayName.replace(CURRENT_PAGE_PLACEHOLDER, "" + page.getPageNumber());
- displayName = displayName.replace(TOTAL_PAGES_PLACEHOLDER, "" + page.getTotalPages());
- itemDisplay.setStackDisplayName(displayName);
- return itemDisplay;
- }
- }
- return button.getDisplay();
- }
- /** {@inheritDoc} */
- @Override
- public void onClick(@Nonnull EntityPlayerMP player, @Nonnull ClickType clickType) {
- button.onClick(player, clickType);
- if(page != null){
- if(buttonType == LinkedButtonType.Previous){
- page.getPreviousPage().ifPresent((previousPage) -> {
- previousPage.forceOpenPage(player);
- });
- }
- else if(buttonType == LinkedButtonType.Next){
- page.getNextPage().ifPresent((nextPage) -> {
- nextPage.forceOpenPage(player);
- });
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement