Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. public abstract class BackpackGUI {
  2.  
  3.     /**
  4.      * The inventory the player will be viewing as a GUI
  5.      * */
  6.     private Inventory gui;
  7.  
  8.     /**
  9.      * The model the view will handle
  10.      * */
  11.     private BackpackModel model;
  12.  
  13.     /**
  14.      * The GUI inventory the player sees
  15.      * */
  16.     private GUIView view;
  17.  
  18.     /**
  19.      * The user viewing the inventory GUI
  20.      * */
  21.     protected BackpackPlayer viewer;
  22.  
  23.     /**
  24.      * The current page of the GUI the user is on
  25.      * */
  26.     protected int page;
  27.  
  28.     protected BackpackGUI(BackpackPlayer viewer) {
  29.         this.viewer = viewer;
  30.         this.page = 0;
  31.     }
  32.  
  33.     public int getPage() {
  34.         return page;
  35.     }
  36.  
  37.     public int getMaxPage() {
  38.         return model.getBackpack().getTotalPagesStored();
  39.     }
  40.  
  41.     public BackpackPlayer getViewer() {
  42.         return viewer;
  43.     }
  44.  
  45.     /**
  46.      * Cycles to the next page in the specified direction
  47.      * @return the next page in the cycle of the direction
  48.      * */
  49.     public int getNextPageInDirection(Direction direction) {
  50.         page = page + (1 * direction.getDirection());
  51.         return page;
  52.     }
  53.  
  54.     /**
  55.      * Opens the backpack to the first page
  56.      * */
  57.     public void open() {
  58.         open(0);
  59.     }
  60.  
  61.     /**
  62.      * Opens the backpack to the specified page number
  63.      * */
  64.     public void open(int page) {
  65.         this.model = buildModel(viewer, page);
  66.         this.view = buildView(model);
  67.         model.addObserver(view);
  68.  
  69.         gui = view.getView();
  70.         Player player = viewer.getPlayer();
  71.         player.openInventory(gui);
  72.     }
  73.  
  74.     /**
  75.      * Closes the GUI of the current viewer
  76.      * */
  77.     public void close() {
  78.         InventoryPage viewedPage = model.getActivePage();
  79.         Inventory pageInventory = viewedPage.getInventory();
  80.         List<HumanEntity> viewers = pageInventory.getViewers();
  81.         for(HumanEntity viewer : viewers) {
  82.             viewer.closeInventory();
  83.         }
  84.  
  85.         syncBackpackWithInventory(gui, model);
  86.     }
  87.  
  88.     protected GUIView getGUIView() {
  89.         return view;
  90.     }
  91.  
  92.     private void syncBackpackWithInventory(Inventory latestInventory, BackpackModel model) {
  93.         latestInventory = removeButtonsFromView(latestInventory);
  94.         InventoryPage updatedPage = new InventoryPage(latestInventory);
  95.         model.setActivePage(updatedPage);
  96.     }
  97.  
  98.     /**
  99.      * Allows the viewer to view another player's backpack
  100.      * */
  101.     public void viewBackpack(AbstractBackpack backpack) {
  102.         Player owner = backpack.getOwner();
  103.         BackpackPlayer backpackPlayer = UserManager.getOrLoadBackpackPlayer(owner);
  104.         BackpackGUI gui = getGUIImpl(backpackPlayer, backpack);
  105.         gui.open();
  106.     }
  107.  
  108.     /**
  109.      * Builds the {@link BackpackModel} the GUI uses for the view
  110.      * */
  111.     protected abstract BackpackModel<?> buildModel(BackpackPlayer player, int page);
  112.  
  113.     /**
  114.      * Builds the GUI the viewer will see
  115.      * */
  116.     protected abstract GUIView buildView(BackpackModel model);
  117.  
  118.     /**
  119.      * Updates the GUI inventory view to the new inventory view
  120.      * */
  121.     protected abstract void updateView(Inventory newInventoryView);
  122.  
  123.     /**
  124.      * @return the Inventory for the specified page number
  125.      * */
  126.     protected abstract Inventory getPageForNumber(int pageNumber);
  127.  
  128.     /**
  129.      * Removes the buttons from the GUI
  130.      * @return the inventory with the buttons removed
  131.      * */
  132.     protected abstract Inventory removeButtonsFromView(Inventory gui);
  133.  
  134.     public static BackpackGUI getGUIImpl(BackpackPlayer player, AbstractBackpack backpack) {
  135.         BackpackType type = backpack.getBackpackType();
  136.         switch (type) {
  137.             case PAGEABLE:
  138.                 return new PageableBackpackGUI(player);
  139.             default:
  140.                 return new PageableBackpackGUI(player);
  141.         }
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement