/* * This file is part of Matter Overdrive * Copyright (c) 2015., Simeon Radivoev, All rights reserved. * * Matter Overdrive is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Matter Overdrive is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Matter Overdrive. If not, see <http://www.gnu.org/licenses>. */ package matteroverdrive.entity.ai; import net.minecraft.command.IEntitySelector; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityCreature; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.ai.EntityAINearestAttackableTarget; import net.minecraft.entity.ai.EntityAITarget; import java.util.Collections; import java.util.List; /** * Created by Simeon on 12/10/2015. */ public class EntityAINearestTarget extends EntityAITarget { private final Class targetClass; private final int targetChance; private final EntityAINearestAttackableTarget.Sorter theNearestAttackableTargetSorter; private final IEntitySelector targetEntitySelector; private EntityLivingBase targetEntity; public EntityAINearestTarget(EntityCreature theEntity, Class targetClass, int targetChance, boolean shouldCheckSight, boolean nearbyOnly, final IEntitySelector p_i1665_6_) { super(theEntity, shouldCheckSight, nearbyOnly); this.targetClass = targetClass; this.targetChance = targetChance; this.theNearestAttackableTargetSorter = new EntityAINearestAttackableTarget.Sorter(theEntity); this.setMutexBits(1); this.targetEntitySelector = new IEntitySelector() { private static final String __OBFID = "CL_00001621"; /** * Return whether the specified entity is applicable to this filter. */ public boolean isEntityApplicable(Entity p_82704_1_) { return !(p_82704_1_ instanceof EntityLivingBase) ? false : (p_i1665_6_ != null && !p_i1665_6_.isEntityApplicable(p_82704_1_) ? false : EntityAINearestTarget.this.isSuitableTarget((EntityLivingBase)p_82704_1_, false)); } }; } public boolean shouldExecute() { if (this.targetChance > 0 && this.taskOwner.getRNG().nextInt(this.targetChance) != 0) { return false; } else { double d0 = this.getTargetDistance(); List list = this.taskOwner.worldObj.selectEntitiesWithinAABB(this.targetClass, this.taskOwner.boundingBox.expand(d0, 4.0D, d0), this.targetEntitySelector); Collections.sort(list, this.theNearestAttackableTargetSorter); if (list.isEmpty()) { return false; } else { this.targetEntity = (EntityLivingBase)list.get(0); return true; } } } public void startExecuting() { this.taskOwner.setAttackTarget(this.targetEntity); super.startExecuting(); } }