Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.69 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(new ItemStack(Material.AIR));
  134. if (e.getClick().equals(ClickType.RIGHT)) {
  135. e.setCurrentItem(new CustomItem(item, new String[]{
  136. "&7This object is on slot number (Inventory) &c" + slot,
  137. "&cOBJECT HAS BEEN MODIFIED",
  138. "&cSAVE_DELETE"
  139. }));
  140. e.setCancelled(true);
  141. } else {
  142. e.setCurrentItem(new CustomItem(item, new String[]{
  143. "&7This object is on slot number (Inventory) &c" + slot,
  144. "&cOBJECT HAS BEEN MODIFIED",
  145. "&cSAVE_MOVE"
  146. }));
  147. }
  148. } else {
  149. e.setCancelled(true);
  150. }
  151. }
  152.  
  153. @Override
  154. public void onMove(InventoryDragEvent e) {
  155. ItemStack item = e.getOldCursor();
  156. Player player = (Player) e.getWhoClicked();
  157. if (e.getInventory().getType().equals(InventoryType.PLAYER)) {
  158. e.setCancelled(true);
  159. return;
  160. }
  161. int slot = new ArrayList<>(e.getRawSlots()).get(0);
  162. if (item == null || item.getType().equals(Material.AIR)) return;
  163. System.out.println("Drag event called!. Changing item to slot " + slot);
  164. e.setCursor(new CustomItem(item, new String[]{
  165. "&7This object is on slot number (Inventory) &c" + slot,
  166. "&cOBJECT HAS BEEN MODIFIED",
  167. "&cSAVE_MOVE"}));
  168. }
  169. });
  170.  
  171. ConfigurationSection cs = main.getConfigManager().getConfig().getConfigurationSection("Slots");
  172.  
  173. for (String s : cs.getKeys(false)) {
  174. Slot slot = (Slot) main.getConfigManager().getConfig().get("Slots." + s);
  175. slots.add(slot);
  176. }
  177.  
  178. slots.sort(Stacker.COMPARATOR);
  179.  
  180. for (Slot s : slots) {
  181. GUIButton button = new GUIButton(new CustomItem(Material.WOOL, "&7[ &cFrame Name &7] &f: " + s.getName().toUpperCase(), 14, new String[] {
  182. "&7This object is on slot number (Inventory) &c" + s.getId(),
  183. "&7MOVE AROUND THIS OBJECT TO CHANGE THE SLOT!",
  184. "&7RIGHT CLICK IT TO TAG IT AS 'GOING TO BE' DELETED OBJECT!"
  185. }));
  186. addButton(button);
  187. }
  188. }
  189.  
  190. @Override
  191. public void onInventoryClose(InventoryCloseEvent inventoryCloseEvent) {
  192. /*
  193. Now save
  194. */
  195. Inventory inventory = inventoryCloseEvent.getInventory();
  196. Player player = (Player) inventoryCloseEvent.getPlayer();
  197. if (!player.getItemOnCursor().getType().equals(Material.AIR)) {
  198. player.setItemOnCursor(new ItemStack(Material.AIR));
  199. player.sendMessage("Inventory saving cancelled because you take a item from the inventory!. Please put it back there before trying to save!");
  200. return;
  201. }
  202.  
  203. FileConfiguration config = Stacker.getInstance().getConfigManager().getConfig();
  204. PaginatedInventory paginatedInventory = Pagination.getInstance().getOpening().get(player.getUniqueId());
  205. int currSlot = 0;
  206. for (int i1 = 0; i1 < getTotalPage(); i1++) {
  207. System.out.println("Checking page number " + i1);
  208. for (int i : toCheckSlot) {
  209. System.out.println("Checking slot " + i);
  210. ItemStack item = inventory.getItem(i);
  211. if (item == null) {
  212. continue;
  213. }
  214. if (!item.hasItemMeta()) continue;
  215. ItemMeta meta = item.getItemMeta();
  216. List<String> lore = meta.getLore();
  217. /*
  218. Save check
  219. */
  220. ModifType type;
  221. try {
  222. type = ModifType.valueOf(ChatColor.stripColor(lore.get(2)));
  223. } catch (Exception e) {
  224. // No changes, continue
  225. continue;
  226. }
  227.  
  228. switch (type) {
  229. case SAVE_MOVE:
  230. String display = Pagination.getInstance().color(meta.getDisplayName());
  231. Slot newObj = new Slot(currSlot, ChatColor.stripColor(display.split(":")[1].split(" ")[1]));
  232. config.set("Slots." + currSlot, newObj);
  233. Stacker.getInstance().getConfigManager().saveConfig();
  234. System.out.println("Saving slot " + newObj.getId() + " to config slot " + currSlot + " on page " + paginatedInventory.getCurrPage());
  235. currSlot++;
  236. case SAVE_DELETE:
  237. config.set("Slots." + currSlot, null);
  238. Stacker.getInstance().getConfigManager().saveConfig();
  239. System.out.println("Slot number " + currSlot + " has been deleted!");
  240. currSlot++;
  241. default:
  242. System.out.println("No save mode found!");
  243. }
  244. }
  245. System.out.println("Trying to go to the page number " + (i1 + 1));
  246. if (silentNextPage()) {
  247. System.out.println("Page is now changed to " + (i1 + 1));
  248. } else {
  249. System.out.println("No page number " + (i1 + 1) + " found!");
  250. }
  251. }
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement