Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent illegal items in chests #314

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -17,6 +19,7 @@
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
Expand All @@ -27,9 +30,12 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.scheduler.BukkitRunnable;
Expand Down Expand Up @@ -471,6 +477,44 @@ public void onPlayerInteract(PlayerInteractEvent e) {
handleInteractEvent(e);
}

@EventHandler
public void validateChestContents(InventoryCloseEvent event) {
Inventory inventory = event.getInventory();
if (!inventory.getType().equals(InventoryType.CHEST)) return;

Location location;
InventoryHolder holder = inventory.getHolder();
if (holder instanceof Chest) {
location = ((Chest) holder).getLocation();
} else if (holder instanceof DoubleChest) {
location = ((DoubleChest) holder).getLocation();
} else {
return;
}

Shop shop = plugin.getShopUtils().getShop(location);
if (shop == null) return;

ItemStack product = shop.getProduct().getItemStack();
ItemStack[] contents = inventory.getContents();
List<ItemStack> removed = new ArrayList<>(contents.length);

for (int i = 0; i < contents.length; i++) {
ItemStack itemStack = contents[i];
if (itemStack != null && !ItemUtils.isAir(itemStack.getType()) && !product.isSimilar(itemStack)) {
contents[i] = null;
removed.add(itemStack);
}
}

inventory.setContents(contents);

World world = location.getWorld();
for (ItemStack itemStack : removed) {
world.dropItemNaturally(location, itemStack);
}
}

/**
* Create a new shop
*
Expand Down