open chest event

Discussion in 'Plugin Development' started by zipron, Sep 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    zipron

  2. Offline

    -_Husky_-

    Yes, then check if the clicked block is a chest
     
  3. Offline

    skore87

    You could just check the inventory open event instead since the interact event is triggered all the time.
    Code:
        @EventHandler
        public void onInventoryOpenEvent(InventoryOpenEvent e){
            if (e.getInventory().getHolder() instanceof Chest || e.getInventory().getHolder() instanceof DoubleChest){
                // rawr
            }
        }
     
    FIRESTORMER and zipron like this.
  4. Offline

    zipron

    that is actually a very nice suggestions, thanks =)
     
  5. Offline

    skore87

    Your question actually got me to figure that out =P. And because of it I just wrote a new protection to my plugin.

    New protection flag for BorderPatrol: CHEST_ACCESS (open)
    It is too bad that a lot of the indenting gets stripped, but if you're really curious you'll be able to see read it. :rolleyes:

    Edit: I goofed on the code, so here's the new version. It allows you to only open a chest piece that you have access to. For example, a doublechest overlaps into another player's region and you have the chestaccess flag and they don't. You'll have full access to the chest where the other person will have only access to the unprotected half.
    Code:java
    1. package com.dpajd.ProtectionPlugin.Protections;
    2.  
    3. import org.bukkit.block.BrewingStand;
    4. import org.bukkit.block.Chest;
    5. import org.bukkit.block.Dispenser;
    6. import org.bukkit.block.DoubleChest;
    7. import org.bukkit.block.Furnace;
    8. import org.bukkit.entity.StorageMinecart;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.inventory.InventoryOpenEvent;
    11. import com.dpajd.ProtectionPlugin.Main.Main;
    12. import com.dpajd.ProtectionPlugin.Main.Main.MsgType;
    13. import com.dpajd.ProtectionPlugin.Regions.Region;
    14.  
    15. public class NoChestAccess extends Protection{
    16.  
    17. public NoChestAccess(Main plugin) {
    18. super(plugin);
    19. }
    20.  
    21. @Override
    22. public ProtectionType getType() {
    23. return ProtectionType.CHEST_ACCESS;
    24. }
    25.  
    26. @EventHandler
    27. public void onInventoryOpenEvent(InventoryOpenEvent e){
    28. if (!plugin.isBypass(e.getPlayer().getName())){
    29. if (plugin.getSettings().hasProtection(this.getType())){
    30. if (e.getInventory().getHolder() instanceof Chest){
    31. Chest c = (Chest) e.getInventory().getHolder();
    32. if (plugin.isProtected(c.getChunk(), this.getType())){
    33. if (!plugin.getRegion(c.getChunk()).hasAccess(e.getPlayer().getName())){
    34. e.setCancelled(true);
    35. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that chest!");
    36. }
    37. }
    38. }else if (e.getInventory().getHolder() instanceof DoubleChest){
    39. DoubleChest dc = (DoubleChest) e.getInventory().getHolder();
    40. Chest left = (Chest) dc.getLeftSide();
    41. Chest right = (Chest) dc.getRightSide();
    42. if (plugin.isProtected(left.getChunk(), this.getType()) || plugin.isProtected(right.getChunk(), this.getType())){
    43. Region rl = plugin.getRegion(left.getChunk());
    44. Region rr = plugin.getRegion(right.getChunk());
    45.  
    46. // At least ONE of the possible two chunks is protected
    47. if (rl == rr){
    48. if (rl != null){
    49. if (rl.hasProtection(this.getType()) && !rl.hasAccess(e.getPlayer().getName())){
    50. e.setCancelled(true);
    51. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that chest!");
    52. }
    53. }
    54. }else{
    55. if ((rl != null) ^ (rr != null)){
    56. // Only ONE is a region
    57. if (rl != null){
    58. if (rl.hasProtection(this.getType())){
    59. System.out.println("rl has protection:" + this.getType());
    60. if (!rl.hasAccess(e.getPlayer().getName())){
    61. e.setCancelled(true);
    62. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that chest!");
    63. }else{
    64. e.setCancelled(true);
    65. e.getPlayer().openInventory(left.getBlockInventory());
    66. }
    67. }
    68. }else{
    69. if (rr.hasProtection(this.getType())){
    70. System.out.println("rr has protection:" + this.getType());
    71. if (!rr.hasAccess(e.getPlayer().getName())){
    72. e.setCancelled(true);
    73. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that chest!");
    74. }else{
    75. e.setCancelled(true);
    76. e.getPlayer().openInventory(right.getBlockInventory());
    77. }
    78. }
    79. }
    80. }else if (rl != null && rr != null){
    81. boolean canOpenLeft = false;
    82. boolean canOpenRight = false;
    83.  
    84. // BOTH are a region
    85. if (rl.hasProtection(this.getType())){
    86. if (rl.hasAccess(e.getPlayer().getName())){
    87. canOpenLeft = true;
    88. }
    89. }else{
    90. canOpenLeft = true;
    91. }
    92. if (rr.hasProtection(this.getType())){
    93. if (rr.hasAccess(e.getPlayer().getName())){
    94. canOpenRight = true;
    95. }
    96. }else{
    97. canOpenRight = true;
    98. }
    99.  
    100. if (!(canOpenLeft == true && canOpenRight == true)){
    101. if (canOpenLeft){
    102. e.setCancelled(true);
    103. e.getPlayer().openInventory(left.getBlockInventory());
    104. }else if (canOpenRight){
    105. e.setCancelled(true);
    106. e.getPlayer().openInventory(right.getBlockInventory());
    107. }else{
    108. e.setCancelled(true);
    109. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that chest!");
    110. }
    111. }
    112. }
    113. }
    114.  
    115. }
    116. }else if (e.getInventory().getHolder() instanceof Furnace){
    117. Furnace f = (Furnace) e.getInventory().getHolder();
    118. if (plugin.isProtected(f.getChunk(), this.getType())){
    119. if (!plugin.getRegion(f.getChunk()).hasAccess(e.getPlayer().getName())){
    120. e.setCancelled(true);
    121. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that furnace!");
    122. }
    123. }
    124. }else if (e.getInventory().getHolder() instanceof StorageMinecart){
    125. StorageMinecart s = (StorageMinecart) e.getInventory().getHolder();
    126. if (plugin.isProtected(s.getLocation().getChunk(), this.getType())){
    127. if (!plugin.getRegion(s.getLocation().getChunk()).hasAccess(e.getPlayer().getName())){
    128. e.setCancelled(true);
    129. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that minecart!");
    130. }
    131. }
    132. }else if (e.getInventory().getHolder() instanceof Dispenser){
    133. Dispenser d = (Dispenser) e.getInventory().getHolder();
    134. if (plugin.isProtected(d.getChunk(), this.getType())){
    135. if (!plugin.getRegion(d.getChunk()).hasAccess(e.getPlayer().getName())){
    136. e.setCancelled(true);
    137. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that dispenser!");
    138. }
    139. }
    140.  
    141. }else if (e.getInventory().getHolder() instanceof BrewingStand){
    142. BrewingStand b = (BrewingStand) e.getInventory().getHolder();
    143. if (plugin.isProtected(b.getChunk(), this.getType())){
    144. if (!plugin.getRegion(b.getChunk()).hasAccess(e.getPlayer().getName())){
    145. e.setCancelled(true);
    146. plugin.sendMessage(e.getPlayer().getName(), MsgType.DENIED, "You do not have access to that brewing stand!");
    147. }
    148. }
    149. }
    150. }
    151. }
    152.  
    153. }
    154. }
    155.  
     
    FIRESTORMER likes this.
Thread Status:
Not open for further replies.

Share This Page