Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.16 KB | None | 0 0
  1. package com.snowgears.shop.listener;
  2.  
  3. import org.bukkit.permissions.*;
  4. import org.bukkit.entity.*;
  5. import java.util.*;
  6. import org.bukkit.event.*;
  7. import com.snowgears.shop.*;
  8. import org.bukkit.event.entity.*;
  9. import org.bukkit.event.block.*;
  10. import com.snowgears.shop.util.*;
  11. import org.bukkit.event.inventory.*;
  12. import org.bukkit.block.*;
  13. import org.bukkit.*;
  14. import org.bukkit.event.player.*;
  15. import org.bukkit.scheduler.*;
  16. import org.bukkit.inventory.*;
  17. import org.bukkit.plugin.*;
  18.  
  19. public class ShopListener implements Listener
  20. {
  21. private final Shop plugin;
  22. private HashMap<String, Integer> shopBuildLimits;
  23.  
  24. public ShopListener(final Shop plugin) {
  25. this.shopBuildLimits = new HashMap<String, Integer>();
  26. this.plugin = plugin;
  27. }
  28.  
  29. @EventHandler
  30. public void onPlayerJoin(final PlayerJoinEvent playerJoinEvent) {
  31. if (this.plugin.usePerms()) {
  32. final Player player = playerJoinEvent.getPlayer();
  33. int n = -1;
  34. for (final PermissionAttachmentInfo permissionAttachmentInfo : player.getEffectivePermissions()) {
  35. if (permissionAttachmentInfo.getPermission().contains("shop.buildlimit.")) {
  36. try {
  37. final int int1 = Integer.parseInt(permissionAttachmentInfo.getPermission().substring(permissionAttachmentInfo.getPermission().lastIndexOf(".") + 1));
  38. if (int1 <= n) {
  39. continue;
  40. }
  41. n = int1;
  42. }
  43. catch (Exception ex) {}
  44. }
  45. }
  46. if (n == -1) {
  47. this.shopBuildLimits.put(player.getName(), 10000);
  48. }
  49. else {
  50. this.shopBuildLimits.put(player.getName(), n);
  51. }
  52. }
  53. }
  54.  
  55. public int getBuildLimit(final Player player) {
  56. if (this.shopBuildLimits.get(player.getName()) != null) {
  57. return this.shopBuildLimits.get(player.getName());
  58. }
  59. return Integer.MAX_VALUE;
  60. }
  61.  
  62. @EventHandler(priority = EventPriority.LOW)
  63. public void onDisplayChange(final PlayerInteractEvent playerInteractEvent) {
  64. if (playerInteractEvent.getHand() == EquipmentSlot.OFF_HAND) {
  65. return;
  66. }
  67. if (playerInteractEvent.getAction() == Action.RIGHT_CLICK_BLOCK) {
  68. final Block clickedBlock = playerInteractEvent.getClickedBlock();
  69. if (clickedBlock == null) {
  70. return;
  71. }
  72. if (Tag.WALL_SIGNS.isTagged((Keyed)clickedBlock.getType())) {
  73. final AbstractShop shop = this.plugin.getShopHandler().getShop(clickedBlock.getLocation());
  74. if (shop == null || !shop.isInitialized()) {
  75. return;
  76. }
  77. final Player player = playerInteractEvent.getPlayer();
  78. if (!shop.getOwnerName().equals(player.getName())) {
  79. if (!player.isSneaking()) {
  80. return;
  81. }
  82. if (player.isOp() || (this.plugin.usePerms() && player.hasPermission("shop.operator"))) {
  83. shop.getDisplay().cycleType();
  84. playerInteractEvent.setCancelled(true);
  85. }
  86. }
  87. else {
  88. if (this.plugin.usePerms() && !player.hasPermission("shop.setdisplay")) {
  89. return;
  90. }
  91. shop.getDisplay().cycleType();
  92. playerInteractEvent.setCancelled(true);
  93. }
  94. }
  95. }
  96. }
  97.  
  98. @EventHandler(priority = EventPriority.HIGHEST)
  99. public void onShopOpen(final PlayerInteractEvent playerInteractEvent) {
  100. if (playerInteractEvent.getAction() == Action.RIGHT_CLICK_BLOCK) {
  101. final Block clickedBlock = playerInteractEvent.getClickedBlock();
  102. if (clickedBlock == null) {
  103. return;
  104. }
  105. if (this.plugin.getShopHandler().isChest(clickedBlock)) {
  106. if (playerInteractEvent.getHand() == EquipmentSlot.OFF_HAND) {
  107. return;
  108. }
  109. final Player player = playerInteractEvent.getPlayer();
  110. final AbstractShop shopByChest = this.plugin.getShopHandler().getShopByChest(clickedBlock);
  111. if (shopByChest == null) {
  112. return;
  113. }
  114. boolean canUseShop = true;
  115. try {
  116. canUseShop = WorldGuardHook.canUseShop(player, shopByChest.getSignLocation());
  117. }
  118. catch (NoClassDefFoundError noClassDefFoundError) {}
  119. if (!canUseShop) {
  120. player.sendMessage(ShopMessage.getMessage("interactionIssue", "regionRestriction", null, player));
  121. playerInteractEvent.setCancelled(true);
  122. return;
  123. }
  124. if (!this.plugin.getShopHandler().isChest(shopByChest.getChestLocation().getBlock()) || !Tag.WALL_SIGNS.isTagged((Keyed)shopByChest.getSignLocation().getBlock().getType())) {
  125. shopByChest.delete();
  126. return;
  127. }
  128. if (shopByChest.getChestLocation().getBlock().getType() == Material.ENDER_CHEST) {
  129. if (player.isSneaking()) {
  130. shopByChest.printSalesInfo(player);
  131. playerInteractEvent.setCancelled(true);
  132. }
  133. return;
  134. }
  135. if (player.isSneaking() && !Tag.WALL_SIGNS.isTagged((Keyed)player.getInventory().getItemInMainHand().getType())) {
  136. shopByChest.printSalesInfo(player);
  137. playerInteractEvent.setCancelled(true);
  138. return;
  139. }
  140. if (!shopByChest.getOwnerName().equals(player.getName())) {
  141. if ((this.plugin.usePerms() && player.hasPermission("shop.operator")) || player.isOp()) {
  142. if (shopByChest.isAdmin()) {
  143. if (shopByChest.getType() == ShopType.GAMBLE) {
  144. return;
  145. }
  146. playerInteractEvent.setCancelled(true);
  147. shopByChest.printSalesInfo(player);
  148. }
  149. else {
  150. player.sendMessage(ShopMessage.getMessage(shopByChest.getType().toString(), "opOpen", shopByChest, player));
  151. }
  152. }
  153. else {
  154. playerInteractEvent.setCancelled(true);
  155. shopByChest.printSalesInfo(player);
  156. }
  157. }
  158. }
  159. }
  160. }
  161.  
  162. @EventHandler
  163. public void onShopClose(final InventoryCloseEvent inventoryCloseEvent) {
  164. final InventoryHolder holder = inventoryCloseEvent.getInventory().getHolder();
  165. if (holder instanceof Chest) {
  166. final AbstractShop shopByChest = this.plugin.getShopHandler().getShopByChest(((Chest)holder).getBlock());
  167. if (shopByChest == null) {
  168. return;
  169. }
  170. if (shopByChest.getType() == ShopType.GAMBLE) {
  171. ((GambleShop)shopByChest).shuffleGambleItem();
  172. }
  173. }
  174. }
  175.  
  176. @EventHandler
  177. public void onExplosion(final EntityExplodeEvent entityExplodeEvent) {
  178. final Iterator iterator = entityExplodeEvent.blockList().iterator();
  179. AbstractShop abstractShop = null;
  180. while (iterator.hasNext()) {
  181. final Block block = iterator.next();
  182. if (Tag.WALL_SIGNS.isTagged((Keyed)block.getType())) {
  183. abstractShop = this.plugin.getShopHandler().getShop(block.getLocation());
  184. }
  185. else if (this.plugin.getShopHandler().isChest(block)) {
  186. abstractShop = this.plugin.getShopHandler().getShopByChest(block);
  187. }
  188. if (abstractShop != null) {
  189. iterator.remove();
  190. }
  191. }
  192. }
  193.  
  194. @EventHandler(priority = EventPriority.HIGHEST)
  195. public void signDetachCheck(final BlockPhysicsEvent blockPhysicsEvent) {
  196. final Block block = blockPhysicsEvent.getBlock();
  197. if (UtilMethods.isWallSign(block.getType()) && this.plugin.getShopHandler() != null && this.plugin.getShopHandler().getShop(block.getLocation()) != null) {
  198. blockPhysicsEvent.setCancelled(true);
  199. }
  200. }
  201.  
  202. @EventHandler(priority = EventPriority.HIGHEST)
  203. public void onInventoryMoveItem(final InventoryMoveItemEvent inventoryMoveItemEvent) {
  204. final InventoryHolder holder = inventoryMoveItemEvent.getSource().getHolder();
  205. if (holder instanceof Hopper) {
  206. return;
  207. }
  208. if (inventoryMoveItemEvent.getDestination().getType() == InventoryType.PLAYER) {
  209. return;
  210. }
  211. AbstractShop abstractShop = null;
  212. if (holder instanceof Chest) {
  213. abstractShop = this.plugin.getShopHandler().getShopByChest(((Chest)holder).getBlock());
  214. }
  215. else if (holder instanceof DoubleChest) {
  216. abstractShop = this.plugin.getShopHandler().getShopByChest(((DoubleChest)holder).getLocation().getBlock());
  217. }
  218. else if (holder instanceof ShulkerBox) {
  219. abstractShop = this.plugin.getShopHandler().getShopByChest(((ShulkerBox)holder).getBlock());
  220. }
  221. else if (holder instanceof Barrel) {
  222. abstractShop = this.plugin.getShopHandler().getShopByChest(((Barrel)holder).getBlock());
  223. }
  224. if (abstractShop != null) {
  225. inventoryMoveItemEvent.setCancelled(true);
  226. }
  227. }
  228.  
  229. @EventHandler
  230. public void onCloseEnderChest(final InventoryCloseEvent inventoryCloseEvent) {
  231. if (inventoryCloseEvent.getPlayer() instanceof Player) {
  232. final Player player = (Player)inventoryCloseEvent.getPlayer();
  233. if (inventoryCloseEvent.getInventory().getType() == InventoryType.ENDER_CHEST && this.plugin.useEnderChests()) {
  234. this.plugin.getEnderChestHandler().saveInventory((OfflinePlayer)player, inventoryCloseEvent.getInventory());
  235. }
  236. }
  237. }
  238.  
  239. @EventHandler
  240. public void onLogin(final PlayerLoginEvent playerLoginEvent) {
  241. if (!this.plugin.useEnderChests()) {
  242. return;
  243. }
  244. final Player player = playerLoginEvent.getPlayer();
  245. new BukkitRunnable() {
  246. final /* synthetic */ Inventory val$inv = ShopListener.this.plugin.getEnderChestHandler().getInventory((OfflinePlayer)player);
  247.  
  248. public void run() {
  249. if (this.val$inv != null) {
  250. player.getEnderChest().setContents(this.val$inv.getContents());
  251. ShopListener.this.plugin.getEnderChestHandler().saveInventory((OfflinePlayer)player, this.val$inv);
  252. }
  253. }
  254. }.runTaskLater((Plugin)this.plugin, 2L);
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement