Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.zyrakia.chestprotect;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockState;
- import org.bukkit.block.Chest;
- import org.bukkit.block.DoubleChest;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.DoubleChestInventory;
- import org.bukkit.inventory.Inventory;
- import net.md_5.bungee.api.ChatColor;
- public class Events implements Listener {
- FileConfiguration config = Main.plugin.getConfig();
- public Events(Main plugin) {
- plugin.getServer().getPluginManager().registerEvents(this, plugin);
- }
- @EventHandler
- public void blockPlace(BlockPlaceEvent e) {
- Player player = e.getPlayer();
- Material block = e.getBlock().getType();
- World blockworld = e.getBlock().getWorld();
- int blockx = e.getBlock().getX();
- int blocky = e.getBlock().getY();
- int blockz = e.getBlock().getZ();
- if (block.equals(Material.CHEST)) {
- Block blockChest = e.getBlock();
- BlockState chestState = blockChest.getState();
- if (chestState instanceof Chest) {
- Chest chest = (Chest) chestState;
- Inventory inventory = chest.getInventory();
- if (inventory instanceof DoubleChestInventory) {
- Main.plugin.saveConfig();
- } else {
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7Look at the chest and type &e/lock &7to make this chest private!"));
- if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
- Main.plugin.saveConfig();
- } else {
- config.set("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, player.getUniqueId().toString());
- config.set("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, "placed");
- Main.plugin.saveConfig();
- }
- }
- }
- }
- }
- @EventHandler
- public void blockBreak(BlockBreakEvent e) {
- Player player = e.getPlayer();
- Material block = e.getBlock().getType();
- World blockworld = e.getBlock().getWorld();
- int blockx = e.getBlock().getX();
- int blocky = e.getBlock().getY();
- int blockz = e.getBlock().getZ();
- if (block.equals(Material.CHEST)) {
- if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
- if (config.get("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals(player.getUniqueId().toString())) {
- if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
- e.setCancelled(true);
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked. It cannot be broken until you unlock it. (Use &e/lock &7again!)"));
- }
- else {
- config.set("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, null);
- config.set("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, null);
- Main.plugin.saveConfig();
- }
- }
- else if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
- e.setCancelled(true);
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked, contact the owner to interact with it!"));
- }
- else {
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked! Ask the person who locked it to access it!"));
- e.setCancelled(true);
- }
- }
- else {
- Main.plugin.saveConfig();
- }
- }
- }
- @EventHandler
- public void interact(PlayerInteractEvent e) {
- if (e.getClickedBlock() != null) {
- if (e.getClickedBlock().getType() == Material.CHEST) {
- Player player = e.getPlayer();
- Block chestBlock = e.getClickedBlock();
- BlockState chestState = chestBlock.getState();
- if (chestState instanceof Chest) {
- Chest chest = (Chest) chestState;
- Inventory inventory = chest.getInventory();
- if (inventory instanceof DoubleChestInventory) {
- Main.plugin.saveConfig();
- DoubleChest doubleChest = (DoubleChest) inventory.getHolder();
- player.sendMessage(doubleChest.getWorld() + ", " + doubleChest.getX() + ", " + doubleChest.getY() + ", " + doubleChest.getZ());
- } else {
- World blockworld = e.getClickedBlock().getWorld();
- int blockx = e.getClickedBlock().getX();
- int blocky = e.getClickedBlock().getY();
- int blockz = e.getClickedBlock().getZ();
- if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
- if (config.get("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals(player.getUniqueId().toString())) {
- e.setCancelled(false);
- }
- else if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
- e.setCancelled(true);
- player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked, contact the owner to interact with it!"));
- }
- else {
- e.setCancelled(false);
- }
- }
- else {
- Main.plugin.saveConfig();
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement