Fix InventoryOpenEvent listener not applying to non chest containers

This commit is contained in:
Phoenix616 2020-05-16 22:16:27 +01:00
parent 009febb5c2
commit 7f32a7a722

View File

@ -3,16 +3,15 @@ package com.Acrobot.ChestShop.Listeners.Player;
import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Security; import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.InventoryHolder;
/** /**
* @author Acrobot * @author Acrobot
@ -20,30 +19,33 @@ import org.bukkit.event.inventory.InventoryType;
public class PlayerInventory implements Listener { public class PlayerInventory implements Listener {
@EventHandler @EventHandler
public static void onInventoryOpen(InventoryOpenEvent event) { public static void onInventoryOpen(InventoryOpenEvent event) {
if (event.getInventory().getType() != InventoryType.CHEST) {
return;
}
if (!Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) { if (!Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
return; return;
} }
HumanEntity entity = event.getPlayer(); if (!(event.getPlayer() instanceof Player)) {
if (!(entity instanceof Player) || (!(event.getInventory().getHolder() instanceof Chest) && !(event.getInventory().getHolder() instanceof DoubleChest))) {
return; return;
} }
Player player = (Player) entity; InventoryHolder holder = event.getInventory().getHolder();
Block chest; if (!(holder instanceof BlockState)) {
return;
if (event.getInventory().getHolder() instanceof Chest) {
chest = ((BlockState) event.getInventory().getHolder()).getBlock();
} else {
chest = ((DoubleChest) event.getInventory().getHolder()).getLocation().getBlock();
} }
if (!Security.canAccess(player, chest)) { Player player = (Player) event.getPlayer();
Block container;
if (holder instanceof DoubleChest) {
container = ((DoubleChest) holder).getLocation().getBlock();
} else {
container = ((BlockState) holder).getBlock();
}
if (!ChestShopSign.isShopBlock(container)) {
return;
}
if (!Security.canAccess(player, container)) {
player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED)); player.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
event.setCancelled(true); event.setCancelled(true);
} }