daily pastebin goal
3%
SHARE
TWEET

Ya

a guest Feb 16th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.mason.chest.listeners;
  2.  
  3. import com.wasteofplastic.askyblock.ASkyBlockAPI;
  4. import com.wasteofplastic.askyblock.Island;
  5. import java.util.List;
  6. import me.mason.chest.Core;
  7. import me.mason.chest.utils.LM;
  8. import me.mason.chest.utils.SignUtil;
  9. import net.brcdev.shopgui.ShopGuiPlugin;
  10. import net.brcdev.shopgui.config.Settings;
  11. import net.brcdev.shopgui.player.PlayerData;
  12. import net.brcdev.shopgui.shop.ShopManager;
  13. import net.brcdev.shopgui.shop.WrappedShopItem;
  14. import org.apache.commons.lang.Validate;
  15. import org.bukkit.Material;
  16. import org.bukkit.block.Block;
  17. import org.bukkit.block.BlockState;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.event.EventHandler;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.block.Action;
  22. import org.bukkit.event.block.SignChangeEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24. import org.bukkit.inventory.Inventory;
  25. import org.bukkit.inventory.InventoryHolder;
  26. import org.bukkit.inventory.ItemStack;
  27. import org.bukkit.material.Sign;
  28.  
  29. public class SignListener implements Listener {
  30.  
  31.     private final Core plugin;
  32.  
  33.     public SignListener(Core plugin) {
  34.         this.plugin = plugin;
  35.     }
  36.  
  37.     @EventHandler
  38.     public void onSignPlace(SignChangeEvent event) {
  39.         final Player player = event.getPlayer();
  40.         final SignUtil signUtil = this.plugin.getSignUtil();
  41.  
  42.         if(!inValidWorld(player) || !signUtil.validSign(event.getLine(0))){
  43.             return;
  44.         }
  45.  
  46.         if (!ASkyBlockAPI.getInstance().playerIsOnIsland(player)) {
  47.             return;
  48.         }
  49.  
  50.         final Block block = event.getBlock();
  51.         if (!block.getType().equals(Material.WALL_SIGN) ) {
  52.             this.denySignChange(event, LM.NOT_AGAINST_CHEST.getValue());
  53.             return;
  54.         }
  55.  
  56.         if(!player.hasPermission(LM.PERMISSION.getValue())){
  57.             this.denySignChange(event, LM.NO_PERMS_PLACE.getValue());
  58.             return;
  59.         }
  60.  
  61.         final Sign signData = (Sign) event.getBlock().getState().getData();
  62.         final Block relative = block.getRelative(signData.getAttachedFace());
  63.         if (!relative.equals(Material.CHEST) || !relative.equals(Material.TRAPPED_CHEST)) {
  64.             this.denySignChange(event, LM.NOT_AGAINST_CHEST.getValue());
  65.             return;
  66.         }
  67.  
  68.         final String[] lines = signUtil.getLines();
  69.  
  70.         for (int i = 0; i < 4; i++) {
  71.             event.setLine(i, lines[i]);
  72.         }
  73.  
  74.         player.sendMessage(LM.PLACE.getValue());
  75.     }
  76.  
  77.     private void denySignChange(SignChangeEvent event, String message) {
  78.         event.getBlock().breakNaturally();
  79.         event.getPlayer().sendMessage(message);
  80.     }
  81.  
  82.     @EventHandler
  83.     public void onSignInteract(PlayerInteractEvent event) {
  84.         final Player player = event.getPlayer();
  85.         final SignUtil signUtil = this.plugin.getSignUtil();
  86.  
  87.         if (!this.inValidWorld(player)) {
  88.             return;
  89.         }
  90.  
  91.         final Action action = event.getAction();
  92.         if (action == Action.LEFT_CLICK_BLOCK || action == Action.RIGHT_CLICK_BLOCK) {
  93.             return;
  94.         }
  95.  
  96.         final Block block = event.getClickedBlock();
  97.         if (block == null || block.getType() != Material.WALL_SIGN) {
  98.             return;
  99.         }
  100.  
  101.         final org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
  102.         if (!signUtil.isSellChestSign(sign)) {
  103.             return;
  104.         }
  105.  
  106.         final ASkyBlockAPI askyblockAPI = ASkyBlockAPI.getInstance();
  107.         if (!askyblockAPI.playerIsOnIsland(player)) {
  108.             return;
  109.         }
  110.  
  111.         final Island island = askyblockAPI.getIslandAt(player.getLocation());
  112.         if (island == null) {
  113.             return;
  114.         }
  115.  
  116.         if (!island.getMembers().contains(player.getUniqueId())) {
  117.             player.sendMessage(LM.NO_PERMS_USE.getValue());
  118.             return;
  119.         }
  120.  
  121.         final Sign signData = (Sign) sign.getData();
  122.  
  123.         final BlockState state = block.getRelative(signData.getAttachedFace()).getState();
  124.         if (state instanceof InventoryHolder) {
  125.             final Inventory inventory = ((InventoryHolder) state).getInventory();
  126.             this.sellItems(inventory, player);
  127.         }
  128.     }
  129.  
  130.     public boolean inValidWorld(Player player) {
  131.         List<String> validWorlds = plugin.getConfig().getStringList("valid-worlds");
  132.         Validate.notEmpty(validWorlds,"[Haunted] Valid?");
  133.         return validWorlds.contains(player.getWorld().getName());
  134.     }
  135.  
  136.     public void sellItems(Inventory chest, Player seller) {
  137.         ShopGuiPlugin sgp = ShopGuiPlugin.getInstance();
  138.  
  139.         int amount = 0;
  140.         double value = 0.0;
  141.  
  142.         ShopManager shopManager = sgp.getShopManager();
  143.         PlayerData playerData = sgp.getPlayerManager().getPlayerData(seller);
  144.  
  145.         for(int i = 0; i < chest.getContents().length; i++) {
  146.             ItemStack stack = chest.getItem(i);
  147.  
  148.             if(stack == null || stack.getType().equals(Material.AIR)) {
  149.                 continue;
  150.             }
  151.  
  152.             WrappedShopItem shopItem = shopManager.findShopItemByItemStack(seller, playerData, stack, Settings.sellHandAllExcludeFreeItems);
  153.  
  154.             if(shopItem == null || shopItem.getShopItem() == null || shopItem.getShop() == null) {
  155.                 continue;
  156.             }
  157.  
  158.             double sellValue = shopItem.getShopItem().getSellPriceForAmount(shopItem.getShop(), seller, playerData, stack.getAmount());
  159.  
  160.             amount += stack.getAmount();
  161.             value += sellValue;
  162.             chest.setItem(i, new ItemStack(Material.AIR));
  163.         }
  164.  
  165.         sgp.getEconomyProvider().deposit(seller, value);
  166.  
  167.         if (amount == 0) {
  168.             seller.sendMessage(LM.EMPTY_CHEST.getValue());
  169.         } else {
  170.             seller.sendMessage(LM.SOLD.getValue().replace("%amount%", amount + "").replace("%value%", value + ""));
  171.         }
  172.     }
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top