Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. package me.ferdz.placeableitems.ai;
  2.  
  3. import java.util.List;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.entity.EntityLiving;
  7. import net.minecraft.entity.ai.EntityAIBase;
  8. import net.minecraft.util.EnumFacing;
  9. import net.minecraft.util.math.BlockPos;
  10.  
  11. public class EntityAIAttractBlock extends EntityAIBase {
  12.  
  13. private EntityLiving entity;
  14. private List<Block> blocks;
  15. private BlockPos destPos;
  16.  
  17. public EntityAIAttractBlock(EntityLiving entity, List<Block> blocks) {
  18. this.entity = entity;
  19. this.blocks = blocks;
  20. }
  21.  
  22. @Override
  23. public boolean shouldExecute() {
  24. try {
  25. for (int i = -6; i < 7; i++) {
  26. for (int j = -6; j < 7; j++) {
  27. for (int k = -6; k < 7; k++) {
  28. BlockPos pos = entity.getPosition().add(i, j, k);
  29. if (blocks.contains(entity.getEntityWorld().getBlockState(pos).getBlock())) {
  30. destPos = pos.offset(EnumFacing.getFacingFromVector(pos.getX(), pos.getY(), pos.getZ()));
  31. return destPos != null;
  32. }
  33. }
  34. }
  35. }
  36. } catch (NullPointerException e) { /* not exactly sure why this happens sometimes */ }
  37.  
  38. return false;
  39. }
  40.  
  41. @Override
  42. public void startExecuting() {
  43. entity.getNavigator().tryMoveToXYZ(destPos.getX(), destPos.getY(), destPos.getZ(), 1);
  44. entity.getLookHelper().setLookPosition(destPos.getX() + 0.5D, destPos.getY(), destPos.getZ() + 0.5D, 30, 30);
  45. }
  46.  
  47. @Override
  48. public void updateTask() {
  49. entity.getLookHelper().setLookPosition(destPos.getX() + 0.5D, destPos.getY(), destPos.getZ() + 0.5D, 30, 30);
  50. }
  51.  
  52. @Override
  53. public boolean continueExecuting() {
  54. return entity.getPosition().getDistance(destPos.getX(), destPos.getY(), destPos.getZ()) > 1 || entity.getNavigator().noPath();
  55. }
  56.  
  57. @Override
  58. public void resetTask() {
  59. destPos = null;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement