Advertisement
Guest User

Untitled

a guest
Jun 30th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.52 KB | None | 0 0
  1. package me.zyrakia.chestprotect;
  2.  
  3. import org.bukkit.Material;
  4. import org.bukkit.World;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.block.BlockState;
  7. import org.bukkit.block.Chest;
  8. import org.bukkit.block.DoubleChest;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.block.BlockBreakEvent;
  14. import org.bukkit.event.block.BlockPlaceEvent;
  15. import org.bukkit.event.player.PlayerInteractEvent;
  16. import org.bukkit.inventory.DoubleChestInventory;
  17. import org.bukkit.inventory.Inventory;
  18.  
  19. import net.md_5.bungee.api.ChatColor;
  20.  
  21. public class Events implements Listener {
  22.  
  23.     FileConfiguration config = Main.plugin.getConfig();
  24.  
  25.     public Events(Main plugin) {
  26.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  27.     }
  28.  
  29.     @EventHandler
  30.     public void blockPlace(BlockPlaceEvent e) {
  31.  
  32.         Player player = e.getPlayer();
  33.         Material block = e.getBlock().getType();
  34.         World blockworld = e.getBlock().getWorld();
  35.         int blockx = e.getBlock().getX();
  36.         int blocky = e.getBlock().getY();
  37.         int blockz = e.getBlock().getZ();
  38.  
  39.         if (block.equals(Material.CHEST)) {
  40.  
  41.             Block blockChest = e.getBlock();
  42.  
  43.             BlockState chestState = blockChest.getState();
  44.  
  45.             if (chestState instanceof Chest) {
  46.                 Chest chest = (Chest) chestState;
  47.                 Inventory inventory = chest.getInventory();
  48.  
  49.                 if (inventory instanceof DoubleChestInventory) {
  50.                     Main.plugin.saveConfig();
  51.                 } else {
  52.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7Look at the chest and type &e/lock &7to make this chest private!"));
  53.  
  54.                     if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
  55.                         Main.plugin.saveConfig();
  56.                     } else {
  57.                         config.set("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, player.getUniqueId().toString());
  58.                         config.set("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, "placed");
  59.                         Main.plugin.saveConfig();
  60.                     }
  61.  
  62.                 }
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.     }
  69.  
  70.     @EventHandler
  71.     public void blockBreak(BlockBreakEvent e) {
  72.  
  73.         Player player = e.getPlayer();
  74.         Material block = e.getBlock().getType();
  75.         World blockworld = e.getBlock().getWorld();
  76.         int blockx = e.getBlock().getX();
  77.         int blocky = e.getBlock().getY();
  78.         int blockz = e.getBlock().getZ();
  79.  
  80.         if (block.equals(Material.CHEST)) {
  81.  
  82.             if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
  83.                 if (config.get("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals(player.getUniqueId().toString())) {
  84.                     if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
  85.                         e.setCancelled(true);
  86.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked. It cannot be broken until you unlock it. (Use &e/lock &7again!)"));
  87.                     }
  88.                     else {
  89.                         config.set("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, null);
  90.                         config.set("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz, null);
  91.                         Main.plugin.saveConfig();  
  92.                     }
  93.                 }
  94.                 else if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
  95.                     e.setCancelled(true);
  96.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked, contact the owner to interact with it!"));
  97.                 }
  98.                 else {
  99.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked! Ask the person who locked it to access it!"));
  100.                     e.setCancelled(true);
  101.                 }
  102.             }
  103.             else {
  104.                 Main.plugin.saveConfig();
  105.             }
  106.  
  107.         }
  108.  
  109.     }
  110.  
  111.     @EventHandler
  112.     public void interact(PlayerInteractEvent e) {
  113.  
  114.         if (e.getClickedBlock() != null) {
  115.             if (e.getClickedBlock().getType() == Material.CHEST) {
  116.                
  117.                 Player player = e.getPlayer();
  118.  
  119.                 Block chestBlock = e.getClickedBlock();
  120.                 BlockState chestState = chestBlock.getState();
  121.  
  122.                 if (chestState instanceof Chest) {
  123.                     Chest chest = (Chest) chestState;
  124.                     Inventory inventory = chest.getInventory();
  125.  
  126.                     if (inventory instanceof DoubleChestInventory) {
  127.                         Main.plugin.saveConfig();
  128.                        
  129.                         DoubleChest doubleChest = (DoubleChest) inventory.getHolder();
  130.                        
  131.                         player.sendMessage(doubleChest.getWorld() + ", " + doubleChest.getX() + ", " + doubleChest.getY() + ", " + doubleChest.getZ());
  132.  
  133.                     } else {
  134.                        
  135.                         World blockworld = e.getClickedBlock().getWorld();
  136.                         int blockx = e.getClickedBlock().getX();
  137.                         int blocky = e.getClickedBlock().getY();
  138.                         int blockz = e.getClickedBlock().getZ();
  139.                        
  140.                         if (config.contains("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz)) {
  141.                             if (config.get("PlacedChests." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals(player.getUniqueId().toString())) {
  142.                                 e.setCancelled(false);
  143.                             }
  144.                             else if (config.get("Status." + blockworld + ", " + blockx + ", " + blocky + ", " + blockz).equals("locked")) {
  145.                                 e.setCancelled(true);
  146.                                 player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l! &r&7This chest is locked, contact the owner to interact with it!"));
  147.                             }
  148.                             else {
  149.                                 e.setCancelled(false);
  150.                             }
  151.                         }
  152.                         else {
  153.                             Main.plugin.saveConfig();
  154.                         }
  155.                        
  156.                     }
  157.                 }
  158.  
  159.             }
  160.         }
  161.  
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement