BlockProtection.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package me.km.blockprotections;
  2. import me.km.KajetansMod;
  3. import me.km.module.Module;
  4. import me.km.utils.Location;
  5. import me.km.utils.Utils;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockButton;
  8. import net.minecraft.block.BlockContainer;
  9. import net.minecraft.block.BlockDoor;
  10. import net.minecraft.block.BlockPistonMoving;
  11. import net.minecraft.block.BlockTrapDoor;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.util.EnumHand;
  16. import net.minecraft.util.math.BlockPos;
  17. import net.minecraft.util.text.TextFormatting;
  18. import net.minecraft.world.World;
  19. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  20. import net.minecraftforge.event.world.BlockEvent;
  21. import net.minecraftforge.fml.common.eventhandler.EventPriority;
  22. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  23. public class BlockProtection extends Module
  24. {
  25. private final static String BLOCK_BYPASS = "block.bypass";
  26. private final BlockProtectionBank bank;
  27. public BlockProtection()
  28. {
  29. super("Blocks", TextFormatting.BLUE);
  30. bank = new BlockProtectionBank();
  31. registerEvent(this);
  32. }
  33. public BlockProtectionBank getDatabank()
  34. {
  35. return bank;
  36. }
  37. private boolean shouldBeProtected(Block b)
  38. {
  39. return (b instanceof BlockDoor ||
  40. b instanceof BlockContainer ||
  41. b == Blocks.LEVER ||
  42. b instanceof BlockButton ||
  43. b instanceof BlockTrapDoor) && !(b instanceof BlockPistonMoving);
  44. }
  45. private BlockPos getSameNearbyBlock(World w, BlockPos pos, Block b)
  46. {
  47. Location l = new Location(w, pos);
  48. if(l.getRelativeBlockState(1, 0, 0).getBlock() == b)
  49. {
  50. return pos.add(1, 0, 0);
  51. }
  52. else if(l.getRelativeBlockState(-1, 0, 0).getBlock() == b)
  53. {
  54. return pos.add(-1, 0, 0);
  55. }
  56. else if(l.getRelativeBlockState(0, 0, 1).getBlock() == b)
  57. {
  58. return pos.add(0, 0, 1);
  59. }
  60. else if(l.getRelativeBlockState(0, 0, -1).getBlock() == b)
  61. {
  62. return pos.add(0, 0, -1);
  63. }
  64. return null;
  65. }
  66. @SubscribeEvent(priority = EventPriority.HIGHEST)
  67. public void onBlockPlace(BlockEvent.PlaceEvent e)
  68. {
  69. Block b = e.getPlacedBlock().getBlock();
  70. EntityPlayer p = e.getPlayer();
  71. World w = e.getWorld();
  72. if(b == Blocks.CHEST || b == Blocks.TRAPPED_CHEST) // Deny placing chests near other protected chests
  73. {
  74. BlockPos otherChest = getSameNearbyBlock(w, e.getPos(), b);
  75. if(otherChest != null && !bank.hasAccess(otherChest, w, p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
  76. {
  77. e.setCanceled(true);
  78. }
  79. }
  80. else if(b == Blocks.HOPPER) // Deny placing of hoppers under blocks
  81. {
  82. BlockPos pos = e.getPos().add(0, 1, 0);
  83. if(w.getBlockState(pos).getBlock() instanceof BlockContainer)
  84. {
  85. if(!bank.hasAccess(pos, w, p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
  86. {
  87. e.setCanceled(true);
  88. }
  89. }
  90. }
  91. }
  92. @SubscribeEvent(priority = EventPriority.HIGHEST)
  93. public void onBlockBreak(BlockEvent.BreakEvent e)
  94. {
  95. EntityPlayer p = e.getPlayer();
  96. IBlockState state = e.getState();
  97. if(shouldBeProtected(state.getBlock()))
  98. {
  99. BlockPos pos = e.getPos();
  100. if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
  101. {
  102. pos = pos.add(0, -1, 0);
  103. }
  104. World w = e.getWorld();
  105. if(bank.hasAccess(pos, w, p) || KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
  106. {
  107. bank.remove(pos, w);
  108. return;
  109. }
  110. e.setCanceled(true);
  111. }
  112. }
  113. @SubscribeEvent(priority = EventPriority.HIGHEST)
  114. public void onContainerOpen(PlayerInteractEvent.RightClickBlock e)
  115. {
  116. if(e.getHand() != EnumHand.MAIN_HAND)
  117. {
  118. return;
  119. }
  120. IBlockState state = e.getWorld().getBlockState(e.getPos());
  121. if(!shouldBeProtected(state.getBlock()))
  122. {
  123. return;
  124. }
  125. EntityPlayer p = e.getEntityPlayer();
  126. BlockPos pos = e.getPos();
  127. if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
  128. {
  129. pos = pos.add(0, -1, 0);
  130. }
  131. if(!bank.hasAccess(pos, e.getWorld(), p) && !KajetansMod.perms.hasPermission(p, BLOCK_BYPASS))
  132. {
  133. e.setCanceled(true);
  134. }
  135. }
  136. }