Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.24 KB | None | 0 0
  1. package me.droreo002.stacker;
  2.  
  3. import me.droreo002.pagination.Pagination;
  4. import me.droreo002.pagination.api.inventory.normal.ButtonListener;
  5. import me.droreo002.pagination.api.inventory.paginated.PaginatedInventory;
  6. import me.droreo002.pagination.api.item.CustomItem;
  7. import me.droreo002.pagination.api.item.GUIButton;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.Material;
  11. import org.bukkit.configuration.ConfigurationSection;
  12. import org.bukkit.configuration.file.FileConfiguration;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.inventory.*;
  15. import org.bukkit.inventory.Inventory;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.bukkit.inventory.meta.ItemMeta;
  18.  
  19. import java.util.*;
  20.  
  21. public class TestInventory extends PaginatedInventory {
  22.  
  23. private final List<Slot> slots = new ArrayList<>();
  24. private boolean isChanged;
  25. private GUIButton back;
  26. private GUIButton next;
  27. private List<Integer> toCheckSlot;
  28. private Map<Integer, Slot> changes = new HashMap<>();
  29. private List<InventoryAction> drag_action = new ArrayList<>();
  30.  
  31. @SuppressWarnings("deprecation")
  32. public TestInventory(Stacker main) {
  33. super("Hello", 18, Pagination.getInstance());
  34. drag_action.add(InventoryAction.PLACE_ONE);
  35. drag_action.add(InventoryAction.PLACE_SOME);
  36. // drag_action.add(InventoryAction.DROP_ALL_CURSOR);
  37. // drag_action.add(InventoryAction.DROP_ALL_SLOT);
  38. // drag_action.add(InventoryAction.DROP_ONE_CURSOR);
  39. // drag_action.add(InventoryAction.DROP_ONE_SLOT);
  40. drag_action.add(InventoryAction.PLACE_ALL);
  41.  
  42. try {
  43. this.back = new GUIButton(new CustomItem("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDU5YmUxNTU3MjAxYzdmZjFhMGIzNjk2ZDE5ZWFiNDEwNDg4MGQ2YTljZGI0ZDVmYTIxYjZkYWE5ZGIyZDEifX19", "&7[ &aPrevious Page &7]", new String[] {
  44. "&7&m--------------------",
  45. "&r",
  46. "&fDo not click this if you made changes in this inventory!",
  47. "&cCLOSE &fthe inventory to save your changes!"
  48. })).setAction(new ButtonListener() {
  49. @Override
  50. public void onClick(InventoryClickEvent inventoryClickEvent) {
  51. System.out.println("Going to prev page!");
  52. Player player = (Player) inventoryClickEvent.getWhoClicked();
  53. prevPage(player);
  54. }
  55.  
  56. @Override
  57. public void onMove(InventoryDragEvent inventoryDragEvent) {
  58.  
  59. }
  60. });
  61. this.next = new GUIButton(new CustomItem("eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNDJiMGMwN2ZhMGU4OTIzN2Q2NzllMTMxMTZiNWFhNzVhZWJiMzRlOWM5NjhjNmJhZGIyNTFlMTI3YmRkNWIxIn19fQ==\\", "&7[ &aNext Page &7]", new String[] {
  62. "&7&m--------------------",
  63. "&r",
  64. "&fDo not click this if you made changes in this inventory!",
  65. "&cCLOSE &fthe inventory to save your changes!"
  66. })).setAction(new ButtonListener() {
  67. @Override
  68. public void onClick(InventoryClickEvent inventoryClickEvent) {
  69. System.out.println("Going to next page!");
  70. Player player = (Player) inventoryClickEvent.getWhoClicked();
  71. nextPage(player);
  72. }
  73.  
  74. @Override
  75. public void onMove(InventoryDragEvent inventoryDragEvent) {
  76.  
  77. }
  78. });
  79. } catch (Exception e) {
  80. this.back = new GUIButton(new CustomItem(Material.ARROW, "&7[ &aPrevious Page &7]", new String[] {
  81. "&7&m--------------------",
  82. "&r",
  83. "&fDo not click this if you made changes in this inventory!",
  84. "&cCLOSE &fthe inventory to save your changes!"
  85. })).setAction(new ButtonListener() {
  86. @Override
  87. public void onClick(InventoryClickEvent inventoryClickEvent) {
  88. Player player = (Player) inventoryClickEvent.getWhoClicked();
  89. prevPage(player);
  90. }
  91.  
  92. @Override
  93. public void onMove(InventoryDragEvent inventoryDragEvent) {
  94.  
  95. }
  96. });
  97. this.next = new GUIButton(new CustomItem(Material.ARROW, "&7[ &aNext Page &7]", new String[] {
  98. "&7&m--------------------",
  99. "&r",
  100. "&fDo not click this if you made changes in this inventory!",
  101. "&cCLOSE &fthe inventory to save your changes!"
  102. })).setAction(new ButtonListener() {
  103. @Override
  104. public void onClick(InventoryClickEvent inventoryClickEvent) {
  105. Player player = (Player) inventoryClickEvent.getWhoClicked();
  106. nextPage(player);
  107. }
  108.  
  109. @Override
  110. public void onMove(InventoryDragEvent inventoryDragEvent) {
  111.  
  112. }
  113. });
  114. }
  115.  
  116. setItemRow(0);
  117. toCheckSlot = getItems_row();
  118. setSearchRow(1, next, back);
  119. setItemCanBeMoved(true);
  120. setListenToInventoryCloseEvent(true);
  121. setUniversalListener(new ButtonListener() {
  122. @Override
  123. public void onClick(InventoryClickEvent e) {
  124. ItemStack item; //(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR)) ? e.getCursor() : e.getCurrentItem();
  125. int slot = e.getSlot();
  126. InventoryAction action = e.getAction();
  127. Player player = (Player) e.getWhoClicked();
  128.  
  129. if (drag_action.contains(action)) {
  130. item = e.getCursor();
  131. if (item == null || item.getType().equals(Material.AIR)) return;
  132. System.out.println("Action " + action + ", new slot is " + slot + ". Item " + item);
  133. player.setItemOnCursor(null);
  134. if (e.getClick().equals(ClickType.RIGHT)) {
  135. Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
  136. @Override
  137. public void run() {
  138. player.setItemOnCursor(new CustomItem(item, new String[]{
  139. "&7This object is on slot number (Inventory) &c" + slot,
  140. "&cOBJECT HAS BEEN MODIFIED",
  141. "&cSAVE_DELETE"
  142. }));
  143. }
  144. }, 1L);
  145. e.setCancelled(true);
  146. } else {
  147. Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
  148. @Override
  149. public void run() {
  150. player.setItemOnCursor(new CustomItem(item, new String[]{
  151. "&7This object is on slot number (Inventory) &c" + slot,
  152. "&cOBJECT HAS BEEN MODIFIED",
  153. "&cSAVE_MOVE"
  154. }));
  155. }
  156. }, 1L);
  157. }
  158. } else {
  159. e.setCancelled(true);
  160. }
  161. }
  162.  
  163. @Override
  164. public void onMove(InventoryDragEvent e) {
  165. ItemStack item = e.getOldCursor();
  166. Player player = (Player) e.getWhoClicked();
  167. if (e.getInventory().getType().equals(InventoryType.PLAYER)) {
  168. e.setCancelled(true);
  169. return;
  170. }
  171. int slot = new ArrayList<>(e.getRawSlots()).get(0);
  172. if (item == null || item.getType().equals(Material.AIR)) return;
  173. System.out.println("Drag event called!. Changing item to slot " + slot);
  174. e.setCursor(new CustomItem(item, new String[]{
  175. "&7This object is on slot number (Inventory) &c" + slot,
  176. "&cOBJECT HAS BEEN MODIFIED",
  177. "&cSAVE_MOVE"}));
  178. }
  179. });
  180.  
  181. ConfigurationSection cs = main.getConfigManager().getConfig().getConfigurationSection("Slots");
  182.  
  183. for (String s : cs.getKeys(false)) {
  184. Slot slot = (Slot) main.getConfigManager().getConfig().get("Slots." + s);
  185. slots.add(slot);
  186. }
  187.  
  188. slots.sort(Stacker.COMPARATOR);
  189.  
  190. for (Slot s : slots) {
  191. GUIButton button = new GUIButton(new CustomItem(Material.WOOL, "&7[ &cFrame Name &7] &f: " + s.getName().toUpperCase(), 14, new String[] {
  192. "&7This object is on slot number (Inventory) &c" + s.getId(),
  193. "&7MOVE AROUND THIS OBJECT TO CHANGE THE SLOT!",
  194. "&7RIGHT CLICK IT TO TAG IT AS 'GOING TO BE' DELETED OBJECT!"
  195. }));
  196. addButton(button);
  197. }
  198. }
  199.  
  200. @Override
  201. public void onInventoryClose(InventoryCloseEvent inventoryCloseEvent) {
  202. /*
  203. Now save
  204. */
  205. Inventory inventory = inventoryCloseEvent.getInventory();
  206. Player player = (Player) inventoryCloseEvent.getPlayer();
  207. if (!player.getItemOnCursor().getType().equals(Material.AIR)) {
  208. player.setItemOnCursor(new ItemStack(Material.AIR));
  209. player.sendMessage("Inventory saving cancelled because you take a item from the inventory!. Please put it back there before trying to save!");
  210. return;
  211. }
  212.  
  213. FileConfiguration config = Stacker.getInstance().getConfigManager().getConfig();
  214. PaginatedInventory paginatedInventory = Pagination.getInstance().getOpening().get(player.getUniqueId());
  215. int currSlot = 0;
  216. for (int i1 = 0; i1 < getTotalPage(); i1++) {
  217. System.out.println("Checking page number " + i1);
  218. for (int i : toCheckSlot) {
  219. System.out.println("Checking slot " + i);
  220. ItemStack item = inventory.getItem(i);
  221. if (item == null) {
  222. continue;
  223. }
  224. if (!item.hasItemMeta()) continue;
  225. ItemMeta meta = item.getItemMeta();
  226. List<String> lore = meta.getLore();
  227. /*
  228. Save check
  229. */
  230. ModifType type;
  231. try {
  232. type = ModifType.valueOf(ChatColor.stripColor(lore.get(2)));
  233. } catch (Exception e) {
  234. // No changes, continue
  235. continue;
  236. }
  237.  
  238. switch (type) {
  239. case SAVE_MOVE:
  240. String display = Pagination.getInstance().color(meta.getDisplayName());
  241. Slot newObj = new Slot(currSlot, ChatColor.stripColor(display.split(":")[1].split(" ")[1]));
  242. config.set("Slots." + currSlot, newObj);
  243. Stacker.getInstance().getConfigManager().saveConfig();
  244. System.out.println("Saving slot " + newObj.getId() + " to config slot " + currSlot + " on page " + paginatedInventory.getCurrPage());
  245. currSlot++;
  246. case SAVE_DELETE:
  247. config.set("Slots." + currSlot, null);
  248. Stacker.getInstance().getConfigManager().saveConfig();
  249. System.out.println("Slot number " + currSlot + " has been deleted!");
  250. currSlot++;
  251. default:
  252. System.out.println("No save mode found!");
  253. }
  254. }
  255. System.out.println("Trying to go to the page number " + (i1 + 1));
  256. if (silentNextPage()) {
  257. System.out.println("Page is now changed to " + (i1 + 1));
  258. } else {
  259. System.out.println("No page number " + (i1 + 1) + " found!");
  260. }
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement