Brad
Brad

Reputation: 111

Checking if the entity is on the list

I'm programming a friend system for my "forcefield" in a game called Minecraft. My idea is that if the player is not on the friend list, the player will it attack. The follow is all the code for my friend system and forcefield.

public static boolean friends = true;
public static List friend = new ArrayList();

public static void friendsList(){
    if(friends){
        try{
            File file = new File("friends.txt");
            BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(file));
            for(int i = 0; i < friend.size(); i++){
                bufferedwriter.write((new StringBuilder()).append((String) friend.get(i)).append("\r\n").toString());
            }
            bufferedwriter.close();
        }
        catch(Exception exception){
            System.err.print(exception.toString());
        }
    }

Forcefield:

if(Camb.nocheat){
    if (Camb.killaura)
      {

    hitDelay++;
        for(Object o: mc.theWorld.loadedEntityList){
    Entity e = (Entity)o;
        if(e != this && *******CHECK IF PLAYER IS NOT ON LIST******* && e instanceof EntityPlayer &&getDistanceToEntity(e) < 3.95D){
        if(e.isEntityAlive()){
        if(hitDelay >= 4){
            if(Camb.criticals){
               if(mc.thePlayer.isSprinting()==false){
                   if(mc.thePlayer.isJumping==false){
                   if(mc.thePlayer.onGround){
                       mc.thePlayer.jump();
                   }
                   }
               }


           }
        swingItem();
        mc.playerController.attackEntity(this, e);
        hitDelay = 0;
    break;

        }
        }
        }
        }
      }
    }

Adding/removing/clearing the friend list:

if(par1Str.startsWith("&friendadd")){
        Camb.friends = true;
        String as0[] = par1Str.split("");
        Camb.friend.add(as0[1]);
        mc.thePlayer.addChatMessage((new StringBuilder()).append("\2479[CAMB]\247e Added Friend.").append("").toString());
        Camb.friendsList();
        Camb.friends = false;
        return;
    }
    if(par1Str.startsWith("&friendremove")){
        Camb.friends = true;
        String as0[] = par1Str.split("");
        Camb.friend.remove(as0[1]);
        mc.thePlayer.addChatMessage((new StringBuilder()).append("\2479[CAMB]\247e Removed Friend.").append("").toString());
        Camb.friendsList();
        Camb.friends = false;
        return;
    }
    if(par1Str.startsWith("&friendclear")){
        Camb.friends = true;
        Camb.friend.clear();
        Camb.friendsList();
        mc.thePlayer.addChatMessage("\2479[CAMB]\247e Friends list cleared.");
        return;
    }

par1Str is the string entered in chat. Basically commands. Additionally, the &friendremove system is broken as well. I'm unsure why.

Thanks, Brad

Upvotes: 0

Views: 93

Answers (2)

Zach Latta
Zach Latta

Reputation: 3341

I'm not too familiar with the SDK you're using to make this, but you can use this oneliner to check if an element is in an ArrayList.

myList.contains("friendname");

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

Upvotes: 0

nanofarad
nanofarad

Reputation: 41281

Use friend.contains(e). It checks for equality albeit by iteration.

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Upvotes: 1

Related Questions