-2

I'm making a "forcefield" for a game called Minecraft. What I commented on is what I need help with.

if (Camb.killaura)
      {

    nchitDelay++;
        for(Object o: mc.theWorld.loadedEntityList){
    Entity e = (Entity)o;
        if(e != this && e instanceof EntitySkeleton || e instanceof EntityCow && getDistanceToEntity(e) <= killauraRange && mc.thePlayer.canEntityBeSeen(e) &&!Camb.friend.contains(e.getEntityName())){ // checking if the entity is either a skeleton or cow
        if(e.isEntityAlive()){
        if(nchitDelay >= 8){

        if(Camb.auraaimbot){
        facetoEntity(e); // facing the skeleton from an infinite distance.
        }
        if(Camb.criticals){
           if(mc.thePlayer.isSprinting()==false){
               if(mc.thePlayer.isJumping==false){
               if(mc.thePlayer.onGround){
                   mc.thePlayer.jump();
               }
               }
               }
           }
        Minecraft.SwitchToSword(mc.thePlayer.inventory.currentItem);
        swingItem();
        mc.playerController.attackEntity(this, e);
        nchitDelay = 0;
        break; 
        }
        }
        }
        }
        }
            }

So, the idea is if a skeleton or cow enters the distance(4 blocks), then it will face it and attack it. Everything is working, but the player is facing the skeleton from any distance. Not just 4 blocks. How do I fix this?

Thanks

Brad
  • 115
  • 3
  • 1
    Do you happening to be using a modding api, such forge? – Joban Aug 18 '13 at 18:13
  • @JobanDhillon Nope, straight MC code. – Brad Aug 18 '13 at 18:14
  • 3
    Your indentation is atrocious, can't bring myself to read the code. – fvu Aug 18 '13 at 18:14
  • 3
    Aside from the bad indentation, check the really long `if` statement. It might be better to move that condition to its own function returning a boolean (just so that you can see the individual pieces at once) – Dennis Meng Aug 18 '13 at 18:32

1 Answers1

1

I think your problem is this:

if (
           e != this
        && e instanceof EntitySkeleton
        || e instanceof EntityCow
        && getDistanceToEntity(e) <= killauraRange
        && mc.thePlayer.canEntityBeSeen(e)
        && !Camb.friend.contains(e.getEntityName())
   ) {

You combine a lot of conditions but not in the way you intended. Operators like || and && have rules how they apply just like 2 + 2 * 10 is 22 instead of 40.

&& has precedence over || just like * has precedence over +. Your condition currently means

(e != this && e instanceof EntitySkeleton)
||
(e instanceof EntityCow && getDistanceToEntity(e) <= killauraRange && mc.thePlayer.canEntityBeSeen(e) && !Camb.friend.contains(e.getEntityName())).

What you want is to put some parentheses around the cow or skeletion part:

if (
           e != this
        && (e instanceof EntitySkeleton || e instanceof EntityCow)
        && getDistanceToEntity(e) <= killauraRange
        && mc.thePlayer.canEntityBeSeen(e)
        && !Camb.friend.contains(e.getEntityName())
   ) {
zapl
  • 63,179
  • 10
  • 123
  • 154