Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.snowgears.shop.listener;
- import org.bukkit.permissions.*;
- import org.bukkit.entity.*;
- import java.util.*;
- import org.bukkit.event.*;
- import com.snowgears.shop.*;
- import org.bukkit.event.entity.*;
- import org.bukkit.event.block.*;
- import com.snowgears.shop.util.*;
- import org.bukkit.event.inventory.*;
- import org.bukkit.block.*;
- import org.bukkit.*;
- import org.bukkit.event.player.*;
- import org.bukkit.scheduler.*;
- import org.bukkit.inventory.*;
- import org.bukkit.plugin.*;
- public class ShopListener implements Listener
- {
- private final Shop plugin;
- private HashMap<String, Integer> shopBuildLimits;
- public ShopListener(final Shop plugin) {
- this.shopBuildLimits = new HashMap<String, Integer>();
- this.plugin = plugin;
- }
- @EventHandler
- public void onPlayerJoin(final PlayerJoinEvent playerJoinEvent) {
- if (this.plugin.usePerms()) {
- final Player player = playerJoinEvent.getPlayer();
- int n = -1;
- for (final PermissionAttachmentInfo permissionAttachmentInfo : player.getEffectivePermissions()) {
- if (permissionAttachmentInfo.getPermission().contains("shop.buildlimit.")) {
- try {
- final int int1 = Integer.parseInt(permissionAttachmentInfo.getPermission().substring(permissionAttachmentInfo.getPermission().lastIndexOf(".") + 1));
- if (int1 <= n) {
- continue;
- }
- n = int1;
- }
- catch (Exception ex) {}
- }
- }
- if (n == -1) {
- this.shopBuildLimits.put(player.getName(), 10000);
- }
- else {
- this.shopBuildLimits.put(player.getName(), n);
- }
- }
- }
- public int getBuildLimit(final Player player) {
- if (this.shopBuildLimits.get(player.getName()) != null) {
- return this.shopBuildLimits.get(player.getName());
- }
- return Integer.MAX_VALUE;
- }
- @EventHandler(priority = EventPriority.LOW)
- public void onDisplayChange(final PlayerInteractEvent playerInteractEvent) {
- if (playerInteractEvent.getHand() == EquipmentSlot.OFF_HAND) {
- return;
- }
- if (playerInteractEvent.getAction() == Action.RIGHT_CLICK_BLOCK) {
- final Block clickedBlock = playerInteractEvent.getClickedBlock();
- if (clickedBlock == null) {
- return;
- }
- if (Tag.WALL_SIGNS.isTagged((Keyed)clickedBlock.getType())) {
- final AbstractShop shop = this.plugin.getShopHandler().getShop(clickedBlock.getLocation());
- if (shop == null || !shop.isInitialized()) {
- return;
- }
- final Player player = playerInteractEvent.getPlayer();
- if (!shop.getOwnerName().equals(player.getName())) {
- if (!player.isSneaking()) {
- return;
- }
- if (player.isOp() || (this.plugin.usePerms() && player.hasPermission("shop.operator"))) {
- shop.getDisplay().cycleType();
- playerInteractEvent.setCancelled(true);
- }
- }
- else {
- if (this.plugin.usePerms() && !player.hasPermission("shop.setdisplay")) {
- return;
- }
- shop.getDisplay().cycleType();
- playerInteractEvent.setCancelled(true);
- }
- }
- }
- }
- @EventHandler(priority = EventPriority.HIGHEST)
- public void onShopOpen(final PlayerInteractEvent playerInteractEvent) {
- if (playerInteractEvent.getAction() == Action.RIGHT_CLICK_BLOCK) {
- final Block clickedBlock = playerInteractEvent.getClickedBlock();
- if (clickedBlock == null) {
- return;
- }
- if (this.plugin.getShopHandler().isChest(clickedBlock)) {
- if (playerInteractEvent.getHand() == EquipmentSlot.OFF_HAND) {
- return;
- }
- final Player player = playerInteractEvent.getPlayer();
- final AbstractShop shopByChest = this.plugin.getShopHandler().getShopByChest(clickedBlock);
- if (shopByChest == null) {
- return;
- }
- boolean canUseShop = true;
- try {
- canUseShop = WorldGuardHook.canUseShop(player, shopByChest.getSignLocation());
- }
- catch (NoClassDefFoundError noClassDefFoundError) {}
- if (!canUseShop) {
- player.sendMessage(ShopMessage.getMessage("interactionIssue", "regionRestriction", null, player));
- playerInteractEvent.setCancelled(true);
- return;
- }
- if (!this.plugin.getShopHandler().isChest(shopByChest.getChestLocation().getBlock()) || !Tag.WALL_SIGNS.isTagged((Keyed)shopByChest.getSignLocation().getBlock().getType())) {
- shopByChest.delete();
- return;
- }
- if (shopByChest.getChestLocation().getBlock().getType() == Material.ENDER_CHEST) {
- if (player.isSneaking()) {
- shopByChest.printSalesInfo(player);
- playerInteractEvent.setCancelled(true);
- }
- return;
- }
- if (player.isSneaking() && !Tag.WALL_SIGNS.isTagged((Keyed)player.getInventory().getItemInMainHand().getType())) {
- shopByChest.printSalesInfo(player);
- playerInteractEvent.setCancelled(true);
- return;
- }
- if (!shopByChest.getOwnerName().equals(player.getName())) {
- if ((this.plugin.usePerms() && player.hasPermission("shop.operator")) || player.isOp()) {
- if (shopByChest.isAdmin()) {
- if (shopByChest.getType() == ShopType.GAMBLE) {
- return;
- }
- playerInteractEvent.setCancelled(true);
- shopByChest.printSalesInfo(player);
- }
- else {
- player.sendMessage(ShopMessage.getMessage(shopByChest.getType().toString(), "opOpen", shopByChest, player));
- }
- }
- else {
- playerInteractEvent.setCancelled(true);
- shopByChest.printSalesInfo(player);
- }
- }
- }
- }
- }
- @EventHandler
- public void onShopClose(final InventoryCloseEvent inventoryCloseEvent) {
- final InventoryHolder holder = inventoryCloseEvent.getInventory().getHolder();
- if (holder instanceof Chest) {
- final AbstractShop shopByChest = this.plugin.getShopHandler().getShopByChest(((Chest)holder).getBlock());
- if (shopByChest == null) {
- return;
- }
- if (shopByChest.getType() == ShopType.GAMBLE) {
- ((GambleShop)shopByChest).shuffleGambleItem();
- }
- }
- }
- @EventHandler
- public void onExplosion(final EntityExplodeEvent entityExplodeEvent) {
- final Iterator iterator = entityExplodeEvent.blockList().iterator();
- AbstractShop abstractShop = null;
- while (iterator.hasNext()) {
- final Block block = iterator.next();
- if (Tag.WALL_SIGNS.isTagged((Keyed)block.getType())) {
- abstractShop = this.plugin.getShopHandler().getShop(block.getLocation());
- }
- else if (this.plugin.getShopHandler().isChest(block)) {
- abstractShop = this.plugin.getShopHandler().getShopByChest(block);
- }
- if (abstractShop != null) {
- iterator.remove();
- }
- }
- }
- @EventHandler(priority = EventPriority.HIGHEST)
- public void signDetachCheck(final BlockPhysicsEvent blockPhysicsEvent) {
- final Block block = blockPhysicsEvent.getBlock();
- if (UtilMethods.isWallSign(block.getType()) && this.plugin.getShopHandler() != null && this.plugin.getShopHandler().getShop(block.getLocation()) != null) {
- blockPhysicsEvent.setCancelled(true);
- }
- }
- @EventHandler(priority = EventPriority.HIGHEST)
- public void onInventoryMoveItem(final InventoryMoveItemEvent inventoryMoveItemEvent) {
- final InventoryHolder holder = inventoryMoveItemEvent.getSource().getHolder();
- if (holder instanceof Hopper) {
- return;
- }
- if (inventoryMoveItemEvent.getDestination().getType() == InventoryType.PLAYER) {
- return;
- }
- AbstractShop abstractShop = null;
- if (holder instanceof Chest) {
- abstractShop = this.plugin.getShopHandler().getShopByChest(((Chest)holder).getBlock());
- }
- else if (holder instanceof DoubleChest) {
- abstractShop = this.plugin.getShopHandler().getShopByChest(((DoubleChest)holder).getLocation().getBlock());
- }
- else if (holder instanceof ShulkerBox) {
- abstractShop = this.plugin.getShopHandler().getShopByChest(((ShulkerBox)holder).getBlock());
- }
- else if (holder instanceof Barrel) {
- abstractShop = this.plugin.getShopHandler().getShopByChest(((Barrel)holder).getBlock());
- }
- if (abstractShop != null) {
- inventoryMoveItemEvent.setCancelled(true);
- }
- }
- @EventHandler
- public void onCloseEnderChest(final InventoryCloseEvent inventoryCloseEvent) {
- if (inventoryCloseEvent.getPlayer() instanceof Player) {
- final Player player = (Player)inventoryCloseEvent.getPlayer();
- if (inventoryCloseEvent.getInventory().getType() == InventoryType.ENDER_CHEST && this.plugin.useEnderChests()) {
- this.plugin.getEnderChestHandler().saveInventory((OfflinePlayer)player, inventoryCloseEvent.getInventory());
- }
- }
- }
- @EventHandler
- public void onLogin(final PlayerLoginEvent playerLoginEvent) {
- if (!this.plugin.useEnderChests()) {
- return;
- }
- final Player player = playerLoginEvent.getPlayer();
- new BukkitRunnable() {
- final /* synthetic */ Inventory val$inv = ShopListener.this.plugin.getEnderChestHandler().getInventory((OfflinePlayer)player);
- public void run() {
- if (this.val$inv != null) {
- player.getEnderChest().setContents(this.val$inv.getContents());
- ShopListener.this.plugin.getEnderChestHandler().saveInventory((OfflinePlayer)player, this.val$inv);
- }
- }
- }.runTaskLater((Plugin)this.plugin, 2L);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement