SHARE
TWEET

LinkedButton

a guest Jan 22nd, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.landonjw.togapi.implementation.inventory.linked;
  2.  
  3. import com.landonjw.togapi.api.inventory.Button;
  4. import com.landonjw.togapi.api.inventory.linked.LinkedButtonType;
  5. import com.landonjw.togapi.api.inventory.linked.LinkedPage;
  6. import net.minecraft.entity.player.EntityPlayerMP;
  7. import net.minecraft.inventory.ClickType;
  8. import net.minecraft.item.ItemStack;
  9.  
  10. import javax.annotation.Nonnull;
  11. import javax.annotation.Nullable;
  12. import java.util.Objects;
  13. import java.util.Optional;
  14.  
  15. /**
  16.  * Creates a button that links to or characterizes a {@link LinkedPage}.
  17.  *
  18.  * @author landonjw
  19.  * @since  2.1.0
  20.  */
  21. public class LinkedButton implements Button {
  22.  
  23.     /** The placeholder to substitute current page button is on. */
  24.     public static final String CURRENT_PAGE_PLACEHOLDER = "{current}";
  25.     /** The placeholder to substitute total page number. */
  26.     public static final String TOTAL_PAGES_PLACEHOLDER = "{total}";
  27.  
  28.     /** The button that links to a page. */
  29.     private Button button;
  30.     /** The page that is being linked to. */
  31.     private LinkedPage page;
  32.     /** The {@link LinkedButtonType} of the button. */
  33.     private LinkedButtonType buttonType;
  34.  
  35.     /**
  36.      * Constructor for a linked button.
  37.      *
  38.      * @param button     the button that links to a page.
  39.      * @param buttonType the page that is being linked to.
  40.      */
  41.     public LinkedButton(@Nonnull Button button, @Nonnull LinkedButtonType buttonType, @Nullable LinkedPage page){
  42.         this.button = Objects.requireNonNull(button, "button must not be null");
  43.         this.buttonType = Objects.requireNonNull(buttonType, "button type must not be null");
  44.         this.page = page;
  45.     }
  46.  
  47.     /**
  48.      * Constructor for a linked button.
  49.      *
  50.      * @param button     the button that links to a page.
  51.      * @param buttonType the page that is being linked to.
  52.      */
  53.     public LinkedButton(@Nonnull Button button, @Nonnull LinkedButtonType buttonType){
  54.         this(button, buttonType, null);
  55.     }
  56.  
  57.     /**
  58.      * Sets the page the button links to.
  59.      *
  60.      * @param page the page the button links to
  61.      */
  62.     public void setLinkedPage(LinkedPage page){
  63.         this.page = page;
  64.     }
  65.  
  66.     /**
  67.      * Gets the page the button is linked to.
  68.      *
  69.      * @return the page the button is linked to, if present
  70.      */
  71.     public Optional<LinkedPage> getLinkedPage(){
  72.         return Optional.ofNullable(page);
  73.     }
  74.  
  75.     /**
  76.      * Gets the {@link LinkedButtonType} of the button.
  77.      *
  78.      * @return the button type
  79.      */
  80.     public LinkedButtonType getButtonType() {
  81.         return buttonType;
  82.     }
  83.  
  84.     /**
  85.      * {@inheritDoc}
  86.      *
  87.      * <p>This will modify the display of the button if it is set as a 'current' linked button,
  88.      * to replace current and total page numbers if the '{current}' or '{total}' placeholders are given.</p>
  89.      */
  90.     @Override
  91.     public ItemStack getDisplay() {
  92.         if(page != null){
  93.             if(buttonType == LinkedButtonType.Current){
  94.                 ItemStack itemDisplay = button.getDisplay().copy();
  95.                 String displayName = itemDisplay.getDisplayName();
  96.                 displayName = displayName.replace(CURRENT_PAGE_PLACEHOLDER, "" + page.getPageNumber());
  97.                 displayName = displayName.replace(TOTAL_PAGES_PLACEHOLDER, "" + page.getTotalPages());
  98.                 itemDisplay.setStackDisplayName(displayName);
  99.                 return itemDisplay;
  100.             }
  101.         }
  102.         return button.getDisplay();
  103.     }
  104.  
  105.     /** {@inheritDoc} */
  106.     @Override
  107.     public void onClick(@Nonnull EntityPlayerMP player, @Nonnull ClickType clickType) {
  108.         button.onClick(player, clickType);
  109.         if(page != null){
  110.             if(buttonType == LinkedButtonType.Previous){
  111.                 page.getPreviousPage().ifPresent((previousPage) -> {
  112.                     previousPage.forceOpenPage(player);
  113.                 });
  114.             }
  115.             else if(buttonType == LinkedButtonType.Next){
  116.                 page.getNextPage().ifPresent((nextPage) -> {
  117.                     nextPage.forceOpenPage(player);
  118.                 });
  119.             }
  120.         }
  121.     }
  122.  
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top