BlockProtectionEvents.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package me.km.blockprotections;
  2. import me.km.overrides.ModEntityPlayerMP;
  3. import me.km.permissions.PermissionManager;
  4. import me.km.utils.Location;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.ContainerBlock;
  7. import net.minecraft.block.DoorBlock;
  8. import net.minecraft.block.BlockState;
  9. import net.minecraft.block.Blocks;
  10. import net.minecraft.state.properties.DoubleBlockHalf;
  11. import net.minecraft.tags.BlockTags;
  12. import net.minecraft.util.Hand;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.world.IWorld;
  15. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  16. import net.minecraftforge.event.world.BlockEvent;
  17. import net.minecraftforge.eventbus.api.EventPriority;
  18. import net.minecraftforge.eventbus.api.SubscribeEvent;
  19. public class BlockProtectionEvents
  20. {
  21. private final static String BLOCK_BYPASS = "block.bypass";
  22. private final IBlockProtection bank;
  23. private final PermissionManager perms;
  24. public BlockProtectionEvents(IBlockProtection bank, PermissionManager perms)
  25. {
  26. this.bank = bank;
  27. this.perms = perms;
  28. }
  29. private BlockPos getSameNearbyBlock(IWorld w, BlockPos pos, Block b)
  30. {
  31. Location l = new Location(w, pos);
  32. if(l.getRelativeBlockState(1, 0, 0).getBlock() == b)
  33. {
  34. return pos.add(1, 0, 0);
  35. }
  36. else if(l.getRelativeBlockState(-1, 0, 0).getBlock() == b)
  37. {
  38. return pos.add(-1, 0, 0);
  39. }
  40. else if(l.getRelativeBlockState(0, 0, 1).getBlock() == b)
  41. {
  42. return pos.add(0, 0, 1);
  43. }
  44. else if(l.getRelativeBlockState(0, 0, -1).getBlock() == b)
  45. {
  46. return pos.add(0, 0, -1);
  47. }
  48. return null;
  49. }
  50. @SubscribeEvent(priority = EventPriority.HIGHEST)
  51. public void onBlockPlace(BlockEvent.EntityPlaceEvent e)
  52. {
  53. Block b = e.getPlacedBlock().getBlock();
  54. if(!(e.getEntity() instanceof ModEntityPlayerMP))
  55. {
  56. return;
  57. }
  58. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntity();
  59. IWorld w = e.getWorld();
  60. if(b == Blocks.CHEST || b == Blocks.TRAPPED_CHEST) // Deny placing chests near other protected chests
  61. {
  62. BlockPos otherChest = getSameNearbyBlock(w, e.getPos(), b);
  63. if(otherChest != null && !bank.hasAccess(otherChest, w, p) && !perms.hasPermission(p, BLOCK_BYPASS))
  64. {
  65. e.setCanceled(true);
  66. }
  67. }
  68. else if(b == Blocks.HOPPER) // Deny placing of hoppers under blocks
  69. {
  70. BlockPos pos = e.getPos().add(0, 1, 0);
  71. if(w.getBlockState(pos).getBlock() instanceof ContainerBlock)
  72. {
  73. if(!bank.hasAccess(pos, w, p) && !perms.hasPermission(p, BLOCK_BYPASS))
  74. {
  75. e.setCanceled(true);
  76. }
  77. }
  78. }
  79. }
  80. @SubscribeEvent(priority = EventPriority.HIGHEST)
  81. public void onBlockBreak(BlockEvent.BreakEvent e)
  82. {
  83. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getPlayer();
  84. BlockState state = e.getState();
  85. BlockPos pos = e.getPos();
  86. if(BlockTags.DOORS.contains(state.getBlock()) && state.get(DoorBlock.HALF) == DoubleBlockHalf.UPPER)
  87. {
  88. pos = pos.add(0, -1, 0);
  89. }
  90. IWorld w = e.getWorld();
  91. if(bank.hasAccess(pos, w, p) || perms.hasPermission(p, BLOCK_BYPASS))
  92. {
  93. // ToDo remove protection
  94. return;
  95. }
  96. e.setCanceled(true);
  97. }
  98. @SubscribeEvent(priority = EventPriority.HIGHEST)
  99. public void onContainerOpen(PlayerInteractEvent.RightClickBlock e)
  100. {
  101. if(e.getHand() != Hand.MAIN_HAND)
  102. {
  103. return;
  104. }
  105. BlockState state = e.getWorld().getBlockState(e.getPos());
  106. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getPlayer();
  107. BlockPos pos = e.getPos();
  108. if(BlockTags.DOORS.contains(state.getBlock()) && state.get(DoorBlock.HALF) == DoubleBlockHalf.UPPER)
  109. {
  110. pos = pos.add(0, -1, 0);
  111. }
  112. if(!bank.hasAccess(pos, e.getWorld(), p) && !perms.hasPermission(p, BLOCK_BYPASS))
  113. {
  114. e.setCanceled(true);
  115. }
  116. }
  117. }