Arctic_Wolfy

EntityAIGoToBlock.class #1

Apr 1st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. package com.arctic.betterPets.entities.ai;
  2.  
  3. import com.arctic.betterPets.entities.IBetterPet;
  4. import net.minecraft.entity.EntityLivingBase;
  5. import net.minecraft.entity.ai.EntityAIMoveToBlock;
  6. import net.minecraft.entity.passive.EntityTameable;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.util.EnumChatFormatting;
  9.  
  10. @SuppressWarnings("StatementWithEmptyBody")
  11. public abstract class EntityAIGoToBlock extends EntityAIMoveToBlock {
  12.     protected final IBetterPet pet;
  13.     protected final boolean shouldSearch;
  14.     protected boolean hasAnnouncedNeed = false;
  15.     protected State state = State.OFF;
  16.  
  17.     public EntityAIGoToBlock(IBetterPet pet, double speedIn, int length, boolean shouldSearch) {
  18.         super(pet.getEntity(), speedIn, length);
  19.         this.setMutexBits(3);
  20.         this.pet = pet;
  21.         this.shouldSearch = shouldSearch;
  22.     }
  23.  
  24.     protected enum State {
  25.         OFF,START,EXECUTING,FINISH,POST,DONE
  26.     }
  27.  
  28.     protected abstract float getNeed();
  29.     protected abstract float getMaxNeed();
  30.     protected abstract void setNeed(float need);
  31.  
  32.     protected abstract String getNeedName(boolean past);
  33.     protected abstract EnumChatFormatting getColor();
  34.  
  35.     protected abstract boolean doesNeedNeedToBeFullFilled();
  36.  
  37.     protected void sendMessageToOwner(String msg){
  38.         EntityTameable tamable = pet.getEntity();
  39.         EntityLivingBase owner = tamable.getOwner();
  40.         if (owner != null && owner instanceof EntityPlayer){
  41.             ((EntityPlayer) owner).addChatComponentMessage(
  42.                     tamable.getDisplayName().appendText(
  43.                             " " + getColor() + msg
  44.                     )
  45.             );
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public boolean shouldExecute() {
  51.         if (state == State.POST) return true;
  52.         state = State.OFF;
  53.         boolean subResult = doesNeedNeedToBeFullFilled();
  54.         if (!subResult) return false;
  55.         int delay = 0;
  56.         if (runDelay > delay) runDelay = delay;
  57.         boolean superResult = super.shouldExecute();
  58.         if (!hasAnnouncedNeed){
  59.             hasAnnouncedNeed = true;
  60.             sendMessageToOwner("has to "+getNeedName(false)+"!");
  61.         }
  62.         if (destinationBlock.up().equals(pet.getEntity().getPosition())) {
  63.             finish(true);
  64.             return false;
  65.         }
  66.         return superResult;
  67.     }
  68.  
  69.     @Override
  70.     public void startExecuting() {
  71.         //if (state==State.OFF)name = pet.getEntity().getName();
  72.         if (state==State.OFF)sendMessageToOwner(destinationBlock.toString());
  73.         if (shouldSearch) {
  74.             super.startExecuting();
  75.             if (state==State.OFF) {
  76.                 sendMessageToOwner("is looking to " + getNeedName(false) + "!");
  77.                 state = State.START;
  78.             }
  79.         } else {
  80.             state = State.FINISH;
  81.         }
  82.     }
  83.  
  84.     @Override
  85.     public boolean continueExecuting() {
  86.         if (!doesNeedNeedToBeFullFilled()) return false;
  87.         boolean superResult = super.continueExecuting();
  88.         boolean isAtDest = getIsAboveDestination();
  89.         boolean resultA = superResult && !isAtDest;
  90.         /*sendMessageToOwner("super: " + superResult +
  91.                 ", dest: " + isAtDest +
  92.                 ", resultA: " + resultA +
  93.                 ", resultB: " + resultB +
  94.                 ", result: " + result
  95.         );*/
  96.         if ((!superResult||isAtDest)) {
  97.             if (state == State.EXECUTING) state = State.FINISH;
  98.         }
  99.  
  100.         switch (state){
  101.             default: case OFF: break;
  102.             case FINISH: finish(isAtDest); return true;
  103.             case START: state = State.EXECUTING; break;
  104.             case DONE:
  105.                 state = State.OFF;
  106.  
  107.                 return false;
  108.             case POST: return true;
  109.         }
  110.  
  111.         return resultA;
  112.     }
  113.  
  114.     @Override
  115.     public void updateTask() {
  116.         if (state == State.POST){
  117.             executeExtra();
  118.             //return;
  119.         }
  120.         super.updateTask();
  121.     }
  122.  
  123.     protected abstract void finish(boolean atDest);
  124.     protected abstract void executeExtra();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment