123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package me.km.plots;
- import me.km.overrides.ModEntityPlayerMP;
- import me.km.permissions.PermissionManager;
- import net.minecraft.block.Block;
- import net.minecraft.block.Blocks;
- import net.minecraft.entity.EntityType;
- import net.minecraft.entity.item.ItemFrameEntity;
- import net.minecraft.entity.merchant.villager.VillagerEntity;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.RayTraceResult;
- import net.minecraft.world.dimension.DimensionType;
- import net.minecraftforge.event.entity.EntityJoinWorldEvent;
- import net.minecraftforge.event.entity.EntityStruckByLightningEvent;
- import net.minecraftforge.event.entity.living.LivingAttackEvent;
- import net.minecraftforge.event.entity.player.AttackEntityEvent;
- import net.minecraftforge.event.entity.player.FillBucketEvent;
- import net.minecraftforge.event.entity.player.PlayerInteractEvent;
- import net.minecraftforge.event.world.BlockEvent;
- import net.minecraftforge.eventbus.api.EventPriority;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- public class ProtectionEvents
- {
- private final static String PLOT_BYPASS = "plot.bypass";
-
- private final IProtection bank;
- private final PermissionManager perms;
-
- public ProtectionEvents(IProtection bank, PermissionManager perms)
- {
- this.bank = bank;
- this.perms = perms;
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onBlockPlace(BlockEvent.EntityPlaceEvent e)
- {
- if(!(e.getEntity() instanceof ModEntityPlayerMP))
- {
- return;
- }
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntity();
- if(!perms.hasPermission(p, PLOT_BYPASS) && !bank.canBuild(e.getWorld(), e.getPos(), p))
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onBlockBreak(BlockEvent.BreakEvent e)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getPlayer();
- if(!perms.hasPermission(p, PLOT_BYPASS) && !bank.canBuild(e.getWorld(), e.getPos(), p))
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void WitherDragonProtection(EntityJoinWorldEvent e)
- {
- EntityType type = e.getEntity().getType();
- if(type == EntityType.WITHER && e.getWorld().getDimension().getType() != DimensionType.NETHER)
- {
- e.setCanceled(true);
- }
- else if(type == EntityType.ENDER_DRAGON && e.getWorld().getDimension().getType() != DimensionType.THE_END)
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onBucketFill(FillBucketEvent e)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
- RayTraceResult ray = e.getTarget();
- if(ray == null || ray.hitInfo == null || ray.getType() != RayTraceResult.Type.BLOCK || perms.hasPermission(p, PLOT_BYPASS))
- {
- return;
- }
- BlockPos pos = new BlockPos(ray.getHitVec());
- if(!bank.canBuild(e.getWorld(), pos, p))
- {
- e.setCanceled(true);
- }
- }
-
- // TODO:protection / entity with uuids
- /*@SubscribeEvent(priority = EventPriority.HIGHEST)
- public void EntityProtection(LivingAttackEvent e)
- {
- e.getEntity().getUniqueID()
- EntityLivingBase ent = e.getEntityLiving();
- if(!this.getProtectionBank().isProtected(ent.getClass()))
- {
- return;
- }
- World w = ent.world;
- BlockPos pos = ent.getPosition();
- if(!this.getProtectionBank().hasProtection(w, pos) ||
- (ent instanceof EntityAnimal && this.getProtectionBank().hasTag(w, pos, "animal")))
- {
- return;
- }
-
- if(e.getSource().getImmediateSource() != null)
- {
- EntityPlayer p = Utils.getDamager(e.getSource());
- if(p != null)
- {
- if(perms.hasPermission(p, Permissions.PLOT_BYPASS) || this.getProtectionBank().canBuild(w, pos, p))
- {
- return;
- }
- e.setCanceled(true);
- }
- }
- e.setCanceled(true);
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void EntityProtectionPotion(ThrowableImpactEvent e)
- {
- if(e.getEntityThrowable() instanceof EntityPotion)
- {
- EntityPotion potion = (EntityPotion) e.getEntityThrowable();
- EntityLivingBase ent = potion.getThrower();
- if(ent instanceof EntityPlayer && !perms.hasPermission(ent, Permissions.PLOT_BYPASS) &&
- !this.getProtectionBank().canBuild(potion.world, potion.getPosition(), (EntityPlayer) ent))
- {
- e.setCanceled(true);
- }
- }
- }*/
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onEntityHit(AttackEntityEvent e)
- {
- if(e.getTarget() instanceof ItemFrameEntity)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
- if(!perms.hasPermission(p, PLOT_BYPASS) &&
- !bank.canBuild(p.world, e.getTarget().getPosition(), p))
- {
- e.setCanceled(true);
- }
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void EntityProtectionPotion(EntityStruckByLightningEvent e)
- {
- e.setCanceled(true);
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onPlayerInteract(PlayerInteractEvent.LeftClickBlock e)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
- if(perms.hasPermission(p, PLOT_BYPASS))
- {
- return;
- }
- Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
- if(b == Blocks.FIRE && !bank.canBuild(e.getWorld(), e.getPos(), p))
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void onPlayerInteract(PlayerInteractEvent.RightClickBlock e)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
- if(perms.hasPermission(p, PLOT_BYPASS))
- {
- return;
- }
- Block b = e.getWorld().getBlockState(e.getPos()).getBlock();
- if(!bank.canBuild(e.getWorld(), e.getPos(), p))
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void protectFromInteract(PlayerInteractEvent.EntityInteract e)
- {
- ModEntityPlayerMP p = (ModEntityPlayerMP) e.getEntityPlayer();
- if(!perms.hasPermission(p, PLOT_BYPASS) &&
- !bank.canBuild(e.getWorld(), e.getTarget().getPosition(), p))
- {
- e.setCanceled(true);
- }
- }
-
- @SubscribeEvent(priority = EventPriority.HIGHEST)
- public void protectFromInteract(BlockEvent.FarmlandTrampleEvent e)
- {
- e.setCanceled(true);
- }
-
- @SubscribeEvent
- public void onAttackVillager(LivingAttackEvent e)
- {
- if(e.getEntityLiving() instanceof VillagerEntity && !e.getSource().isCreativePlayer())
- {
- e.setCanceled(true);
- }
- }
- }
|