BlockProtection.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package me.km.blockprotections;
  2. import me.km.KajetansMod;
  3. import me.km.api.Utils;
  4. import me.km.api.Module;
  5. import me.km.api.ModuleListener;
  6. import me.km.permissions.Permissions;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockContainer;
  9. import net.minecraft.block.BlockDoor;
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.util.EnumHand;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.world.World;
  16. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  17. import net.minecraftforge.event.world.BlockEvent;
  18. import net.minecraftforge.fml.common.eventhandler.EventPriority;
  19. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  20. public class BlockProtection extends ModuleListener
  21. {
  22. private final BlockProtectionBank bank;
  23. public BlockProtection(Module m)
  24. {
  25. super(m);
  26. bank = KajetansMod.blocks.getDataBank(BlockProtectionBank.class);
  27. }
  28. @SubscribeEvent(priority = EventPriority.HIGHEST)
  29. public void onBlockPlace(BlockEvent.PlaceEvent e)
  30. {
  31. if(!KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection)
  32. {
  33. return;
  34. }
  35. Block b = e.getPlacedBlock().getBlock();
  36. if(!Utils.shouldBeProtected(b))
  37. {
  38. return;
  39. }
  40. EntityPlayer p = e.getPlayer();
  41. World w = e.getWorld();
  42. // Deny placing chests near other protected chests
  43. if(b == Blocks.CHEST || b == Blocks.TRAPPED_CHEST)
  44. {
  45. BlockPos otherChest = Utils.getSameNearbyBlock(w, e.getPos(), b);
  46. if(otherChest != null && !bank.hasAccess(otherChest, w, p.getGameProfile(), true) && !KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS))
  47. {
  48. this.getModule().send(p, "Du darfst diesen Block hier nicht setzen");
  49. e.setCanceled(true);
  50. return;
  51. }
  52. }
  53. // Deny placing of hoppers under blocks
  54. if(b == Blocks.HOPPER)
  55. {
  56. BlockPos pos = e.getPos().add(0, 1, 0);
  57. if(w.getBlockState(pos).getBlock() instanceof BlockContainer)
  58. {
  59. if(!bank.hasAccess(pos, w, p, true) && !KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS))
  60. {
  61. this.getModule().send(p, "Du darfst diesen Block hier nicht setzen");
  62. e.setCanceled(true);
  63. }
  64. }
  65. }
  66. }
  67. @SubscribeEvent(priority = EventPriority.HIGHEST)
  68. public void onBlockBreak(BlockEvent.BreakEvent e)
  69. {
  70. if(!KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection)
  71. {
  72. return;
  73. }
  74. EntityPlayer p = e.getPlayer();
  75. IBlockState state = e.getState();
  76. if(Utils.shouldBeProtected(state.getBlock()))
  77. {
  78. BlockPos pos = e.getPos();
  79. if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
  80. {
  81. pos = pos.add(0, -1, 0);
  82. }
  83. World w = e.getWorld();
  84. if(!bank.doesExist(pos, w))
  85. {
  86. return;
  87. }
  88. if(bank.hasAccess(pos, w, p, false) || KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS))
  89. {
  90. bank.removeBlock(pos, w, p);
  91. return;
  92. }
  93. e.setCanceled(true);
  94. this.getModule().send(p, "Du hast keinen Zugriff auf diesen Block.");
  95. }
  96. }
  97. @SubscribeEvent(priority = EventPriority.HIGHEST)
  98. public void onContainerOpen(PlayerInteractEvent.RightClickBlock e)
  99. {
  100. if(e.getHand() != EnumHand.MAIN_HAND || !KajetansMod.worldManager.getWorldPreferences(e.getWorld()).blockProtection)
  101. {
  102. return;
  103. }
  104. IBlockState state = e.getWorld().getBlockState(e.getPos());
  105. if(!Utils.shouldBeProtected(state.getBlock()))
  106. {
  107. return;
  108. }
  109. EntityPlayer p = e.getEntityPlayer();
  110. BlockPos pos = e.getPos();
  111. if(Utils.getStateValue(state, BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER)
  112. {
  113. pos = pos.add(0, -1, 0);
  114. }
  115. if(bank.hasAccess(pos, e.getWorld(), p, true) || KajetansMod.perms.has(p, Permissions.BLOCK_BYPASS))
  116. {
  117. return;
  118. }
  119. this.getModule().send(p, "Du hast keinen Zugriff auf diesen Block.");
  120. e.setCanceled(true);
  121. }
  122. }