ProtectionEvents.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package me.km.plots;
  2. import me.km.overrides.ModEntityPlayerMP;
  3. import me.km.permissions.PermissionManager;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.block.Blocks;
  6. import net.minecraft.entity.EntityType;
  7. import net.minecraft.entity.item.ItemFrameEntity;
  8. import net.minecraft.entity.merchant.villager.VillagerEntity;
  9. import net.minecraft.util.math.BlockPos;
  10. import net.minecraft.util.math.RayTraceResult;
  11. import net.minecraft.world.dimension.DimensionType;
  12. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  13. import net.minecraftforge.event.entity.EntityStruckByLightningEvent;
  14. import net.minecraftforge.event.entity.living.LivingAttackEvent;
  15. import net.minecraftforge.event.entity.player.AttackEntityEvent;
  16. import net.minecraftforge.event.entity.player.FillBucketEvent;
  17. import net.minecraftforge.event.entity.player.PlayerInteractEvent;
  18. import net.minecraftforge.event.world.BlockEvent;
  19. import net.minecraftforge.eventbus.api.EventPriority;
  20. import net.minecraftforge.eventbus.api.SubscribeEvent;
  21. public class ProtectionEvents
  22. {
  23. private final static String PLOT_BYPASS = "plot.bypass";
  24. private final IProtection bank;
  25. private final PermissionManager perms;
  26. public ProtectionEvents(IProtection bank, PermissionManager perms)
  27. {
  28. this.bank = bank;
  29. this.perms = perms;
  30. }
  31. @SubscribeEvent(priority = EventPriority.HIGHEST)
  32. public void onBlockPlace(BlockEvent.EntityPlaceEvent e)
  33. {
  34. if(!(e.getEntity() instanceof ModEntityPlayerMP))
  35. {
  36. return;
  37. }
  38. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntity();
  39. if(!perms.hasPermission(p, PLOT_BYPASS) && !bank.canBuild(e.getWorld(), e.getPos(), p))
  40. {
  41. e.setCanceled(true);
  42. }
  43. }
  44. @SubscribeEvent(priority = EventPriority.HIGHEST)
  45. public void onBlockBreak(BlockEvent.BreakEvent e)
  46. {
  47. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getPlayer();
  48. if(!perms.hasPermission(p, PLOT_BYPASS) && !bank.canBuild(e.getWorld(), e.getPos(), p))
  49. {
  50. e.setCanceled(true);
  51. }
  52. }
  53. @SubscribeEvent(priority = EventPriority.HIGHEST)
  54. public void WitherDragonProtection(EntityJoinWorldEvent e)
  55. {
  56. EntityType type = e.getEntity().getType();
  57. if(type == EntityType.WITHER && e.getWorld().getDimension().getType() != DimensionType.NETHER)
  58. {
  59. e.setCanceled(true);
  60. }
  61. else if(type == EntityType.ENDER_DRAGON && e.getWorld().getDimension().getType() != DimensionType.THE_END)
  62. {
  63. e.setCanceled(true);
  64. }
  65. }
  66. @SubscribeEvent(priority = EventPriority.HIGHEST)
  67. public void onBucketFill(FillBucketEvent e)
  68. {
  69. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
  70. RayTraceResult ray = e.getTarget();
  71. if(ray == null || ray.hitInfo == null || ray.getType() != RayTraceResult.Type.BLOCK || perms.hasPermission(p, PLOT_BYPASS))
  72. {
  73. return;
  74. }
  75. BlockPos pos = new BlockPos(ray.getHitVec());
  76. if(!bank.canBuild(e.getWorld(), pos, p))
  77. {
  78. e.setCanceled(true);
  79. }
  80. }
  81. // TODO:protection / entity with uuids
  82. /*@SubscribeEvent(priority = EventPriority.HIGHEST)
  83. public void EntityProtection(LivingAttackEvent e)
  84. {
  85. e.getEntity().getUniqueID()
  86. EntityLivingBase ent = e.getEntityLiving();
  87. if(!this.getProtectionBank().isProtected(ent.getClass()))
  88. {
  89. return;
  90. }
  91. World w = ent.world;
  92. BlockPos pos = ent.getPosition();
  93. if(!this.getProtectionBank().hasProtection(w, pos) ||
  94. (ent instanceof EntityAnimal && this.getProtectionBank().hasTag(w, pos, "animal")))
  95. {
  96. return;
  97. }
  98. if(e.getSource().getImmediateSource() != null)
  99. {
  100. EntityPlayer p = Utils.getDamager(e.getSource());
  101. if(p != null)
  102. {
  103. if(perms.hasPermission(p, Permissions.PLOT_BYPASS) || this.getProtectionBank().canBuild(w, pos, p))
  104. {
  105. return;
  106. }
  107. e.setCanceled(true);
  108. }
  109. }
  110. e.setCanceled(true);
  111. }
  112. @SubscribeEvent(priority = EventPriority.HIGHEST)
  113. public void EntityProtectionPotion(ThrowableImpactEvent e)
  114. {
  115. if(e.getEntityThrowable() instanceof EntityPotion)
  116. {
  117. EntityPotion potion = (EntityPotion) e.getEntityThrowable();
  118. EntityLivingBase ent = potion.getThrower();
  119. if(ent instanceof EntityPlayer && !perms.hasPermission(ent, Permissions.PLOT_BYPASS) &&
  120. !this.getProtectionBank().canBuild(potion.world, potion.getPosition(), (EntityPlayer) ent))
  121. {
  122. e.setCanceled(true);
  123. }
  124. }
  125. }*/
  126. @SubscribeEvent(priority = EventPriority.HIGHEST)
  127. public void onEntityHit(AttackEntityEvent e)
  128. {
  129. if(e.getTarget() instanceof ItemFrameEntity)
  130. {
  131. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
  132. if(!perms.hasPermission(p, PLOT_BYPASS) &&
  133. !bank.canBuild(p.world, e.getTarget().getPosition(), p))
  134. {
  135. e.setCanceled(true);
  136. }
  137. }
  138. }
  139. @SubscribeEvent(priority = EventPriority.HIGHEST)
  140. public void EntityProtectionPotion(EntityStruckByLightningEvent e)
  141. {
  142. e.setCanceled(true);
  143. }
  144. @SubscribeEvent(priority = EventPriority.HIGHEST)
  145. public void onPlayerInteract(PlayerInteractEvent.LeftClickBlock e)
  146. {
  147. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
  148. if(perms.hasPermission(p, PLOT_BYPASS))
  149. {
  150. return;
  151. }
  152. Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
  153. if(b == Blocks.FIRE && !bank.canBuild(e.getWorld(), e.getPos(), p))
  154. {
  155. e.setCanceled(true);
  156. }
  157. }
  158. @SubscribeEvent(priority = EventPriority.HIGHEST)
  159. public void onPlayerInteract(PlayerInteractEvent.RightClickBlock e)
  160. {
  161. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
  162. if(perms.hasPermission(p, PLOT_BYPASS))
  163. {
  164. return;
  165. }
  166. Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
  167. if(!bank.canBuild(e.getWorld(), e.getPos(), p))
  168. {
  169. e.setCanceled(true);
  170. }
  171. }
  172. @SubscribeEvent(priority = EventPriority.HIGHEST)
  173. public void protectFromInteract(PlayerInteractEvent.EntityInteract e)
  174. {
  175. ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
  176. if(!perms.hasPermission(p, PLOT_BYPASS) &&
  177. !bank.canBuild(e.getWorld(), e.getTarget().getPosition(), p))
  178. {
  179. e.setCanceled(true);
  180. }
  181. }
  182. @SubscribeEvent(priority = EventPriority.HIGHEST)
  183. public void protectFromInteract(BlockEvent.FarmlandTrampleEvent e)
  184. {
  185. e.setCanceled(true);
  186. }
  187. @SubscribeEvent
  188. public void onAttackVillager(LivingAttackEvent e)
  189. {
  190. if(e.getEntityLiving() instanceof VillagerEntity && !e.getSource().isCreativePlayer())
  191. {
  192. e.setCanceled(true);
  193. }
  194. }
  195. }