2012-09-12 12:35:48 +02:00
|
|
|
package com.Acrobot.ChestShop.Listeners.Player;
|
|
|
|
|
2021-02-15 17:28:33 +01:00
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2012-11-23 21:03:29 +01:00
|
|
|
import com.Acrobot.ChestShop.Configuration.Messages;
|
|
|
|
import com.Acrobot.ChestShop.Configuration.Properties;
|
2021-02-15 17:28:33 +01:00
|
|
|
import com.Acrobot.ChestShop.Events.ShopInfoEvent;
|
|
|
|
import com.Acrobot.ChestShop.Permission;
|
2019-05-07 17:13:56 +02:00
|
|
|
import com.Acrobot.ChestShop.Security;
|
2020-05-16 23:16:27 +02:00
|
|
|
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
2021-02-15 17:28:33 +01:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-09-12 12:35:48 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockState;
|
|
|
|
import org.bukkit.block.DoubleChest;
|
2021-02-15 17:28:33 +01:00
|
|
|
import org.bukkit.block.Sign;
|
2012-09-12 12:35:48 +02:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
|
|
|
import org.bukkit.event.inventory.InventoryOpenEvent;
|
2020-05-16 23:16:27 +02:00
|
|
|
import org.bukkit.inventory.InventoryHolder;
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2020-09-24 17:54:36 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-01-28 17:18:42 +01:00
|
|
|
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
|
|
|
|
|
2012-09-12 12:35:48 +02:00
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class PlayerInventory implements Listener {
|
|
|
|
@EventHandler
|
|
|
|
public static void onInventoryOpen(InventoryOpenEvent event) {
|
2020-05-16 23:16:27 +02:00
|
|
|
if (!Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
|
2012-09-12 12:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:16:27 +02:00
|
|
|
if (!(event.getPlayer() instanceof Player)) {
|
2012-09-12 12:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:18:42 +01:00
|
|
|
InventoryHolder holder = getHolder(event.getInventory(), false);
|
2020-09-24 17:54:36 +02:00
|
|
|
if (!(holder instanceof BlockState) && !(holder instanceof DoubleChest)) {
|
2012-09-12 12:35:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:16:27 +02:00
|
|
|
Player player = (Player) event.getPlayer();
|
2020-09-24 17:54:36 +02:00
|
|
|
List<Block> containers = new ArrayList<>();
|
2012-11-23 21:03:29 +01:00
|
|
|
|
2020-05-16 23:16:27 +02:00
|
|
|
if (holder instanceof DoubleChest) {
|
2020-09-24 17:54:36 +02:00
|
|
|
containers.add(((BlockState) ((DoubleChest) holder).getLeftSide()).getBlock());
|
|
|
|
containers.add(((BlockState) ((DoubleChest) holder).getRightSide()).getBlock());
|
2012-11-23 21:03:29 +01:00
|
|
|
} else {
|
2020-09-24 17:54:36 +02:00
|
|
|
containers.add(((BlockState) holder).getBlock());
|
2020-05-16 23:16:27 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:54:36 +02:00
|
|
|
boolean canAccess = false;
|
|
|
|
for (Block container : containers) {
|
|
|
|
if (ChestShopSign.isShopBlock(container)) {
|
2021-05-16 01:31:33 +02:00
|
|
|
if (Security.canView(player, container, false)) {
|
2020-09-24 17:54:36 +02:00
|
|
|
canAccess = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canAccess = true;
|
|
|
|
}
|
2012-11-23 21:03:29 +01:00
|
|
|
}
|
2012-09-12 12:35:48 +02:00
|
|
|
|
2020-09-24 17:54:36 +02:00
|
|
|
if (!canAccess) {
|
2021-02-15 17:28:33 +01:00
|
|
|
if (Permission.has(player, Permission.SHOPINFO)) {
|
|
|
|
for (Block container : containers) {
|
|
|
|
Sign sign = uBlock.getConnectedSign(container);
|
|
|
|
if (sign != null) {
|
|
|
|
ChestShop.callEvent(new ShopInfoEvent((Player) event.getPlayer(), sign));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Messages.ACCESS_DENIED.sendWithPrefix(event.getPlayer());
|
|
|
|
}
|
2012-09-12 12:35:48 +02:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|