svdragster

AIVendor

Feb 8th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package de.svdragster.rpg;
  2.  
  3. import java.util.List;
  4.  
  5. import net.canarymod.api.ai.AIBase;
  6. import net.canarymod.api.entity.living.EntityLiving;
  7. import net.canarymod.api.entity.living.humanoid.Player;
  8. import net.canarymod.api.world.position.Location;
  9. import net.canarymod.api.world.position.Vector3D;
  10.  
  11. public class AIVendor implements AIBase {
  12.  
  13.     private EntityLiving mob = null;
  14.     private Player target = null;
  15.     private Location spawn = null;
  16.     private boolean search = true;
  17.     private boolean speak = true;
  18.    
  19.     public AIVendor(EntityLiving mob, Location spawn) {
  20.         this.mob = mob;
  21.         this.spawn = spawn;
  22.     }
  23.    
  24.     @Override
  25.     public boolean continueExecuting() {
  26.         return this.target != null;
  27.     }
  28.  
  29.     @Override
  30.     public boolean isContinuous() {
  31.         return true;
  32.     }
  33.  
  34.     @Override
  35.     public void resetTask() {
  36.         this.target = null;
  37.         this.mob.teleportTo(spawn);
  38.         search = true;
  39.         speak = true;
  40.     }
  41.  
  42.     @Override
  43.     public boolean shouldExecute() {
  44.         if (this.target == null) {
  45.             return this.getTarget();
  46.         }
  47.         return false;
  48.     }
  49.  
  50.     @Override
  51.     public void startExecuting() {
  52.         if (search) {
  53.             mob.getPathFinder().setPathToLocation(target.getLocation());
  54.             mob.getPathFinder().setSpeed(0.5f);
  55.         }
  56.         Vector3D v = new Vector3D(mob.getLocation());
  57.         if (v.getDistance(spawn) > 20 || !search) {
  58.             mob.getPathFinder().setPathToLocation(spawn);
  59.             mob.getPathFinder().setSpeed(0.5f);
  60.             search = false;
  61.         }
  62.     }
  63.  
  64.     @Override
  65.     public void updateTask() {
  66.         if (mob.isInWater()) {
  67.             this.resetTask();
  68.         }
  69.         if (target == null) {
  70.             this.resetTask();
  71.             return;
  72.         }
  73.         if (Math.abs(target.getX() - mob.getX()) < 2 && Math.abs(target.getZ() - mob.getZ()) < 2) {
  74.             mob.lookAt(target);
  75.             if (speak) {
  76.                 mob.playLivingSound();
  77.                 speak = false;
  78.             }
  79.         }
  80.         Vector3D v = new Vector3D(mob.getLocation());
  81.         if (v.getDistance(spawn) < 3 && !search) {
  82.             this.resetTask();
  83.         }
  84.         if (v.getDistance(spawn) > 20 || !search) {
  85.             //mob.getPathFinder().setSpeed(0.5f);
  86.             //mob.getPathFinder().setPathToLocation(spawn);
  87.             search = false;
  88.             resetTask();
  89.         } else {
  90.             mob.getPathFinder().setPathToEntity(target);
  91.         }
  92.     }
  93.    
  94.     public boolean getTarget() {
  95.         if (search) {
  96.             List<Player> players = mob.getWorld().getPlayerList();
  97.             Vector3D v = new Vector3D(mob.getLocation());
  98.             for (int i=0; i<players.size(); i++) {
  99.                 Player t = players.get(i);
  100.                 if (v.getDistance(t.getLocation()) < 15) {
  101.                     this.target = t;
  102.                     mob.getPathFinder().setPathToEntity(target);
  103.                     return true;
  104.                 }
  105.             }
  106.         }
  107.         this.resetTask();
  108.         return false;
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment