Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class BackpackGUI {
- /**
- * The inventory the player will be viewing as a GUI
- * */
- private Inventory gui;
- /**
- * The model the view will handle
- * */
- private BackpackModel model;
- /**
- * The GUI inventory the player sees
- * */
- private GUIView view;
- /**
- * The user viewing the inventory GUI
- * */
- protected BackpackPlayer viewer;
- /**
- * The current page of the GUI the user is on
- * */
- protected int page;
- protected BackpackGUI(BackpackPlayer viewer) {
- this.viewer = viewer;
- this.page = 0;
- }
- public int getPage() {
- return page;
- }
- public int getMaxPage() {
- return model.getBackpack().getTotalPagesStored();
- }
- public BackpackPlayer getViewer() {
- return viewer;
- }
- /**
- * Cycles to the next page in the specified direction
- * @return the next page in the cycle of the direction
- * */
- public int getNextPageInDirection(Direction direction) {
- page = page + (1 * direction.getDirection());
- return page;
- }
- /**
- * Opens the backpack to the first page
- * */
- public void open() {
- open(0);
- }
- /**
- * Opens the backpack to the specified page number
- * */
- public void open(int page) {
- this.model = buildModel(viewer, page);
- this.view = buildView(model);
- model.addObserver(view);
- gui = view.getView();
- Player player = viewer.getPlayer();
- player.openInventory(gui);
- }
- /**
- * Closes the GUI of the current viewer
- * */
- public void close() {
- InventoryPage viewedPage = model.getActivePage();
- Inventory pageInventory = viewedPage.getInventory();
- List<HumanEntity> viewers = pageInventory.getViewers();
- for(HumanEntity viewer : viewers) {
- viewer.closeInventory();
- }
- syncBackpackWithInventory(gui, model);
- }
- protected GUIView getGUIView() {
- return view;
- }
- private void syncBackpackWithInventory(Inventory latestInventory, BackpackModel model) {
- latestInventory = removeButtonsFromView(latestInventory);
- InventoryPage updatedPage = new InventoryPage(latestInventory);
- model.setActivePage(updatedPage);
- }
- /**
- * Allows the viewer to view another player's backpack
- * */
- public void viewBackpack(AbstractBackpack backpack) {
- Player owner = backpack.getOwner();
- BackpackPlayer backpackPlayer = UserManager.getOrLoadBackpackPlayer(owner);
- BackpackGUI gui = getGUIImpl(backpackPlayer, backpack);
- gui.open();
- }
- /**
- * Builds the {@link BackpackModel} the GUI uses for the view
- * */
- protected abstract BackpackModel<?> buildModel(BackpackPlayer player, int page);
- /**
- * Builds the GUI the viewer will see
- * */
- protected abstract GUIView buildView(BackpackModel model);
- /**
- * Updates the GUI inventory view to the new inventory view
- * */
- protected abstract void updateView(Inventory newInventoryView);
- /**
- * @return the Inventory for the specified page number
- * */
- protected abstract Inventory getPageForNumber(int pageNumber);
- /**
- * Removes the buttons from the GUI
- * @return the inventory with the buttons removed
- * */
- protected abstract Inventory removeButtonsFromView(Inventory gui);
- public static BackpackGUI getGUIImpl(BackpackPlayer player, AbstractBackpack backpack) {
- BackpackType type = backpack.getBackpackType();
- switch (type) {
- case PAGEABLE:
- return new PageableBackpackGUI(player);
- default:
- return new PageableBackpackGUI(player);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement